Support Complementary PWM mode

This commit is contained in:
murphy
2022-08-24 14:37:20 +08:00
parent 4a256414a2
commit dce9c6229e
40 changed files with 2080 additions and 4157 deletions
+90 -78
View File
@@ -13,18 +13,18 @@
#include <string.h>
#include "M0564.h"
#include "define.h"
#define DEAD_ZONE_01 2 // us, PWM0.0 PWM0.1 Dead time
#define DEAD_ZONE_23 3 // us, PWM0.2 PWM0.3 Dead time
#define DEAD_ZONE_45 4 // us, PWM0.4 PWM0.5 Dead time
// 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
#define MAX_DELTA_FREQ 10000
#define MIN_DELTA_FREQ 100
const int DELTA_FREQ_TABLE[] = {10000, 5000, 1000, 100, 25, 5};
int CurrFreq = MAX_FREQ;
int DeltaFreq = MAX_DELTA_FREQ;
int DeltaFreq = 0;
int Number = '0';
/**
@@ -50,35 +50,33 @@ inline void UART_Func() {
if(Func) {
switch(Func) {
case 'f': // read f
print("CurrFreq = %d DeltaFreq = %d\r\n", CurrFreq, DeltaFreq);
print("CurrFreq = %d DeltaFreq = %d\r\n", CurrFreq, DELTA_FREQ_TABLE[DeltaFreq]);
break;
case 'a': // left
CurrFreq -= DeltaFreq;
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, DeltaFreq);
print("CurrFreq = %d DeltaFreq = %d\r\n", CurrFreq, DELTA_FREQ_TABLE[DeltaFreq]);
break;
case 'w': // up
DeltaFreq += MIN_DELTA_FREQ;
if(DeltaFreq > MAX_DELTA_FREQ) DeltaFreq = MAX_DELTA_FREQ;
print("CurrFreq = %d DeltaFreq = %d\r\n", CurrFreq, DeltaFreq);
case 'w': // up
if(--DeltaFreq < 0) DeltaFreq = 0;
print("CurrFreq = %d DeltaFreq = %d\r\n", CurrFreq, DELTA_FREQ_TABLE[DeltaFreq]);
break;
case 'd': // right
CurrFreq += DeltaFreq;
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, DeltaFreq);
print("CurrFreq = %d DeltaFreq = %d\r\n", CurrFreq, DELTA_FREQ_TABLE[DeltaFreq]);
break;
case 'x': // down
DeltaFreq -= MIN_DELTA_FREQ;
if(DeltaFreq < MIN_DELTA_FREQ) DeltaFreq = MIN_DELTA_FREQ;
print("CurrFreq = %d DeltaFreq = %d\r\n", CurrFreq, DeltaFreq);
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
@@ -96,65 +94,59 @@ inline void UART_Func() {
/*---------------------------------------------------------------------------------------------------------*/
inline void BUTTON_Func() {
unsigned int t;
if(TIMER_GetIntFlag(TIMER1)) {
TIMER_ClearIntFlag(TIMER1);
static int t;
if(++t >= BLINK_TIME) {
t = 0;
SEG_DP = 0;
LED = 0;
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 {
SEG_DP = 1;
LED = 1;
if(--Number < '0') Number = '9';
Display(Number);
}
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;
DeltaFreq += MIN_DELTA_FREQ;
if(DeltaFreq > MAX_DELTA_FREQ) DeltaFreq = MAX_DELTA_FREQ;
} else if(!DOWN) {// DOWN is pressed
pressed = 1;
deboounce_cnt = DEBOUNCE_TIME;
DeltaFreq -= MIN_DELTA_FREQ;
if(DeltaFreq < MIN_DELTA_FREQ) DeltaFreq = MIN_DELTA_FREQ;
} else if(!LEFT) {// LEFT is pressed
pressed = 1;
deboounce_cnt = DEBOUNCE_TIME;
CurrFreq -= 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(!RIGHT) {// RIGHT is pressed
pressed = 1;
deboounce_cnt = DEBOUNCE_TIME;
CurrFreq += 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);
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 */
@@ -213,7 +205,27 @@ int main(void)
UART_Func();
BUTTON_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);
}
}
}
}
}