#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;
//LED灯光相关
sbit LED1 = P1^0;
sbit LED2 = P1^1;
sbit LED3 = P1^2;
sbit LED4 = P1^3;
sbit LED5 = P1^4;
sbit LED6 = P1^5;
sbit LED7 = P1^6;
sbit LED8 = P1^7;
//接收到的数据
uchar receiveData;
bit flag;
uint temp;
uint count;
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 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 send_temperature(uchar tempData){
SBUF=tempData;//将数据传递到缓存区
while(!TI); //检测数据是否发送完成
TI=0;//清0,发送下一帧的数据
}
//显示温度数据到数码管,及发送温度数据到PC端
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);
//简单的延迟
count++;
if(count==200){
//send_temperature(temp);
send_temperature(bai);//发送温度数据
send_temperature(shi);
send_temperature(ge);
count=0;
}
//send_temperature(shi);
//send_temperature(ge);
}
//DS18b20初始化
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();//DS18b20初始化
ds_write_byte(0xcc);//发送跳跃rom指令
ds_write_byte(0x44);//发送温度转换指令
}
uint get_temperature()
{
uchar a,b;
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;//加0.5保留小数点后一位,
return temp;
}
//初始化串口
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 main()
{
UART_init(); //串口初始化
ds_change();//DS18b20初始化
delay(1000);
while(1)
{
display(get_temperature());
ds_change();//DS18b20初始化
if(get_temperature()>300)//高于30度蜂鸣器发声
beep=0;
else
beep=1;
//recevice();//接收上位机指令
}
}
//接收数据
void rece() interrupt 4{
if(RI) //检测是否接收完成
{
receiveData=SBUF;//接收完成向下执行
if(receiveData=='1'){
LED1 = 0;
LED2 = 0;
LED3 = 0;
LED4 = 0;
LED5 = 0;
LED6 = 0;
LED7 = 0;
LED8 = 0;
}else{
LED1 = 1;
LED2 = 1;
LED3 = 1;
LED4 = 1;
LED5 = 1;
LED6 = 1;
LED7 = 1;
LED8 = 1;
}
RI=0;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
C#桌面软件与单片机温度测控系统
共80个文件
bmp:15个
cs:8个
cache:8个
需积分: 12 45 下载量 32 浏览量
2017-05-08
17:55:27
上传
评论 2
收藏 1.3MB ZIP 举报
温馨提示
C#桌面软件与单片机测控系统,代码注释都写得蛮详细,希望对后来人有所帮助
资源推荐
资源详情
资源评论
收起资源包目录
5月6号终版_02.zip (80个子文件)
5月6号终版_02
运行截图
运行截图--报警记录.png 19KB
运行截图-历史记录.png 42KB
运行截图——主界面.png 56KB
运行截图--单片机.jpg 327KB
5月6号单片机代码终稿.txt 4KB
thermoscope
thermoscope.suo 47KB
thermoscope.v12.suo 60KB
thermoscope
Form1.Designer.cs 29KB
Program.cs 492B
app.config 417B
Form2.Designer.cs 3KB
Form2.resx 6KB
obj
x86
Release
thermoscope.csproj.GenerateResource.Cache 2KB
thermoscope.Form2.resources 180B
DesignTimeResolveAssemblyReferencesInput.cache 9KB
thermoscope.Form1.resources 425KB
ResolveAssemblyReference.cache 9KB
GenerateResource.read.1.tlog 3KB
TempPE
Properties.Resources.Designer.cs.dll 7KB
thermoscope.exe 2.82MB
thermoscope.Properties.Resources.resources 2.37MB
thermoscope.pdb 48KB
thermoscope.csprojResolveAssemblyReference.cache 19KB
thermoscope.csproj.FileListAbsolute.txt 2KB
GenerateResource.write.1.tlog 2KB
DesignTimeResolveAssemblyReferences.cache 6KB
Debug
thermoscope.Form3.resources 180B
thermoscope.Form2.resources 180B
DesignTimeResolveAssemblyReferencesInput.cache 8KB
thermoscope.Form1.resources 566KB
ResolveAssemblyReference.cache 9KB
GenerateResource.read.1.tlog 3KB
TempPE
Properties.Resources.Designer.cs.dll 7KB
testDataSet.Designer.cs.dll 28KB
thermoscope.exe 2.96MB
thermoscope.Properties.Resources.resources 2.37MB
thermoscope.pdb 52KB
thermoscope.csproj.FileListAbsolute.txt 1024B
GenerateResource.write.1.tlog 2KB
DesignTimeResolveAssemblyReferences.cache 5KB
bin
Release
thermoscope.vshost.exe 24KB
thermoscope.exe 2.82MB
mysql.data.dll 402KB
thermoscope.vshost.exe.config 417B
thermoscope.pdb 48KB
thermoscope.exe.config 417B
Debug
thermoscope.vshost.exe 11KB
thermoscope.exe 2.96MB
thermoscope.vshost.exe.config 417B
thermoscope.pdb 52KB
thermoscope.vshost.exe.manifest 490B
thermoscope.exe.config 417B
thermoscope.csproj 7KB
Form1.cs 23KB
Resources
_CC.jpg 8KB
_7.bmp 142KB
_8.bmp 142KB
_2.bmp 142KB
_5.bmp 141KB
_4.bmp 142KB
_3.bmp 141KB
red_off.bmp 230KB
yellow_on.bmp 228KB
_0.bmp 141KB
_6.bmp 141KB
_9.bmp 142KB
yellow_off.bmp 229KB
red_on.bmp 228KB
_1.bmp 141KB
_C.jpg 7KB
blackpoint.bmp 79KB
thermoscope.csproj.user 478B
Form2.cs 497B
Form1.resx 643KB
Properties
AssemblyInfo.cs 1KB
Settings.Designer.cs 2KB
Resources.resx 10KB
Settings.settings 1012B
Resources.Designer.cs 7KB
thermoscope.sln 875B
共 80 条
- 1
资源评论
BtWangZhi
- 粉丝: 52
- 资源: 15
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功