📄 sine_pwm_voltage-regulator_32_int.c
字号:
break;
case 2: // stop mode;
sine_pwm_stop();
if(index==31) inv_mode=4;
break;
case 3: // standby mode;
sine_pwm_stdby();
stdby_delay--;
break;
case 4: // idle mode;
set_pwm1_duty(0); // M=0;
if((!high_t_stop)&&(!low_curr)&&(!low_acc_delay)) {
if(stdby_delay>1023) output_high(PIN_B4); // Blink Green LED;
else output_low(PIN_B4);
}
if(!stdby_delay) {
stdby_delay=2047;
if(stdby) inv_mode=3;
else inv_mode=0;
}
else stdby_delay--;
break;
}
if(index==15) {
set_adc_channel(acc_chan);
delay_us(10);
read_adc(ADC_START_ONLY);
delay_us(20);
acc_v=read_adc(ADC_READ_ONLY);
acc_v_temp+=acc_v;
acc_avg_counter++;
if(acc_avg_counter==32) {
acc_avg_counter=0;
acc_v_avg=acc_v_temp>>5;
if(acc_v_avg<=acc_min_l) {
low_acc_delay++;
if(!high_t_stop) {
output_low(PIN_B4);
output_high(PIN_B5);
}
if(low_acc_delay==16) {
low_acc=1;
low_acc_delay=0;
}
}
else {
if(acc_v_avg>=acc_max_h) high_acc=1;
low_acc_delay=0;
}
acc_v_temp=0;
acc_v_avg=0;
}
output_low(PIN_C3); // check vref value;
set_adc_channel(stdby_chan);
delay_us(10);
read_adc(ADC_START_ONLY);
delay_us(20);
volt_ref=read_adc(ADC_READ_ONLY);
volt_ref_temp+=volt_ref;
volt_ref_counter++;
if(volt_ref_counter==32) {
volt_ref_avg=volt_ref_temp>>8;
volt_ref_delta=volt_ref_avg-30;
volt_ref_counter=0;
volt_ref_temp=0;
}
}
if(index==31) {
if(over_curr_delay>1600) over_curr=1;
if(!input(PIN_C4)) {
high_t_delay++;
output_high(PIN_C5);
if((high_t_delay>255)&&(high_t_delay<1024)) {
high_t_stop=1;
output_low(PIN_B4);
if(high_t_blink>45) output_high(PIN_B5); // Blink Red LED;
else output_low(PIN_B5);
if(!high_t_blink) high_t_blink=90;
else high_t_blink--;
}
else if(high_t_delay>1023) high_t_stop++;
}
else {
high_t_delay=0;
high_t_stop=0;
high_t_blink=90;
output_low(PIN_C5);
}
output_high(PIN_C3); // check stdby current value;
set_adc_channel(stdby_chan);
delay_us(10);
read_adc(ADC_START_ONLY);
delay_us(20);
stdby_i=read_adc(ADC_READ_ONLY);
stdby_i_temp+=stdby_i;
stdby_i_avg_counter++;
if(stdby_i_avg_counter==32) {
stdby_i=stdby_i_temp>>5;
if(stdby_i>=stdby_max) inv_on=1;
else inv_on=0;
if(stdby_i<=stdby_min) inv_off=1;
stdby_i_h=(stdby_i>>1)+128;
//if(stdby_i_h>stdby_i_h_max) stdby_i_h=stdby_i_h_max;
//else if(stdby_i_h<stdby_i_h_min) stdby_i_h=stdby_i_h_min;
stdby_i_l=255-stdby_i_h;
stdby_i_avg_counter=0;
stdby_i_temp=0;
}
}
index++;
if(index>31) index=0;
}
else { // acc low mode;
set_pwm1_duty(0); // M=0;
output_high(PIN_C0); // Z=1;
output_low(PIN_B4); // Green LED-OFF;
if(!inv_off) output_high(PIN_B5); // Red LED-ON;
inv_mode=0;
index=0;
short_curr=0;
over_curr=0;
over_curr_delay=0;
high_ab=0;
high_t_delay=0;
high_t_stop=0;
stdby=0;
pwm_pos=0;
pwm_neg=0;
delta_h_v=0;
delta_l_v=0;
//delta_h_ref_v=0;
//delta_l_ref_v=0;
//diff_h_v=0;
//diff_l_v=0;
delta_h_ref=0;
delta_l_ref=0;
//delta_h_ref_adj=0;
//delta_l_ref_adj=0;
low_curr_h=0;
low_curr_l=0;
curr_h_temp=0;
curr_h_avg=0;
curr_l_temp=0;
curr_l_avg=0;
low_curr=0;
volt_h_temp=0;
volt_h_avg=0;
volt_l_temp=0;
volt_l_avg=0;
volt_ref_temp=0;
volt_ref_avg=0;
volt_ref_delta=0;
volt_ref_counter=0;
volt_h_avg_counter=0;
volt_l_avg_counter=0;
//volt_l_avg_counter=0;
stdby_i_temp=0;
//stdby_i_avg_counter=0;
stdby_go_delay=0;
stdby_delay=2047;
high_t_blink=90;
low_acc_delay=0;
stdby_go_blink=60;
//level=0;
//level_corr=0;
//level_l_corr=0;
for(i=0; i<32; i++) {
shape_corr[i]=0;
//shape_l_corr[i]=0;
level_corr[i]=0;
//level_adj[i]=0;
//shape_h_corr_temp[i]=0;
//shape_l_corr_temp[i]=0;
}
output_high(PIN_C3); // check stdby current value;
set_adc_channel(stdby_chan);
delay_us(10);
read_adc(ADC_START_ONLY);
delay_us(20);
stdby_i=read_adc(ADC_READ_ONLY);
stdby_i_temp+=stdby_i;
stdby_i_avg_counter++;
if(stdby_i_avg_counter==32) {
stdby_i=stdby_i_temp>>5;
if(stdby_i>stdby_min) inv_off=0;
//else inv_off=0;
stdby_i_h=(stdby_i>>1)+128;
//if(stdby_i_h>stdby_i_h_max) stdby_i_h=stdby_i_h_max;
//else if(stdby_i_h<stdby_i_h_min) stdby_i_h=stdby_i_h_min;
stdby_i_l=255-stdby_i_h;
stdby_i_avg_counter=0;
stdby_i_temp=0;
}
set_adc_channel(acc_chan);
delay_us(10);
read_adc(ADC_START_ONLY);
delay_us(20);
acc_v=read_adc(ADC_READ_ONLY);
acc_v_temp+=acc_v;
acc_avg_counter++;
if(acc_avg_counter==32) {
acc_avg_counter=0;
acc_v_avg=acc_v_temp>>5;
if(acc_v_avg<=acc_max_l) high_acc=0;
if(acc_v_avg>=acc_min_h) low_acc=0;
acc_v_temp=0;
acc_v_avg=0;
}
if(input(PIN_C4)) output_low(PIN_C5);
}
}
void main()
{
set_tris_b(0xCD);
set_tris_c(0x90);
output_high(PIN_C0); // Z=1;
output_high(PIN_B4); // test LED's;
output_low(PIN_B5);
delay_ms(1000);
output_low(PIN_B4);
output_high(PIN_B5);
delay_ms(1000);
output_low(PIN_B5);
index=0;
inv_mode=0; // init to normal start mode;
inv_on=0;
inv_off=0;
stdby_mode=0;
short_curr=0;
over_curr=0;
over_curr_delay=0;
high_ab=0;
high_t_delay=0;
high_t_stop=0;
low_acc=0;
high_acc=0;
acc_v_temp=0;
acc_avg_counter=0;
acc_v_avg=0;
stdby=0;
pwm_pos=0;
pwm_neg=0;
delta_h_v=0;
delta_l_v=0;
//delta_h_ref_v=0;
//delta_l_ref_v=0;
//diff_h_v=0;
//diff_l_v=0;
delta_h_ref=0;
delta_l_ref=0;
//delta_h_ref_corr=0;
//delta_l_ref_corr=0;
low_curr_h=0;
low_curr_l=0;
low_curr=0;
curr_h_temp=0;
curr_h_avg=0;
curr_l_temp=0;
curr_l_avg=0;
low_curr=0;
volt_h_temp=0;
volt_h_avg=0;
volt_l_temp=0;
volt_l_avg=0;
volt_ref_temp=0;
volt_ref_avg=0;
volt_ref_delta=0;
volt_ref_counter=0;
volt_h_avg_counter=0;
volt_l_avg_counter=0;
//volt_l_avg_counter=0;
stdby_i_temp=0;
stdby_i_avg_counter=0;
stdby_go_delay=0;
stdby_delay=2047;
high_t_blink=90;
low_acc_delay=0;
stdby_go_blink=60;
//level=0;
//level_corr=0;
//level_l_corr=0;
for(i=0; i<32; i++) {
shape_corr[i]=0;
//shape_l_corr[i]=0;
level_corr[i]=0;
//shape_h_corr_temp[i]=0;
//shape_l_corr_temp[i]=0;
//level_adj[i]=0;
}
setup_adc_ports(ALL_ANALOG); // init the ADC;
setup_adc(ADC_CLOCK_DIV_32); // Tad=1.6us;
set_adc_channel(acc_chan);
delay_us(10);
for(i=0; i<32; i++) {
read_adc(ADC_START_ONLY);
delay_us(20);
acc_v=read_adc(ADC_READ_ONLY);
acc_v_temp+=acc_v;
if(i==31) {
acc_v_avg=acc_v_temp>>5;
if(acc_v_avg<=acc_min_l) low_acc=1;
if(acc_v_avg>=acc_max_h) high_acc=1;
acc_v_temp=0;
acc_v_avg=0;
}
}
output_high(PIN_C3); // check stdby current value;
set_adc_channel(stdby_chan);
delay_us(10);
for(i=0; i<32; i++) {
read_adc(ADC_START_ONLY);
delay_us(20);
stdby_i=read_adc(ADC_READ_ONLY);
stdby_i_temp+=stdby_i;
if(i==31) {
stdby_i=stdby_i_temp>>5;
if(stdby_i<=stdby_min) inv_off=1;
else if(stdby_i>=stdby_max) inv_on=1;
stdby_i_h=(stdby_i>>1)+128;
//if(stdby_i_h>stdby_i_h_max) stdby_i_h=stdby_i_h_max;
//else if(stdby_i_h<stdby_i_h_min) stdby_i_h=stdby_i_h_min;
stdby_i_l=255-stdby_i_h;
stdby_i_temp=0;
}
}
output_low(PIN_C3);
set_adc_channel(stdby_chan); // check vref value;
delay_us(10);
for(i=0; i<32; i++) {
read_adc(ADC_START_ONLY);
delay_us(20);
volt_ref=read_adc(ADC_READ_ONLY);
volt_ref_temp+=volt_ref;
if(i==31) {
volt_ref_avg=volt_ref_temp>>8;
volt_ref_delta=volt_ref_avg-30;
//volt_ref_l=255-volt_ref_h;
volt_ref_temp=0;
}
}
setup_timer_2(T2_DIV_BY_1, 255, 1); // Init the PWM;
setup_ccp1(CCP_PWM);
set_pwm1_duty(0);
//setup_ccp2(CCP_PWM);
//set_pwm2_duty(0);
setup_wdt(WDT_18MS);
set_timer0(60);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_8); // Init the TIMER0;
enable_interrupts(INT_TIMER0);
enable_interrupts(GLOBAL);
while(TRUE) {
restart_wdt();
if((short_curr)||(high_ab)||(over_curr)||(high_t_stop>1)) { // stop and wait for 60 sec;
set_pwm1_duty(0); // M=0;
output_high(PIN_C0); // Z=1;
disable_interrupts(GLOBAL);
output_low(PIN_B4);
output_low(PIN_B5);
short_curr=0;
over_curr=0;
over_curr_delay=0;
high_ab=0;
high_t_stop=0;
high_t_delay=0;
low_acc=0;
high_acc=0;
acc_avg_counter=0;
acc_v_temp=0;
acc_v_avg=0;
low_curr_h=0;
low_curr_l=0;
index=0;
inv_mode=0;
inv_on=0;
inv_off=0;
stdby_mode=0;
stdby=0;
pwm_pos=0;
pwm_neg=0;
delta_h_v=0;
delta_l_v=0;
//delta_h_ref_v=0;
//delta_l_ref_v=0;
//diff_h_v=0;
//diff_l_v=0;
delta_h_ref=0;
delta_l_ref=0;
//delta_h_ref_corr=0;
//delta_l_ref_corr=0;
//level=0;
//level_corr=0;
//level_l_corr=0;
for(i=0; i<32; i++) {
shape_corr[i]=0;
//shape_l_corr[i]=0;
level_corr[i]=0;
//level_adj[i]=0;
//shape_h_corr_temp[i]=0;
//shape_l_corr_temp[i]=0;
}
curr_h_temp=0;
curr_h_avg=0;
curr_l_temp=0;
curr_l_avg=0;
low_curr=0;
volt_h_temp=0;
volt_h_avg=0;
volt_l_temp=0;
volt_l_avg=0;
volt_ref_temp=0;
volt_ref_avg=0;
volt_ref_delta=0;
volt_ref_counter=0;
volt_h_avg_counter=0;
volt_l_avg_counter=0;
//volt_l_avg_counter=0;
stdby_i_temp=0;
stdby_i_avg_counter=0;
stdby_go_delay=0;
stdby_delay=2047;
high_t_blink=90;
low_acc_delay=0;
stdby_go_blink=60;
delay_ms(10000);
if(input(PIN_C4)) output_low(PIN_C5);
clear_interrupt(int_timer0);
set_timer0(60);
enable_interrupts(GLOBAL);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -