#include
#include
#include "CH453IF.H" // 修改该文件以适应硬件环境/单片机型号等
unsigned char CH453_buf[16]; //定义16个数码管的数据映象缓存区
const unsigned char BCD_decode_tab[0x10] = { 0X3F, 0X06, 0X5B, 0X4F, 0X66, 0X6D, 0X7D, 0X07, 0X7F, 0X6F, 0X77, 0X7C, 0X58, 0X5E, 0X79, 0X71 };
void mDelaymS( unsigned char ms ) // 延时毫秒
{
unsigned short i;
while ( ms -- ) {
for(i=0;i!=1000;i++);
}
}
void mDelayS( unsigned char s ) // 延时秒
{
while ( s -- )
{ mDelaymS( 250 );
mDelaymS( 250 );
mDelaymS( 250 );
mDelaymS( 250 );
}
}
void CH453_buf_write( unsigned short cmd ) // 向CH453输出数据或者操作命令,自动建立数据映象
{
if ( cmd & 0x1000 )
{ // 加载数据的命令,需要备份数据到映象缓冲区
CH453_buf[ (unsigned char)( cmd >> 8 ) & 0x0F ] = (unsigned char)( cmd & 0xFF ); // 备份数据到相应的映象单元
}
CH453_Write( cmd ); // 发出
}
void CH453_buf_index( unsigned char index, unsigned char dat ) // 向CH453指定的数码管输出数据,自动建立数据映象
// index 为数码管序号,有效值为0到15,分别对应DIG0到DIG15
{
unsigned short cmd;
CH453_buf[ index ] = dat; // 备份数据到相应的映象单元
cmd = ( CH453_DIG0 + ( (unsigned short)index << 8 ) ) | dat ; // 生成操作命令,short 双字节
CH453_Write( cmd ); // 发出
}
void CH453_set_bit(unsigned char bit_addr) //段位点亮
{
unsigned char byte_addr;
byte_addr=(bit_addr>>3)&0x0F;
CH453_buf_index( byte_addr, CH453_buf[byte_addr] | (1<<(bit_addr&0x07)) );
}
void CH453_clr_bit(unsigned char bit_addr) //段位熄灭
{
unsigned char byte_addr;
byte_addr=(bit_addr>>3)&0x0F;
CH453_buf_index( byte_addr, CH453_buf[byte_addr] & ~(1<<(bit_addr&0x07)) );
}
void mInitSTDIO( void )
{
SCON = 0x50;
PCON = 0x80;
TMOD = 0x21;
TH1 = 0xf3; /* 24MHz晶振, 9600bps ,通过串口发送命令,或接收返回的键值 */
TR1 = 1;
ti = 1;
}
main()
{
unsigned char i,t,s;
mInitSTDIO( );
mDelaymS( 50 );
for ( i = 0; i < 16; i ++ )
{
CH453_buf_index( i, 0 ); // 因为CH453复位时不清空显示内容,所以刚开电后必须人为清空,再开显示
}
CH453_buf_write( 0x040B ); // 0X 480B 0000 1011开启、键盘显示,DIG15用于键盘中断输出、
// 如果需要定期刷新显示内容,那么只要执行17个命令,包括16个数据加载命令,以及1个开启显示命令
CH453_buf_write(CH453_DIG15 | BCD_decode_tab[0]); // 显示BCD码0
CH453_buf_write(CH453_DIG14 | BCD_decode_tab[1]);
CH453_buf_write(CH453_DIG13 | BCD_decode_tab[2]);
CH453_buf_write(CH453_DIG12 | BCD_decode_tab[3]);
CH453_buf_write(CH453_DIG11 | BCD_decode_tab[4]);
CH453_buf_write(CH453_DIG10 | BCD_decode_tab[5]);
CH453_buf_write(CH453_DIG9 | BCD_decode_tab[6]);
CH453_buf_write(CH453_DIG8 | BCD_decode_tab[7]);
CH453_buf_write(CH453_DIG7 | BCD_decode_tab[8]);
CH453_buf_write(CH453_DIG6 | BCD_decode_tab[9]);
CH453_buf_write(CH453_DIG5 | BCD_decode_tab[10]);
CH453_buf_write(CH453_DIG4 | BCD_decode_tab[11]);
CH453_buf_write(CH453_DIG3 | BCD_decode_tab[12]);
CH453_buf_write(CH453_DIG2 | BCD_decode_tab[13]);
CH453_buf_write(CH453_DIG1 | BCD_decode_tab[14]);
CH453_buf_write(CH453_DIG0 | BCD_decode_tab[15]);
mDelaymS( 50 );
while ( 1 )
{ // 演示
t = CH453_Read( );//按键读入
s = CH453_Read( ); //按键读入,抗干扰
if( s == t && s != 0xff )
{
printf(" %02x ",(unsigned short)s);
}
}
}#include "CH453IF.H" // 修改该文件以适应硬件环境/单片机型号等
void CH453_I2c_Start(void) // 操作起始
{
CH453_SDA_SET; /*发送起始条件的数据信号*/
CH453_SDA_D_OUT; /* 设置SDA为输出方向 */
CH453_SCL_SET;
CH453_SCL_D_OUT; /* 设置SCL为输出方向 */
DELAY_0_1US;
CH453_SDA_CLR; /*发送起始信号*/
DELAY_0_1US;
CH453_SCL_CLR; /*钳住I2C总线,准备发送或接收数据 */
}
void CH453_I2c_Stop(void) // 操作结束
{
CH453_SDA_CLR;
DELAY_0_1US;
CH453_SCL_SET;
DELAY_0_1US;
CH453_SDA_SET; /*发送I2C总线结束信号*/
DELAY_0_1US;
}
void CH453_I2c_WrByte(unsigned char dat) //写一个字节数据
{
unsigned char i;
for(i=0;i!=8;i++) // 输出8位数据
{
if(dat&0x80) {CH453_SDA_SET;}
else {CH453_SDA_CLR;}
DELAY_0_1US;
CH453_SCL_SET;
dat<<=1;
DELAY_0_1US; // 可选延时
CH453_SCL_CLR;
}
CH453_SDA_SET;
DELAY_0_1US;
CH453_SCL_SET; // 接收应答
DELAY_0_1US;
CH453_SCL_CLR;
}
unsigned char CH453_I2c_RdByte(void) //读一个字节数据
{
unsigned char dat,i;
CH453_SDA_SET;
dat=0;
for(i=0;i!=8;i++) // 输入8位数据
{
CH453_SCL_SET;
DELAY_0_1US;
DELAY_0_1US;
dat<<=1;
if(CH453_SDA_IN) dat++; // 输入1位
CH453_SCL_CLR;
DELAY_0_1US;
// DELAY_0_1US;
}
CH453_SDA_SET;
DELAY_0_1US;
CH453_SCL_SET; // 发出无效应答
DELAY_0_1US;
DELAY_0_1US;
CH453_SCL_CLR;
DELAY_0_1US;
return(dat);
}
void CH453_Write(unsigned short cmd) //写命令
{
CH453_I2c_Start(); /*启动总线*/
CH453_I2c_WrByte(((unsigned char)(cmd>>7)&CH453_I2C_MASK)|CH453_I2C_ADDR1);
CH453_I2c_WrByte((unsigned char)cmd); /*发送数据*/
CH453_I2c_Stop(); /*结束总线*/
}
unsigned char CH453_Read(void) //读取按键
{
unsigned char keycode;
CH453_I2c_Start(); /*启动总线*/
CH453_I2c_WrByte(0x4F);
keycode=CH453_I2c_RdByte(); /*读取数据*/
CH453_I2c_Stop(); /*结束总线*/
return(keycode);
}
/ 硬件相关定义, 请根据实际硬件修改本文件
#include
#include
#pragma NOAREGS // 如果MCS51在中断服务程序中调用CH453子程序或者使用键盘中断功能,那么建议加入此编译选项
/* 延时0.1uS子程序,与单片机速度有关,对于双向I/O可以不需要延时,对于带上拉的准双向I/O建议加上延时 */
//#define DELAY_0_1US {} // MCS51<=10MHz
#define DELAY_0_1US {_nop_();} // MCS51>10MHz
/* 2线接口的连接,与实际电路有关 */
***it CH453_SCL=P1^7;
***it CH453_SDA=P1^6;
/* 2线接口的位操作,与单片机有关 */
#define CH453_SCL_SET {CH453_SCL=1;}
#define CH453_SCL_CLR {CH453_SCL=0;}
#define CH453_SCL_D_OUT {} // 设置SCL为输出方向,对于双向I/O需切换为输出
#define CH453_SDA_SET {CH453_SDA=1;}
#define CH453_SDA_CLR {CH453_SDA=0;}
#define CH453_SDA_D_OUT {} // 设置SDA为输出方向,对于双向I/O需切换为输出
#define CH453_SDA_IN (CH453_SDA)
/* ********************************************************************************************* */
/* 常量定义 */
/* CH453的常用命令码,如果考虑与I2C兼容,那么高8位应该右移1位 */
#define CH453_SYSOFF 0x0400 // 关闭显示
#define CH453_SYSON1 0x0401 // 开启显示
#define CH453_DIG0 0x1000 // 数码管位0显示,需另加8位数据
#define CH453_DIG1 0x1100 // 数码管位1显示,需另加8位数据
#define CH453_DIG2 0x1200 // 数码管位2显示,需另加8位数据
#define CH453_DIG3 0x1300 // 数码管位3显示,需另加8位数据
#define CH453_DIG4 0x1400 // 数码管位4显示,需另加8位数据
#define CH453_DIG5 0x1500 // 数码管位5显示,需另加8位数据
#define CH453_DIG6 0x1600 // 数码管位6显示,需另加8位数据
#define CH453_DIG7 0x1700 // 数码管位7显示,需另加8位数据
#define CH453_DIG8 0x1800 // 数码管位8显示,需另加8位数据
#define CH453_DIG9 0x1900 // 数码管位9显示,需另加8位数据
#define CH453_DIG10 0x1A00 // 数码管位10显示,需另加8位数据
#define CH453_DIG11 0x1B00 // 数码管位11显示,需另加8位数据
#define CH453_DIG12 0x1C00 // 数码管位12显示,需另加8位数据
#define CH453_DIG13 0x1D00 // 数码管位13显示,需另加8位数据
#define CH453_DIG14 0x1E00 // 数码管位14显示,需另加8位数据
#define CH453_DIG15 0x1F00 // 数码管位15显示,需另加8位数据
// CH453接口定义
#define CH453_I2C_ADDR1 0x40 // CH453的地址
#define CH453_I2C_MASK 0x3E // CH453的高字节命令掩码
// 对外子程序
extern void CH453_Write(unsigned short cmd); // 向CH453发出操作命令
|