#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit ds = P2^2;
sbit dula = P2^6;
sbit wela = P2^7;
sbit beep = P2^3;
bit flag;
uint temp;
uchar num;
uint tmp1;
uint i = 0;
uint a,b;
uchar baie,shie,gee;
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
uchar code table1[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,
0x87,0xff,0xef};
void UART_init()
{
TMOD = 0x20; //T1工作模式2 8位自动重装
TH1 = 0xfd;
TL1 = 0xfd; //比特率9600
TR1 = 1; //启动T1定时器
SM0 = 0;
SM1 = 1; //串口工作方式1 10位异步
REN = 1; //串口允许接收
EA = 1; //开总中断
ES = 1; //串口中断打开
}
void TempDelay(uchar us)
{
while(us--); //6.51us
}
void delay(int z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=115;y>0;y--);
}
void display(uint temp)
{
uchar bai,shi,ge;
bai=temp/100;
shi=temp%100/10;
ge=temp%100%10;
dula=1;
P0=table[bai];
dula=0;
P0=0xff;
wela=1;
P0=0xfe;
wela=0;
delay(3);
dula=1;
P0=table1[shi];
dula=0;
P0=0xff;
wela=1;
P0=0xfd;
wela=0;
delay(3);
dula=1;
P0=table[ge];
dula=0;
P0=0xff;
wela=1;
P0=0xfb;
wela=0;
delay(3);
}
void ds_reset()
{
ds=1;
_nop_();
ds=0;
TempDelay(80);//520us
ds=1;
TempDelay(5);
if(ds==0)
flag=1;
else
flag=0;
TempDelay(20);
ds=1;
}
bit ds_read_bit()
{
bit dat;
ds=0;
_nop_();
_nop_();
ds=1;
_nop_();
dat=ds;
TempDelay(12);
return dat;
}
uchar ds_read_byte()
{
uchar i,j,value;
for(i=0;i<8;i++)
{
j=ds_read_bit();
value=(j<<7)|(value>>1);
}
return value;
}
void ds_write_byte(uchar dat)
{
uchar i,onebit;
for(i=0;i<8;i++)
{
onebit=dat&0x01;
if(onebit) //写1
{
ds=0;
TempDelay(1);
ds=1;
_nop_();
}
else //写0
{
ds=0;
TempDelay(11);
ds=1;
_nop_();
}
dat>>=1;
}
}
void ds_change()
{
ds_reset();
ds_write_byte(0xcc);
ds_write_byte(0x44);
}
uint get_temperature()
{
float temperature;
ds_reset();
ds_write_byte(0xcc);
ds_write_byte(0xbe);
a=ds_read_byte();
b=ds_read_byte();
temp=b;
temp<<=8;
temp|=a;
temperature=temp*0.0625;
temp=temperature*10+0.5;
return temp;
}
void main()
{
UART_init(); //串口初始化
// while(1);
ds_change();
delay(1000);
while(1)
{
display(get_temperature());
ds_change();
// if(get_temperature()>350)//35度蜂鸣器发声
// beep=0;
// else
// beep=1;
}
}
void UART() interrupt 4
{
if(RI) //检测是否接收完成
{
num = SBUF; //num 取出接收缓存器的值
P1 = num;
RI = 0;
tmp1 = get_temperature();
baie=tmp1/100;
shie=tmp1%100/10;
gee=tmp1%100%10;
// if(num ==238)
// {
// SBUF = gee;
// }
//// else if (num ==0xff)
// {
// SBUF = shie;
// }
// else if(num ==0x00)
// {
SBUF = baie;
// }
RI = 0; //清除接收标志位,以便于下次接收
}
while(!
ti);
TI = 0;
}
这是51
单片机的代码
通过串口发送大于255的数值到上位机
-
-
fight.Uv2
下载积分: 积分 -1 分
2.1 KB, 下载次数: 9, 下载积分: 积分 -1 分
一周热门 更多>