由于本人在单片机数菜鸟级别,下面这程序已经调试半个来月,搞得寝食难安,现已经筋疲力尽,在这发出求助 还请大神们伸出援助之手 帮帮小弟,小弟将万分感激。本程序为电池保护程序 ,过放切断负载 过充切断 充电太阳能板
// PIC16F616头文件
//************************************************************************
#include <pic.h>
#include <pic16f616.h>
//************************************************************************
// PIC16F616配置
//************************************************************************
__CONFIG(0x3F04);
//************************************************************************
// 宏定义
#define uint unsigned int
#define uchar unsigned char
#define Solar_SW RA2
#define Load_SW RA4
#define led RA5
#define LOW_V 224 //2.8V
#define LOW_renew 253 //3.1V
#define HIGH_V 341 //4.25V
#define HIGH_renew 327 //4.08V
#define Solar_V 200 //2.5V
//=======================================================================
void delay1(uint x) //延时程序 1ms
{
uint a,b;
for(a=x;a>0;a--)
for(b=110;b>0;b--);
}
//================================================================
//************************************************************************
// 全局变量
//************************************************************************
uint DC_vbat,DC_vbat1;
void initialIO()
{
TRISA=0x01;
TRISC=0x01;
TRISA3=1;
PORTA=0xfe;
ANSEL=0x11;
delay1(10);
}
//========================================================================
// AD初始化
//***********************************************************************
void ad_init(uchar channel)
{
while(GO_nDONE);
switch(channel)
{
case 0:TRISA0=1;ANS0=1; break;
case 1:TRISA1=1;ANS1=1; break;
case 2:TRISA2=1;ANS2=1; break;
case 3:TRISA4=1;ANS3=1; break;
case 4:TRISC0=1;ANS4=1; break;
case 5:TRISC1=1;ANS5=1; break;
case 6:TRISC2=1;ANS6=1; break;
case 7:TRISC3=1;ANS7=1; break;
}
ADCON0=0xc1;
ADCON1=0x04;
delay1(10);
}
//***********************************************************************
// AD读电压
//************************************************************************
uint ad_read(uchar channel)
{
uint adval;
while(GO_nDONE);
ADON=1;
switch(channel)
{
case 0: {CHS2=0;CHS1=0;CHS0=0;}break;
case 1: {CHS2=0;CHS1=0;CHS0=1;}break;
case 2: {CHS2=0;CHS1=1;CHS0=0;}break;
case 3: {CHS2=0;CHS1=1;CHS0=1;}break;
case 4: {CHS2=1;CHS1=0;CHS0=0;}break;
case 5: {CHS2=1;CHS1=0;CHS0=1;}break;
case 6: {CHS2=1;CHS1=1;CHS0=0;}break;
case 7: {CHS2=1;CHS1=1;CHS0=1;}break;
default:break;
}
delay1(10);
GO_nDONE=1;
while(GO_nDONE);
adval=ADRESH;
adval=adval<<8|ADRESL;
return(adval);
}
//************************************************************************
void chargingled()
{
DC_vbat1=ad_read(4);
if(DC_vbat1<=Solar_V)
{
led=1;
delay1(600);
led=0;
delay1(600);
DC_vbat1=ad_read(4);
}
else
led=1;
}
//=======================================================================
// 主函数
//************************************************************************
void Dischang()
{
delay1(10);
DC_vbat=ad_read(0);
if(DC_vbat<=LOW_V) //低压保护
{
Solar_SW=0;
Load_SW=1;
DC_vbat1=ad_read(4);
chargingled();
delay1(10);
do{
DC_vbat=ad_read(0);
delay1(10);
Solar_SW=0;
delay1(20);
chargingled();
Load_SW=1;
delay1(50);
}
while(DC_vbat<=LOW_renew);//2.5~2.9低压恢复;
//Load_SW=1;
}
else if((DC_vbat>LOW_V)&&(DC_vbat< HIGH_V)) //充电放电
{
Load_SW=0;
delay1(10);
Solar_SW=0;
delay1(10);
chargingled();
delay1(10);
}
else if (DC_vbat>=HIGH_V) //高压保护
{
Solar_SW=1;
do{
Solar_SW=1;
Load_SW=0;
delay1(20);
chargingled();
delay1(10);
}
while((DC_vbat>=HIGH_renew)); //4.08~4.35高压恢复
}
}
//=======================================================================
// 主函数
//************************************************************************
void main(void)
{
delay1(1000);
initialIO();
ad_init(0);
ad_init(4);
while(1)
{
Dischang();
}
}
此帖出自
小平头技术问答
好吧 还是得自己解决
一周热门 更多>