#include <reg52.h> //包含单片机的头文件
//#include <intrins.h>
//#include <string.h>
//#include <math.h>
//#include "stdio.h"
//#include <stdarg.h>
//#include<stdlib.h>
#include"lcd.h" //包含LCD的头文件
#include"iic.h" //包含IIC通信的头文件
#define uchar unsigned char //自定义的数据类型
#define uint unsigned int //自定义的数据类型
sbit RED = P3^5; //定义RED接在单片机的P3^5引脚上
sbit YELLOW = P3^6; //定义YELLOW接在单片机的P3^6引脚上
sbit GREEN = P3^7; //定义GREEN接在单片机的P3^7引脚上
int AD; //AD
int CYCLE; //电压
float CYC; //电压
int ELE; //电量(百分比)
/******************************************//*
*
*函数名:void delay(uint z)
*参数:uint z
*返回值:void
*函数功能:延时函数
*
*//******************************************/
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=120;y>0;y--);
}
/******************************************//*
*
*函数名:int main() 改为int main()
*参数:void
*返回值:int
*函数功能:主函数
*
*//******************************************/
void main()
{
LcdInit(); //调用LCD初始化函数,进行LCD的初始化
init_pcf8591(0x03); //应该也是个初始化函数
RED = 0;YELLOW=0;GREEN=0; //让RED,YELLOW,GREEN引脚都为零
while(1)
{
AD = adc_pcf8591(); //采集电压,让数值送入AD变量
LcdWriteCom(0x80); //对LCD显示屏进行写命令
LcdWriteData('A'); //对LCD显示屏进行写数据(显示的东西)
LcdWriteData('D');
LcdWriteData(':');
LcdWriteData('0'+AD/100); //‘0’+AD/100这个加‘0’的意思是进行ASCII码的转换
LcdWriteData('0'+AD/10%10);
LcdWriteData('0'+AD%10);
CYC = (AD*100)/51; //CYC变量进行运算
CYCLE = CYC;
LcdWriteCom(0x80+0X40); //对LCD显示屏进行写命令
LcdWriteData('V'); //对LCD显示屏进行写数据(显示的东西)
LcdWriteData('O');
LcdWriteData(':');
LcdWriteData('0'+CYCLE/100);
LcdWriteData('.');
LcdWriteData('0'+CYCLE/10%10);
LcdWriteData('0'+CYCLE%10);
LcdWriteData('V');
//3.20V-4.20V
ELE = CYCLE-320;
LcdWriteCom(0x8A+0X40);
LcdWriteData('E');
LcdWriteData(':');
LcdWriteData('0'+ELE/100);
LcdWriteData('0'+ELE/10%10);
LcdWriteData('0'+ELE%10);
LcdWriteData('%');
//以下内容就是对数据进行选择处理,读取并显示电压以及电量百分比 并依据不同的百分比驱动不同的指示灯
switch(ELE/10%10)
{
case 0:
case 1:
case 2:
case 3:
{
RED = 1;
YELLOW=0;
GREEN=0;
} break; //电量在0-30% 红灯亮
case 4:
case 5:
case 6:
case 7:
{
RED = 0;
YELLOW=1;
GREEN=0;
} break;
case 8: case 9: {RED = 0;YELLOW=0;GREEN=1;} break;
default : {RED = 0;YELLOW=0;GREEN=0;} break;
}
// delay(100);
}
} |