|
內(nèi)含輔助電路 功率電路 控制電路
/*2015全國電子設(shè)計(jì)大賽A題作品程序*/
#include <STC12C5A60S2.H>
#include <intrins.h> /*use _nop_() function*/
#define uchar unsigned char
#define uint unsigned int
#define ADC_FLAG 0X10
/*****************************************
端口功能設(shè)定
*****************************************/
sbit PWM_1 = P1^4; //PWM1_Boost/Buck
sbit SD_1 = P1^3; //SD1_Boost_Buck
sbit Key_1 = P2^1; //Key_1
sbit Key_2 = P2^2; //Key_2
sbit Key_3 = P2^3; //Key_3
uint ADC_value;
uchar PWM_temp=2;
uchar last_key; //按鍵變量
uchar channel=0; //第一通道
uchar Caes=0; //菜單
uint OUT_Current=300;
uint Battery_voltage; //輸入電壓
uint Boost_voltage; //Boost電壓
uint Boost_Buck_current; //Buck_Boost電流8位
uint O_current; //輸出電流
uint A_value=90;
bit lock=0; //PWM輸出鎖定
bit ADC_ready=0; //ADC完畢
bit K=0;
bit Charge=0;
void delay();
void Key_0_();
void Key_1_();
void Key_2_();
void Key_3_();
void init_pca();
void init_timer();
/**********************************************************
函數(shù)說明: 延時(shí)程序
**********************************************************/
void delay(uchar t)
{
uint j;
uchar i;
for(i=0;i<t;i++)
for(j=0;j<1000;j++);
}
/**********************************************************
函數(shù)說明: PWM初始化
**********************************************************/
void init_pwm()
{
CCON = 0;
CL = 0;
CH = 0;
CMOD = 0X08; //0X08:43KHZ // 0X02,22KHZ // 0X0A,10KHZ //
CCAPM1 =0x42;//8位PWM輸出,無中斷
CCAP1L =CCAP1H =250;//初始化spwm輸出的占空比
CR=1;
}
/**********************************************************
函數(shù)說明: 中斷 始化函數(shù)
**********************************************************/
void InitADC()
{
P1M1=0x47;
P1M0=0x00;
P1ASF=0X47; //選擇ADC通道0126
ADC_RES=0;
ADC_CONTR=0X88; //1110 0000 E8 開電源,最高速
EA=1;
EADC=1;
}
/**************************************
1602液晶
**************************************/
sbit RS = P2^5;
sbit RW = P2^6;
sbit E = P2^7;
/**************************************
短延時(shí)
**************************************/
void delay5ms()
{
uchar i,v,k;
for(i=1;i>0;i--)
for(v=168;v>0;v--)
for(k=22;k>0;k--);
}
/**************************************
寫指令
**************************************/
void lcd_Write_com(uchar com)
{
RS=0; //定義指令寄存器
RW=0; //寫允許
P0=com; //寫指令
delay5ms(); E=1; //片選端上拉
delay5ms(); E=0; //下降沿鎖存
}
/**************************************
寫數(shù)據(jù)
**************************************/
void lcd_write_date(uchar date)
{
RS=1; //定義數(shù)據(jù)寄存器
RW=0; //寫允許
P0=date; //寫數(shù)據(jù)
delay5ms(); E=1; //片選端上拉
delay5ms(); E=0; //下降沿鎖存
}
/**************************************
顯示程序
**************************************/
void dis_lcd1602(uchar x,uchar y,uchar dat)
{
uchar add;
if(y==1) add=(0x80+x);
if(y==2) add=(0xc0+x);
lcd_Write_com(add); //寫指令
lcd_write_date(dat); //寫數(shù)據(jù)
}
/**************************************
液晶初始化
**************************************/
void lcd1602_init()
{
lcd_Write_com(0x38); //設(shè)置8位格式,2行,5*7
lcd_Write_com(0x01); //清屏
lcd_Write_com(0x0c); //整體顯示,關(guān)光標(biāo),不閃爍
lcd_Write_com(0x06); //設(shè)定輸入方式,增量不移位
lcd_Write_com(0x80); //初始坐標(biāo)
}
/**************************************
液晶顯示漢字程序
**************************************/
void hz_lcdDis(uchar x,uchar y,uchar *p) //漢字顯示
{
uchar i=0,temp;
if(x)temp = 0x40; else temp = 0;
for(i=y;*p!='\0';i++,p++)
{
lcd_Write_com(i|0x80+temp);
lcd_write_date(*p);
delay5ms();
}
}
/**************************************
液晶顯示數(shù)字程序
**************************************/
void num_lcdDis(uchar X,uchar Y,uint num,uchar n)
{
uint i=0,temp,hang;
n +=1;
if(X)hang = 0x40;else hang = 0;
for(i=(n-1);i>0;i--)
{
lcd_Write_com((i+Y-1)|0x80+hang); //從個(gè)位開始顯示
temp = num%10+0x30;
lcd_write_date(temp);
delay5ms();
num/=10;
}
}
/**********************************************************
函數(shù)說明: 平均濾波函數(shù)
**********************************************************/
#define N 50
uint Filter(uint adc_data)
{
uint value=0;
uchar count,count_ADC;
for(count=0;count<N;count++)
{
for(count_ADC=0;count_ADC<3;count_ADC++)
{
while(!ADC_ready){}
ADC_ready=0;
}
value+=adc_data;
}
return (value);
}
/**********************************************************
函數(shù)說明:按鍵延時(shí)防抖動(dòng)
P3^5 Key_1
P3^6 Key_2
P3^7 Key_3
**********************************************************/
void key_scan()
{
uchar temp;
temp = P2&0x0F;
if(temp != 0x0F)
{
delay(10);
temp = P2&0x0F;
if(temp!= 0x0F)
{
switch(temp)
{
case 0x0E: Key_0_(); break; //20
case 0x0D: Key_1_(); break; //21
case 0x0B: Key_2_(); break; //22
case 0x07: Key_3_(); break; //23
default:break;
}
}
}
// last_key = temp;
}
/**********************************************************
函數(shù)說明: 按鍵程序
**********************************************************/
void Key_0_()
{
Charge=~Charge;
if(Charge) {CCAP1L =CCAP1H =128; SD_1=1; hz_lcdDis(0,8,"d");}
else {SD_1=0; hz_lcdDis(0,8,"c");}
}
void Key_1_()
{
if(K) {SD_1=0; lock=1; CCAP1L=CCAP1H =128; hz_lcdDis(0,8,"g"); } //boost的軟啟動(dòng),防止瞬間沖擊電流過大,影響后級(jí)穩(wěn)定性
else {SD_1=1; lock=0; hz_lcdDis(0,8,"k"); }
K=~K;
}
void Key_2_()
{
OUT_Current=OUT_Current+15;
if(OUT_Current>600)OUT_Current=600;
}
void Key_3_()
{
OUT_Current=OUT_Current-15;
if(OUT_Current<150)OUT_Current=150;
}
/**********************************************************
函數(shù)說明: 主函數(shù)
**********************************************************/
void main(void)
{
uint U1,U2,I1,S;
SD_1=0;
lcd1602_init();
hz_lcdDis(0,4,"Welcome!");
init_pwm();
InitADC();
delay(250);
// delay(250);
EA=1;//開總中斷
lcd_Write_com(0x01); //清屏
// SD_1=1;
hz_lcdDis(0,0,"IN= . V S= . A");
hz_lcdDis(1,0,"OU= . V I= . A");
while(1)//主循環(huán)
{
U1=Filter(Boost_voltage)/92; //輸入電壓 1:10采樣電阻 93,
U2=Filter(Battery_voltage)/92; //輸出電壓 1:10采樣電阻 93
I1=Filter(O_current)/150; //輸出電流
// if(I1>1) I1=I1-1;//輸出電流顯示校準(zhǔn)
if(U2>242)
{SD_1=0; lock=1; CCAP1L =CCAP1H =128; hz_lcdDis(0,8,"P"); }
num_lcdDis(0,3,U1/10,2);
num_lcdDis(0,6,U1%10,1);
num_lcdDis(1,3,U2/10,2);
num_lcdDis(1,6,U2%10,1);
……………………
|
|