用温度传感器和烟雾传感器来设置报警装置的话,怎样用单片机无线发送呢
- 提问者网友:兔牙战士
- 2021-12-29 14:25
- 五星知识达人网友:白昼之月
- 2022-01-06 12:22
- 1楼网友:独行浪子会拥风
- 2022-01-06 12:50
一块温度传感器ds18b20和一块单片机就行了(欢迎提问)
#include<reg52.h> #define uchar unsigned char #define uint unsigned int
#define jump_rom 0xcc #define start 0x44 #define read_eerom 0xbe
sbit dq = p2^3; //ds18b20数据口
unsigned char tmph,tmpl;
sbit e=p2^7; //1602使能引脚 sbit rw=p2^6; //1602读写引脚 sbit rs=p2^5; //1602数据/命令选择引脚
void delay_1() { int i,j; for(i=0; i<=10; i++) for(j=0; j<=2; j++) ; }
void enable(uchar del) { p0 = del; rs = 0; rw = 0; e = 0; delay_1(); e = 1; delay_1(); }
void write(uchar del) { p0 = del; rs = 1; rw = 0; e = 0; delay_1(); e = 1; delay_1(); }
void l1602_init(void) { enable(0x01); enable(0x38); enable(0x0c); enable(0x06); enable(0xd0); }
void l1602_char(uchar hang,uchar lie,char sign) { uchar a; if(hang == 1) a = 0x80; if(hang == 2) a = 0xc0; a = a + lie - 1; enable(a); write(sign); }
void l1602_string(uchar hang,uchar lie,uchar *p) { uchar a; if(hang == 1) a = 0x80; if(hang == 2) a = 0xc0; a = a + lie - 1; enable(a); while(1) { if(*p == '\0') break; write(*p); p++; } }
void delay(uint n) { int i; for(i=0; i<n; i++) ; }
uchar reset(void) { uchar deceive_ready; dq = 0; delay(29); dq = 1; delay(3); deceive_ready = dq; delay(25); return(deceive_ready); }
uchar read_bit(void) { uchar i; dq = 0; dq = 1; for(i=0; i<3; i++); return(dq); }
void write_bit(uchar bitval) { dq=0;if(bitval==1) dq=1; delay(5); dq=1; }
uchar read_byte(void) { uchar i,m,receive_data; m = 1; receive_data = 0; for(i=0; i<8; i++) { if(read_bit()) { receive_data = receive_data + (m << i); } delay(6); } return(receive_data); }
void write_byte(uchar val) { uchar i,temp; for(i=0; i<8; i++) { temp = val >> i; temp = temp & 0x01; write_bit(temp); delay(5); } }
void main() { float tt; uint temp; p2 = 0x00; l1602_init(); l1602_string(1,1," ds18b20 test "); l1602_string(2,1,"temperature:00`c"); while(1) { reset(); write_byte(jump_rom); write_byte(start); reset(); write_byte(jump_rom); write_byte(read_eerom); tmpl = read_byte(); tmph = read_byte(); temp = tmph; temp <<= 8; temp = temp | tmpl; tt = tmpl * 0.0625; temp = tmpl / 16 + tmph * 16; l1602_char(2,13,temp/10%10 + 48); l1602_char(2,14,temp%10 + 48); } }