- #include <stc12c5410ad.h>
- #include<absacc.h>
- #include<intrins.h>
- #include<math.h>
- #define byte unsigned char
- #define word unsigned int
- /*======================TMR0定義==============================*/
- #define t0h (65536-9216)/256 //12MHz定時器定時10ms 減10000
- #define t0l (65536-9216)%256 //11.0592MHz定時器定時10ms 減9216
- uchar t0_count=0;
- uchar t0_1h=0;
- uint t0_hour=0;
- uchar t0_10ms,t0_10s,t0_20s,t0_2m=0,t0_5m,t0_10m,t0_10m_e,t0_1m,ad_10m;
- bit first_1m=1;
- bit normal=1;
- /*=======================其他定義================================*/
- void change_temp(uchar b20_num); //溫度值處理函數
- uchar temp_bai,temp_shi,temp_ge;
- bit ad_en=0;
- /*======================EEPROM ISP/IAP定義======================*/
- uchar ADH=0X10;
- uchar ADL=0X00;
- uchar ISP_READ(uchar adh,adl);//讀EEPROM函數
- void ISP_WRITE(uchar adh,adl,write_byte);//寫EEPROM函數
- void ISP_ERASE(uchar adh,adl);//擦除EEPROM函數
- void ISP_DISABLE();//關閉ISP功能函數
- uchar read_eeprom[11]; //讀EEPROM數據存儲器
- /*=====================UART定義=========================*/
- //#define reload_count 0xD0;//Fosc=11.0592MHz,12T模式,SMOD=0時,計算出的自動重裝數和波特率600
- //#define reload_count 0xe8;//Fosc=11.0592MHz,12T模式,SMOD=0時,計算出的自動重裝數和波特率1200
- #define reload_count 0xfd;//Fosc=11.0592MHz,12T模式,SMOD=0時,計算出的自動重裝數和波特率9600
- void initial_uart();
- void tx_dat(uchar len,uchar stemp[]); //串口送數據子函數
- void send_one_byte(uchar tx_data);
- uchar xdata rc_count=0;
- uchar xdata rc_bytes[200];
- bit rc_end=0; //接受結束標志為
- bit changenumber_flag=0;
- //===============GSM指令定義===========================
- uchar code cm0[]="AT\r\n";
- uchar code cm01[]="ATE0\r\n"; //ATE0關閉回顯
- uchar code cm1[]="AT+CMGF=0\r\n"; //AT+CMGF=0 采用PDU格式,長度11
- uchar code cm2[]="AT+CNMI=2,2,0,0,1\r\n"; //AT+CNMI=2,2,0,0,1 設置接收顯示
- uchar code fuhao[3]="002";
- uchar code cm9[]="5EA6"; //度
- uchar code cm10[16]="6E295EA68FC79AD8"; //溫度過高
- uchar temp_bai,temp_shi,temp_ge;
- uchar false_count=0; //失敗計數值
- sbit cs=P1^2;
- sbit dat=P1^1;
- sbit clk=P1^0;
- void DelayNS(unsigned int no);
- void senddat(unsigned char d);
- void send_i(unsigned char d);
- void send_d(unsigned char d);
- void hzkdis(uchar *s);
- void init_lcm(void);
- void senddat(unsigned char d)/*寫字節*/
- { unsigned char j;
- for(j=0;j<8;j++)
- {
- clk=0;
- if(d&0x80)dat=1;
- else dat=0;
- clk=1;
- d=d<<1;
- }
- }
- void send_i(unsigned char d)/*寫命令函數*/
- {
- unsigned char i;
- cs=1;
- senddat(0xf8);
- i=d&0xf0;
- senddat(i);
- i=(d%16)*16;
- senddat(i);
- cs=0;
- }
- void send_d(unsigned char d)/*寫數據函數 */
- {
- unsigned char i;
- cs=1;
- senddat(0xfa);
- i=d&0xf0;
- senddat(i);
- i=(d%16)*16;
- senddat(i);
- cs=0;
- }
- //===================鍵盤定義==============================
- void key_ctrl();
- void key();
- sbit key1_add=P1^3;
- sbit key2_set_enter=P3^4;
- sbit key3_down=P3^5;
- bit b_add,b_set_enter,b_down,k_key1,k_key2,k_key3;
- bit set_flag,set_end_flag;
- bit sf;
- bit disp_flag=0;
- uchar key_count=1,add_count=0,addr_disp=0;
- uchar set_temp,set_temp_shi=0,set_temp_ge=0,set_num=8,set_time=1;
- bit set_temp_flag,set_num_flag,set_time_flag,set_mobile_flag;
- bit move_flag;
- uchar xdata mobile_num[11]="13000000000";
- void key()
- {
- );
- set_temp=set_temp_shi*10+set_temp_ge;
- }
- if(set_time_flag==1)
- {
- set_time_flag=0;
- set_mobile_flag=0;
- move_flag=0;
- void DelayNS(unsigned int no)
- {
- unsigned char j;
- for(; no>0; no--)
- {
- for(j=0; j<255; j++);
- }
- }
- uchar ISP_READ(uchar adh,adl)
- {
- uchar read_byte;
- ISP_CONTR=0x83;
- ISP_CMD=0x01;
- ISP_ADDRH=adh;
- ISP_ADDRL=adl;
- ISP_TRIG=0X46;
- ISP_TRIG=0XB9;
- EA=0;
- _nop_();
- read_byte=ISP_DATA;
- EA=1;
- return(read_byte);
- }
- void ISP_WRITE(uchar adh,adl,write_byte)
- {
- ISP_CONTR=0x83;
- ISP_CMD=0x02;
- ISP_ADDRH=adh;
- ISP_ADDRL=adl;
- ISP_DATA=write_byte;
- EA=0;
- ISP_TRIG=0X46;
- ISP_TRIG=0XB9;
- _nop_();
- EA=1;
- }
- ROM_NO[rom_byte_number] &= ~rom_byte_mask;
- // serial number search direction write bit
- OWWriteBit(search_direction);
- //OWWriteBit(0);
- // increment the byte counter id_bit_number
- // and shift the mask rom_byte_mask
- id_bit_number++;
- rom_byte_mask <<= 1;
- // if the mask is 0 then go to new SerialNum byte rom_byte_number and reset mask
- if (rom_byte_mask == 0)
- {
- docrc8(ROM_NO[rom_byte_number]); // accumulate the CRC
- rom_byte_number++;
- rom_byte_mask = 1;
- }
- }
- }
- while(rom_byte_number < 8); // loop until through all ROM bytes 0-7
- // if the search was successful then
- if (!((id_bit_number < 65) || (crc8 != 0)))
- {
- // search successful so set LastDiscrepancy,LastDeviceFlag,search_result
- LastDiscrepancy = last_zero;
- // check for last device
- if (LastDiscrepancy == 0)
- LastDeviceFlag = TRUE;
- search_result = TRUE;
- }
- }
- // if no device found then reset counters so next 'search' will be like a first
- if ((!search_result
- // 1-Wire Functions to be implemented for a particular platform
- //--------------------------------------------------------------------------
- /
- *延時函數,us級
- *函數名稱:delay(word seconds)
- *說明:不精確延時
- /
- void delay(uint seconds)
- {
- uint sec=0;
- sec=seconds*12;
- while(--sec);
- }
- uchar OWReset()
- {
- uchar presence;
- DQ = 0; /*pull DQ line low*/
- delay(44); /*至少480微秒, leave it low for 501us*/
- DQ = 1; /*allow line to return high?*/
- delay(4); /* wait for presence18b20 36us*/
- presence = DQ; /*get presence signal 0= DS18B20 ready, 1= busy*/
- delay(13); // wait for end of timeslot
- return(presence);
- }
- uchar OWReadByte(void)
- {
- int j=0;
- uchar i,byte_value;
- for (i=8;i>0;i--)
- {
- byte_value>>=1;
- DQ = 0; /* pull DQ low to start timeslot*/
- _nop_();_nop_();_nop_();_nop_();
- DQ = 1; /*then return high*/
- if(DQ) byte_value|=0x80;
- delay(4); /*wait for rest of timeslot*/
- }
- return(byte_value);
- }
- temp_b20[num]=Read_Temperature(num);/*num DS18B20號*/
- delay(50);
- if(again_flag==1){again_flag=0;goto L2; }
- if((error_flag==1)&&(num==(set_num-1)))
- {
- error_flag=0;
- device_error=1;
- send_error_flag=1;
- ds18b20_error=1;
- ISP_ERASE(0x2e,0x00);
- void SEARCH_SAVE()
- {
- uchar rslt,ds18b20_num=0,i,m=1,j,addr;
- rslt=OWFirst();
- if(rslt==1)new_flag=1;
- while(ds18b20_num<set_num) //將來可能改為 ds18b20_num<set_num 設置個數
- {
- while(!rslt)
- {rslt=OWFirst();if(rslt==1)new_flag=1;}
- for(i=m;i>0;i--)
- if((send_gsm_flag==1)&&(en_gsm==1)) //向GSM發送數據
- {
- send_gsm_flag=0;
- send_to_gsm();
- }
- if(disp_flag==1)
- {
- disp_flag=0;
- send_i(0x01);
- DelayNS(12);
- send_i(0x84);
- hzkdis("度");
- send_i(0x81);
- send_d(set_temp_shi+0x30);
- send_d(set_temp_ge+0x30);
- send_i(0x94);
- hzkdis("路") ;
- send_i(0x91);
- send_d(set_num/10+0x30);
- send_d(set_num%10+0x30);
- send_i(0x8b);
- hzkdis("小時") ;
- send_i(0x89);
- send_d(set_time/10+0x30);
- send_d(set_time%10+0x30);
- send_i(0x98);
- for(i=0;i<11;i++) send_d(mobile_num[ i]);
- }
- }
復制代碼
|