|
樓主 |
發表於 2014-8-6 01:41:27
|
顯示全部樓層
最新進度:
程式已經第一版暫定,內容如下:
電路板已經重焊一片,不幸的是因為接錯一條線導致一個MCU 報銷了!! 太陽電池(~12V)接到 PIN腳上 ...
程式上面抓臭蟲一兩天,發現 讀出太陽是否下山(電池電壓分壓) ==0 OK, ==1 NG, 發現應該為 !=0 就好了....
顯示電壓
顯示電流
顯示倒數時間(分)
驅動部分
等待外殼中
/*
20140801大改寫程式 中斷定時器1作為1/10 秒 1秒 1分 1時 計數並且把輸出 電流電壓顯示時候可以選擇三種模式on auto off(初始設定為AUTO模式) 太陽下山啟動輸出 太陽升起切斷輸出 電壓過低(10.8v) 或是時間到達就停止輸出,太陽再度升起時回復原先倒數計時的設定.
電池電壓充到 14.1V 或是電流超過4.0A 就停止充電,一方面保護電池充電不致過充膨龜,一方面利用限流保護 MOS 和 電池
溫度顯示功能,但是沒有把溫度的係數加到充電電壓裡面.
板子上的LED都沒作用,功力不夠,會影響到顯示和其他功能...
因為這電路板的IO位置無法使用內建的PWM(正常PC1PC2)
所以改用PB6控制輸出MOS PB7控制充電MOS1 PA2控制充電MOS2
10.8V 停放 14.1v 停充 13.1v~14.1 PWM 控制 13.1V以下全充
在電壓電流模式按鍵SW1 SW2 輸出ON/OFF
計時倒數終止時關閉輸出 MOS
太陽板輸入電壓停止時(沒有陽光) 輸出MOS 時間由設定值決定,當倒數計時完畢後回覆原設定
值 20140722
0804 ==1 !=0 差異很大
電壓超過14.1v 電流超過4.0A會停止充電,形成限流功能 20140805
*/
#include "stm8s.h"
#include "stm8s_tim1.h"
#include "stm8s_tim6.h"
#include "stm8s_adc1.h"
#define BUZZER GPIO_PIN_0
#define DCVoltage ADC1_CHANNEL_0
#define DCCurrent ADC1_CHANNEL_1
#define DCTemperature ADC1_CHANNEL_2
#define OUT_PUT_MOS GPIO_PIN_2//pin 5
#define OUT_PUT_MOS_PORT GPIOA
#define CHARGE_PORT GPIOB
#define CHARGE1_MOS GPIO_PIN_6
#define CHARGE2_MOS GPIO_PIN_7
#define PV_PORT GPIOF //太陽電池狀態接腳 0730
#define PV_INPUT GPIO_PIN_4 //0730
/*
#define LED_PORT GPIOC
#define OUT_PUT_LED4 GPIO_PIN_4
#define OUT_PUT_LED3 GPIO_PIN_5
#define LED_DRIVE_PORT GPIOD
#define OUT_PUT_DRIVE_PIN GPIO_PIN_5
*/
u16 Real_Voltage;//作為充電電壓放電電壓讀出
u16 Real_Current;//充電電流讀出
//typedef enum {DISABLE = 0, ENABLE = !DISABLE} FunctionalState;
u8 pcdis[]={0xd6,0x14,0xcc,0x5c,0x1e,0x5a,0xda,0x14,0xde,0x5e};//定義pc port 顯示的值
u8 pedis[]={0x01,0x00,0x01,0x01,0x00,0x01,0x01,0x01,0x01,0x01};//定義pe port 顯示的值對應于7段顯示
unsigned const int tempresist[]={28709,27421,26200,25042,23943,22899,21908, 20967, 20072, 19221, 18412, 17642, 16910,16213, 15549,14916,14314,13740,13192,12670,12172, 11696,11243, 10809, 10395,10000,9622,9261, 8916, 8585, 8269, 7967,7677,7400,7135,
6880, 6637, 6403, 6179, 5964, 5758, 5561, 5371, 5189, 5014, 4846, 4685, 4530, 4381, 4238, 4101, 3968, 3841, 3719,
3601, 3487, 3378, 3273, 3172, 3074, 2980, 2890, 2803, 2718, 2637, 2559, 2483, 2411, 2340, 2272, 2207, 2144, 2082, 2023, 1966, 1911, 1858, 1807, 1757, 1709, 1662, 1617, 1573, 1531, 1491, 1451, 1413,
1376, 1340, 1305, 1271, 1239, 1207, 1177, 1147, 1118, 1090, 1063,1037, 1011,987,963,939,917,895,874,853,833,813,794,776,758
};//b值3435 25度 10k NTC電阻=((1024count/NTCcount)-1)/10k
u16 Turn_off_Time;//倒數計時分鐘
u8 Hundreds,Tens,Units;//顯示百位十位個位
u16 voltage;//
u8 count6;//timer 6
u8 adc_update_flag;//ADC 更新1次的旗標
u8 display_time;//interrupt for display
u8 Start_Flag=0;
u16 Voltage_AD_count; //電壓AD值
u16 Current_AD_count; //電流AD值
u16 Temperature_AD_count; //電阻AD值
u8 AD_read_count;//0~3 輪流read out AD count
u16 ADCValue[10]={0};//電壓值平均用減少誤差雜訊
u16 DataADC=0;//最後的電壓值
u16 NTCvalue;//10K溫度電阻
u8 Degree_C,Degree_0C;//小數點
u8 PWM_Cycle_Complete;
u8 PWM_Time_Count;
u8 One_Sec_Flag;
u8 PWM_On_Count;
u8 PWM_Off_Count;
u8 PWM_Do;
u8 PhotoVoltaic_Status;//--------------------0730
u8 Turn_MOS_Out_Flag;
u8 OutPut_Mode=0;//select for on auto off
u8 One_Tenth_Count;//1/10 second
u8 One_Sec_Count;
u8 One_Minute_Count;
u16 Store_Turn_off_Time;
u8 Low_Bat_Flag;//Flag low battery voltage 10.8V
u8 One_Tenth_Flag;
u8 One_Minute_Flag;
u8 Night_Mode_Status;
u8 MOS2_Status;
u8 One_Hour_Flag;
u8 LED3_Flag;
u8 LED4_Flag;
void AD_Data_Update(void);
unsigned int ADC1Set(ADC1_Channel_TypeDef ADC_Channel);
void delay(unsigned int time);
void GPIOinit(void);
void GPIOflash(void);
void GPIO10flash(void);
void tim1init(void);//匯總 TIM1
void tim1svc(void);//中斷服務程式 閃爍10倍
unsigned int NTCresist(void);
void ADC_Temperature(void);
void KeyScan(void);
void delay(unsigned int time);
void TransData(void);
void Display(void);
void GPIOinit(void);
void tim1init(void);
void tim1svc(void);
void tim6init(void);
void tim6svc(void);
void ADConvert(void);
void DigitalFiltering(void);
unsigned char Compare_tempres(unsigned int TR);
void Display_Time(void);
void CHARGE_OUTPUT_Init(void);//----20140723 充電用
void Charge_MOS1(FunctionalState NewState);
void Charge_MOS2(FunctionalState NewState);
void Turn_MOS_OutPut(FunctionalState NewState);
void Charge_MOS1_PWM_Mode(void);
void PV_Down_Work (void);
void FORCE_Turn_MOS_ON(void);
void FORCE_Turn_MOS_OFF(void);
void Charge_Condition_Work(void);
//void LED3_Work (FunctionalState NewState);//會影響顯示
//void LED4_Work (FunctionalState NewState);
@far @interrupt void TIM1_OVF_IRQ(void);
@far @interrupt void TIM6_OVF_IRQ(void);
void main(void)
{
GPIOinit();//顯示用
CHARGE_OUTPUT_Init();//充電&輸出 太陽電池狀態用 0730
tim1init();
tim6init();
rim();
Turn_off_Time=120;
Store_Turn_off_Time=180;
while(1)
{
AD_Data_Update();
switch(Start_Flag)//顯示用
{
case 0: voltage = Real_Current;
break;
case 1: voltage = Real_Voltage;
break;
case 2:ADC_Temperature();
break;
case 3isplay_Time();
break;
default:break;
}
if(Real_Voltage<108)//電池過放電保護
{
Low_Bat_Flag=1;
FORCE_Turn_MOS_OFF();
}
else
Low_Bat_Flag=0;//低壓旗標
switch(OutPut_Mode)//手動on 自動 手動off
{
case 0: PV_Down_Work ();//太陽下山的自動開啟動作
break;
case 1: FORCE_Turn_MOS_OFF();
break;
case 2: FORCE_Turn_MOS_ON();
break;
default:break;
}
Charge_Condition_Work();//執行相關充電模式
}
}
void KeyScan(void)
{
GPIO_WriteLow(GPIOD,GPIO_PIN_2);
GPIO_WriteLow(GPIOD,GPIO_PIN_3);
GPIO_WriteLow(GPIOD,GPIO_PIN_4);
GPIO_WriteLow(GPIOD,GPIO_PIN_5);
GPIO_Init(GPIOC,(GPIO_PIN_6|GPIO_PIN_7),GPIO_MODE_IN_PU_NO_IT);//變更 sw4 sw5 成為 input
if(GPIO_ReadInputPin(GPIOC,GPIO_PIN_7)==0)//PC7 press sw4
{
delay(50);
if(GPIO_ReadInputPin(GPIOC,GPIO_PIN_7)==0)
{
while(GPIO_ReadInputPin(GPIOC,GPIO_PIN_7)==0);
if(Start_Flag==3)
{
Store_Turn_off_Time -=10;
if(Store_Turn_off_Time > 500)//0803
Store_Turn_off_Time=0;
Turn_off_Time = Store_Turn_off_Time;
Night_Mode_Status =0;
}
}
}
if(GPIO_ReadInputPin(GPIOB,GPIO_PIN_3)==0)//sw3
{
delay(50);
if(GPIO_ReadInputPin(GPIOB,GPIO_PIN_3)==0)
{
while(GPIO_ReadInputPin(GPIOB,GPIO_PIN_3)==0);
Start_Flag ++;
if (Start_Flag > 3)
Start_Flag=0;
}
}
if(GPIO_ReadInputPin(GPIOA,GPIO_PIN_1)==0)
{
delay(50);
if(GPIO_ReadInputPin(GPIOA,GPIO_PIN_1)==0)//sw1
{
while(GPIO_ReadInputPin(GPIOA,GPIO_PIN_1)==0);
if(Start_Flag==3)
{
Store_Turn_off_Time+=1;
if(Store_Turn_off_Time > 500)//0803
Store_Turn_off_Time=0;
Turn_off_Time = Store_Turn_off_Time;
Night_Mode_Status =0;
}
if(Start_Flag < 2)//-顯示電壓電流都可以手動輸出------------0728
{
OutPut_Mode++;//ON -- AUTO -- OFF
if(OutPut_Mode > 2)
OutPut_Mode=0;
}
}
}
if(GPIO_ReadInputPin(GPIOA,GPIO_PIN_3)==0)//sw2
{
delay(50);
if(GPIO_ReadInputPin(GPIOA,GPIO_PIN_3)==0)
{
while(GPIO_ReadInputPin(GPIOA,GPIO_PIN_3)==0);
if(Start_Flag==3)
{Turn_off_Time;
Store_Turn_off_Time+=10;
if(Store_Turn_off_Time > 500)//0803
Store_Turn_off_Time=0;
Turn_off_Time = Store_Turn_off_Time;
Night_Mode_Status =0;
}
}
}
if(GPIO_ReadInputPin(GPIOC,GPIO_PIN_6)==0)//PC6 press sw5
{
delay(50);
if(GPIO_ReadInputPin(GPIOC,GPIO_PIN_6)==0)
{
while(GPIO_ReadInputPin(GPIOC,GPIO_PIN_6)==0);
if(Start_Flag==3)
{
Store_Turn_off_Time-=1;
if(Store_Turn_off_Time > 501)//0803
Store_Turn_off_Time=0;
Turn_off_Time = Store_Turn_off_Time;
Night_Mode_Status =0;
}
}
}
}
void delay(unsigned int time)
{
while(time--);
}
void TransData(void)//顯示用的轉換
{
if(Start_Flag==2)
{ //溫度顯示
Hundreds=(Degree_C%100)/10;
Tens=Degree_C%10;
Units=Degree_0C;
}
else
{//電壓電流顯示
Hundreds=voltage/100;
Tens=(voltage%100)/10;
Units=voltage%10;
}
}
void Display(void)
{
GPIO_Init(GPIOC,GPIO_PIN_ALL,GPIO_MODE_OUT_PP_HIGH_FAST);//DISPLAY 7SEGMENT
GPIO_WriteLow(GPIOE,GPIO_PIN_5);
if(Hundreds>0)
{
GPIO_Write(GPIOC,pcdis[Hundreds]);
if(pedis[Hundreds])
GPIO_WriteHigh(GPIOE,GPIO_PIN_5);
else
GPIO_WriteLow(GPIOE,GPIO_PIN_5);
}
else
{
GPIO_Write(GPIOC,0x00);//百位消零
GPIO_WriteLow(GPIOE,GPIO_PIN_5);//去除A節
}
GPIO_Write(GPIOD,0x10);//趨動百位數
if(Start_Flag==0)//電流加上小數點
GPIO_WriteHigh(GPIOC,GPIO_PIN_5);//加上小數點
delay(500);
GPIO_WriteLow(GPIOC,GPIO_PIN_ALL);
delay(5);
GPIO_Write(GPIOC,pcdis[Tens]);
if(pedis[Tens])
GPIO_WriteHigh(GPIOE,GPIO_PIN_5);
else
GPIO_WriteLow(GPIOE,GPIO_PIN_5);
GPIO_Write(GPIOD,0x08);//趨動十位數
if((Start_Flag==1)||(Start_Flag==2))//溫度加上小數點
GPIO_WriteHigh(GPIOC,GPIO_PIN_5);//加上小數點
delay(500);
GPIO_WriteLow(GPIOC,GPIO_PIN_ALL);
delay(5);
GPIO_Write(GPIOC,pcdis[Units]);
if(pedis[Units])//消除a節問題
GPIO_WriteHigh(GPIOE,GPIO_PIN_5);
else
GPIO_WriteLow(GPIOE,GPIO_PIN_5);
GPIO_Write(GPIOD,0x04);//趨動個位數
delay(500);
GPIO_WriteLow(GPIOC,GPIO_PIN_ALL);//消除殘影
delay(5);
}
void GPIOinit(void)
{
GPIO_Init(GPIOC,GPIO_PIN_ALL,GPIO_MODE_OUT_PP_HIGH_FAST);//DISPLAY 7SEGMENT
GPIO_Init(GPIOD,GPIO_PIN_ALL,GPIO_MODE_OUT_PP_HIGH_FAST);//DISPLAY 3DIGIT
GPIO_Init(GPIOE,GPIO_PIN_5,GPIO_MODE_OUT_PP_HIGH_FAST);//DISPLAY SEGMENT A
GPIO_WriteLow(GPIOE,GPIO_PIN_5);
GPIO_Init(GPIOA,GPIO_PIN_1|GPIO_PIN_3,GPIO_MODE_IN_PU_NO_IT);//INPUT +/- TIME
GPIO_Init(GPIOB,GPIO_PIN_3,GPIO_MODE_IN_PU_NO_IT);//INPUT + FIRE POWER
}
void tim1init(void)//1秒1計數
{
TIM1_TimeBaseInit(19,TIM1_COUNTERMODE_UP,10000,0);//2M/20=100khz 除10000次=10hz
TIM1_ITConfig(TIM1_IT_UPDATE,ENABLE);
TIM1_GenerateEvent(TIM1_EVENTSOURCE_UPDATE);
TIM1_Cmd(ENABLE);
}
void tim1svc(void)//1/10秒1計數
{
//-------1/10 秒 作一次的工作 One_Tenth_Flag --> AD轉換
//-------1秒 作一次的工作 One_Sec_Flag --> 計秒 PWM 開關
//-------1分鐘作一次的工作 One_Minute_Flag --> 倒數計時關 斷MOS_OUTPUT 檢查日落
//-------1小時作一次的工作 One_Hour_Flag --> 計時
One_Tenth_Flag=1;
One_Tenth_Count++;
if(One_Tenth_Count == 10)
{
One_Sec_Flag=1;
One_Tenth_Count=0;
// PhotoVoltaic_Status=(GPIO_ReadInputPin(PV_PORT,PV_INPUT));//太陽電池lo就是下山
One_Sec_Count++;
if(One_Sec_Count == 60)//----------------60
{
One_Sec_Count=0;
One_Minute_Flag=1;
One_Minute_Count++;
PhotoVoltaic_Status=(GPIO_ReadInputPin(PV_PORT,PV_INPUT));//太陽電池lo就是下山
if(One_Minute_Count==60)
{
One_Minute_Count=0;
One_Hour_Flag=1;
}
}
}
adc_update_flag=1;//ADC轉換一次以避免過度忙碌
if(PWM_Do)//中斷中執行pwm 充電
{
Charge_MOS1_PWM_Mode();
}
TIM1_ClearFlag(TIM1_FLAG_UPDATE);
}
void tim6init(void)
{
TIM6_TimeBaseInit(TIM6_PRESCALER_32,250);//TIM6_PRESCALER_8 TIM6_TimeBaseInit
TIM6_SetCounter(250);
TIM6_ITConfig(TIM6_IT_UPDATE,ENABLE);//2Mhz/8/250
TIM6_Cmd(ENABLE);
}
void tim6svc(void)
{
display_time++;
if(display_time==3)
{
if(Start_Flag < 3)
TransData();
Display();
display_time=0;
}
count6++;
if (count6==50)//250>>25
{
KeyScan();
count6=0;
}
if(Turn_off_Time==1)//倒數計時到1分鐘
GPIO_WriteReverse(GPIOD,BUZZER);
TIM6_ClearITPendingBit(TIM6_IT_UPDATE);
}
unsigned int NTCresist(void)
{
unsigned int resist;
resist=(unsigned int)((unsigned long)(Temperature_AD_count)*10000/(1000-Temperature_AD_count));
return resist;
}
void ADConvert(void)
{
unsigned char ADCcount =0;
ADC1->CR1|=0x02;
ADC1_StartConversion();
while(ADCcount<10)
{
while(ADC1_GetFlagStatus(ADC1_FLAG_EOC)==RESET);
ADC1_ClearFlag(ADC1_FLAG_AWS0);
ADCValue[ADCcount]=ADC1_GetConversionValue();
ADCcount++;
}
ADC1->CR1 &= ~0x02;
}
void DigitalFiltering(void)
{
unsigned char i,j;
unsigned char cptemp;
for(i=10;i>=1;i--)
{
for(j-0;j<(i-1);j++)
{
if(ADCValue[j]>ADCValue[j+1])
{
cptemp=ADCValue[j];
ADCValue[j]=ADCValue[j+1];
ADCValue[j+1]=cptemp;
}
}
}
DataADC=0;
for(i=2;i<=7;i++)
{
DataADC +=ADCValue;
}
DataADC /=6;
}
unsigned char Compare_tempres(unsigned int TR)//電阻查表成為溫度
{
unsigned char cmp_cnt;
cmp_cnt =0;
while (TR<tempresist[cmp_cnt])
{
cmp_cnt++;
if (cmp_cnt>140)
break;
}
return cmp_cnt;
}
void ADC_Temperature(void)
{
//DataADC= Temperature_AD_count;//把溫度更新值
NTCvalue=NTCresist();//溫度AD值轉換成溫度電阻
Degree_C = Compare_tempres(NTCvalue);//溫度電阻查表成溫度值
Degree_0C= ((tempresist[Degree_C-1]-NTCvalue)*10)/(tempresist[Degree_C-1]-tempresist[Degree_C]);//差分成小數
}
void Display_Time(void)//顯示時間
{
Hundreds=Turn_off_Time/100;
Tens=(Turn_off_Time%100)/10;
Units=Turn_off_Time%10;
}
//改成電壓 電流 溫度 每秒讀取一次用為系統及時需要
void AD_Data_Update(void)
{
if(adc_update_flag==1)//定時更新減少mcu負擔
{
switch(AD_read_count)
{
case 0:Voltage_AD_count = ADC1Set(DCVoltage);
Real_Voltage=(unsigned int)((unsigned long)Voltage_AD_count*200/1023);//電壓ok 顯示12.0v
break;
case 1:Current_AD_count = ADC1Set(DCCurrent);
Real_Current=(unsigned int)((unsigned long)Current_AD_count*3000/1023);//電流有誤差約大0.2a使用.1*2歐姆電阻顯示1.00A
break;
case 2:Temperature_AD_count = ADC1Set(DCTemperature);
break;
default: break;
}
AD_read_count++;
if(AD_read_count > 2)
AD_read_count = 0;
}
adc_update_flag=0;
}
unsigned int ADC1Set(ADC1_Channel_TypeDef ADC_Channel)//讀取不同的AD通道
{
u16 ADresult;
{
ADC1_Init(ADC1_CONVERSIONMODE_CONTINUOUS,ADC_Channel,ADC1_PRESSEL_FCPU_D2,ADC1_EXTTRIG_TIM,DISABLE,ADC1_ALIGN_RIGHT,ADC1_SCHMITTTRIG_CHANNEL0,DISABLE);
ADConvert();
DigitalFiltering();
ADresult=DataADC;
DataADC=0;
adc_update_flag=0;
}
return ADresult;
}
void CHARGE_OUTPUT_Init(void)
{
GPIO_Init(CHARGE_PORT,CHARGE1_MOS,GPIO_MODE_OUT_PP_LOW_SLOW );
GPIO_WriteLow(CHARGE_PORT,CHARGE1_MOS);
GPIO_Init(CHARGE_PORT,CHARGE2_MOS,GPIO_MODE_OUT_PP_LOW_SLOW );
GPIO_WriteLow(CHARGE_PORT,CHARGE2_MOS);
GPIO_Init(OUT_PUT_MOS_PORT,OUT_PUT_MOS,GPIO_MODE_OUT_PP_LOW_SLOW );
GPIO_WriteLow(OUT_PUT_MOS_PORT,OUT_PUT_MOS);
GPIO_Init(PV_PORT,PV_INPUT,GPIO_MODE_IN_FL_NO_IT);//-----------0730
PWM_Cycle_Complete=1;
}
void Charge_MOS1(FunctionalState NewState)
{
if(NewState!=DISABLE)
GPIO_WriteHigh(CHARGE_PORT,CHARGE1_MOS);
else
GPIO_WriteLow(CHARGE_PORT,CHARGE1_MOS);
}
void Charge_MOS2(FunctionalState NewState)
{
if(NewState!=DISABLE)
GPIO_WriteHigh(CHARGE_PORT,CHARGE2_MOS);
else
GPIO_WriteLow(CHARGE_PORT,CHARGE2_MOS);
}
void Turn_MOS_OutPut(FunctionalState NewState)//sw1--ON sw2--OFF---0803
{
if(NewState!=DISABLE)
{
GPIO_WriteHigh(OUT_PUT_MOS_PORT,OUT_PUT_MOS);
}
else
{
GPIO_WriteLow(OUT_PUT_MOS_PORT,OUT_PUT_MOS);
}
}
void Charge_MOS1_PWM_Mode(void)
{
if(One_Sec_Flag)//One_Sec_Flag
{
if(PWM_On_Count != 0)
{
PWM_On_Count--;
One_Sec_Flag=0;
if(PWM_On_Count == 0)
{
Charge_MOS1(DISABLE);
}
}
if(PWM_On_Count==0)
{
PWM_Off_Count--;
One_Sec_Flag=0;
}
}
if(PWM_Off_Count==0)
{
PWM_Cycle_Complete=1;
PWM_Do=0;
}
}
//----------------0801--------------
void PV_Down_Work (void)
{
if ((PhotoVoltaic_Status==0)&(Low_Bat_Flag==0))//太陽電池lo就是下山&電壓正常
{
if (Night_Mode_Status ==0)//第一次重新載入
{
Night_Mode_Status =1;
Turn_off_Time = Store_Turn_off_Time;
}
if (One_Minute_Flag ==1)
{
One_Minute_Flag=0;
if(Turn_off_Time == 0)
{
Turn_MOS_OutPut(DISABLE);
MOS2_Status=0;
}
else
{
Turn_off_Time --;
FORCE_Turn_MOS_ON();//----0803
}
}
//-------------------------------------
}
else //電池電壓過低
{
Turn_MOS_OutPut(DISABLE);
MOS2_Status=0;
}
if (PhotoVoltaic_Status!=0)//
{
Turn_off_Time = Store_Turn_off_Time;
Night_Mode_Status =0;//可以再次設定定時
}
}
void FORCE_Turn_MOS_ON(void)
{
if(Low_Bat_Flag==0)
{
if(MOS2_Status !=1)
{
Turn_MOS_OutPut(ENABLE);
MOS2_Status=1;
}
}
else
{
Turn_MOS_OutPut(DISABLE);
MOS2_Status=0;
}
}
void FORCE_Turn_MOS_OFF(void)
{
if(MOS2_Status != 0)
{
Turn_MOS_OutPut(DISABLE);
MOS2_Status=0;
}
}
void Charge_Condition_Work(void)
{
//充電部分;比較假如電池電壓高於14.5v就不輸出切斷充電,13.5v就進入 pwm 模式,低於13.5v就全輸出不控制電流
//放電部分;若低於11.0v就停止輸出
//pwm 模式就要作部份的分割可以分成5段
if((Real_Voltage>141)||(Real_Current>400))//tune off charging operation overcurrent or overvoltage
{
Charge_MOS1(DISABLE);
PWM_Do =0;
}
if(Real_Voltage<=141 && Real_Voltage>130)//PWM Mode
{
PWM_Do = 1;//start PWM charging in Timer mode
if(PWM_Cycle_Complete==1)
{
PWM_On_Count=(unsigned char)(142-Real_Voltage);
PWM_Off_Count=(unsigned char)(Real_Voltage-129);
Charge_MOS1(ENABLE);
PWM_Cycle_Complete=0;
}
}
if(Real_Voltage <= 130)
{
Charge_MOS1(ENABLE);
PWM_Do =0;
}
}
/*
void LED4_Work (FunctionalState NewState)
{
GPIO_Init(LED_DRIVE_PORT,OUT_PUT_DRIVE_PIN,GPIO_MODE_OUT_PP_LOW_SLOW );
GPIO_Init(LED_PORT,OUT_PUT_LED4,GPIO_MODE_OUT_PP_LOW_SLOW );
if(NewState!=DISABLE)
{
GPIO_WriteHigh(LED_DRIVE_PORT,OUT_PUT_DRIVE_PIN);
GPIO_WriteHigh(LED_PORT,OUT_PUT_LED4);
LED4_Flag=1;
}
else
{
GPIO_WriteLow(LED_DRIVE_PORT,OUT_PUT_DRIVE_PIN);
GPIO_WriteLow(LED_PORT,OUT_PUT_LED4);
LED4_Flag=0;
}
}
void LED3_Work (FunctionalState NewState)
{
GPIO_Init(LED_DRIVE_PORT,OUT_PUT_DRIVE_PIN,GPIO_MODE_OUT_PP_LOW_SLOW );
GPIO_Init(LED_PORT,OUT_PUT_LED3,GPIO_MODE_OUT_PP_LOW_SLOW );
if(NewState!=DISABLE)
{
GPIO_WriteHigh(LED_DRIVE_PORT,OUT_PUT_DRIVE_PIN);
GPIO_WriteHigh(LED_PORT,OUT_PUT_LED3);
LED3_Flag=1;
}
else
{
GPIO_WriteLow(LED_DRIVE_PORT,OUT_PUT_DRIVE_PIN);
GPIO_WriteLow(LED_PORT,OUT_PUT_LED3);
LED3_Flag=0;
}
}
*/
//--------------------------------------------------------------------------
@far @interrupt void TIM1_OVF_IRQ(void)
{
tim1svc();
}
@far @interrupt void TIM6_OVF_IRQ(void)
{
tim6svc();
}
//Degree_0C= ((tempresist[Degree_C-1]-NTCvalue)*10)/(tempresist[Degree_C-1]-(tempresist[Degree_C]);
/*
unsigned int Get_ADCCH_Value(ADC1_Channel_TypeDef ADC_Channel)//網路上的例子
{
ADC1_Init(ADC1_CONVERSIONMODE_CONTINUOUS,ADC_Channel, ADC1_PRESSEL_FCPU_D2,\
ADC1_EXTTRIG_TIM, DISABLE, ADC1_ALIGN_RIGHT, ADC1_SCHMITTTRIG_ALL, DISABLE);
//你也可以用操作寄存器的方式代码如下,但未验证。
ADC1_ConversionConfig(ADC1_CONVERSIONMODE_CONTINUOUS, ADC_Channel,ADC1_ALIGN_RIGHT);
ADC_CR1 |= 0x01; //开始启动转换
while(ADC1_GetFlagStatus()==RESET); //等待转换结束
return ADC1_GetConversionValue(); //返回ADC结果
}
//这个初始化代码很重要,不能省,每次切换通道都要初始化一次!
//main函数中直接调用
// TempADC=Get_ADCCH_Value(ADC1_CHANNEL_1); //获取AD转化值
*/
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval : None
*/
void assert_failed(u8* file, u32 line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
中斷程式部分
/* BASIC INTERRUPT VECTOR TABLE FOR STM8 devices
* Copyright (c) 2007 STMicroelectronics
*/
typedef void @far (*interrupt_handler_t)(void);
struct interrupt_vector {
unsigned char interrupt_instruction;
interrupt_handler_t interrupt_handler;
};
@far @interrupt void NonHandledInterrupt (void)
{
/* in order to detect unexpected events during development,
it is recommended to set a breakpoint on the following instruction
*/
return;
}
@far @interrupt void TIM1_OVF_IRQ(void);
@far @interrupt void TIM6_OVF_IRQ(void);
extern void _stext(); /* startup routine */
struct interrupt_vector const _vectab[] = {
{0x82, (interrupt_handler_t)_stext}, /* reset */
{0x82, NonHandledInterrupt}, /* trap */
{0x82, NonHandledInterrupt}, /* irq0 */
{0x82, NonHandledInterrupt}, /* irq1 */
{0x82, NonHandledInterrupt}, /* irq2 */
{0x82, NonHandledInterrupt}, /* irq3 */
{0x82, NonHandledInterrupt}, /* irq4 */
{0x82, NonHandledInterrupt}, /* irq5 */
{0x82, NonHandledInterrupt}, /* irq6 */
{0x82, NonHandledInterrupt}, /* irq7 */
{0x82, NonHandledInterrupt}, /* irq8 */
{0x82, NonHandledInterrupt}, /* irq9 */
{0x82, NonHandledInterrupt}, /* irq10 */
{0x82, TIM1_OVF_IRQ}, /* irq11 每分鐘計數*/
{0x82, NonHandledInterrupt}, /* irq12 */
{0x82, NonHandledInterrupt}, /* irq13 */
{0x82, NonHandledInterrupt}, /* irq14 */
{0x82, NonHandledInterrupt}, /* irq15 */
{0x82, NonHandledInterrupt}, /* irq16 */
{0x82, NonHandledInterrupt}, /* irq17 */
{0x82, NonHandledInterrupt}, /* irq18 */
{0x82, NonHandledInterrupt}, /* irq19 */
{0x82, NonHandledInterrupt}, /* irq20 */
{0x82, NonHandledInterrupt}, /* irq21 */
{0x82, NonHandledInterrupt}, /* irq22 */
{0x82, TIM6_OVF_IRQ}, /* irq23 led閃爍 */
{0x82, NonHandledInterrupt}, /* irq24 */
{0x82, NonHandledInterrupt}, /* irq25 */
{0x82, NonHandledInterrupt}, /* irq26 */
{0x82, NonHandledInterrupt}, /* irq27 */
{0x82, NonHandledInterrupt}, /* irq28 */
{0x82, NonHandledInterrupt}, /* irq29 */
};
|
評分
-
3
查看全部評分
-
|