完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
大佬们,使用iic模拟xgzp传感器进行测量气压,但是改了很久还是只能显示0000000,请问哪里出现了问题吗? ***it SDA = P2^0; ***it SCL = P2^1; xdata char i[8]; void I2C_init() { SDA = 1; _nop_(); SCL = 1; _nop_(); } static void Start_I2c() //Æô¶¯×ÜÏß { SDA=1; //·¢ËÍÆðʼÌõ¼þµÄÊý¾ÝÐźŠ_Nop(); SCL=1; _Nop(); //ÆðʼÌõ¼þ½¨Á¢Ê±¼ä´óÓÚ4.7us,ÑÓʱ _Nop(); _Nop(); _Nop(); _Nop(); SDA=0; //·¢ËÍÆðʼÐźŠ_Nop(); //ÆðʼÌõ¼þËø¶¨Ê±¼ä´óÓÚ4¦Ì _Nop(); _Nop(); _Nop(); _Nop(); SCL=0; //ǯסI2C×ÜÏߣ¬×¼±¸·¢ËÍ»ò½ÓÊÕÊý¾Ý _Nop(); _Nop(); } static void Stop_I2c() //½áÊø×ÜÏß { SDA=0; //·¢ËͽáÊøÌõ¼þµÄÊý¾ÝÐźŠ_Nop(); //·¢ËͽáÊøÌõ¼þµÄʱÖÓÐźŠSCL=1; //½áÊøÌõ¼þ½¨Á¢Ê±¼ä´óÓÚ4¦Ì _Nop(); _Nop(); _Nop(); _Nop(); _Nop(); SDA=1; //·¢ËÍI2C×ÜÏß½áÊøÐźŠ_Nop(); _Nop(); _Nop(); _Nop(); } static unsigned char I2C_SendByte(unsigned char Byte) //·¢ËÍÊý¾Ý { unsigned char i,I2C_ACK; for(i=0;i<8;i++) //Òª´«Ë͵ÄÊý¾Ý³¤¶ÈΪ8λ { if((Byte< else SDA=0; _Nop(); SCL=1; //ÖÃʱÖÓÏßΪ¸ß£¬Í¨Öª±»¿ØÆ÷¿ªÊ¼½ÓÊÕÊý¾Ýλ _Nop(); _Nop(); //±£Ö¤Ê±ÖӸߵçƽÖÜÆÚ´óÓÚ4¦Ì _Nop(); _Nop(); _Nop(); SCL=0; } _Nop(); _Nop(); SDA=1; //8λ·¢ËÍÍêºóÊÍ·ÅÊý¾ÝÏߣ¬×¼±¸½ÓÊÕÓ¦´ðλ _Nop(); _Nop(); SCL=1; _Nop(); _Nop(); _Nop(); if(SDA==1) I2C_ACK=0; else I2C_ACK=1; //ÅжÏÊÇ·ñ½ÓÊÕµ½Ó¦´ðÐźŠSCL=0; _Nop(); _Nop(); return I2C_ACK; } static unsigned char I2C_RcvByte() //¶ÁÈ¡Êý¾Ý { unsigned char retc; unsigned char i; retc=0; SDA=1; //ÖÃÊý¾ÝÏßΪÊäÈ뷽ʽ for(i=0;i<8;i++) { _Nop(); SCL=0; //ÖÃʱÖÓÏßΪµÍ£¬×¼±¸½ÓÊÕÊý¾Ýλ _Nop(); _Nop(); //ʱÖӵ͵çƽÖÜÆÚ´óÓÚ4.7us _Nop(); _Nop(); _Nop(); SCL=1; //ÖÃʱÖÓÏßΪ¸ßʹÊý¾ÝÏßÉÏÊý¾ÝÓÐЧ _Nop(); _Nop(); retc=retc<<1; if(SDA==1)retc=retc+1; //¶ÁÊý¾Ýλ,½ÓÊÕµÄÊý¾Ýλ·ÅÈëretcÖÐ _Nop(); _Nop(); } SCL=0; _Nop(); _Nop(); return(retc); } static void Ack_I2c(void) //Ó¦´ð×Óº¯Êý { SCL=0; SDA=0; _Nop(); _Nop(); _Nop(); SCL=1; _Nop(); _Nop(); //ʱÖӵ͵çƽÖÜÆÚ´óÓÚ4¦Ì _Nop(); _Nop(); _Nop(); SCL=0; //ÇåʱÖÓÏߣ¬Ç¯×¡I2C×ÜÏßÒÔ±ã¼ÌÐø½ÓÊÕ _Nop(); _Nop(); } static void NoAck_I2c(void) //·ÇÓ¦´ð×Óº¯Êý { SCL=0; SDA=1; _Nop(); _Nop(); _Nop(); SCL=1; _Nop(); _Nop(); //ʱÖӵ͵çƽÖÜÆÚ´óÓÚ4¦Ì _Nop(); _Nop(); _Nop(); SCL=0; //ÇåʱÖÓÏߣ¬Ç¯×¡I2C×ÜÏßÒÔ±ã¼ÌÐø½ÓÊÕ _Nop(); _Nop(); } static void I2C_SendByteLen(unsigned char addr, unsigned char thedata) { Start_I2c(); I2C_SendByte(0xda); Ack_I2c(); I2C_SendByte(addr); Ack_I2c(); I2C_SendByte(thedata); Ack_I2c(); Stop_I2c(); } unsigned char Read_One_Byte(unsigned char addr) { char mydata; Start_I2c(); I2C_SendByte(0xda); Ack_I2c(); I2C_SendByte(addr); Ack_I2c(); Start_I2c(); I2C_SendByte(0xdb); Ack_I2c(); mydata = I2C_RcvByte(); Stop_I2c(); return mydata; } void Get_XGZPC_Value(float *XGZPC_Date) { char yali1, yali2, yali3; char temp_a5; long int ad; long float XGZPC_Value; temp_a5 = Read_One_Byte(0xA5); temp_a5 = temp_a5 & 0xFD; I2C_SendByteLen(0xA5, temp_a5); I2C_SendByteLen(0x30, 0x0A); delay_ms(10); while ((Read_One_Byte(0x30) & 0x08) > 0); delay_ms(10); yali1 = Read_One_Byte(0x06); yali2 = Read_One_Byte(0x07); yali3 = Read_One_Byte(0x08); ad = yali1 * 65536 + yali2 * 256 + yali3; if(ad > 8388608) { XGZPC_Value = (ad - 16777216) / 8388608 * 8.192; } else { XGZPC_Value = ad / 8388608 * 8.192; } i[0]=(long int)XGZPC_Value / 10000000; i[1]=(long int)XGZPC_Value % 10000000 / 1000000; i[2]=(long int)XGZPC_Value % 1000000 / 100000; i[3]=(long int)XGZPC_Value % 100000 / 10000; i[4]=(long int)XGZPC_Value % 10000 / 1000; i[5]=(long int)XGZPC_Value % 1000 / 100; i[6]=(long int)XGZPC_Value % 100 / 10; i[7]=(long int)XGZPC_Value % 10; Lcd1602_WriteCom(0x80+0x40); Lcd1602_WriteData(i[0]+0x30); Lcd1602_WriteCom(0x80+0x41); Lcd1602_WriteData(i[1]+0x30); Lcd1602_WriteCom(0x80+0x42); Lcd1602_WriteData(i[2]+0x30); Lcd1602_WriteCom(0x80+0x43); Lcd1602_WriteData(i[3]+0x30); Lcd1602_WriteCom(0x80+0x44); Lcd1602_WriteData(i[4]+0x30); Lcd1602_WriteCom(0x80+0x45); Lcd1602_WriteData(i[5]+0x30); Lcd1602_WriteCom(0x80+0x46); Lcd1602_WriteData(i[6]+0x30); Lcd1602_WriteCom(0x80+0x47); Lcd1602_WriteData(i[7]+0x30); *XGZPC_Date = XGZPC_Value; } void main() { unsigned char DisplayBuff[20]; float XGZPC_Date= 0; Lcd1602_init(); I2C_init(); LCD_Write_String(0,0,"ZPC: KPa"); while(1) { Get_XGZPC_Value(&XGZPC_Date); sprintf(DisplayBuff,"%6.2f",XGZPC_Date); LCD_Write_String(5,0,DisplayBuff); } } ***it RS=P2^6; ***it RW=P2^5; ***it E=P2^7; unsigned char text1[]="0"; //µÚÒ»ÐÐÒªÏÔʾµÄ×Ö·û unsigned char text2[]="0"; //µÚ¶þÐÐÒªÏÔʾµÄ×Ö·û void delay_ms(unsigned int z) //ºÁÃ뼶ÑÓʱº¯Êý { unsigned int i,j; for(i=z;i>0;i--) for(j=110;j>0;j--); } unsigned char Lcd1602_ReadBusy() //ÅжÏlcd1602ÊÇ·ñ´¦ÓÚæµÄ״̬£¬¼´¶Áæ { unsigned char temp; RS=0; RW=1; _nop_(); P0=0xff; _nop_(); E=1; _nop_(); temp=P0; _nop_(); E=0; return (temp&0x80); } void Lcd1602_WriteCom(unsigned char com) //дÃüÁî { while(Lcd1602_ReadBusy()); //ÅÐæ RS=0; RW=0; _nop_(); P0=com; //×¼±¸·¢ËÍÃüÁî _nop_(); E=1; _nop_(); _nop_(); E=0; _nop_(); _nop_(); } void Lcd1602_WriteData(unsigned char dat) //дÊý¾Ý { while(Lcd1602_ReadBusy()); //ÅÐæ RS=1; //Êý¾Ý RW=0; //д _nop_(); P0=dat; _nop_(); E=1; _nop_(); _nop_(); E=0; _nop_(); _nop_(); } void Lcd1602_init() //³õʼ»¯º¯Êý { delay_ms(15); Lcd1602_WriteCom(0x38); //ÏÔʾģʽÉèÖà delay_ms(5); Lcd1602_WriteCom(0x38); delay_ms(5); Lcd1602_WriteCom(0x38); Lcd1602_WriteCom(0x0c); //ÏÔʾ¿ª Lcd1602_WriteCom(0x01); //ÏÔʾÇåÆÁ Lcd1602_WriteCom(0x06); //ÏÔʾ¹â±ê } void LCD_Write_String(unsigned char x,unsigned char y,unsigned char *s) { if (y == 0) { Lcd1602_WriteCom(0x80 + x); //±íʾµÚÒ»ÐÐ } else { Lcd1602_WriteCom(0xC0 + x); //±íʾµÚ¶þÐÐ } while (*s!= ' |