/**************************************************************************** * @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 #include #include #include "M0564.h" #include "define.h" // dead time unit = 1/72us // ex. dead time 72 = 1us, 36 = 0.5us, 18=0.25us #define DEAD_ZONE_01 36 // unit = 1/72us, PWM0.0 PWM0.1 Dead time #define DEAD_ZONE_23 72 // unit = 1/72us, PWM0.2 PWM0.3 Dead time #define DEAD_ZONE_45 18 // unit = 1/72us, PWM0.4 PWM0.5 Dead time #define MAX_FREQ 150000 #define MIN_FREQ 40000 const int DELTA_FREQ_TABLE[] = {10000, 5000, 1000, 100, 25, 5}; int CurrFreq = MAX_FREQ; int DeltaFreq = 0; 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 print("CurrFreq = %d DeltaFreq = %d\r\n", CurrFreq, DELTA_FREQ_TABLE[DeltaFreq]); break; case 'd': // right CurrFreq -= DELTA_FREQ_TABLE[DeltaFreq]; if(CurrFreq < MIN_FREQ) CurrFreq = MIN_FREQ; t= (BASE_CLOCK + (CurrFreq/2))/CurrFreq - 1; ComplementaryPWM0(0, t); ComplementaryPWM0(2, t); ComplementaryPWM0(4, t); print("CurrFreq = %d DeltaFreq = %d\r\n", CurrFreq, DELTA_FREQ_TABLE[DeltaFreq]); break; case 'w': // up if(--DeltaFreq < 0) DeltaFreq = 0; print("CurrFreq = %d DeltaFreq = %d\r\n", CurrFreq, DELTA_FREQ_TABLE[DeltaFreq]); break; case 'a': // left CurrFreq += DELTA_FREQ_TABLE[DeltaFreq]; if(CurrFreq > MAX_FREQ) CurrFreq = MAX_FREQ; t = (BASE_CLOCK + (CurrFreq/2))/CurrFreq - 1; ComplementaryPWM0(0, t); ComplementaryPWM0(2, t); ComplementaryPWM0(4, t); print("CurrFreq = %d DeltaFreq = %d\r\n", CurrFreq, DELTA_FREQ_TABLE[DeltaFreq]); break; case 'x': // down 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]); 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() { 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; } else { if(--Number < '0') Number = '9'; Display(Number); } 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); } t = (BASE_CLOCK + (CurrFreq/2))/CurrFreq - 1; ComplementaryPWM0(0, t); ComplementaryPWM0(2, t); ComplementaryPWM0(4, t); } } /*---------------------------------------------------------------------------------------------------------*/ /* 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(); if(TIMER_GetIntFlag(TIMER1)) { // timer1 elapsed? TIMER_ClearIntFlag(TIMER1); // clear timer1 flags BUTTON_Func(); int S = CHECK_SHUTDOWN(); if(S >= 0) { if(S) { 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); } } } } } /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/