📄 alcohol.c
字号:
/**********************************************************/
#include "reg51.h"
unsigned char mysbuf;
unsigned char timeCnt=0;
bit myRxBit=0;
bit myToBit=0;
sbit BELL = P0^2;
//*****************************UART*************************/
void initUart(void) //初始化串行口
{ //最大的波特率:57600 bit/s
TMOD|=0x20;
SCON=0x50;
TH1=0xff;
TL1=0xff;
PCON|=0x80;
TR1=1;
ES=1;
EA=1;
}
void uart(void) interrupt 4 //串行口中断服务程序
{
if(RI)
{
RI=0;
mysbuf=SBUF;
myRxBit=1;
}
}
void sendByte(unsigned char byte) //串行口发一字节数据
{
SBUF=byte;
while(TI==0);
TI=0;
}
//*****************************UART*************************/
//**************************** Timer0 **********************/
void initTimer(void) //初始化定时器0
{
TMOD |=0x1; //10000us
TH0=0xb8;
TL0=0x63;
TR0=1;
ET0=1;
EA=1;
}
void timer0(void) interrupt 1 //定时器0中断服务程序
{
TH0=0xb8;
TL0=0x63;
//add your code here.
if(++timeCnt == 5)
{
timeCnt = 0;
myToBit = 1;
}
}
//**************************end Timer0 *********************/
/**************************LCD程序**************************/
//#define dataport P2
sbit D0=P2^3;
sbit D1=P2^4;
sbit D2=P2^5;
sbit D3=P2^6;
sbit D4=P2^7;
sbit D5=P0^7;
sbit D6=P0^6;
sbit D7=P0^5;
sbit RS=P2^0;
sbit RW=P2^1;
sbit ET=P2^2;
void Wait() //延时程序
{
unsigned int j;
for(j=0;j<300;j++){;}
}
void WriCom(unsigned char comm) //LCD发一命令字
{
RS=0;
RW=0;
ET=0;
//dataport=comm;
P2 &= 0x07;
P2 |= ((comm<<3)&0xf8);
D5 = comm & 0x20;
D6 = comm & 0x40;
D7 = comm & 0x80;
ET=1;
Wait();
ET=0;
}
void WriData(unsigned char wdata) //LCD发一字节数据
{
RS=1;
RW=0;
ET=0;
//dataport=comm;
P2 &= 0x07;
P2 |= ((wdata<<3)&0xf8);
D5 = wdata & 0x20;
D6 = wdata & 0x40;
D7 = wdata & 0x80;
ET=1;
Wait();
ET=0;
}
void InitLcd(void) //LCD初始化
{
Wait();
WriCom(0x38);
Wait();
WriCom(0x38);
Wait();
WriCom(0x38);
Wait();
WriCom(0x38);
Wait();
WriCom(0x08);
WriCom(0x01);
WriCom(0x06);
WriCom(0x0C);
}
void SetCursor(unsigned char row) //设置光标即插入点
{ if(row>15)
row+=(0x40-16);
WriCom(row | 0x80);
}/*
void ClrLCD()
{
WriCom(0x01);
}
void CursorGlint(unsigned char NoOff)
{
unsigned char com;
com=NoOff<<2;
com|=13;
WriCom(com);
}
*/
void WriStr(char *str) //LCD显示一字符串
{
while(*str != 0)
{
WriData(*str);
str++;
}
}
/**********************end LCD程序**************************/
/*********************** ADCTLC1543模块 ********************/
sbit ADC_EOC=P3^3;
sbit ADC_CLK=P3^4;
sbit ADC_ADDR=P3^5;
sbit ADC_DAT=P3^6;
sbit ADC_CS=P3^7;
unsigned int ADC_Read(void) //ADC采集
{
unsigned int buf=0;
unsigned char i;
ADC_CS = 1;
ADC_DAT =1;
ADC_CLK = 0;
ADC_ADDR = 0;
ADC_CS = 0;
while(!ADC_EOC);
for(i=0;i<10;i++) //采集10位
{
buf <<=1;
ADC_CLK = 1;
if(ADC_DAT)
buf ++;
ADC_CLK = 0;
}
ADC_CS = 1;
return buf;
}
/********************end ADCTLC1543模块 ********************/
/*
void Delay(unsigned int t)
{
for(;t>0;t--);
}
*/
void main(void) //主函数
{
unsigned int i;
unsigned int adc;
InitLcd(); //初始化LCD
WriStr("measure ..."); //LCD显示
initUart(); //初始化串行口
initTimer(); //初始化定时器0
while(1)
{
sendByte(0xaa); //发帧头
sendByte(0x55); //发帧头
for(i=0;i<20;i++)
{
while(!myToBit);
myToBit = 0;
adc = ADC_Read();//ADC采集
sendByte(adc>>2);//发送数据到上位机
SetCursor(16); //设置光标
WriData(adc/1000 + '0'); //LCD显示
WriData(adc%1000/100 + '0'); //LCD显示
WriData(adc%100/10 + '0'); //LCD显示
WriData(adc%10 + '0'); //LCD显示
if(adc > 250 )
BELL = 0;
else
BELL = 1;
}
sendByte(0xa5); //发帧尾
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -