如题.. 电路图附上 用两线接口 C51[P12] ---- SCL ----- CH452[SCL] C51[P23] ---- SDA ----- CH452[SDA]
不知道为什么数码管就是不亮
程序如下:
#include //#include #include
// ************************************** // 类型定义 // ************************************** typedef unsigned char UINT8; typedef unsigned short UINT16;
// ************************************** // CH452与单片机的接口连接 // ************************************** sbit CH452_SCL = P3^6; sbit CH452_SDA = P2^5; //sbit ch452_addr0 = P1^5; sbit CH452_INT0 = P3^3; //地址0中断脚 //sbit ch452_addr1 = P3^0; sbit CH452_INT1 = P3^1; //地址1中断脚
sbit LEDA = P1^4; sbit LEDB = P1^5; #define LEDAA { LEDA = 0; } #define LEDBB { LEDB = 1; }
// ************************************** // CH452的定义 // **************************************
#define CH452_SCL_SET { CH452_SCL = 1; } #define CH452_SCL_CLR { CH452_SCL = 0; } #define CH452_SDA_SET { CH452_SDA = 1; } #define CH452_SDA_CLR { CH452_SDA = 0; } #define CH452_SDA_IN ( CH452_SDA )
#define CH452_DIG0 0x0800 // 数码管位0显示,需另加8位数据 #define CH452_DIG1 0x0900 // 数码管位1显示,需另加8位数据 #define CH452_DIG2 0x0a00 // 数码管位2显示,需另加8位数据 #define CH452_DIG3 0x0b00 // 数码管位3显示,需另加8位数据 #define CH452_DIG4 0x0c00 // 数码管位4显示,需另加8位数据 #define CH452_DIG5 0x0d00 // 数码管位5显示,需另加8位数据 #define CH452_DIG6 0x0e00 // 数码管位6显示,需另加8位数据 #define CH452_DIG7 0x0f00 // 数码管位7显示,需另加8位数据
#define CH452_I2C_ADDR0 0x40 // CH452的ADDR=0时的地址 #define CH452_I2C_ADDR1 0x60 // CH452的ADDR=1时的地址,默认值 #define CH452_I2C_MASK 0x3E // CH452的2线接口高字节命令掩码
#define DELAY_IO { _nop_();_nop_();_nop_();}
// ************************************** // 模拟I2C START // ************************************** void CH452_I2c_Start( void ) { CH452_SDA_SET; /*发送起始条件的数据信号*/ DELAY_IO; CH452_SCL_SET; DELAY_IO;
CH452_SDA_CLR; /*发送起始信号*/ DELAY_IO;
CH452_SCL_CLR; /*钳住I2C总线,准备发送或接收数据 */ DELAY_IO; }
// ************************************** // 模拟I2C STOP // ************************************** void CH452_I2c_Stop( void ) { CH452_SDA_CLR; DELAY_IO;
CH452_SCL_SET; DELAY_IO;
CH452_SDA_SET; /*发送I2C总线结束信号*/ DELAY_IO; }
// ************************************** // 模拟I2C 写一个字节数据 // ************************************** void CH452_I2c_WrByte( UINT8 dat ) { UINT8 i; for( i = 0; i != 8; i++ ) // 输出8位数据 { if( dat&0x80 ) { CH452_SDA_SET; } else { CH452_SDA_CLR; } DELAY_IO;
CH452_SCL_SET; dat <<= 1; DELAY_IO;
CH452_SCL_CLR; DELAY_IO; } CH452_SDA_SET;/* 设置SDA为输入方向 */ DELAY_IO;
CH452_SCL_SET; // 接收应答 DELAY_IO;
CH452_SCL_CLR; DELAY_IO; }
// ************************************** // 模拟I2C 读一个字节数据 // ************************************** UINT8 CH452_I2c_RdByte( void ) { UINT8 dat; UINT8 i;
CH452_SDA_SET;/* 设置SDA为输入方向 */ DELAY_IO;
dat = 0; for( i = 0; i != 8; i++ ) // 输入8位数据 { CH452_SCL_SET; DELAY_IO;
dat <<= 1; if( CH452_SDA_IN ) dat++; // 输入1位
CH452_SCL_CLR; DELAY_IO; }
//CH452_SDA_CLR;//ACK CH452_SDA_SET;//NAK DELAY_IO;
CH452_SCL_SET; // 发出无效应答 DELAY_IO;
CH452_SCL_CLR; DELAY_IO;
return dat; }
// ************************************** // 模拟I2C 写命令 // ************************************** void ch452_write( UINT16 cmd, UINT8 mADDR ) { CH452_I2c_Start(); /*启动总线*/
if( mADDR == 0 ) { CH452_I2c_WrByte((UINT8)(cmd>>7)&CH452_I2C_MASK|CH452_I2C_ADDR0); // CH452的ADDR=0时 } else { CH452_I2c_WrByte((UINT8)(cmd>>7)&CH452_I2C_MASK|CH452_I2C_ADDR1); // CH452的ADDR=1时 }
CH452_I2c_WrByte((UINT8)cmd); /*发送数据*/
CH452_I2c_Stop(); /*结束总线*/ }
// ************************************** // 模拟I2C 读数据 // ************************************** UINT8 CH452_Read( UINT16 read_cmd, UINT8 mADDR ) { UINT8 val;
CH452_I2c_Start(); /*启动总线*/
if( mADDR == 1 ) { CH452_I2c_WrByte((UINT8)(read_cmd>>7)&CH452_I2C_MASK|CH452_I2C_ADDR1|0x01); } else { CH452_I2c_WrByte((UINT8)(read_cmd>>7)&CH452_I2C_MASK|CH452_I2C_ADDR0|0x01); }
val = CH452_I2c_RdByte(); /*读取数据*/
CH452_I2c_Stop(); /*结束总线*/
return val; }
// ************************************** // 直接译码使用 // ************************************** UINT8 BCD[ 29 ] = { 0X3F, 0X06, 0X5B, 0X4F, 0X66, 0X6D, 0X7D, 0X07, 0X7F, 0X6F, 0X77, 0X7C, 0X58, 0X5E, 0X79, 0X71, 0x00, 0x46, 0x40, 0x41, 0x39, 0x0F, 0x08, 0x76, 0x38, 0x73, 0x80, 0xFF, 0x00 };
// ************************************** // mS延时 // ************************************** void delayms( UINT8 i ) { UINT16 j;
do { for( j = 0; j != 1000; j++ ) { ; } } while(--i); }
// ************************************** // 主程序 // ************************************** void main( void ) { UINT8 ch452_key; UINT8 n;
//ch452_addr0 = 0; //ch452_addr1 = 1;
delayms(60); for( n = 0; n != 2; n++ )//地址0和1 { ch452_write( 0x403, n );//开显示键盘 ch452_write( 0x580, n );//BCD译码
ch452_write( CH452_DIG0 | ( 0 + ( n<<3 ) ), n ); ch452_write( CH452_DIG1 | ( 1 + ( n<<3 ) ), n ); ch452_write( CH452_DIG2 | ( 2 + ( n<<3 ) ), n ); ch452_write( CH452_DIG3 | ( 3 + ( n<<3 ) ), n ); ch452_write( CH452_DIG4 | ( 4 + ( n<<3 ) ), n ); ch452_write( CH452_DIG5 | ( 5 + ( n<<3 ) ), n ); ch452_write( CH452_DIG6 | ( 6 + ( n<<3 ) ), n ); ch452_write( CH452_DIG7 | ( 7 + ( n<<3 ) ), n );
} LEDAA; LEDBB; while(1)//读按键 { if( CH452_INT0 == 0 )//地址0 CH452按键中断 { ch452_key = CH452_Read( 0x0700, 0 )&0x3F;
ch452_write( 0x300, 0 );//左移两位 ch452_write( 0x300, 0 ); ch452_write( CH452_DIG1|(ch452_key/10), 0 ); ch452_write( CH452_DIG0|(ch452_key%10), 0 ); }
if( CH452_INT1 == 0 )//地址1 CH452按键中断 { ch452_key = CH452_Read( 0x0700, 1 )&0x3F;
ch452_write( 0x300, 1 );//左移两位 ch452_write( 0x300, 1 ); ch452_write( CH452_DIG1|(ch452_key/10), 1 ); ch452_write( CH452_DIG0|(ch452_key%10), 1 ); } } }