首先就是这个题目中eeprom保存修改次数的时候。是个int型,而一位eeprom只能保存一个char型的,所以要两个eeprom来保存,温度保存同理所得。还有就是串口输出error还是不会,希望有大佬可以指点一下。
main.c
#include "reg52.h"
#include"intrins.h"
#include"iic.h"
#include"onewire.h"
#include"string.h"
sbit TX=P1^0;
sbit RX=P1^1;
sfr AUXR=0x8e;
sfr P4=0xC0;
sbit R1=P3^0;
sbit R2=P3^1;
sbit R3=P3^2;
sbit R4=P3^3;
sbit C4=P3^4;
sbit C3=P3^5;
sbit C2=P4^2;
sbit C1=P4^4;
int judge_temp=0;
int k12=0;int k13=0;
char uart_dat[10];
unsigned int change_time=0;
int distance;int set_distance=35;
int temperature;int set_temperature=30;
int all_mode=0;
int smg_mode=0;
int set_mode=0;
int da_mode=1;
int led=0xff;
char code xianshi[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xc6,0xc7,0xc8,0x8c,0x7f};
void smg_display();
void choose_573(int i)
{
switch(i)
{
case(0):P2=(P2&0x1f)|0x00;break;
case(4):P2=(P2&0x1f)|0x80;break;
case(5):P2=(P2&0x1f)|0xa0;break;
case(6):P2=(P2&0x1f)|0xc0;break;
case(7):P2=(P2&0x1f)|0xe0;break;
}
}
void init_system()
{
choose_573(4);
P0=0xff;
choose_573(5);
P0=0x00;
choose_573(0);
}
//========================距离
void Delay12us() //@11.0592MHz
{
unsigned char i;
_nop_();
_nop_();
_nop_();
i = 30;
while (--i);
}
void send_wave()
{
int i=0;
for(i=0;i<8;i++)
{
TX=0;
Delay12us();
TX=1;
Delay12us();
}
}
void get_distance()
{
unsigned int time;
EA=1;
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0x00; //设置定时初始值
TH0 = 0x28; //设置定时初始值
TF0 = 0; //清除TF0标志
TR0 = 0; //定时器0开始计时
TL0 = 0x00; //设置定时初始值
TH0 = 0x00; //设置定时初始值
send_wave();
TR0=1;
while(RX==1&&TF0==0);
TR0=1;
if(TF0==0)
{
time=(TH0<<8)|TL0;
distance=time*0.017;
distance=distance/12;
if(distance>=99)
{
distance=99;
}
}
else
{
distance=99;
TF0=0;
}
TH0=TL0=0x00;
}
//========================
//========================温度
void get_temperature()
{
temperature=rd_temperature();
}
//========================
//========================按键
void Delay10ms() //@11.0592MHz
{
unsigned char i, j;
i = 108;
j = 145;
do
{
while (--j);
} while (--i);
}
void key_board()
{
R1=R2=R4=1;R3=0;
C1=C2=C3=C4=1;
//S13
if(C3==0)
{
k13=1;
Delay10ms();
if(C3==0)
{
if(all_mode==0)
{
all_mode=1;
set_mode=0;
}
else
{
all_mode=0;
smg_mode=0;
}
}
while(!C3)
{
smg_display();
}
}
else
{
k13=0;
}
//S17
if(C4==0)
{
Delay10ms();
if(C4==0)
{
if(all_mode==1)
{
if(set_mode==0)
{
set_temperature=set_temperature+2;
if(set_temperature>98)
{
set_temperature=98;
}
change_time++;
write_EEPROM(0x01,change_time);
Delay10ms();
write_EEPROM(0x02,change_time>>8);
}
if(set_mode==1)
{
set_distance=set_distance+5;
if(set_distance>99)
{
set_distance=95;
}
change_time++;
write_EEPROM(0x01,change_time);
Delay10ms();
write_EEPROM(0x02,change_time>>8);
}
}
}
while(!C4)
{
smg_display();
}
}
R1=R2=R3=1;R4=0;
C1=C2=C3=C4=1;
//S12
if(C3==0)
{
k12=1;
Delay10ms();
if(C3==0)
{
if(all_mode==0)
{
smg_mode++;
if(smg_mode>=3)
{
smg_mode=0;
write_EEPROM(0x03,set_temperature);
Delay10ms();
write_EEPROM(0x04,set_distance);
}
}
if(all_mode==1)
{
set_mode++;
if(set_mode>=2)
{
set_mode=0;
}
}
}
while(!C3)
{
smg_display();
}
}
else
{
k12=0;
}
//S16
if(C4==0)
{
Delay10ms();
if(C4==0)
{
if(all_mode==1)
{
if(set_mode==0)
{
set_temperature=set_temperature-2;
if(set_temperature<0)
{
set_temperature=0;
}
change_time++;
write_EEPROM(0x01,change_time);
Delay10ms();
write_EEPROM(0x02,change_time>>8);
}
if(set_mode==1)
{
set_distance=set_distance-5;
if(set_distance<0)
{
set_distance=0;
}
change_time++;
write_EEPROM(0x01,change_time);
Delay10ms();
write_EEPROM(0x02,change_time>>8);
}
}
}
while(!C4)
{
smg_display();
}
}
}
//========================
//========================read_EEPROM
void readeeprom()
{
int change_time_h;int change_time_l;
set_temperature=read_EEPROM(0x03);
Delay10ms();
set_distance=read_EEPROM(0x04);
Delay10ms();
change_time_h=read_EEPROM(0x02);
Delay10ms();
change_time_l=read_EEPROM(0x01);
change_time=change_time_h*256+change_time_l;
}
//========================
//========================数码管
void Delay400us() //@11.0592MHz
{
unsigned char i, j;
i = 5;
j = 74;
do
{
while (--j);
} while (--i);
}
void SMG(int wei,int dat)
{
choose_573(6);
P0=0x80>>(wei-1);
choose_573(7);
P0=xianshi[dat];
choose_573(0);
Delay400us();
Delay400us();
choose_573(7);
P0=xianshi[10];
choose_573(0);
}
void smg_display()
{
if(all_mode==0)
{
if(smg_mode==0)
{
SMG(1,temperature%10);
SMG(2,(temperature%100)/10);
SMG(3,(temperature%1000)/100);
SMG(3,15);
if(temperature>=1000)
{
SMG(4,(temperature%10000)/1000);
}
else
{
SMG(4,10);
}
SMG(5,10);
// SMG(6,judge_temp);
SMG(6,10);
SMG(7,10);
SMG(8,11);
}
if(smg_mode==1)
{
SMG(1,distance%10);
SMG(2,(distance%100)/10);
SMG(3,10);
SMG(4,10);
SMG(5,10);
SMG(6,10);
SMG(7,10);
SMG(8,12);
}
if(smg_mode==2)
{
SMG(1,change_time%10);
if(change_time>=10)
{
SMG(2,(change_time%100)/10);
}
else
{
SMG(2,10);
}
if(change_time>=100)
{
SMG(3,(change_time%1000)/100);
}
else
{
SMG(3,10);
}
if(change_time>=1000)
{
SMG(4,(change_time%10000)/1000);
}
else
{
SMG(4,10);
}
if(change_time>=10000)
{
SMG(5,(change_time%100000)/10000);
}
else
{
SMG(5,10);
}
SMG(6,10);
SMG(7,10);
SMG(8,13);
}
}
if(all_mode==1)
{
if(set_mode==0)
{
SMG(1,set_temperature%10);
SMG(2,(set_temperature%100)/10);
SMG(3,10);
SMG(4,10);
SMG(5,1);
SMG(6,10);
SMG(7,10);
SMG(8,14);
}
if(set_mode==1)
{
SMG(1,set_distance%10);
SMG(2,(set_distance%100)/10);
SMG(3,10);
SMG(4,10);
SMG(5,2);
SMG(6,10);
SMG(7,10);
SMG(8,14);
}
}
}
//========================
//========================中断
void Timer0Init(void) //5毫秒@11.0592MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0x00; //设置定时初始值
TH0 = 0x28; //设置定时初始值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
ET0=1;
EA=1;
}
int count_1;int count_2;
void long_key_board() interrupt 1
{
// R1=R2=R3=1;R4=0;
// C1=C2=C3=C4=1;
if(k12==1)
{
count_1++;
if(count_1>=200)
{
change_time=0;
}
}
else
{
count_1=0;
}
if(k13==1)
{
count_2++;
if(count_2>=200)
{
if(da_mode==1)
{
da_mode=0;
}
else
{
da_mode=1;
}
}
}
else
{
count_2=0;
}
}
//========================
//========================da
void da_work()
{
if(da_mode==1)
{
if(distance<=set_distance)
{
DA(102);
}
else
{
DA(204);
}
}
if(da_mode==0)
{
DA(20);
}
}
//========================
//========================LED
void led_work()
{
if(all_mode==0)
{
//L1
if(smg_mode==0)
{
led=(led&0xfe)|0x00;
choose_573(4);
P0=led;
choose_573(0);
}
else
{
led=(led&0xfe)|0x01;
choose_573(4);
P0=led;
choose_573(0);
}
//L2
if(smg_mode==1)
{
led=(led&0xfd)|0x00;
choose_573(4);
P0=led;
choose_573(0);
}
else
{
led=(led&0xfd)|0x02;
choose_573(4);
P0=led;
choose_573(0);
}
}
//L3
if(da_mode==1)
{
led=(led&0xfb)|0x00;
choose_573(4);
P0=led;
choose_573(0);
}
else
{
led=(led&0xfb)|0x04;
choose_573(4);
P0=led;
choose_573(0);
}
}
//========================
//========================串口设置 4800
void UartInit(void) //[email protected]
{
SCON = 0x50; //8位数据,可变波特率
AUXR |= 0x40; //定时器时钟1T模式
AUXR &= 0xFE; //串口1选择定时器1为波特率发生器
TMOD &= 0x0F; //设置定时器模式
TL1 = 0xC0; //设置定时初始值
TH1 = 0xFD; //设置定时初始值
ET1 = 0; //禁止定时器%d中断
TR1 = 1; //定时器1开始计时
ES=1;
EA=1;
}
int uart_temp;
void uart_service() interrupt 4
{
if(RI==1)
{
uart_dat[uart_temp]=SBUF;
uart_temp++;
RI=0;
}
}
void send_dat(unsigned char dat)
{
SBUF=dat;
while(TI==0);
TI=0;
}
void send_string(unsigned char *str)
{
while(*str!='\0')
{
send_dat(*str++);
}
}
void send_1()
{
send_dat('$');
send_dat((distance/10)+0x30);
send_dat((distance%10)+0x30);
send_dat(',');
send_dat((temperature/1000)+0x30);
send_dat(((temperature%1000)/100)+0x30);
send_dat('.');
send_dat(((temperature%100)/10)+0x30);
send_dat((temperature%10)+0x30);
send_string("\r\n");
}
void send_2()
{
send_dat('#');
send_dat((set_distance/10)+0x30);
send_dat((set_distance%10)+0x30);
send_dat(',');
send_dat((set_temperature/10)+0x30);
send_dat((set_temperature%10)+0x30);
send_string("\r\n");
}
//========================
//========================串口比较
void uart_judge()
{
// if(uart_temp>=1)
// {
// if(uart_dat[0]=="S")
// {
// judge_temp=1;
// }
// if(uart_dat[0]=="P")
// {
// judge_temp=2;
// }
// }
// if(judge_temp==1&&uart_temp==4)
// {
// if(strcmp(uart_dat,"ST\r\n")==0)
// {
// send_1();
// uart_temp=0;
// judge_temp=0;
// }
// else
// {
// uart_temp=0;
// judge_temp=0;
// }
// }
// if(judge_temp==2&&uart_temp==6)
// {
// if(strcmp(uart_dat,"PARA\r\n")==0)
// {
// send_2();
// uart_temp=0;
// judge_temp=0;
// }
// else
// {
// uart_temp=0;
// judge_temp=0;
// }
// }
if(uart_temp==4&&strcmp(uart_dat,"ST\r\n")==0)
{
send_1();
uart_temp=0;
judge_temp=0;
}
if(uart_temp==6&&strcmp(uart_dat,"PARA\r\n")==0)
{
send_2();
uart_temp=0;
judge_temp=0;
}
}
//========================
void main()
{
init_system();
Timer0Init();
readeeprom();
UartInit();
while(1)
{
get_distance();
get_temperature();
smg_display();
key_board();
da_work();
led_work();
// send_1();
// send_2();
uart_judge();
}
}
iic.c
/*
程序说明: IIC总线驱动程序
软件环境: Keil uVision 4.10
硬件环境: CT107单片机综合实训平台 8051,12MHz
日 期: 2011-8-9
*/
#include "reg52.h"
#include "intrins.h"
#define DELAY_TIME 5
#define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1
//总线引脚定义
sbit SDA = P2^1; /* 数据线 */
sbit SCL = P2^0; /* 时钟线 */
void IIC_Delay(unsigned char i)
{
do{_nop_();}
while(i--);
}
//总线启动条件
void IIC_Start(void)
{
SDA = 1;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 0;
}
//总线停止条件
void IIC_Stop(void)
{
SDA = 0;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 1;
IIC_Delay(DELAY_TIME);
}
//发送应答
void IIC_SendAck(bit ackbit)
{
SCL = 0;
SDA = ackbit; // 0:应答,1:非应答
IIC_Delay(DELAY_TIME);
SCL = 1;
IIC_Delay(DELAY_TIME);
SCL = 0;
SDA = 1;
IIC_Delay(DELAY_TIME);
}
//等待应答
bit IIC_WaitAck(void)
{
bit ackbit;
SCL = 1;
IIC_Delay(DELAY_TIME);
ackbit = SDA;
SCL = 0;
IIC_Delay(DELAY_TIME);
return ackbit;
}
//通过I2C总线发送数据
void IIC_SendByte(unsigned char byt)
{
unsigned char i;
for(i=0; i<8; i++)
{
SCL = 0;
IIC_Delay(DELAY_TIME);
if(byt & 0x80) SDA = 1;
else SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 1;
byt <<= 1;
IIC_Delay(DELAY_TIME);
}
SCL = 0;
}
//从I2C总线上接收数据
unsigned char IIC_RecByte(void)
{
unsigned char i, da;
for(i=0; i<8; i++)
{
SCL = 1;
IIC_Delay(DELAY_TIME);
da <<= 1;
if(SDA) da |= 1;
SCL = 0;
IIC_Delay(DELAY_TIME);
}
return da;
}
void write_EEPROM(int add,int dat)
{
IIC_Start();
IIC_SendByte(0xa0);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_SendByte(dat);
IIC_WaitAck();
IIC_Stop();
}
int read_EEPROM(int add)
{
int temp;
IIC_Start();
IIC_SendByte(0xa0);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_Start();
IIC_SendByte(0xa1);
IIC_WaitAck();
temp=IIC_RecByte();
IIC_SendAck(1);
IIC_Stop();
return temp;
}
void DA(int dat)
{
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x40);
IIC_WaitAck();
IIC_SendByte(dat);
IIC_WaitAck();
IIC_Stop();
}
iic.h
#ifndef _IIC_H
#define _IIC_H
void IIC_Start(void);
void IIC_Stop(void);
bit IIC_WaitAck(void);
void IIC_SendAck(bit ackbit);
void IIC_SendByte(unsigned char byt);
unsigned char IIC_RecByte(void);
int read_EEPROM(int add);
void write_EEPROM(int add,int dat);
void DA(int dat);
#endif
onewire.c
/*
程序说明: 单总线驱动程序
软件环境: Keil uVision 4.10
硬件环境: CT107单片机综合实训平台(外部晶振12MHz) STC89C52RC单片机
日 期: 2011-8-9
*/
#include "reg52.h"
sbit DQ = P1^4; //单总线接口
//单总线延时函数
void Delay_OneWire(unsigned int t) //STC89C52RC
{
t=t*12;
while(t--);
}
//通过单总线向DS18B20写一个字节
void Write_DS18B20(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
DQ = 0;
DQ = dat&0x01;
Delay_OneWire(5);
DQ = 1;
dat >>= 1;
}
Delay_OneWire(5);
}
//从DS18B20读取一个字节
unsigned char Read_DS18B20(void)
{
unsigned char i;
unsigned char dat;
for(i=0;i<8;i++)
{
DQ = 0;
dat >>= 1;
DQ = 1;
if(DQ)
{
dat |= 0x80;
}
Delay_OneWire(5);
}
return dat;
}
//DS18B20设备初始化
bit init_ds18b20(void)
{
bit initflag = 0;
DQ = 1;
Delay_OneWire(12);
DQ = 0;
Delay_OneWire(80);
DQ = 1;
Delay_OneWire(10);
initflag = DQ;
Delay_OneWire(5);
return initflag;
}
unsigned int rd_temperature(void)
{
int HBS,LBS;
int temp;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
Delay_OneWire(200);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
LBS=Read_DS18B20();
HBS=Read_DS18B20();
temp=HBS;
temp=(HBS<<8)|LBS;
temp=temp*6.25;
return temp;
}
onewire.h
#ifndef __ONEWIRE_H
#define __ONEWIRE_H
unsigned int rd_temperature(void); //; ;
bit init_ds18b20(void);
unsigned char Read_DS18B20(void);
void Write_DS18B20(unsigned char dat);
#endif