示例一:
uint iPwmDuty; double temp; temp = (double)AdConvert(AN_TEMPERATURE); temp = temp/250; iPwmDuty = (int)(temp+0.5)>(int)temp?(int)temp+1:(int)temp;
如果参数在xx.5附近,易产生不稳定摆动
SMT(SF)
uint iPwmDuty; uint temp,temp4,temp6; static uint LastiPwmDuty; temp = AdConvert(AN_TEMPERATURE); temp = temp/25; //放大10倍,保留一位小数 temp6 = (temp+4)/10; temp4 = (temp+6)/10; temp = temp/10; if(temp6 > temp) { iPwmDuty = temp6; } else if(temp4 == temp) { iPwmDuty = temp; } else { if(temp >= LastiPwmDuty)//由小到大 { iPwmDuty = temp6; } else //(iPwmDuty < LastiPwmDuty) { iPwmDuty = temp4; } } LastiPwmDuty = iPwmDuty;
示例二:
uchar CanCharge(void) { uint temp; static uchar ChgState = FALSE; if(AdResult[AD_SUNBORD] > AdResult[AD_OPEN_BAT] ) { temp = AdResult[AD_SUNBORD] - AdResult[AD_OPEN_BAT]; } else { temp = 0; }
if (temp > 80)// 1V = 1/2.5/4.096*4096 = 400; 0.1V = 40; { ChgState = TRUE; } else if(temp <= 0) { ChgState = FALSE; } return ChgState; }
示例三:
//#include<pic16f1828.h> #include<htc.h> #include "adc.h" __CONFIG(FOSC_INTOSC&WDTE_OFF&PWRTE_ON&MCLRE_OFF&CP_ON&CPD_ON&BOREN_ON&CLKOUTEN_OFF&IESO_OFF&FCMEN_OFF); __CONFIG(WRT_ALL&PLLEN_OFF&STVREN_ON&LVP_OFF); #define uchar unsigned char #define uint unsigned int #define RLY1 LATA5 #define RLY2 LATA4 #define RLY3 LATC4 #define RLY_ON 0 #define RLY_OFF 1 #define LED_ON 1 #define LED_OFF 0 #define TRUE 1 #define FALSE 0 #define LED1 LATC2 //LED1(DS3)-RLY1 #define LED2 LATC1 //LED2(DS2)-RLY2 #define LED3 LATC0 //LED3(DS1)-RLY3 #define Connect3 262 //26.2V #define DisConnect3 238 //23.8V #define Connect2 260 #define DisConnect2 225 #define Connect1 255 //25.5V #define DisConnect1 215 //21.5V void DisplayLed(void); void main() { uint BAT_SENSE ,BAT_VOLT; static uint LoadConnect3,LoadConnect2,LoadConnect1; static uint LoadDisConnect3,LoadDisConnect2,LoadDisConnect1; static uchar RLY1_States,RLY2_States,RLY3_States; init_a2d(); TRISA = 0x08; //MCLR = 1; TRISC = 0x40; ANSELC= 0x40; //set anolog in BAT_SENSE = MiddleADValue(8); BAT_VOLT = (uint)(BAT_SENSE*0.78125);//AD/1024*5*16 = (V); 5*160/1024 =800/1024 = 0.78125(参数放大10倍) //Automatic system voltage recognition if(BAT_VOLT <180){//12V LoadConnect3 = Connect3>>1; LoadDisConnect3 = DisConnect3>>1; LoadConnect2 = Connect2>>1; LoadDisConnect2 = DisConnect2>>1; LoadConnect1 = Connect1>>1; LoadDisConnect1 = DisConnect1>>1; }else if(BAT_VOLT >360){//48V LoadConnect3 = Connect3<<1; LoadDisConnect3 = DisConnect3<<1; LoadConnect2 = Connect2<<1; LoadDisConnect2 = DisConnect2<<1; LoadConnect1 = Connect1<<1; LoadDisConnect1 = DisConnect1<<1; }else{//24V LoadConnect3 = Connect3; LoadDisConnect3 = DisConnect3; LoadConnect2 = Connect2; LoadDisConnect2 = DisConnect2; LoadConnect1 = Connect1; LoadDisConnect1 = DisConnect1; } RLY1_States = FALSE; RLY2_States = FALSE; RLY3_States = FALSE; RLY1 = RLY_OFF; RLY2 = RLY_OFF; RLY3 = RLY_OFF; LED1 = LED_OFF; LED2 = LED_OFF; LED3 = LED_OFF; } while(1) { BAT_SENSE = MiddleADValue(8); BAT_VOLT = (uint)(BAT_SENSE*0.78125);//5*160/1024 =800/1024 = 0.78125 if(BAT_VOLT < LoadDisConnect1) { RLY1_States = FALSE; } else if(BAT_VOLT > LoadConnect1) { RLY1_States = TRUE; } if(BAT_VOLT < LoadDisConnect2) { RLY2_States = FALSE; } else if(BAT_VOLT > LoadConnect2) { RLY2_States = TRUE; } if(BAT_VOLT < LoadDisConnect3) { RLY3_States = FALSE; } else if(BAT_VOLT > LoadConnect3) { RLY3_States = TRUE; } //===================================================== if(RLY1_States == TRUE) { RLY1 = RLY_ON; } else { RLY1 = RLY_OFF; } if(RLY2_States == TRUE) { RLY2 = RLY_ON; } else { RLY2 = RLY_OFF; } if(RLY3_States == TRUE) { RLY3 = RLY_ON; } else { RLY3 = RLY_OFF; } DisplayLed(); _delay(30000); } } void DisplayLed(void) { //LED state set synchronization if(RLY1_States == TRUE) { LED1 = LED_ON; } else { LED1 = !LED1; } if(RLY2_States == TRUE) { LED2 = LED_ON; } else { LED2 = !LED2; } if(RLY3_States == TRUE) { LED3 = LED_ON; } else { LED3 = !LED3; //One LED twinkle } }
//#include<pic16f1828.h> #include<htc.h> #include "adc.h" __CONFIG(FOSC_INTOSC&WDTE_ON&PWRTE_OFF&MCLRE_OFF&CP_ON&CPD_ON&BOREN_ON&CLKOUTEN_OFF&IESO_OFF&FCMEN_OFF); __CONFIG(WRT_ALL&PLLEN_OFF&STVREN_ON&LVP_OFF); #define uchar unsigned char #define uint unsigned int #define RLY1 LATA5 #define RLY2 LATA4 #define RLY3 LATC4 #define RLY_ON 0 #define RLY_OFF 1 #define LED_ON 1 #define LED_OFF 0 #define LED1 LATC2 //LED1(DS3)-RLY1 #define LED2 LATC1 //LED2(DS2)-RLY2 #define LED3 LATC0 //LED3(DS1)-RLY3 #define Connect3 262 //26.2V #define DisConnect3 238 //23.8V #define Connect2 260 #define DisConnect2 225 #define Connect1 255 //25.5V #define DisConnect1 215 //21.5V void DisplayLed(void); void main() { uchar flag; uint BAT_SENSE ,BAT_VOLT; static uint LoadConnect3,LoadConnect2,LoadConnect1; static uint LoadDisConnect3,LoadDisConnect2,LoadDisConnect1; init_a2d(); TRISA = 0x08; //MCLR = 1; TRISC = 0x40; ANSELC= 0x40; //set anolog in BAT_SENSE = MiddleADValue(8); BAT_VOLT = (uint)(BAT_SENSE*0.78125);//AD/1024*5*16 = (V); 5*160/1024 =800/1024 = 0.78125(参数放大10倍) //Automatic system voltage recognition if(BAT_VOLT <180){//12V LoadConnect3 = Connect3>>1; LoadDisConnect3 = DisConnect3>>1; LoadConnect2 = Connect2>>1; LoadDisConnect2 = DisConnect2>>1; LoadConnect1 = Connect1>>1; LoadDisConnect1 = DisConnect1>>1; }else if(BAT_VOLT >360){//48V LoadConnect3 = Connect3<<1; LoadDisConnect3 = DisConnect3<<1; LoadConnect2 = Connect2<<1; LoadDisConnect2 = DisConnect2<<1; LoadConnect1 = Connect1<<1; LoadDisConnect1 = DisConnect1<<1; }else{//24V LoadConnect3 = Connect3; LoadDisConnect3 = DisConnect3; LoadConnect2 = Connect2; LoadDisConnect2 = DisConnect2; LoadConnect1 = Connect1; LoadDisConnect1 = DisConnect1; } flag = 0; RLY1 = RLY_OFF; RLY2 = RLY_OFF; RLY3 = RLY_OFF; LED1 = LED_OFF; LED2 = LED_OFF; LED3 = LED_OFF; } while(1) { BAT_SENSE = MiddleADValue(8); BAT_VOLT = (uint)(BAT_SENSE*0.78125);//5*160/1024 =800/1024 = 0.78125 //Determine which range of BAT_VOLT if(BAT_VOLT<LoadDisConnect1) { RLY1 = RLY_OFF; RLY2 = RLY_OFF; RLY3 = RLY_OFF; flag = 0; } else if((BAT_VOLT>=LoadDisConnect1)&&(BAT_VOLT<LoadDisConnect2)) { if(flag > 1)//由大到小 { //RLY1 = RLY1; RLY2 = RLY_OFF; RLY3 = RLY_OFF; }else if(flag <= 1){ } flag = 1; } else if((BAT_VOLT>=LoadDisConnect2)&&(BAT_VOLT<LoadDisConnect3)) { if(flag > 2) { //RLY1 = RLY1; //RLY2 = RLY2; RLY3 = RLY_OFF; } flag = 2; } /*-------------------------------------------------------*/ else if((BAT_VOLT>=LoadDisConnect3)&&(BAT_VOLT<LoadConnect1)) { //RLY1 = RLY1; //RLY2 = RLY2; //RLY3 = RLY3; flag = 3; } /*-------------------------------------------------------*/ else if((BAT_VOLT>=LoadConnect1)&&(BAT_VOLT<LoadConnect2)) { if(flag < 4){ RLY1 = RLY_ON; //RLY2 = RLY2; //RLY3 = RLY3; } flag = 4; }else if((BAT_VOLT>=LoadConnect2)&&(BAT_VOLT<LoadConnect3)) { if(flag < 5) { RLY1 = RLY_ON; RLY2 = RLY_ON; //RLY3 = RLY3; } flag = 5; }else if(BAT_VOLT>=LoadConnect3) { RLY1 = RLY_ON; RLY2 = RLY_ON; RLY3 = RLY_ON; flag = 6; } DisplayLed(); _delay(30000); } } void DisplayLed(void) { //LED state set synchronization if(RLY1 == RLY_ON) { LED1 = LED_ON; } else { LED1 = !LED1; } if(RLY2 == RLY_ON) { LED2 = LED_ON; } else { LED2 = !LED2; } if(RLY3 == RLY_ON) { LED3 = LED_ON; } else { LED3 = !LED3; //One LED twinkle } }
施密特算法