#include <reg52.h>
#include <intrins.h>
sbit ADDR0=P1^0;
sbit ADDR1=P1^1;
sbit ADDR2=P1^2;
sbit ADDR3=P1^3;
sbit ENLED=P1^4;
sbit BUZZ = P1^6; //蜂鸣器控制引脚
bit flag300ms = 1; //300ms定时标志
unsigned char T0RH = 0; //T0重载值的高字节
unsigned char T0RL = 0; //T0重载值的低字节
bit flagUart=0;
bit d=0;
static unsigned int cnt=0;
unsigned int t=0;
unsigned int t0=0;
static unsigned char m=1;
static unsigned char c=0;
static unsigned char v=0;
unsigned char RxdByte = 0; //串口接收到的字节
void ConfigTimer0(unsigned int ms);
unsigned char GetADCValue(unsigned char chn);
void ValueToString(unsigned char *str, unsigned long val);
extern void KeyScan();
extern void KeyDriver();
extern void I2CStart();
extern void I2CStop();
extern unsigned char I2CReadACK();
extern unsigned char I2CReadNAK();
extern bit I2CWrite(unsigned char dat);
extern void Led1();
extern void Led2();
extern void Led3();
extern void Led4();
extern void Led5();
extern void Led6();
extern void Led7();
extern void Led8();
extern void Led9();
extern void Led0();
extern void InitLcd1602();
extern void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str);
extern void UartDriver();
extern void ConfigUART(unsigned int baud);
extern void UartRxMonitor(unsigned char ms);
extern void UartWrite(unsigned char *buf, unsigned char len);
void UsartConfiguration();
unsigned char code LedChar[] = { //数码管显示字符转换表
0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E
};
unsigned char LedBuff[7] = { //数码管+独立 LED 显示缓冲区
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
};
unsigned char code L1[7]={0xE7,0xDB,0xBD,0x7E,0xBD,0xDB};
//其实就是定义了一个亮灯状态表,由中间到两边,再由两边倒中间,然后P0循环此数组
//1110 0111 , 1101 1011 , 1011 1101 ,0111 1110, 1011 1101, 1101 1011, 1110 0111
unsigned char code L2[30]={0xE7,0xDB,0xBD,0x7E,0xBD,0xDB,
0x7F,0xBF,0xDF,0xEF,0xF7,0xFB,0xFD,0xFE,
0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F,0xEF}; //22
unsigned char code L3[50]={0xEF,0xDF,0xBF,0x7F, 0xF7,0xFB,0xFD,0xFE,
0xEF,0xDF,0xBF,0x7F,0xBF,0xDF,0xEF,0xF7,0xFB,0xFD,0xFE,
0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F,0xBF,0xDF}; //28
unsigned char ledd3[20]={0xFC,0xF9,0xE7,0xCF,0x9F,0x3F,0x7F,0x3F,0x9F,0xCF,0xE7,0xF9,0xFC,0xFE}; //14
unsigned char code ledd4[25]={0x7F,0x3F,0x1F,0x0F,0x87,0xC3,0xE1,
0xF0,0xF8,0xFC,0xFE,0xFC,0xF8,0xF0,0xE1,0xC3,
0x87,0x0F,0x1F,0x3F,0x7F}; // 21
unsigned char code ledd5[20]={0xFA,0xFD,0xF5,0xF4,0xEB,0xF7,0xD7,0xEF,0xAF,0xDF,0x5F,0xBF};
//unsigned char val;
unsigned char code image[11][8] = {
{0xC3, 0x81, 0x99, 0x99, 0x99, 0x99, 0x81, 0xC3}, //数字0
{0xEF, 0xE7, 0xE3, 0xE7, 0xE7, 0xE7, 0xE7, 0xC3}, //数字1
{0xC3, 0x81, 0x9D, 0x87, 0xC3, 0xF9, 0xC1, 0x81}, //数字2
{0xC3, 0x81, 0x9D, 0xC7, 0xC7, 0x9D, 0x81, 0xC3}, //数字3
{0xCF, 0xC7, 0xC3, 0xC9, 0xC9, 0x81, 0xCF, 0xCF}, //数字4
{0x81, 0xC1, 0xF9, 0xC3, 0x87, 0x9D, 0x81, 0xC3}, //数字5
{0xC3, 0x81, 0xF9, 0xC1, 0x81, 0x99, 0x81, 0xC3}, //数字6
{0x81, 0x81, 0x9F, 0xCF, 0xCF, 0xE7, 0xE7, 0xE7}, //数字7
{0xC3, 0x81, 0x99, 0xC3, 0xC3, 0x99, 0x81, 0xC3}, //数字8
{0xC3, 0x81, 0x99, 0x81, 0x83, 0x9F, 0x83, 0xC1}, //数字9
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, //全亮
};
static char str[10]={0,0,0,0,0,0,0,0};
unsigned int code NoteFrequ[] = { //中音 1-7 和高音 1-7 对应频率列表
523, 587, 659, 698, 784, 880, 988, //中音 1-7
1047, 1175, 1319, 1397, 1568, 1760, 1976 //高音 1-7
};
unsigned int code NoteReload[] = { //中音 1-7 和高音 1-7 对应的定时器重载值
65536 - (11059200/12) / (523*2), //中音 1
65536 - (11059200/12) / (587*2), //2
65536 - (11059200/12) / (659*2), //3
65536 - (11059200/12) / (698*2), //4
65536 - (11059200/12) / (784*2), //5
65536 - (11059200/12) / (880*2), //6
65536 - (11059200/12) / (988*2), //7
65536 - (11059200/12) / (1047*2), //高音 1
65536 - (11059200/12) / (1175*2), //2
65536 - (11059200/12) / (1319*2), //3
65536 - (11059200/12) / (1397*2), //4
65536 - (11059200/12) / (1568*2), //5
65536 - (11059200/12) / (1760*2), //6
65536 - (11059200/12) / (1976*2), //7
0,
};
bit flagBuzz = 0; //蜂鸣器发声使能标志
bit tmrflag = 0; //定时器中断完成标志
void PlayTwoTiger();
void main()
{
unsigned int i;
unsigned char val;
EA = 1; //开总中断
ENLED=0;
ConfigTimer0(10); //配置T0定时10ms
ConfigUART(9600); //配置波特率为9600
InitLcd1602(); //初始化液晶
//led1();
while (1)
{
switch(m)
{
case 1: LcdShowStr(5, 0,"1");c=0;break;
case 2: LcdShowStr(5, 0,"2");c=0;break;
case 3: LcdShowStr(5, 0,"3");Led3();c=0;break;
case 4: LcdShowStr(5, 0,"4");Led4();c=0;break;
case 5: LcdShowStr(5, 0,"5");Led5();c=0;break;
case 6: LcdShowStr(5, 0,"6");Led6();c=0;break;
case 7: LcdShowStr(5, 0,"7");Led7();c=0;break;
case 8: LcdShowStr(5, 0,"8");Led8();c=0;break;
case 9: LcdShowStr(5, 0,"9");Led9();c=0;break;
case 10: LcdShowStr(5, 0,"*");c=0;break;
case 0: LcdShowStr(5, 0,"0");Led0();c=0;PlayTwoTiger();Led9();break;
default:
P0=0xFF;break;
}
ENLED=1; //关闭LED使能端,防止闪烁
KeyDriver(); //调用按键驱动
UartDriver(); //调用串口驱动
//if (flag300ms)
{
flag300ms = 0;
//显示通道0的电压
LcdShowStr(9, 0, "TJ 50"); //显示通道指示
val=GetADCValue(0); //获取ADC通道0的转换值
LcdShowStr(0, 0, "Mode="); //显示通道指示
ADDR2=1;
ADDR1=1;
ADDR0=0;
P0=0xFF;
ValueToString(str, val); //转为字符串格式的电压值
LcdShowStr(0, 1, "Voltage="); //显示到液晶上
LcdShowStr(8, 1, str); //显示到液晶
TI=1;
if(flagUart)
{
UartWrite(str,8);
//UartWrite('\n',1);
}
}
}
ENLED=0; //打开LED使能端
}
/* 读取当前的ADC转换值,chn-ADC通道号0~3 */
unsigned char GetADCValue(unsigned char chn)
{
signed long val;
I2CStart();
if (!I2CWrite(0x48<<1)) //寻址PCF8591,如未应答,则停止操作并返回0
{
I2CStop();
return 0;
}
I2CWrite(0x40|chn); //写入控制字节,选择转换通道
I2CStart();
I2CWrite((0x48<<1)|0x01); //寻址PCF8591,指定后续为读操作
I2CReadACK(); //先空读一个字节,提供采样转换时间
val = I2CReadNAK(); //读取刚刚转换完的值
I2CStop();
return val;
}
/* ADC转换值转为实际电压值的字符串形式,str-字符串指针,val-AD转换值 */
void ValueToString(unsigned char *str, unsigned long val)
{
//电压值=转换结果*2.5V/255,式中的25隐含了一位十进制小数
val = (val*2500) / 255;
t=val*6;
t0=val/100;
str[0] = (val/1000)%10 + '0'; //整数位字符
str[1] = '.'; //小数点
str[2] = (val/100)%10 + '0'; //小数位字符
str[3] = (val/10)%10 +'0';
str[4] = (val%10) + '0';
str[5] = 'V'; //电压单位
str[6] = '\0'; //结束符
}
/* 内存比较函数,比较两个指针所指向的内存数据是否相同,
ptr1-待比较指针1,ptr2-待比较指针2,len-待比较长度
返回值-两段内存数据完全相同时返回1,不同返回0 */
bit CmpMemory(unsigned char *ptr1, unsigned char *ptr2, unsigned char len)
{
while (len--)
{
if (*ptr1++ != *ptr2++) //遇到不相等数据时即刻返回0
{
return 0;
}
}
return 1; //比较完全部长度数据都相等则返回1
}
/* 串口动作函数,根据接收到的命令帧执行响应的动作
buf-接收到的命令帧指针,len-命令帧长度 */
void UartAction(unsigned char *buf, unsigned char len)
{
unsigned char i;
unsigned char code cmd0[] = "1"; //开蜂鸣器命令
unsigned char code cmd1[] = "2"; //关蜂鸣器命令
unsigned char code cmd2[] = "3"; //字符串显示命令
unsigned char code cmd3[] = "4"; //字符串显示命令
unsigned char code cmd4[] = "5"; //字符串显示命令
unsigned char code cmd5[] = "6"; //字符串显示命令
unsigned char code cmd6[] = "7"; //字符串显示命令
unsigned char code cmd7[]