AVR CH451驱动程序
扫描二维码
随时随地手机看文章
AVRCH451驱动程序头文件
#ifndef_CH451_H
#define_CH451_H
#include
//CH451命令宏定义
#defineCH451_RESET0x0201//复位
#defineCH451_LEFT MOV 0x0300//设置移动方式-左移
#defineCH451_LEFTCYC0x0301//设置移动方式-左循
#defineCH451_RIGHT MOV 0x0302//设置移动方式-右移
#defineCH451_RIGHTCYC0x0303//设置移动方式-右循
#defineCH451_SYSOFF0x0400//关显示、键盘、看门狗
#defineCH451_SYSON10x0401//开显示
#defineCH451_SYSON20x0403//开显示、键盘
#defineCH451_SYSON30x0407//开显示、键盘、看门狗功能
#defineCH451_DSP0x0500//设置默认显示方式
#defineCH451_BCD0x0580//设置BCD译码方式
#defineCH451_TWINKLE0x0600//设置闪烁控制
#defineCH451_KEY0x0700//读键盘代码
#defineCH451_DIG00x0800//数码管位0显示
#defineCH451_DIG10x0900//数码管位1显示
#defineCH451_DIG20x0A00//数码管位2显示
#defineCH451_DIG30x0B00//数码管位3显示
#defineCH451_DIG40x0C00//数码管位4显示
#defineCH451_DIG50x0D00//数码管位5显示
#defineCH451_DIG60x0E00//数码管位6显示
#defineCH451_DIG70x0F00//数码管位7显示
//端口宏定义
#defineDCLK(1<<;PA3)
#defineDIN(1<<;PA5)
#defineLOAD(1<<;PA7)
#defineDOUT(1<<;PE5)
#defineCH451_DCLK_HIGHPORTA|=DCLK//时钟信号
#defineCH451_DCLK_LOWPORTA&=~DCLK
#defineCH451_DIN_HIGHPORTA|=DIN//数据输入
#defineCH451_DIN_LOWPORTA&=~DIN
#defineCH451_LOAD_HIGHPORTA|=LOAD//命令加载信号,上升延激活
#defineCH451_LOAD_LOWPORTA&=~LOAD
#defineCH451_DOUTPINE&DOUT//键盘中断和键值数据输出
externucharKeyValue;//键值全局声明
//函数申明
voidCH451_WriteCommand(uintCommand);//CH451写命令函数
voidCH451_Init();//CH451初始化函数
voidCH451_GetKeyValue();//CH451键盘代码读取函数
voidCH451_DisplayDig(ucharDIG,ucharData,ucharPoint);//CH451显示一位数码管函数
#endif
AVR CH451驱动程序文件
#include"CH451.h"
ucharKeyValue;//键值
//共阴数码管编码表
constucharDSYCode[18]={0x3F,0x06,0x5B,0x4F,//0,1,2,3
0x66,0x6D,0x7D,0x07,//4,5,6,7
0x7F,0x6F,0x77,0x7C,//8,9,A,b
0x39,0x5E,0x79,0x71,//C,d,E,F
0x40,0x00};//-,全灭
constuintDigCode[8]={CH451_DIG0,CH451_DIG1,CH451_DIG2,CH451_DIG3,
CH451_DIG4,CH451_DIG5,CH451_DIG6,CH451_DIG7};
//键盘编码表
constucharKeyCode[16]={0x40,0x41,0x42,0x43,//0,1,2,3
0x48,0x49,0x4A,0x4B,//4,5,6,7
0x50,0x51,0x52,0x53,//8,9,A,B
0x58,0x59,0x5A,0x5B};//C,D,E,F
//CH451写命令函数
voidCH451_WriteCommand(uintCommand){
uchari;
CH451_LOAD_LOW;//命令开始
for(i=0;i<12;i++){//送入12位数据,低位在前
if(Command&0x0001)CH451_DIN_HIGH;//输出低位数据
elseCH451_DIN_LOW;
CH451_DCLK_LOW;
Command>>=1;//数据右移一位
CH451_DCLK_HIGH;//上升沿有效
}
CH451_LOAD_HIGH;//加载数据
}
//CH451初始化函数
voidCH451_Init(){
CH451_DIN_LOW;//低电平使能串行接口
CH451_DIN_HIGH;
CH451_WriteCommand(CH451_RESET);//复位CH451
CH451_WriteCommand(CH451_SYSON2);//开显示、键盘
DDRE&=~DOUT;//PE5输入方向
EICRB|=(1<
}
//CH451键值读取函数
voidCH451_GetKeyValue(){
uchari;
ucharGetKeyValue=0x00;//键盘代码
CH451_WriteCommand(CH451_KEY);//发送读取键值命令
for(i=0;i<7;i++){
GetKeyValue<<=1;
if(CH451_DOUT)GetKeyValue|=1;//从高到低读入451的数据
elseGetKeyValue|=0;
CH451_DCLK_LOW;//下降沿有效
CH451_DCLK_HIGH;
}
if(GetKeyValue==0x00)return;//读出键码为0跳过
for(i=0;i<16;i++){//查找键盘代码对应的键值
if(GetKeyValue==KeyCode){
KeyValue=i;
break;
}
}
}
//CH451显示一位数码管,DIG选择数码管,Data要显示的数字,Point是否显示小数点
voidCH451_DisplayDig(ucharDIG,ucharData,ucharPoint){
if(Point==1)CH451_WriteCommand(DigCode[DIG]|(DSYCode[Data]|0x80));
elseCH451_WriteCommand(DigCode[DIG]|DSYCode[Data]);
}