51单片机怎么编程从BQ78412中取出自己想要的电压值?我编的问题在哪里
[复制链接]
#include "reg51.h"
#include "intrins.h"
typedef unsigned char BYTE;
typedef unsigned int WORD;
#define FOSC 18432000L //System frequency
#define BAUD 115200 //UART baudrate
/*Define UART parity mode*/
#define NONE_PARITY 0 //不校验
#define ODD_PARITY 1 //Odd parity
#define EVEN_PARITY 2 //Even parity
#define MARK_PARITY 3 //Mark parity
#define SPACE_PARITY 4 //Space parity
#define PARITYBIT EVEN_PARITY //Testing even parity
/*Declare SFR associated with the UART2 */
sfr AUXR = 0x8e; //Auxiliary register
sfr S2CON = 0x9a; //UART2 control register
sfr S2BUF = 0x9b; //UART2 data buffer
sfr BRT = 0x9c; //Baudrate generator
sfr IE2 = 0xaf; //Interrupt control 2
#define S2RI 0x01 //S2CON.0
#define S2TI 0x02 //S2CON.1
#define S2RB8 0x04 //S2CON.2
#define S2TB8 0x08 //S2CON.3
#define uchar unsigned char
#define uint unsigned int
sbit ds=P2^2;
sbit rs=P2^5;
sbit rw=P2^6;
sbit en=P2^7;
sbit k1=P2^1;
sbit k2=P2^3;
sbit k3=P2^4;
sbit jinggao=P1^4;
bit busy;
int flay;
BYTE temp[4];
WORD value;
WORD tem;
WORD vol;
WORD cur;
WORD rcd;
/*定义字符串*/
uchar code table1[]={"Temperature(C):"};
uint code table_T[]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};
uchar code table2[]={"Voltage(100mV):"};
uint code table_V[]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};
uchar code table3[]={"Current(100mA):"};
uint code table_C[]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};
uchar code table4[]={"RCD(100mAH):"};
uint code table_R[]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};
/*定义函数*/
void init();
void panduan();
void Zhuanhuan();
void SendData(BYTE dat);
void Uart2();
void write_FCC();
void delay(uint a);
void delay_ms(uint a);
void write_data(uchar date);
void write_com(uchar com);
void read_vol();
void read_tem();
void read_cur();
void read_rcd();
void display_cur();
void display_rcd();
void display_tem();
void display_vol();
void display_C(uint cur);
void display_R(uint rcd);
void display_T(uint tem);
void display_V(uint vol);
void main() //主函数
{
BRT = -(FOSC/32/BAUD); //定义波特率
AUXR = 0x14; //定义串口通信工作模式为T1
IE2 = 0x01; //允许外部中断
EA = 1; //开中断
write_FCC(); //向BQ78412中写入蓄电池总容量
while(1)
{
read_tem(); //从BQ78412中读取温度值
read_vol(); //从BQ78412中读取电压值
read_cur(); //从BQ78412中读取电流值
read_rcd(); //从BQ78412中读取剩余电量值
panduan(); //根据电量值进行判断
display_tem(); //显示温度值
display_vol(); //显示电压值
display_cur(); //显示电流值
display_rcd(); //显示剩余电量值
}
}
void display_rcd()
{
uchar i;
init(); //初始化液晶显示屏
write_com(0x80);
for(i=0;i<12;i++)
{
write_data(table4[i]);
}
write_com(0x80+0x4c);
write_data(0xdf);
write_com(0x80+0x4d);
write_data(0x43);
write_com(0x80+0x48);
write_data(0x2e);
display_R(rcd);
delay(30);
}
void display_R(uint rcd) //显示
{
uchar q,b,a,d;
q=rcd/1000;
b=rcd/100%10;
a=rcd/10%10;
d=rcd%10;
write_com(0x80+0x46);
write_data(table_R[q]);
write_com(0x80+0x47);
write_data(table_R[b]);
write_com(0x80+0x48);
write_data(table_R[a]);
write_com(0x80+0x49);
write_data(table_R[d]);
delay_ms(10);
}
void display_cur()
{
uchar i;
init(); //初始化液晶显示屏
write_com(0x80);
for(i=0;i<15;i++)
{
write_data(table3[i]);
}
write_com(0x80+0x4c);
write_data(0xdf);
write_com(0x80+0x4d);
write_data(0x43);
write_com(0x80+0x48);
write_data(0x2e);
display_C(cur);
delay(30);
}
void display_C(uint cur) //显示
{
uchar q,b,a;
q=cur/100;
b=cur/10%10;
a=cur%10;
write_com(0x80+0x46);
write_data(table_C[q]);
write_com(0x80+0x47);
write_data(table_C[b]);
write_com(0x80+0x48);
write_data(table_C[a]);
delay_ms(10);
}
void display_vol()
{
uchar i;
init(); //初始化液晶显示屏
write_com(0x80);
for(i=0;i<16;i++)
{
write_data(table2[i]);
}
write_com(0x80+0x4c);
write_data(0xdf);
write_com(0x80+0x4d);
write_data(0x43);
write_com(0x80+0x48);
write_data(0x2e);
display_V(vol);
delay(30);
}
void display_tem() //
{
uchar i;
init(); //初始化液晶显示屏
write_com(0x80);
for(i=0;i<16;i++)
{
write_data(table1[i]);
}
write_com(0x80+0x4c);
write_data(0xdf);
write_com(0x80+0x4d);
write_data(0x43);
write_com(0x80+0x48);
write_data(0x2e);
display_T(tem);
delay(30);
}
void display_V(uint vol) //显示
{
uchar q,b,a,d,e;
q=vol/10000;
b=vol/1000%10;
a=vol/100%10;
q=vol/10%10;
q=vol%10;
write_com(0x80+0x46);
write_data(table_V[q]);
write_com(0x80+0x47);
write_data(table_V[b]);
write_com(0x80+0x48);
write_data(table_V[a]);
write_com(0x80+0x49);
write_data(table_V[d]);
write_com(0x80+0x50);
write_data(table_V[e]);
delay_ms(10);
}
void write_com(uchar com) //写函数
{
rs=0;
P0=com;
delay_ms(5);
en=1;
delay_ms(5);
en=0;
}
void write_data(uchar date) //写数据
{
rs=1;
P0=date;
delay_ms(5);
en=1;
delay_ms(5);
en=0;
}
void delay_ms(uint a) //延时ms
{
uint x,y;
for(x=a;x>0;x--)
for(y=110;y>0;y--);
}
void delay(uint a) //显示延时
{
uint x,y,z;
for(x=a;x>0;x--)
for(y=110;y>0;y--)
for(z=1000;z>0;z--);
}
void display_T(uint tem) //显示
{
uchar q,b;
q=tem/10;
b=tem%10;
write_com(0x80+0x46);
write_data(table_T[q]);
write_com(0x80+0x47);
write_data(table_T[b]);
delay_ms(10);
if(flay)
{
flay=0;
write_com(0x80+0x45);
write_data(0x2d);
delay_ms(10);
}
}
void panduan()
{
if(cur>=2000&&k1==1&&k2==1&&k3==0)
{
k1=0;
}
else if(cur>=2000&&k1==0&&k2==1&&k3==0)
{
k3=1;
}
else if(cur>=2000&&k1==0&&k2==1&&k3==1)
{
k2=0;
}
else if(cur>=2000&&k1==0&&k2==0&&k3==1);
else if(cur<2000&&cur>=1600);
else if(cur<1600&&cur>=400)
{
k1=1;
k2=1;
k3=0;
}
else if(cur<400&&k1==1&&k2==1&&k3==0)
{
jinggao=1;
}
}
void write_FCC()
{
#if (PARITYBIT == NONE_PARITY)
S2CON = 0x50; //8-bit variable UART
#elif (PARITYBIT == ODD_PARITY) || (PARITYBIT == EVEN_PARITY) || (PARITYBIT == MARK_PARITY)
S2CON = 0xda; //9-bit variable UART, parity bit initial to 1
#elif (PARITYBIT == SPACE_PARITY)
S2CON = 0xd2; //9-bit variable UART, parity bit initial to 0
#endif
SendData(0xff);
SendData(0x17);
SendData(0x24);
SendData(0x00);
SendData(0xd0);
SendData(0x07);
}
void read_rcd()
{
#if (PARITYBIT == NONE_PARITY)
S2CON = 0x50; //8-bit variable UART
#elif (PARITYBIT == ODD_PARITY) || (PARITYBIT == EVEN_PARITY) || (PARITYBIT == MARK_PARITY)
S2CON = 0xda; //9-bit variable UART, parity bit initial to 1
#elif (PARITYBIT == SPACE_PARITY)
S2CON = 0xd2; //9-bit variable UART, parity bit initial to 0
#endif
SendData(0xff);
SendData(0x16);
SendData(0x02);
SendData(0x00);
SendData(0x01);
SendData(0x00);
Zhuanhuan();
rcd=value;
}
void read_cur()
{
#if (PARITYBIT == NONE_PARITY)
S2CON = 0x50; //8-bit variable UART
#elif (PARITYBIT == ODD_PARITY) || (PARITYBIT == EVEN_PARITY) || (PARITYBIT == MARK_PARITY)
S2CON = 0xda; //9-bit variable UART, parity bit initial to 1
#elif (PARITYBIT == SPACE_PARITY)
S2CON = 0xd2; //9-bit variable UART, parity bit initial to 0
#endif
SendData(0xff);
SendData(0x16);
SendData(0x06);
SendData(0x00);
SendData(0x01);
SendData(0x00);
Zhuanhuan();
cur=value;
}
void read_tem()
{
#if (PARITYBIT == NONE_PARITY)
S2CON = 0x50; //8-bit variable UART
#elif (PARITYBIT == ODD_PARITY) || (PARITYBIT == EVEN_PARITY) || (PARITYBIT == MARK_PARITY)
S2CON = 0xda; //9-bit variable UART, parity bit initial to 1
#elif (PARITYBIT == SPACE_PARITY)
S2CON = 0xd2; //9-bit variable UART, parity bit initial to 0
#endif
SendData(0xff);
SendData(0x16);
SendData(0x02);
SendData(0x00);
SendData(0x01);
SendData(0x00);
Zhuanhuan();
tem=value;
}
void read_vol()
{
#if (PARITYBIT == NONE_PARITY)
S2CON = 0x50; //8-bit variable UART
#elif (PARITYBIT == ODD_PARITY) || (PARITYBIT == EVEN_PARITY) || (PARITYBIT == MARK_PARITY)
S2CON = 0xda; //9-bit variable UART, parity bit initial to 1
#elif (PARITYBIT == SPACE_PARITY)
S2CON = 0xd2; //9-bit variable UART, parity bit initial to 0
#endif
SendData(0xff);
SendData(0x16);
SendData(0x04);
SendData(0x00);
SendData(0x01);
SendData(0x00);
Zhuanhuan();
vol=value;
}
void init() //初始化1602液晶
{
rw=0;
en=0;
delay_ms(20);
write_com(0x38);
delay_ms(10);
write_com(0x38);
delay_ms(10);
write_com(0x38);
write_com(0x08);
write_com(0x01);
write_com(0x06);
write_com(0x0c);
write_com(0x80);
}
/*----------------------------
UART2 interrupt service routine
----------------------------*/
void Uart2() interrupt 8 using 1
{
int i;
if (S2CON & S2RI)
{
S2CON &= ~S2RI; //Clear receive interrupt flag
for(i=0; i<4; i++)
{
temp[i] = S2BUF;
}
P2 = (S2CON & S2RB8);//P2.2 show parity bit
}
if (S2CON & S2TI)
{
S2CON &= ~S2TI; //Clear transmit interrupt flag
busy = 0; //Clear transmit busy flag
}
}
/*----------------------------
Send a byte data to UART
Input: dat (data to be sent)
Output:None
----------------------------*/
void SendData(BYTE dat)
{
while (busy); //Wait for the completion of the previous data is sent
ACC = dat; //Calculate the even parity bit P (PSW.0)
if (P) //Set the parity bit according to P
{
#if (PARITYBIT == ODD_PARITY)
S2CON &= ~S2TB8; //Set parity bit to 0
#elif (PARITYBIT == EVEN_PARITY)
S2CON |= S2TB8; //Set parity bit to 1
#endif
}
else
{
#if (PARITYBIT == ODD_PARITY)
S2CON |= S2TB8; //Set parity bit to 1
#elif (PARITYBIT == EVEN_PARITY)
S2CON &= ~S2TB8; //Set parity bit to 0
#endif
}
busy = 1;
S2BUF = ACC; //Send data to UART2 buffer
}
void Zhuanhuan()
{
value = temp[4];
value <<= 8;
value |= temp[3];
}