//±¾Àý×Ó³ÌÐòÌṩһÖÖ²Ù×÷·½·¨£¬½ö¹©²Î¿¼ // ******************************************************************* // Website: http://wch.cn // Email: tech@wch.cn // Author: TECH 2008.10.14 // *******************************************************************
//ATMEGA128 PE2 ------- CH452 SCL //ATMEGA128 PE4 ------- CH452 INT# //ATMEGA128 PE3 ------- CH452 SDA //CH452ÕâÀï½ÓµØÖ·1
//ICCAVRϱàÒë
// ******************* // ¼ÓÔØÍ·Îļþ // ******************* #include #include
#define uchar unsigned char #define uint unsigned int #define ulong unsigned long // ******************* // CH452µÄ¶¨Òå // ******************* #define CH452_DIG0 0x0800 //ÊýÂë¹Üλ0ÏÔʾ #define CH452_DIG1 0x0900 //ÊýÂë¹Üλ1ÏÔʾ #define CH452_DIG2 0x0a00 //ÊýÂë¹Üλ2ÏÔʾ #define CH452_DIG3 0x0b00 //ÊýÂë¹Üλ3ÏÔʾ #define CH452_DIG4 0x0c00 //ÊýÂë¹Üλ4ÏÔʾ #define CH452_DIG5 0x0d00 //ÊýÂë¹Üλ5ÏÔʾ #define CH452_DIG6 0x0e00 //ÊýÂë¹Üλ6ÏÔʾ #define CH452_DIG7 0x0f00 //ÊýÂë¹Üλ7ÏÔʾ
#define CH452_SDA_OUT { DDRE |= (1<#define CH452_SDA_IN { PORTE |= (1<#define CH452_SDA_SET PORTE |= (1<#define CH452_SDA_CLR PORTE &= ~(1<#define CH452_SDA ( PINE & (1<#define CH452_SCL_OUT { PORTE |= (1<#define CH452_SCL_SET PORTE |= (1<#define CH452_SCL_CLR PORTE &= ~(1<#define CH452_INT_IN { PORTE |= (1<#define mINT ( PINE&(1<#define DELAY mDelayuS(1)
#define CH452_I2C_ADDR0 0x40 // CH452µÄADDR=0ʱµÄµØÖ· #define CH452_I2C_ADDR1 0x60 // CH452µÄADDR=1ʱµÄµØÖ·,ĬÈÏÖµ #define CH452_I2C_MASK 0x3E // CH452µÄ2Ïß½Ó¿Ú¸ß×Ö½ÚÃüÁîÑÚÂë
// ******************* // ÀàÐͶ¨Òå // ******************* typedef unsigned char UINT8; typedef unsigned short UINT16;
// ************************************** // CH452µÄÈ«¾Ö±äÁ¿ // ************************************** UINT8 mADDR;//I2CµØÖ· UINT8 Usart0Tx_State=0, Usart1Tx_State=0; uchar dat[7]; // ******************* // ÑÓʱuS // ******************* void mDelayuS( UINT8 n ) { while( --n ) { NOP(); NOP(); } }
// ******************* // ÑÓʱmS // ******************* void mDelaymS( UINT8 delay ) { UINT8 ms; UINT8 sm; UINT8 c; for( ms = delay; ms != 0; ms-- ) { for( sm = 200; sm != 0; sm-- ) c += 3; for( sm = 200; sm != 0; sm-- ) c += 3; for( sm = 200; sm != 0; sm-- ) c += 3; for( sm = 200; sm != 0; sm-- ) c += 3; for( sm = 240; sm != 0; sm-- ) c += 3; } }
// ************************************** // I2C START // ************************************** void CH452_I2c_Start( void ) { CH452_SDA_SET; /*·¢ËÍÆðʼÌõ¼þµÄÊý¾ÝÐźÅ*/ CH452_SDA_OUT; /* ÉèÖÃSDAΪÊä³ö·½Ïò */ DELAY; CH452_SCL_SET;/* ÉèÖÃSCLΪÊä³ö·½Ïò */ DELAY; CH452_SDA_CLR; /*·¢ËÍÆðʼÐźÅ*/ DELAY; CH452_SCL_CLR; /*ǯסI2C×ÜÏߣ¬×¼±¸·¢ËÍ»ò½ÓÊÕÊý¾Ý */ DELAY; }
// ************************************** // I2C STOP // ************************************** void CH452_I2c_Stop( void ) { CH452_SDA_CLR; CH452_SDA_OUT; /* ÉèÖÃSDAΪÊä³ö·½Ïò */ DELAY; CH452_SCL_SET; DELAY; CH452_SDA_SET; /*·¢ËÍI2C×ÜÏß½áÊøÐźÅ*/ DELAY; CH452_SDA_IN; /* ÉèÖÃSDAΪÊäÈë·½Ïò */ DELAY; }
// ************************************** // I2C дһ¸ö×Ö½Ú // ************************************** void CH452_I2c_WrByte( UINT8 c ) //дһ¸ö×Ö½ÚÊý¾Ý { UINT8 i; CH452_SDA_SET; CH452_SDA_OUT; /* ÉèÖÃSDAΪÊä³ö·½Ïò */ DELAY; for( i = 0; i != 8; i++ ) { if( c & 0x80 ) { CH452_SDA_SET; } else { CH452_SDA_CLR; } DELAY; CH452_SCL_SET; c <<= 1; DELAY; CH452_SCL_CLR; } CH452_SDA_IN; /* ÉèÖÃSDAΪÊäÈë·½Ïò */ DELAY; CH452_SCL_SET; DELAY; CH452_SCL_CLR; DELAY; }
// ************************************** // I2C ¶ÁÒ»¸ö×Ö½Ú // ************************************** UINT8 CH452_I2c_RdByte( void ) //¶ÁÒ»¸ö×Ö½ÚÊý¾Ý { UINT8 c, i; CH452_SDA_IN; /* ÉèÖÃSDAΪÊäÈë·½Ïò */ c = 0; for( i = 0; i != 8; i++ ) // ÊäÈë8λÊý¾Ý { CH452_SCL_SET; DELAY; c <<= 1; if( CH452_SDA ) c++; // ÊäÈë1λ CH452_SCL_CLR; DELAY; } CH452_SDA_SET; CH452_SDA_OUT; CH452_SCL_SET; // ·¢³öÎÞЧӦ´ð DELAY; CH452_SCL_CLR; DELAY; return(c); }
// ************************************** // CH452дÃüÁî // ************************************** void CH452_Write( UINT16 command ) //дÃüÁî { CH452_I2c_Start(); /*Æô¶¯×ÜÏß*/ if( mADDR == 0 ) { CH452_I2c_WrByte((UINT8)(command>>7)&CH452_I2C_MASK|CH452_I2C_ADDR0); // CH452µÄADDR=0ʱ } else { CH452_I2c_WrByte((UINT8)(command>>7)&CH452_I2C_MASK|CH452_I2C_ADDR1); // CH452µÄADDR=1ʱ(ĬÈÏ) } CH452_I2c_WrByte( (UINT8)command );