设计要求
从DS18B20温度传感器上获取环境温度,并将其温度值显示到数码管上(保留两位小数)。
当“S4”定义为发送按键,按键S4按下时,串口向PC端发送当前采集的温度值;
串口发送格式:
Temp:26.26C
数码管显示格式如下图所示:
固定使用5位数码管显示数据,数码管最低位显示固定字符‘C’
备注:
单片机IRC振荡器频率设置为12MHz。
按键模式:BTN;扩展模式:IO模式。
温度值保留小数点后两位有效数字。
代码
sys.h
#ifndef __SYS_H__
#define __SYS_H__
#include <STC15F2K60S2.H>
extern unsigned char Seg_Buff[8];
extern bit flag_send;
extern float temper;
//onewire.c
float getT();
//sys.c
void init74hc138(unsigned char n);
void init();
void Seg_Loop();
void Key_Loop();
void seg_temper();
#endif
main.c
#include "sys.h"
bit flag_seg=0;
bit flag_key=0;
bit flag_temper=0;
unsigned char *str="Temp:";
void Timer0_Init(void) //100微秒@12.000MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0x50; //设置定时初始值
TH0 = 0xFB; //设置定时初始值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
ET0 = 1; //使能定时器0中断
EA = 1;
}
void UartInit(void) //9600bps@12.000MHz
{
SCON = 0x50; //8位数据,可变波特率
AUXR |= 0x40; //定时器时钟1T模式
AUXR &= 0xFE; //串口1选择定时器1为波特率发生器
TMOD &= 0x0F; //设置定时器模式
TL1 = 0xC7; //设置定时初始值
TH1 = 0xFE; //设置定时初始值
ET1 = 0; //禁止定时器中断
TR1 = 1; //定时器1开始计时
ES = 1;
EA = 1;
}
void uart_send_date(unsigned char date){
SBUF=date;
while(TI==0);
TI=0;
}
void uart_send_str(unsigned char *str){
while(*str!='\0'){
uart_send_date(*str);
str++;
}
}
void uart_send(){
uart_send_str(str);
if(Seg_Buff[3]!=11)
uart_send_date(Seg_Buff[3]+'0');
uart_send_date(Seg_Buff[4]+'0');
uart_send_date('.');
uart_send_date(Seg_Buff[5]+'0');
uart_send_date(Seg_Buff[6]+'0');
uart_send_date('C');
uart_send_str("\r\n");
}
void main(){
init();
Timer0_Init();
while((int)getT()==85);
UartInit();
while(1){
if(flag_seg){
flag_seg=0;
Seg_Loop();
}
if(flag_key){
flag_key=0;
Key_Loop();
if(flag_send){
flag_send=0;
uart_send();
}
}
if(flag_temper){
flag_temper=0;
temper=getT();
seg_temper();
}
}
}
void uart() interrupt 4
{
if(RI){
RI=0;
}
}
void Timer0_Isr(void) interrupt 1
{
static unsigned char count1=0,count2=0;
static unsigned int count3=0;
count1++;count2++;count3++;
if(count1==2){
count1=0;
flag_seg=1;
}
if(count2==50){
count2=0;
flag_key=1;
}
if(count3==1000){
count3=0;
flag_temper=1;
}
}
sys.c
#include "sys.h"
code unsigned char Seg_Table[] =
{
0xc0, //0
0xf9, //1
0xa4, //2
0xb0, //3
0x99, //4
0x92, //5
0x82, //6
0xf8, //7
0x80, //8
0x90, //9
0xc6, //C 10
0xff //熄灭 11
};
unsigned char Seg_Buff[8]={11,11,11,11,11,11,11,11};
unsigned char keyval,keyold,keyup,keydown;
bit flag_send=0;
float temper=0;
void init74hc138(unsigned char n){
P2=(P2&0x1f)|(n<<5);
P2&=0x1f;
}
void init(){
P0=0x00;
init74hc138(5);
P0=0xff;
init74hc138(4);
}
void seg(unsigned char addr,num){
P0=0xff;
init74hc138(7);
P0=0x01<<addr;
init74hc138(6);
P0=Seg_Table[num];
if(addr==4)P0&=0x7f;
init74hc138(7);
}
void Seg_Loop(){
static unsigned char i=0;
seg(i,Seg_Buff[i]);
i++;
if(i==8)i=0;
}
void Key_Loop(){
keyval=0;
if(P33==0)keyval=1;
keydown=keyval&(keyold^keyval);
keyup=~keyval&(keyold^keyval);
if(keyval&&!keyold)flag_send=1;
keyold=keyval;
}
void seg_temper(){
Seg_Buff[0]=11;
Seg_Buff[1]=11;
Seg_Buff[2]=11;
if((int)(temper*100)>=1000)
Seg_Buff[3]=(int)temper/10;
else
Seg_Buff[3]=11;
Seg_Buff[4]=(int)(temper*100+0.5)/100%10;
Seg_Buff[5]=(int)(temper*100+0.5)/10%10;
Seg_Buff[6]=(int)(temper*100+0.5)%10;
Seg_Buff[7]=10;
}
onewire.c
#include "sys.h"
sbit DQ=P1^4;
void Delay_OneWire(unsigned int t)
{
unsigned char i;
while(t--){
for(i=0;i<12;i++);
}
}
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);
}
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;
}
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;
}
float getT(){
unsigned char tl,th;
float temp;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
tl=Read_DS18B20();
th=Read_DS18B20();
temp=(th<<8|tl)*0.0625;
return temp;
}