code MSP4302274 giao tiep AM2301 hiển thị LCD, đọc nhiệt độ và độ ẩm hiển thị LCD 1602. Mạch đo nhiệt độ độ ẩm dùng cảm biến số DHT21. CODE được trích từ ứng dụng tự tạo.Đọc chính xác nhiệt độ và độ ẩm.
#include "msp430f2274.h" #include "uart.c" #include "LCD.c" #define DTH_option (_P3_DIRECT->_BIT.b6) #define DTH_Start (_P3_OUT->_BIT.b6) #define DTH_Read (_P3_IN->_BIT.b6) void main( ) { // Stop watchdog timer to prevent time out reset WDTCTL = WDTPW + WDTHOLD; BCSCTL1 = CALBC1_1MHZ; DCOCTL = CALDCO_1MHZ; uart_init(); init_port(); init_NRF24L01(); init_lcd(); _P4_OUT->_BIT.b0=0; _P4_OUT->_BIT.b1=0; unsigned int i=0,j=0; send_cmd("AT"); send_cmd("AT+RST"); while(1) {lb0: ClearLcmScreen(); // thiet lap IO // khoi tao nRF24L01 PrintStr("MONITOR SYSTERM"); LCDmove(1,0); PrintStr("=================================+++++++");goto lb1; lb01: HUMI_TEMP(); lb1: //lcd_write_cmd(0x18); // delay_cycles(400); j=j+1; if(j==50) { goto lb01; } if(j==100){ j=0; goto lb0; } //=================Cho nhan canh bao====================================== RX_Mode(); lbw: if(!(nRF24L01_RxPacket(RxBuf)))//Neu chua nhan duoc du lieu { if(b==10) { b=0; goto lb11; } b=b+1; goto lbw; } else { if(RxBuf[4]=='3') //NEU LA DU LIEU CUA MASTER { for(j=0;j_BIT.b6=1; delay_cycles(100000); } } } lb11: //====================Quet phim============================================= if(_P3_IN->_BIT.b3==0) {ClearLcmScreen(); PrintStr("Sent SLAVE1"); TxBuf[0]='8'; unsigned char TX_ADDRESS1[5]={0x17,0x18,0x19,0x1A,0x1B}; lb2: SpiWriteReg(WRITE_REG+STATUS,0xff); TX_Mode(TxBuf,TX_ADDRESS1); Delay(600);// xem lai600 SpiWriteReg1(0xE1); RX_Mode(); if(!(nRF24L01_RxPacket(RxBuf)))//Neu nhan duoc du lieu { i=i+1; if(i==10){i=0;goto lb3;} goto lb2; } lb3: if(RxBuf[0]=='A'){ClearLcmScreen();LCDmove(0,0);PrintStr("SL1 ON");} else {ClearLcmScreen();LCDmove(0,0);PrintStr("SL1 OFF");} clear_RXBUF_RF(); delay_cycles(640000);ClearLcmScreen(); ClearLcmScreen(); PrintStr("Sent SLAVE2"); TxBuf[0]='8'; unsigned char TX_ADDRESS2[5]={0x12,0x13,0x14,0x15,0x16}; lb21: SpiWriteReg(WRITE_REG+STATUS,0xff); TX_Mode(TxBuf,TX_ADDRESS2); Delay(600);// xem lai600 SpiWriteReg1(0xE1); RX_Mode(); if(!(nRF24L01_RxPacket(RxBuf)))//Neu nhan duoc du lieu { i=i+1; if(i==10){i=0;goto lb31;} goto lb21; } lb31: if(RxBuf[0]=='A'){ClearLcmScreen();LCDmove(0,0);PrintStr("SL1 ON");} else {ClearLcmScreen();LCDmove(0,0);PrintStr("SL1 OFF");} clear_RXBUF_RF(); delay_cycles(320000);ClearLcmScreen();goto lb0; } // end mode -goto lb1; }//dong while }//dong main //==========================doc AM2301=================================================== void HUMI_TEMP(void) { char HUMI; char TEMP; char HUMI_1; char TEMP_1; char SUM=0; int a=0,b=0,c=0,d=0,e=0,f=0; read: DTH_option=1; DTH_Start=1; delay_cycles(10000); DTH_Start=0; delay_cycles(10000); DTH_Start=1; DTH_option=0; while(DTH_Read) { delay_cycles(1); a=a+1;if(a>200){ClearLcmScreen();PrintStr("Khong co phan hoi"); delay_cycles(320000);goto read;} } while(!(DTH_Read)) { delay_cycles(1);} while(DTH_Read) { delay_cycles(1);} for(a=0;a