Files
systemvalidation-neupfc/heater.cpp
T
2022-08-24 14:37:20 +08:00

281 lines
7.8 KiB
C++

/****************************************************************************
* @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"
#define INTERRUPT
#define GPIO_PIN PC0
#define GPIO_ON 1
int Sync = 0;
volatile int Positve = -1;
int StableCnt;
volatile int err;
// phase angle, pwm freq in KHZ
int TableIndex, PercentCnt;
volatile int TI;
// phase angle to turn off signal
const int ON_TBL[] = {
100,
90,
80,
70,
60,
50,
40,
30,
20,
10
};
/*---------------------------------------------------------------------------------------------------------*/
/* Init TABLE */
/*---------------------------------------------------------------------------------------------------------*/
/**
* @brief ACMP01 IRQ
*
* @param None
*
* @return None
*
* @details The ACMP01 default IRQ, declared in startup_M0564.s.
*/
extern "C" {
void ACMP01_IRQHandler(void) {
if(ACMP_GET_INT_FLAG(ACMP01, 1)) {
ACMP_CLR_INT_FLAG(ACMP01, 1); // clear interrupt flag
int T = TIMER_GetCounter(TIMER0); // get timer0 counter
if(TIMER0->CTL & TIMER_CTL_CNTEN_Msk) {
if(T < NOISE) return; // noise. skip
} else {
//StableCnt = 0;
//Positve = -1;
}
TIMER_Stop(TIMER0); // stop timer0
TIMER_ClearIntFlag(TIMER0); // clear timer0 flag
TIMER0->CNT = 0; // set timer0 cnt to 0
TIMER_Start(TIMER0); // start timer0
if(HZ_50U < T || T < HZ_60L) { // not in range
Positve = -1;
StableCnt = 0;
err = 1;
PercentCnt = 0;
GPIO_PIN = !GPIO_ON;
} else {
if(StableCnt < STABLE_CNT) { // wait until stable
StableCnt++;
} else {
if(ACMP_GET_OUTPUT(ACMP01, 1)) { // N > L
if(Positve < 0) {
Positve = 1; // positve
TI = TableIndex;
PercentCnt = 0;
} else if(TI != TableIndex) {
TI = TableIndex;
PercentCnt = 0;
}
if(PercentCnt >= ON_TBL[TI]) {
GPIO_PIN = !GPIO_ON;
} else {
GPIO_PIN = GPIO_ON;
}
PercentCnt += 10;
if(PercentCnt >= 100) {
PercentCnt = 0;
}
} else { // L <= N
}
}
}
}
}
void TMR2_IRQHandler(void) {
TIMER_ClearIntFlag(TIMER2); // clear interrupt flag
PWM_Func();
}
}
/*---------------------------------------------------------------------------------------------------------*/
/* Init TABLE */
/*---------------------------------------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------------------------------------*/
/* WM callback */
/*---------------------------------------------------------------------------------------------------------*/
void PWM_Func() {
int currState;
do { // Get Curr State, Period and time
currState = Positve;
} while(currState != Positve); // ACMP interrupt occurs ?
if(currState < 0) { // Unrecognized signal?
} else if(TIMER_GetIntFlag(TIMER0)) { // Unrecognized signal?
Positve = -1;
StableCnt = 0;
GPIO_PIN = !GPIO_ON;
} else {
Sync = SYNC_LOST_TIME;
}
}
/*---------------------------------------------------------------------------------------------------------*/
/* UART function */
/*---------------------------------------------------------------------------------------------------------*/
void UART_Func() {
int P1, P2;
int Func = UART_Command(P1, P2);
if(Func) { // command is received?
switch(Func) {
case 'v': // read v
print("%d\r\n", ADC_GET_CONVERSION_DATA(ADC, P1));
break;
case 'f': // read f
case 't': // read t
break;
case 'F': // write F
PWM(P1, P2>>1, P2);
break;
case 'T': // write T
case 'V': // write V
case 'a': // left
case 'w': // up
case 'd': // right
case 'x': // down
case ' ': // space
print("NA\r\n");
break;
}
}
}
/*---------------------------------------------------------------------------------------------------------*/
/* BUTTON function */
/*---------------------------------------------------------------------------------------------------------*/
void BUTTON_Func() {
if(Sync) {
Sync--;
}
static int t;
if(++t >= BLINK_TIME) {
t = 0;
if(Sync) SEG_DP = 0; // if signal is valid, blink DP
LED = 0;
if(err) { // if signal is unrecognized, display number counting
err = 0;
static unsigned char disp_num;
if(++disp_num < '0' || disp_num > '9') disp_num = '0';
Display(disp_num);
}
} else {
SEG_DP = 1;
LED = 1;
}
static unsigned int pressed = 0, deboounce_cnt;
if(pressed) {
if(!UP || !DOWN || !LEFT || !RIGHT) { // key is not released. precess key debounce
deboounce_cnt = DEBOUNCE_TIME;
} else if(--deboounce_cnt == 0) {
pressed = 0;
}
} else if(!UP) {// UP is pressed
pressed = 1;
deboounce_cnt = DEBOUNCE_TIME;
} else if(!DOWN) {// DOWN is pressed
pressed = 1;
deboounce_cnt = DEBOUNCE_TIME;
} else if(!LEFT) {// LEFT is pressed
pressed = 1;
deboounce_cnt = DEBOUNCE_TIME;
if(TableIndex) TableIndex--; // dec Table index
Display('0'+TableIndex);
} else if(!RIGHT) {// RIGHT is pressed
pressed = 1;
deboounce_cnt = DEBOUNCE_TIME;
if(TableIndex < sizeof(ON_TBL)/sizeof(int)-1) TableIndex++; // inc Table index
Display('0'+TableIndex);
}
}
void GPIO_Init() {
SYS->GPC_MFPL &= ~SYS_GPC_MFPL_PC0MFP_Msk;
GPIO_PIN = !GPIO_ON;
}
/*---------------------------------------------------------------------------------------------------------*/
/* MAIN function */
/*---------------------------------------------------------------------------------------------------------*/
int main(void)
{
/* Unlock protected registers */
SYS_UnlockReg();
/* Init System, IP clock and multi-function I/O */
SYS_Init();
GPIO_Init();
/* Lock protected registers */
SYS_LockReg();
// Display 8
Display('8');
/* Init TIMER0 */
TIMER0_Init();
/* Init TIMER1 */
TIMER1_Init();
// Initial ADC
ADC_Init();
/* Init UART1 */
UART1_Init();
// Delay 0.5 sec
TIMER1_Delay(500000);
// Display 0
Display('0');
/* Init ACMP */
ACMP_Init();
/* Enable ACMP01 interrupt */
NVIC_EnableIRQ(ACMP01_IRQn);
#ifdef INTERRUPT
/* Init TIMER2 */
TIMER2_Init();
#endif
while(1) {
#ifndef INTERRUPT
PWM_Func();
#endif
UART_Func();
if(TIMER_GetIntFlag(TIMER1)) { // timer1 elapsed?
TIMER_ClearIntFlag(TIMER1); // clear timer1 flags
BUTTON_Func();
}
}
}
/*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/