#include<PIC18.h>
__CONFIG(1,RETEN_OFF & INTOSCSEL_LOW & SOSCSEL_DIG & XINST_OFF & FOSC_XT & PLLCFG_OFF & FCMEN_ON & IESO_ON );
__CONFIG(2,PWRTEN_OFF & BOREN_ON & BORV_0 & BORPWR_MEDIUM & WDTEN_OFF & WDTPS_256 ); //PWRTEN_OFF-->PWRTEN_ON
// WDTEN_OFF-->WDTEN_ON
__CONFIG(3,RTCOSC_T1OSCREF & EASHFT_OFF & WAIT_OFF & CCP2MX_PORTC & ECCPMX_PORTH & MCLRE_ON );
__CONFIG(4,STVREN_OFF); // add DEBUG_EN &
__CONFIG(5,CP1_OFF & CP2_OFF & CP3_OFF & CP4_OFF & CP5_OFF & CP6_OFF & CP7_OFF & CPB_OFF & CPD_OFF);
__CONFIG(6,WRT0_OFF & WRT1_OFF & WRT2_OFF & WRT3_OFF & WRT4_OFF & WRT5_OFF & WRT6_OFF & WRT7_OFF & WRTC_OFF & WRTB_OFF & WRTD_OFF );
__CONFIG(7,EBRT0_OFF & EBRT1_OFF & EBRT2_OFF & EBRT3_OFF & EBRT4_OFF & EBRT5_OFF & EBRT6_OFF & EBRT7_OFF& EBRTB_OFF );
#define uchar unsigned char
#define uint unsigned int
# define DQ PORTHbits.RH4 //定义18B20数据端口
# define DQ_DIR TRISHbits.TRISH4 //定义18B20D口方向寄存器
# define DQ_HIGH() DQ_DIR =1 //设置数据口为输入
# define DQ_LOW() DQ = 0; DQ_DIR = 0 //设置数据口为输出
#define E LATJbits.LATJ1
#define RW LATBbits.LATB2
#define RS LATJbits.LATJ0
#define busy PORTEbits.RE7
#define Busy 0x80 //用于检测LCD状态字中的Busy标识
#define _XTAL_FREQ 4000000UL
const unsigned char AC_TABLE[]={
0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87, //第一行汉字位置
0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97, //第二行汉字位置
0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f, //第三行汉字位置
0x98,0x99,0x9a,0x9b,0x9c,0x9d,0x9e,0x9f, //第四行汉字位置
};
const unsigned char AC_TABLE1[]={
0x80, //第一行汉字位置
0x90, //第二行汉字位置
0x88, //第三行汉字位置
0x98, //第四行汉字位置
};
unsigned char Data_TABLE[4][17]=
{
{0xce,0xc2,0xb6,0xc8,0xa3,0xba,0xA1,0xa0,0xA1,0xa0,0xA1,0xa0,0xA1,0xa0,0xA1,0xa0,},
{0xca,0xaa,0xb6,0xc8,0xa3,0xba,0xA1,0xa0,0xA1,0xa0,0xA1,0xa0,0xA1,0xa0,0xA1,0xa0,},
{0xd1,0xb9,0xc1,0xa2,0xa3,0xba,0xA1,0xa0,0xA1,0xa0,0xA1,0xa0,0xA1,0xa0,0xA1,0xa0,},
{0xd1,0xb9,0xc7,0xbf,0xa3,0xba,0xA1,0xa0,0xA1,0xa0,0xA1,0xa0,0xA1,0xa0,0xA1,0xa0,},
};
unsigned int Send_AT1[3] = {'+','+','+'};
unsigned char Send_AT2[5]={'A','T','S','H','\r'};
unsigned char Send_AT3[5]={'A','T','S','L','\r'};
unsigned char Send_AT4[5]={'A','T','C','N','\r'};
unsigned char Rec_ZigBee_ID[30]; //接收zigbee的16位ID号
unsigned char ZigBee_ID[16]; //存放zigbee的16位ID号
unsigned char ID; //接收计数值
uchar TLV; //采集到的温度高8位
uchar THV; //采集到的温度低8位
uchar TZ; //转换后的温度值整数部分
uchar TX; //转换后的温度值小数部分
uchar wd; //转换后的温度值BCD码形式
unsigned char ShiDu;
unsigned char YaLi;
unsigned char YaQiang;
float shidu1,yali1,yaqiang1;
unsigned char adresulth;
unsigned char adresultl;
unsigned int adresult1,adresult2,adresult3;
unsigned char RH;
unsigned char Send_Data[30]={0x7E,0x1C,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x14,0x13,0x12,0x11,0x10,0x01,0x01,0x00,0x28,0x00}; //存储要发送的数据
unsigned char Test_Data[30]={0x7E,0x1C,0x02,0x00,0x30,0x30,0x31,0x32,0x34,0x42,0x30,0x30,0x30,0x31,0x32,0x38,0x42,0x33,0x30,0x39,0x3C,0x25,0x3A,0x23,0x24,0x01,0x01,0x00,0x28,0x6A};
void uart_init();
void IO_init();
void Read_ZigBee_ID();
reset(void);
void write_byte(unsigned char val);
unsigned char read_byte(void);
void get_temp();
void ad_init();
void PutAllStr(const unsigned char *puts);
void send_i(unsigned char x);
void send_d(unsigned char x);
void wr_zb();
void chk_busy();
void lcd_init();
void clear_p();
void Read_AD_Data();
void delay();
void senddata();
void delay_500ms();
void delay_5s();
void Delay500Us(void);
void DelayUs(unsigned char x);
void DelayMs(unsigned char cnt);
void display();
void main()
{
unsigned char j1;
TZ=22;
IO_init();
ad_init();
uart_init();
__delay_us(500);
lcd_init(); //LCD初始化
clear_p(); //清除显示
__delay_us(500);
Read_ZigBee_ID();
delay_500ms();
for(j1=0;j1<16;j1++)
{
Send_Data[j1+4]=ZigBee_ID[j1];
}
PutAllStr(Data_TABLE);
display();
while(1)
{
get_temp();
__delay_us(100);
Read_AD_Data();
__delay_us(100);
senddata();
delay_500ms();
}
}
void uart_init()
{
TRISCbits.TRISC6=0;
TRISCbits.TRISC7=1;
ODCON3 = 0b11000000; //设置串口1串口2的TX输出方式为开漏输出
BAUDCON1= 0b01001000; //使能串口,允许接收.自动波特率关闭
SPBRGH1 = 0x00;
SPBRG1 = 0x67; //9600
TXSTA1 = 0b00000100; // 8位数据,异步方式 高波特率选择
RCSTA1 = 0b10010000;
IPR1bits.RC1IP=1;
INTCON = 0b11000000;
PIE1= 0b00100000;
}
void IO_init()
{
ANCON1bits.ANSEL12=0;
TRISB=0X00; //设置B口为输出
TRISE=0X00; //设置D口为输出
TRISJ=0x00;
}
void Read_ZigBee_ID()
{
unsigned char i,i1,i2,i3,i4;
TX1IF=0;
ID=0;
for(i1=0;i1<3;i1++)
{
TXREG1=Send_AT1[i1];
TXSTA1bits.TXEN=1;
while(!TX1IF);
TX1IF=0;
}
delay_500ms();
delay_5s();
ID=0;
for(i2=0;i2<5;i2++)
{
TXREG1=Send_AT2[i2];
TXSTA1bits.TXEN=1;
while(!TX1IF);
TX1IF=0;
}
__delay_us(500);
delay_500ms();
delay_5s();
for(i=0;i<8;i++)
{
ZigBee_ID[i]=Rec_ZigBee_ID[i];
}
ID=0;
i=0;
for(i3=0;i3<5;i3++)
{
TXREG1=Send_AT3[i3];
TXSTA1bits.TXEN=1;
while(!TX1IF);
TX1IF=0;
}
delay_500ms();
delay_5s();
for(i=0;i<8;i++)
{
ZigBee_ID[8+i]=Rec_ZigBee_ID[i];
}
for(i4=0;i4<5;i4++)
{
TXREG1=Send_AT4[i4];
TXSTA1bits.TXEN=1;
while(!TX1IF);
TX1IF=0;
}
delay_500ms();
delay_5s();
}
reset(void)
{
char presence=1;
while(presence)
{
DQ_LOW() ; //主机拉至低电平
__delay_us(480); //共延时480us
DQ_HIGH(); //释放总线等电阻拉高总线,并保持15~60us
__delay_us(40); //延时40us
if(DQ==1) presence=1; //没有接收到应答信号,继续复位
else presence=0; //接收到应答信号
__delay_us(480); //共延时440us
}
}
void write_byte(unsigned char val)
{
unsigned char i;
unsigned char temp;
for(i=8;i>0;i--)
{
temp=val&0x01; //最低位移出
DQ_LOW();
__delay_us(15); //保持拉低5us
if(temp==1) DQ_HIGH(); //如果写1,拉高电平
__delay_us(60); //延时60us
DQ_HIGH();
__delay_us(3); //在两位之间插入3us延时
val=val>>1; //右移一位
}
}
unsigned char read_byte(void)
{
unsigned char i;
unsigned char value=0x00; //读出温度
for(i=8;i>0;i--)
{
value>>=1;
DQ_LOW();
__delay_us(2); //保持总线拉低3us
DQ_HIGH(); //拉至高电平
__delay_us(20); //释放总线后保持7us再读取数据
if(DQ)
value|=0x80;
__delay_us(60); //延时50us,保证每1位的60us延时
}
return(value);
}
void get_temp()
{
int i;
DQ_HIGH();
reset(); //复位等待从机应答
write_byte(0XCC); //忽略ROM匹配
write_byte(0X44); //发送温�