//±?ày×ó3ìDòìá1?ò???2ù×÷·?·¨£???1?2??? // ******************************************************************* // 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?aà??óμ??·1
//ICCAVR??±àò?
// ******************* // ?ó??í·???t // ******************* #include #include
#define uchar unsigned char #define uint unsigned int #define ulong unsigned long // ******************* // CH452μ??¨ò? // ******************* #define CH452_DIG0 0x0800 //êy??1ü??0??ê? #define CH452_DIG1 0x0900 //êy??1ü??1??ê? #define CH452_DIG2 0x0a00 //êy??1ü??2??ê? #define CH452_DIG3 0x0b00 //êy??1ü??3??ê? #define CH452_DIG4 0x0c00 //êy??1ü??4??ê? #define CH452_DIG5 0x0d00 //êy??1ü??5??ê? #define CH452_DIG6 0x0e00 //êy??1ü??6??ê? #define CH452_DIG7 0x0f00 //êy??1ü??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???ó?ú??×??ú?üá??ú??
// ******************* // ààDí?¨ò? // ******************* 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(); } }
// ******************* // ?óê±m(xù)S // ******************* 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; /*·¢?í?eê?ì??tμ?êy?YD?o?*/ CH452_SDA_OUT; /* éè??SDA?aê?3?·??ò */ DELAY; CH452_SCL_SET;/* éè??SCL?aê?3?·??ò */ DELAY; CH452_SDA_CLR; /*·¢?í?eê?D?o?*/ DELAY; CH452_SCL_CLR; /*?ˉ×?I2C×ü??£?×?±?·¢?í?ò?óê?êy?Y */ DELAY; }
// ************************************** // I2C STOP // ************************************** void CH452_I2c_Stop( void ) { CH452_SDA_CLR; CH452_SDA_OUT; /* éè??SDA?aê?3?·??ò */ DELAY; CH452_SCL_SET; DELAY; CH452_SDA_SET; /*·¢?íI2C×ü???áê?D?o?*/ DELAY; CH452_SDA_IN; /* éè??SDA?aê?è?·??ò */ DELAY; }
// ************************************** // I2C D′ò???×??ú // ************************************** void CH452_I2c_WrByte( UINT8 c ) //D′ò???×??úêy?Y { UINT8 i; CH452_SDA_SET; CH452_SDA_OUT; /* éè??SDA?aê?3?·??ò */ 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?aê?è?·??ò */ DELAY; CH452_SCL_SET; DELAY; CH452_SCL_CLR; DELAY; }
// ************************************** // I2C ?áò???×??ú // ************************************** UINT8 CH452_I2c_RdByte( void ) //?áò???×??úêy?Y { UINT8 c, i; CH452_SDA_IN; /* éè??SDA?aê?è?·??ò */ c = 0; for( i = 0; i != 8; i++ ) // ê?è?8??êy?Y { CH452_SCL_SET; DELAY; c <<= 1; if( CH452_SDA ) c++; // ê?è?1?? CH452_SCL_CLR; DELAY; } CH452_SDA_SET; CH452_SDA_OUT; CH452_SCL_SET; // ·¢3??TD§ó|′e DELAY; CH452_SCL_CLR; DELAY; return(c); }
// ************************************** // CH452D′?üá? // ************************************** void CH452_Write( UINT16 command ) //D′?üá? { 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 );