Physical map
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/8B/2F/wKioL1hGsMORxBttAAJMGUmukvs286.png "title=" Am2320.png "alt=" Wkiol1hgsmorxbttaajmgumukvs286.png "/>
Pin diagram
650) this.width=650; "src=" http://s5.51cto.com/wyfs02/M00/8B/33/wKiom1hGsM-Db6GDAADX2K1req8130.png "title=" pin. png "alt=" Wkiom1hgsm-db6gdaadx2k1req8130.png "/>
Wiring diagram
650) this.width=650; "src=" http://s2.51cto.com/wyfs02/M01/8B/33/wKiom1hGsN7i8SMAAAGFqcnBRuE198.jpg "title=" Img_ 20161206_203345.jpg "alt=" Wkiom1hgsn7i8smaaagfqcnbrue198.jpg "/>
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/8B/2F/wKioL1hGsPGwzKfYAAJZONvylYs835.png "title=" Untitled. png "alt=" Wkiol1hgspgwzkfyaajzonvylys835.png "/>
source code
Am Series temperature and humidity sensor, read IIC Use example //microcontroller :at89s52 or stc89c52rc //function : Serial send temperature and humidity data baud rate 9600 //Crystal :12m (user system clock if not 12m please change the relevant macro definition and comment delay time)//Baud rate: 9600//compilation environment: keil 4//********************** #include "reg52.h" #include <intrins.h> #define USE_T2 #define &NBSP;FOSC&NBSP;&NBSP;12000000#DEFINE&NBSP;BAUD&NBSP;&NBSP;9600//Port bit definition, can be modified sbit sda=p1^0;sbit scl=p1^1;//internal data definition #define iic_add 0xb8 //device address #define iic_rx_length 15unsigned char iic_tx_buffer[]={0x03,0x00,0x04}; //read temperature and Humidity command (no CRC check) unsigned char iic_rx_buffer[iic_rx_length] = {0x00};//read back temperature and humidity Unsigned char uart_rx_buffer [30] = {0x00};unsigned char *string;unsigned char wr_flag;//string Definition #define s_ function "Function: 03 04 "#define S_Temp Temp:" #define S_RH "RH:" #define s _crct "Crc: true" #define S_CRCF "Crc: wrong" #define S_Data "data: " # define s_nots "sensor not connected" void ack (void); Void noack (void); void delay10us (void) //This delay function to be greater than 5US { _nop_ (); _nop_ (); _nop_ (); _nop_ (); _nop_ (); _nop_ (); } void delay1ms (unsigned int t) { unsigned int i; unsigned int j; for (j=t;j>0;j--) for (i=124;i>0;i--); }void inituart (void) {unsigned int itmpbaud; unsigned long ltmpbaud;itmpbaud = 0;//First select Timer 2 as the baud rate generator, 16-bit timer, automatic loading scon = 0x50;// Sm0 sm1 sm2 ren tb8 rb8 ti ri//0 1 0 1 &nbSp;0 0 0 0pcon = 0x00;//pcon's address is 87H, here Smod =0t2con = 0x30;//tf2 exf2 rclk tclk exen2 tr2 c (/T2) CP (/RL2) //0 0 1 1 0 0 0 0 T2MOD = 0x00;// //////T2OEDCEN //0 0 0 0 0 0 0 0//fosc = 22.1184m,6t: 144, setting the baud rate (rcap2h,rcap2l) = 65536- fosc/(N*baud). N:32 (12t-mode),: (6t-mode) ltmpbaud = fosc/baud;ltmpbaud /= 32;//12t-modeitmpbaud = lTmpBaud & 0xFFFF;iTmpBaud = 65536 - iTmpBaud; rcap2h = (itmpbaud>>8) & 0x0FF; rcap2l = itmpbaud & 0x0ff; ri = 0;//clear receive interrupt flag ren = 1;//allow serial receive es = 1;//allow serial interrupt tr2 = 1;//start timer 1 ea=1;//Open Total interrupt} //serial port send Void uartsend (Char uchar) { sbuf=uchar; while (ti==0); ti=0;} Void uartread (void) interrupt 4{ char temp; if (RI) { ri=0; temp = sbuf; }}//******************* Send starting Bit sda=1->0void i2c_start () { sda=1; scl=1; delay10us (); sda=0; delay10us (); scl=0; }//***************** Send Stop Bit sda=0->1void i2c_stop () { sda=0; delay10us (); scl=1; delay10us (); sda=1;} Main answer (contains ack:sda=0 and no_ack:sda=0) void ack (void) { //set sda port as output sda=0; scl=0; delay10us (); &NBSP;&NBSP;SCL=1;&NBSP;&NBSP;&NBSP;DELAY10US (); scl=0; sda=1;} Void noack (void) { //set sda port as output sda=1; scl=0 ; delay10us (); scl=1; delay10us (); SDA=1; scl=0;} detects if &NBSP;SDA back Ackbit test_ack () { //set sda port as input bit ack_flag= 0;&NBSP;&NBSP;&NBSP;SCL=0;&NBSP;&NBSP;&NBSP;SDA=1;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;DELAY10US (); scl=1; delay10us (); if (sda==0) ack_ flag = 1; else ack_flag = 0; scl=0; return ack_flag;} BYTE sender//Send C (can be data also address), sent after receiving from answer//not considered from the answer bit void SendData (unsigned char buffer) { unsigned char bitcnt=8;//byte 8 bits //set sda port as output do { scl=0; delay10us (); if ((buffer&0x80) ==0) //determine whether the highest bit is 0 or 1 sda=0; else SDA=1; scl=1; delay10us (); buffer=buffer<<1;// Move data in buffer to the left one bitcnt--; } while (BitCnt) ; scl=0; }//********************************** BYTE receiver//receiving device data transmitted, this program should match | The main answer function |i2c_ack_main () using the//return: uchar type 1 byte unsigned char receivedata () { unsigned char bitcnt=8,iic_rx_data=0; unsigned char temp=0; sda=1; //read-in data Set sda port as input d o { scl=0; delay10us (); iic_rx_data=_crol_ (IIC _rx_data,1) //data shift left one &NBSP;BITCNT--;&NBSP;&NBSP;&NBSP;SCL=1;&NBSP;DELAY10US (); if (sda==1) iic_rx_data = iic_rx_data|0x01 ; //Low Position 1 else iic_rx_data = iic_rx_data&0x0fe; //Low 0 } while (bitcnt); scl=0; return iic_rx_data;} Bit writenbyte (unsigned char sla,unsigned char *s,unsigned char n) { unsigned char i; i2c_start (); //start I2c senddata (SLA);//Send Device Address if ( ! Test_ack ()) &NBSP;&NBSP;&NBSP;{&NBSP;&NBSP;&NBSP;&NBsp; wr_flag = 1; return (0); } for (i=0;i< n;i++)//write 8 bytes of data { senddata (* (s+i)); if (! Test_ack ()) { wr_flag = 1;return (0); } } i2c_stop (); return (1);} Bit readnbyte (unsigned char sal, unsigned char *p,unsigned char n) { unsigned char i; i2c_start (); // start i2c senddata (Sal) | 0x01); //Send device address if (! Test_ack ()) { wr_flag = 1;return (0); } delay10us (); delay10us (); delay10us (); // delay time must be greater than 30us as long as it is greater than 30us Above the value can be but preferably not too long , test, tried 25MS are ok! for (i=0;i<n-1;i++) //read byte data { * (P+i) =receivedata (); //Read Data ack (); } * (p+n-1) =receivedata (); noack (); i2c_stop (); return (1); }/// Calculate CRC Check Code UNSIGNED&NBSP;INT&NBSP;CRC16 (Unsigned char *ptr, unsigned char len) { unsigned int crc=0xffff; unsigned char i; while (len--) { crc ^=*ptr++; for (i=0;i<8;i++) { if (CRC & 0x1) { crc>>=1; crc^= 0xa001; } else { crc> >=1; }&nbsP;&NBSP;&NBSP;}&NBSP;&NBSP;&NBSP;}&NBSP;&NBSP;&NBSP;RETURN&NBSP;CRC;} Check that the CRC check code is correct UNSIGNED&NBSP;CHAR&NBSP;CHECKCRC (Unsigned char *ptr,unsigned char len) { unsigned int crc;crc= (Unsigned int) CRC16 (ptr,len-2); if (ptr[len-1]== (crc>>8) && ptr[len-2]== (CRC&NBSP;&&NBSP;0X00FF)) { return 0xff;} else{ return 0x0;}} Void waken (void) { i2c_start (); // start I2c senddata (iic_add); // send device Address test_ack (); // wake-up instruction sensor does not return to ack but the first must send a clock to detect Ack otherwise error delay1ms (2); // at least 1 ms instructions, There is a maximum value of In practice you only need to be greater than 1ms i2c_stop (); }void uart_putstring ( Unsigned char *buf&nbsP;) {while (*BUF) uartsend (*buf++);} void uart_putstringandnum (unsigned char *buf ,unsigned int num) {unsigned char a[3],i;a[3] = ' 0 ' +num%10;a[2] = '; a[1] = ' 0 ' +num/10%10;a[0] = ' 0 ' +num/100%10;while (*buf) Uartsend (*buf++); Uartsend (' '); for (i=0;i<4;i++) {uartsend (a[i]);} }void uart_putstringand_data (Unsigned char *buf ,unsigned char *bufdata) {unsigned char a[2],i,j;while (*BUF) uartsend (*buf++); Uartsend (' '); for (i=0;i<8;i++) {a[0] = bufdata[i]/16; a[1] = bufdata[i]%16;for (j=0;j<2;j++) { if (a[j]>9) { a[j] = (a[j]-10) + ' a '; } else { a[j] = a[j]+ ' 0 '; } Uartsend(A[j]);} Uartsend (' ');} }void uartsend_nbyte (void) { int tmp; if (wr_flag == 0) { if (CheckCRC (IIC_RX_Buffer , 8)) { String = S_Function; // "function: 03 04" uart_putstring (String); uartsend (' '); uartsend (' '); string = s_rh;//"RH:" tmp = iic_rx_buffer[2]*256+iic_rx_buffer[3]; uart_putstringandnum (string,tmp); Uartsend (' '); uartsend (' '); string = s_temp; //"Temp:" Tmp = IIC_RX_Buffer[4]*256+IIC_RX_Buffer[5]; uart_putstringandnum (string,tmp); uartsend (' '); uartsend (' '); string = s_crct;//"Crc: true"; uart_putstring (String); }else {string = s_data;//"data: "; Uart_putstringand_data (String,iic_rx_buffer); uartsend (' '); uartsend (' '); string = s_crcf;//"Crc: wrong"; uart_putstring (String); } }else{ string = s_nots;//"Sensor Not connected "; uart_putstring (String);} Uartsend (0x0A); } void clear_data (void) { int i; for (i=0; i<iic_rx_length;i++) { iic_rx_buffer[i] = 0x00; }//receive data clear 0 }void main (void) { scl = 1; sda = 1; //Power on the two bus to ensure that the high inituart (); clear_data (); while (1) {clear_data (); // Clear received data wr_flag = 0; Waken (); // wake-up sensor //send Read command writenbyte (IIC_Add,IIC _tx_buffer,3) //at least 2MS after sending the read or write Data command (to prepare the probe for time to return data) delay1ms (2); //read return Data readnbyte (iic_add,iic_rx_buffer,8); scl = 1; sda = 1;//confirm the release bus //send sensor data up through the serial port Uartsend_Nbyte (); delay1ms (two), //time delay 2s (at least 2S) }}
Compiling diagram:
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/8B/33/wKiom1hGsWnjDamZAAGB3ioU3M4868.png "title=" Untitled. 2png.png "alt=" Wkiom1hgswnjdamzaagb3iou3m4868.png "/>
This article is from the "Soul Bucket" blog, please be sure to keep this source http://990487026.blog.51cto.com/10133282/1880145
AM2320 temperature and humidity sensor C51 program UART serial output temperature and humidity