2022-08-01 17:11:54 +08:00
|
|
|
/****************************************************************************
|
|
|
|
|
* @file main.c
|
|
|
|
|
* @version V3.0err
|
|
|
|
|
* $Revision: 4 $
|
|
|
|
|
* $Date: 17/05/04 12:57p $
|
|
|
|
|
* @brief Perform A/D Conversion with ADC continuous scan mode.
|
|
|
|
|
* @note
|
|
|
|
|
* Copyright (C) 2016 Nuvoton Technology Corp. All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
******************************************************************************/
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include "M0564.h"
|
|
|
|
|
#include "define.h"
|
2022-08-24 14:37:20 +08:00
|
|
|
// dead time unit = 1/72us
|
|
|
|
|
// ex. dead time 72 = 1us, 36 = 0.5us, 18=0.25us
|
2022-08-24 15:08:08 +08:00
|
|
|
// ex. dead time 2 = 2/72 us, 3 = 3/72us, 4=4/72us
|
|
|
|
|
#define DEAD_ZONE_01 2 // unit = 1/72us, PWM0.0 PWM0.1 Dead time
|
|
|
|
|
#define DEAD_ZONE_23 3 // unit = 1/72us, PWM0.2 PWM0.3 Dead time
|
|
|
|
|
#define DEAD_ZONE_45 4 // unit = 1/72us, PWM0.4 PWM0.5 Dead time
|
2022-08-01 17:11:54 +08:00
|
|
|
|
|
|
|
|
#define MAX_FREQ 150000
|
|
|
|
|
#define MIN_FREQ 40000
|
2022-08-24 14:37:20 +08:00
|
|
|
const int DELTA_FREQ_TABLE[] = {10000, 5000, 1000, 100, 25, 5};
|
2022-08-01 17:11:54 +08:00
|
|
|
|
|
|
|
|
int CurrFreq = MAX_FREQ;
|
2022-08-24 14:37:20 +08:00
|
|
|
int DeltaFreq = 0;
|
2022-08-01 17:11:54 +08:00
|
|
|
int Number = '0';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Timer2 IRQ
|
|
|
|
|
*
|
|
|
|
|
* @param None
|
|
|
|
|
*
|
|
|
|
|
* @return None
|
|
|
|
|
*
|
|
|
|
|
* @details The Timer2 default IRQ, declared in startup_M0564.s.
|
|
|
|
|
*/
|
|
|
|
|
extern "C" {
|
|
|
|
|
void ACMP01_IRQHandler(void) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/*---------------------------------------------------------------------------------------------------------*/
|
|
|
|
|
/* UART function */
|
|
|
|
|
/*---------------------------------------------------------------------------------------------------------*/
|
|
|
|
|
inline void UART_Func() {
|
|
|
|
|
int P1, P2;
|
|
|
|
|
unsigned int t;
|
|
|
|
|
int Func = UART_Command(P1, P2);
|
|
|
|
|
if(Func) {
|
|
|
|
|
switch(Func) {
|
|
|
|
|
case 'f': // read f
|
2022-08-24 14:37:20 +08:00
|
|
|
print("CurrFreq = %d DeltaFreq = %d\r\n", CurrFreq, DELTA_FREQ_TABLE[DeltaFreq]);
|
2022-08-01 17:11:54 +08:00
|
|
|
break;
|
2022-08-24 14:37:20 +08:00
|
|
|
case 'd': // right
|
|
|
|
|
CurrFreq -= DELTA_FREQ_TABLE[DeltaFreq];
|
2022-08-01 17:11:54 +08:00
|
|
|
if(CurrFreq < MIN_FREQ) CurrFreq = MIN_FREQ;
|
|
|
|
|
t= (BASE_CLOCK + (CurrFreq/2))/CurrFreq - 1;
|
|
|
|
|
ComplementaryPWM0(0, t);
|
|
|
|
|
ComplementaryPWM0(2, t);
|
|
|
|
|
ComplementaryPWM0(4, t);
|
2022-08-24 14:37:20 +08:00
|
|
|
print("CurrFreq = %d DeltaFreq = %d\r\n", CurrFreq, DELTA_FREQ_TABLE[DeltaFreq]);
|
2022-08-01 17:11:54 +08:00
|
|
|
break;
|
2022-08-24 14:37:20 +08:00
|
|
|
case 'w': // up
|
|
|
|
|
if(--DeltaFreq < 0) DeltaFreq = 0;
|
|
|
|
|
print("CurrFreq = %d DeltaFreq = %d\r\n", CurrFreq, DELTA_FREQ_TABLE[DeltaFreq]);
|
2022-08-01 17:11:54 +08:00
|
|
|
break;
|
2022-08-24 14:37:20 +08:00
|
|
|
case 'a': // left
|
|
|
|
|
CurrFreq += DELTA_FREQ_TABLE[DeltaFreq];
|
2022-08-01 17:11:54 +08:00
|
|
|
if(CurrFreq > MAX_FREQ) CurrFreq = MAX_FREQ;
|
|
|
|
|
t = (BASE_CLOCK + (CurrFreq/2))/CurrFreq - 1;
|
|
|
|
|
ComplementaryPWM0(0, t);
|
|
|
|
|
ComplementaryPWM0(2, t);
|
|
|
|
|
ComplementaryPWM0(4, t);
|
2022-08-24 14:37:20 +08:00
|
|
|
print("CurrFreq = %d DeltaFreq = %d\r\n", CurrFreq, DELTA_FREQ_TABLE[DeltaFreq]);
|
2022-08-01 17:11:54 +08:00
|
|
|
break;
|
|
|
|
|
case 'x': // down
|
2022-08-24 14:37:20 +08:00
|
|
|
if(++DeltaFreq >= sizeof(DELTA_FREQ_TABLE)/sizeof(int)) DeltaFreq = sizeof(DELTA_FREQ_TABLE)/sizeof(int) - 1;
|
|
|
|
|
print("CurrFreq = %d DeltaFreq = %d\r\n", CurrFreq, DELTA_FREQ_TABLE[DeltaFreq]);
|
2022-08-01 17:11:54 +08:00
|
|
|
break;
|
|
|
|
|
case 't': // read t
|
|
|
|
|
case 'v': // read v
|
|
|
|
|
case 'F': // write F
|
|
|
|
|
case 'T': // write T
|
|
|
|
|
case 'V': // write V
|
|
|
|
|
case ' ': // space
|
|
|
|
|
print("NA\r\n");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/*---------------------------------------------------------------------------------------------------------*/
|
|
|
|
|
/* BUTTON function */
|
|
|
|
|
/*---------------------------------------------------------------------------------------------------------*/
|
|
|
|
|
inline void BUTTON_Func() {
|
|
|
|
|
|
2022-08-24 14:37:20 +08:00
|
|
|
static int t;
|
|
|
|
|
if(++t >= BLINK_TIME) {
|
|
|
|
|
t = 0;
|
|
|
|
|
SEG_DP = 0;
|
|
|
|
|
LED = 0;
|
|
|
|
|
} else {
|
|
|
|
|
SEG_DP = 1;
|
|
|
|
|
LED = 1;
|
|
|
|
|
}
|
|
|
|
|
static unsigned int pressed = 0, deboounce_cnt;
|
|
|
|
|
if(pressed) {
|
|
|
|
|
if(!UP || !DOWN || !LEFT || !RIGHT) {// key is not released
|
|
|
|
|
deboounce_cnt = DEBOUNCE_TIME;
|
|
|
|
|
} else if(--deboounce_cnt == 0) {
|
|
|
|
|
pressed = 0;
|
|
|
|
|
}
|
|
|
|
|
} else if(!UP) {// UP is pressed
|
|
|
|
|
pressed = 1;
|
|
|
|
|
deboounce_cnt = DEBOUNCE_TIME;
|
|
|
|
|
if(--DeltaFreq < 0) DeltaFreq = 0;
|
|
|
|
|
} else if(!DOWN) {// DOWN is pressed
|
|
|
|
|
pressed = 1;
|
|
|
|
|
deboounce_cnt = DEBOUNCE_TIME;
|
|
|
|
|
if(++DeltaFreq >= sizeof(DELTA_FREQ_TABLE)/sizeof(int)) DeltaFreq = sizeof(DELTA_FREQ_TABLE)/sizeof(int) - 1;
|
|
|
|
|
} else if(!RIGHT) {// RIGHT is pressed
|
|
|
|
|
pressed = 1;
|
|
|
|
|
deboounce_cnt = DEBOUNCE_TIME;
|
|
|
|
|
CurrFreq -= DELTA_FREQ_TABLE[DeltaFreq];
|
|
|
|
|
if(CurrFreq < MIN_FREQ) {
|
|
|
|
|
CurrFreq = MIN_FREQ;
|
2022-08-01 17:11:54 +08:00
|
|
|
} else {
|
2022-08-24 14:37:20 +08:00
|
|
|
if(--Number < '0') Number = '9';
|
|
|
|
|
Display(Number);
|
2022-08-01 17:11:54 +08:00
|
|
|
}
|
2022-08-24 14:37:20 +08:00
|
|
|
t = (BASE_CLOCK + (CurrFreq/2))/CurrFreq - 1;
|
|
|
|
|
ComplementaryPWM0(0, t);
|
|
|
|
|
ComplementaryPWM0(2, t);
|
|
|
|
|
ComplementaryPWM0(4, t);
|
|
|
|
|
} else if(!LEFT) {// LEFT is pressed
|
|
|
|
|
pressed = 1;
|
|
|
|
|
deboounce_cnt = DEBOUNCE_TIME;
|
|
|
|
|
CurrFreq += DELTA_FREQ_TABLE[DeltaFreq];
|
|
|
|
|
if(CurrFreq > MAX_FREQ) {
|
|
|
|
|
CurrFreq = MAX_FREQ;
|
|
|
|
|
} else {
|
|
|
|
|
if(++Number > '9') Number = '0';
|
|
|
|
|
Display(Number);
|
2022-08-01 17:11:54 +08:00
|
|
|
}
|
2022-08-24 14:37:20 +08:00
|
|
|
t = (BASE_CLOCK + (CurrFreq/2))/CurrFreq - 1;
|
|
|
|
|
ComplementaryPWM0(0, t);
|
|
|
|
|
ComplementaryPWM0(2, t);
|
|
|
|
|
ComplementaryPWM0(4, t);
|
|
|
|
|
}
|
2022-08-01 17:11:54 +08:00
|
|
|
}
|
|
|
|
|
/*---------------------------------------------------------------------------------------------------------*/
|
|
|
|
|
/* MAIN function */
|
|
|
|
|
/*---------------------------------------------------------------------------------------------------------*/
|
|
|
|
|
int main(void)
|
|
|
|
|
{
|
|
|
|
|
/* Unlock protected registers */
|
|
|
|
|
SYS_UnlockReg();
|
|
|
|
|
|
|
|
|
|
/* Init System, IP clock and multi-function I/O */
|
|
|
|
|
SYS_Init();
|
|
|
|
|
|
|
|
|
|
/* Lock protected registers */
|
|
|
|
|
SYS_LockReg();
|
|
|
|
|
|
|
|
|
|
/* Display 8 */
|
|
|
|
|
Display('8');
|
|
|
|
|
|
|
|
|
|
/* Init TIMER0 */
|
|
|
|
|
TIMER0_Init();
|
|
|
|
|
|
|
|
|
|
/* Init TIMER1 */
|
|
|
|
|
TIMER1_Init();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Set Pwm mode as complementary mode */
|
|
|
|
|
PWM_ENABLE_COMPLEMENTARY_MODE(PWM0);
|
|
|
|
|
// Initial PWM
|
|
|
|
|
PWM_Init();
|
|
|
|
|
|
|
|
|
|
// Set PWM0.0 PWM0.1 Dead time
|
|
|
|
|
SetPWM0DeadZone(0, DEAD_ZONE_01);
|
|
|
|
|
// Set PWM0.2 PWM0.3 Dead time
|
|
|
|
|
SetPWM0DeadZone(2, DEAD_ZONE_23);
|
|
|
|
|
// Set PWM0.4 PWM0.5 Dead time
|
|
|
|
|
SetPWM0DeadZone(4, DEAD_ZONE_45);
|
|
|
|
|
|
|
|
|
|
// Initial ADC
|
|
|
|
|
ADC_Init();
|
|
|
|
|
|
|
|
|
|
/* Init UART1 */
|
|
|
|
|
UART1_Init();
|
|
|
|
|
|
|
|
|
|
// Delay 0.5 sec
|
|
|
|
|
TIMER1_Delay(500000);
|
|
|
|
|
|
|
|
|
|
/* Display 0 */
|
|
|
|
|
Display(Number);
|
|
|
|
|
|
|
|
|
|
unsigned int t = (BASE_CLOCK + (CurrFreq/2))/CurrFreq - 1;
|
|
|
|
|
ComplementaryPWM0(0, t);
|
|
|
|
|
ComplementaryPWM0(2, t);
|
|
|
|
|
ComplementaryPWM0(4, t);
|
|
|
|
|
print("\r\nOK ");
|
|
|
|
|
while(1) {
|
|
|
|
|
|
|
|
|
|
UART_Func();
|
|
|
|
|
|
2022-08-24 14:37:20 +08:00
|
|
|
if(TIMER_GetIntFlag(TIMER1)) { // timer1 elapsed?
|
2022-08-24 15:08:08 +08:00
|
|
|
TIMER_ClearIntFlag(TIMER1); // clear timer1 flags
|
|
|
|
|
if(Shutdown == 0) {
|
|
|
|
|
BUTTON_Func();
|
|
|
|
|
}
|
2022-08-24 14:37:20 +08:00
|
|
|
|
2022-08-24 15:08:08 +08:00
|
|
|
if(SHUTDOWN_MODE_CHANGED() != 0) {
|
|
|
|
|
if(Shutdown == 1) {
|
2022-08-24 14:37:20 +08:00
|
|
|
Display('E');
|
|
|
|
|
/* Enable TIMER2 interrupt */
|
|
|
|
|
ComplementaryPWM0(0, 0); // Turn off PWM
|
|
|
|
|
ComplementaryPWM0(2, 0); // Turn off PWM
|
|
|
|
|
ComplementaryPWM0(4, 0); // Turn off PWM
|
|
|
|
|
} else {
|
|
|
|
|
Display('0');
|
|
|
|
|
t = (BASE_CLOCK + (CurrFreq/2))/CurrFreq - 1;
|
|
|
|
|
ComplementaryPWM0(0, t);
|
|
|
|
|
ComplementaryPWM0(2, t);
|
|
|
|
|
ComplementaryPWM0(4, t);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-08-01 17:11:54 +08:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/
|
|
|
|
|
|