我想通过
单片机完成温度与酒精浓度的测量并通过蓝牙模块进行串口
通信。
#include <reg52.h> //????????
#define uchar unsigned char //?????? ??? ????0~255
#define uint unsigned int //????? ??? ????0~65535
#include "eeprom52.h"
#include <stdio.h>
#include <intrins.h>
//??????? 0 1 2 3 4 5 6 7 8 9
uchar code smg_du[]={0x28,0xee,0x32,0xa2,0xe4,0xa1,0x21,0xea,0x20,0xa0,
0x60,0x25,0x39,0x26,0x31,0x71,0xff}; //??
//???????
uchar code smg_we[]={0xef,0xdf,0xbf,0x7f,0xf7,0xfb,0xfd,0xfe};
uchar dis_smg[8] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8};
uchar smg_i = 8; //?????????
uchar a_a;
uchar flag,a;
unsigned char flag_uart,flag_
time,flag_on,a,i,t0_num,ad_val;
float temp;
float alcohol;
sbit dq = P2^3; //18b20 IO????
sbit beep = P2^4; //???IO???
sbit CS=P2^5; //CS???P1???4??,??ADC0832CS?
sbit SCL=P2^6; //SCL???P1???3??,??ADC0832SCL?
sbit DO=P2^7; //DO???P1???4??,??ADC0832DO?
bit flag_lj_en; //??????
bit flag_lj_3_en; //???3?????? ???????
uchar key_time,key_value; //?????????
bit key_500ms ;
uint temperature ; //
bit flag_300ms ;
uchar menu_1; //???????
uint t_high = 300,t_low = 100; //????????
long dengji,s_dengji = 50; //????
#define RdCommand 0x01 //??ISP?????
#define PrgCommand 0x02
#define EraseCommand 0x03
#define Error 1
#define Ok 0
#define WaitTime 0x01 //??CPU?????
/***********************1ms????*****************************/
void delay_1ms(uint q)
{
uint i,j;
for(i=0;i<q;i++)
for(j=0;j<120;j++);
}
/***********************?????*****************************/
void delay_uint(uint q)
{
while(q--);
}
/***********************??????*****************************/
void display()
{
static uchar i;
i++;
if(i >= smg_i)
i = 0;
P1 = 0xff; //??
P0= smg_we[i]; //??
P1 = dis_smg[i]; //??
}
/******************???????????eepom?******************/
void write_eeprom()
{
sectorerase(0x2000);
byte_write(0x2000, t_high % 256);
byte_write(0x2001, t_high / 256);
byte_write(0x2002, t_low % 256);
byte_write(0x2003, t_low / 256);
byte_write(0x2004, s_dengji % 256);
byte_write(0x2005, s_dengji / 256);
byte_write(0x2055, a_a);
}
/******************?????????eepom????*****************/
void read_eeprom()
{
t_high = byte_read(0x2001);
t_high <<= 8;
t_high |= byte_read(0x2000);
t_low = byte_read(0x2003);
t_low <<= 8;
t_low |= byte_read(0x2002);
s_dengji = byte_read(0x2005);
s_dengji <<= 8;
s_dengji |= byte_read(0x2004);
a_a = byte_read(0x2055);
}
/**************??????????*****************/
void init_eeprom()
{
read_eeprom(); //??
if(a_a != 1) //????????????eeprom
{
t_high = 400;
t_low = 100;
s_dengji = 80;
a_a = 1;
write_eeprom(); //????
}
}
/***********???????********************************************************/
unsigned char ad0832read(bit SGL,bit ODD)
{
unsigned char i=0,value=0,value1=0;
SCL=0;
DO=1;
CS=0; //??
SCL=1; //??????
SCL=0;
DO=SGL;
SCL=1; //??????
SCL=0;
DO=ODD;
SCL=1; //??????
SCL=0; //??????
DO=1;
for(i=0;i<8;i++)
{
SCL=1;
SCL=0; //?????????????
value<<=1;
if(DO)
value++;
}
for(i=0;i<8;i++)
{ //??????
value1>>=1;
if(DO)
value1+=0x80;
SCL=1;
SCL=0;
}
CS=1;
SCL=1;
if(value==value1) //???????,???????,????0
return value;
return 0;
}
void init()
{
TMOD=0x21;
// SCON=0x50;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TH1=0xfd;
TL1=0xfd;
TR1=1;
ET0=1;
SM0=0;
SM1=1;
REN=1;
EA=1;
ES=1;
}
/***********************18b20?????*****************************/
void init_18b20()
{
bit q;
dq = 1; //?????
delay_uint(1); //15us
dq = 0; //?????
delay_uint(80); //750us
dq = 1; //????? ??
delay_uint(10); //110us
q = dq; //??18b20?????
delay_uint(20); //200us
dq = 1; //????? ????
}
/*************?18b20????***************/
void write_18b20(uchar dat)
{
uchar i;
for(i=0;i<8;i++)
{ //????????
dq = 0; //???????????
dq = dat & 0x01; //?18b20??????
delay_uint(5); // 60us
dq = 1; //????
dat >>= 1;
}
}
/*************??18b20????***************/
uchar read_18b20()
{
uchar i,value;
for(i=0;i<8;i++)
{
dq = 0; //???????????
value >>= 1; //????????
dq = 1; //????
if(dq == 1) //??????
value |= 0x80;
delay_uint(5); //60us ???????????60us???
}
return value; //????
}
/*************?????? ???????***************/
uint read_temp()
{
uint value;
uchar low; //?????????????????,?????????,??????18b20???
init_18b20(); //???18b20
write_18b20(0xcc); //??64?ROM
write_18b20(0x44); //??????????
delay_uint(50); //500us
init_18b20(); //???18b20
write_18b20(0xcc); //??64?ROM
write_18b20(0xbe); //?????????
EA = 0;
low = read_18b20(); //??????
value = read_18b20(); //??????
EA = 1;
value <<= 8; //????????8?
value |= low; //??????????value?????
value *= 0.625; //?????? ??
return value; //??????? ???
}
/*************???0?????***************/
void time_init()
{
EA = 1; //????
TMOD = 0X01; //???0????1????1
ET0 = 1; //????0??
TR0 = 1; //?????0??
}
/********************??????*****************/
uchar key_can; //???
void key() //??????
{
static uchar key_new;
key_can = 20; //?????
P2 |= 0x07;
if((P2 & 0x07) != 0x07) //????
{
if(key_500ms == 1) //??
{
key_500ms = 0;
key_new = 1;
}
delay_1ms(1); //?????
if(((P2 & 0x07) != 0x07) && (key_new == 1))
{ //???????
key_new = 0;
switch(P2 & 0x07)
{
case 0x06: key_can = 3; break; //??k2??
case 0x05: key_can = 2; break; //??k3??
case 0x03: key_can = 1; break; //??k4??
}
flag_lj_en = 1; //????
}
}
else
{
if(key_new == 0)
{
key_new = 1;
write_eeprom(); //????
flag_lj_en = 0; //??????
flag_lj_3_en = 0; //??3????
key_value = 0; //??
key_time = 0;
key_500ms = 0;
}
}
}
/****************???????????***************/
void key_with()
{
if(key_can == 1) //???
{
menu_1 ++;
if(menu_1 >= 4)
{
menu_1 = 0;
smg_i = 8; //?????8?
}
}
if(menu_1 == 1) //??????
{
smg_i = 8; //?????4?
if(key_can == 2)
{
if(flag_lj_3_en == 0)
t_high ++ ; //????????????
else
t_high += 10; //???????????????????10
if(t_high > 990)
t_high = 990;
}
if(key_can == 3)
{
if(flag_lj_3_en == 0)
t_high -- ; //????????????
else
t_high -= 10; //???????????????????10
}
dis_smg[7] = smg_du[t_high % 10]; //?????
dis_smg[6] = smg_du[t_high / 10 % 10] & 0xdf; //?????
dis_smg[5] = smg_du[t_high / 100 % 10] ; //?????
dis_smg[4] = 0x64; //H
dis_smg[0] = smg_du[t_low % 10]; //?????
dis_smg[1] = smg_du[t_low / 10 % 10] & 0xdf; //?????
dis_smg[2] = smg_du[t_low / 100 % 10] ; //?????
dis_smg[3] = 0x3D; //L
}
if(menu_1 == 2) //??????
{
smg_i = 8; //?????4?
if(key_can == 2)
{
if(flag_lj_3_en == 0)
t_low ++ ; //????????????
else
t_low += 10; //???????????????????10
if(t_low >= t_high)
t_low = t_high - 1;
}
if(key_can == 3)
{
if(flag_lj_3_en == 0)
t_low -- ; //????????????
else
t_low -= 10; //???????????????????10
if(t_low <= 10)
t_low = 10;
}
dis_smg[0] = smg_du[t_low % 10]; //?????
dis_smg[1] = smg_du[t_low / 10 % 10] & 0xdf; //?????
dis_smg[2] = smg_du[t_low / 100 % 10] ; //?????
dis_smg[3] = 0x3D; //L
}
if(menu_1 == 3) //????????
{
smg_i = 8; //?????4?
if(key_can == 2)
{
if(flag_lj_3_en == 0)
s_dengji ++ ; //????????????
else
s_dengji += 10; //???????????????????10
}
if(key_can == 3)
{
if(flag_lj_3_en == 0)
s_dengji -- ; //????????????
else
s_dengji -= 10; //???????????????????10
}
dis_smg[0] = smg_du[s_dengji % 10]; //?????
dis_smg[1] = smg_du[s_dengji / 10 % 10] ; //?????
dis_smg[2] = smg_du[s_dengji / 100 % 10] ; //
dis_smg[3] = 0x60; //a
}
}
/****************????***************/
void clock_h_l()
{
static uchar value;
if((temperature <= t_high)|| (dengji >= s_dengji) || (temperature >= t_high))
{
beep = ~beep; //?????
}
else
{
beep = 1;
}
}
/****************???***************/
void main()
{
delay_1ms(150);
P0 = P1 = P2 = 0xff;
temperature = read_temp(); //???????
init();
init_eeprom(); //??????????
delay_1ms(650);
temperature = read_temp(); //???????
dis_smg[7] = smg_du[temperature % 10]; //????????
dis_smg[6] = smg_du[temperature / 10 % 10] & 0xdf; //????????
dis_smg[5] = smg_du[temperature / 100 % 10] ; //????????6
time_init(); //??????
while(1)
{
key(); //????
if(key_can < 10)
{
key_with(); //??????
}
if(flag_300ms == 1) //300ms ????????
{
flag_300ms = 0;
temperature = read_temp(); //???????
clock_h_l();
dengji = ad0832read(1,0);
dengji = dengji * 450 / 255.0;
dengji = dengji - 100; //????????,???100mV
if(dengji < 0)
dengji = 0;
dengji = dengji * 2; //?mV???mg/L,??????
//?????0.1V,???????????20ppm
//1ppm=1mg/kg=1mg/L=1×10-6 ?????????,???????
if(menu_1 == 0)
{
smg_i = 8;
dis_smg[7] = smg_du[temperature % 10]; //????????
dis_smg[6] = smg_du[temperature / 10 % 10] & 0xdf; //????????
dis_smg[5] = smg_du[temperature / 100 % 10] ; //????????
dis_smg[4] = 0xff;
dis_smg[3]=smg_du[dengji/1000%10]; //??
dis_smg[2]=smg_du[dengji/100%10]; //??
dis_smg[1]=smg_du[dengji/10%10]; //??
dis_smg[0]=smg_du[dengji%10]; //?? ADC0832?8?ADC,???0~255,????????l_tmpdate?????
}
}
if(flag_uart==1)
{
flag_uart=0;
ES=0;
TI=1;
switch(flag_on)
{
case 0: puts("open
");
TR0=1;
break;
case 1: printf("close!
");
TR0=0;
break;
case 2: puts("Error!
");
break;
}
while(!TI);//????
TI=0;
ES=1;
}
if(flag_time==1)
{
flag_time=0;
temperature = read_temp();
dengji = ad0832read(1,0);
ES=0;
TI=1;
printf("The temp is %fV
",temperature);
printf("The alcohol is %fV
",dengji);
while(!TI);
TI=0;
ES=1;
}
}
}
/*************???0??????***************/
void time0_int() interrupt 1
{
static uchar value; //??2ms????
TH0 = 0xf8;
TL0 = 0x30; //2ms
display(); //???????
value++;
if(value >= 150)
{
value = 0;
flag_300ms = 1;
}
if(flag_lj_en == 1) //??????
{
key_time ++;
if(key_time >= 250) //500ms
{ flag_time=1;
key_time = 0;
key_500ms = 1; //500ms
key_value ++;
if(key_value > 3)
{
key_value = 10;
flag_lj_3_en = 1; //3??1.5?????
}
}
}
}
void ser() interrupt 4
{
RI=0;
a=SBUF;
flag_uart=1;
if(a==1)
flag_on=0;
else if(a==2)
flag_on=1;
else
flag_on=2;
}
这是我的程序 ,我想请大家帮我看看哪里有问题 ,我已经完成了数码管显示温度和酒精浓度,只是串口通信部分的问题了!不太懂串口,现在每次
手机输入指令 ,单片机不回数据
一周热门 更多>