AVR CH451驱动程序头文件
#ifndef _CH451_H
#define _CH451_H
#include
//CH451命令宏定义
#define CH451_RESET 0x0201 //复位
#define CH451_LEFT MOV 0x0300 //设置移动方式-左移
#define CH451_LEFTCYC 0x0301 //设置移动方式-左循
#define CH451_RIGHT MOV 0x0302 //设置移动方式-右移
#define CH451_RIGHTCYC 0x0303 //设置移动方式-右循
#define CH451_SYSOFF 0x0400 //关显示、键盘、看门狗
#define CH451_SYSON1 0x0401 //开显示
#define CH451_SYSON2 0x0403 //开显示、键盘
#define CH451_SYSON3 0x0407 //开显示、键盘、看门狗功能
#define CH451_DSP 0x0500 //设置默认显示方式
#define CH451_BCD 0x0580 //设置BCD译码方式
#define CH451_TWINKLE 0x0600 //设置闪烁控制
#define CH451_KEY 0x0700 //读键盘代码
#define CH451_DIG0 0x0800 //数码管位0显示
#define CH451_DIG1 0x0900 //数码管位1显示
#define CH451_DIG2 0x0A00 //数码管位2显示
#define CH451_DIG3 0x0B00 //数码管位3显示
#define CH451_DIG4 0x0C00 //数码管位4显示
#define CH451_DIG5 0x0D00 //数码管位5显示
#define CH451_DIG6 0x0E00 //数码管位6显示
#define CH451_DIG7 0x0F00 //数码管位7显示
//端口宏定义
#define DCLK (1<<;PA3)
#define DIN (1<<;PA5)
#define LOAD (1<<;PA7)
#define DOUT (1<<;PE5)
#define CH451_DCLK_HIGH PORTA|=DCLK //时钟信号
#define CH451_DCLK_LOW PORTA&=~DCLK
#define CH451_DIN_HIGH PORTA|=DIN //数据输入
#define CH451_DIN_LOW PORTA&=~DIN
#define CH451_LOAD_HIGH PORTA|=LOAD //命令加载信号,上升延激活
#define CH451_LOAD_LOW PORTA&=~LOAD
#define CH451_DOUT PINE&DOUT //键盘中断和键值数据输出
extern uchar KeyValue; //键值全局声明
//函数申明
void CH451_WriteCommand(uint Command); //CH451写命令函数
void CH451_Init(); //CH451初始化函数
void CH451_GetKeyValue(); //CH451键盘代码读取函数
void CH451_DisplayDig(uchar DIG,uchar Data,uchar Point);//CH451显示一位数码管函数
#endif
AVR CH451驱动程序文件
#include "CH451.h"
uchar KeyValue;//键值
//共阴数码管编码表
const uchar DSYCode[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};//-,全灭
const uint DigCode[8]={CH451_DIG0,CH451_DIG1,CH451_DIG2,CH451_DIG3,
CH451_DIG4,CH451_DIG5,CH451_DIG6,CH451_DIG7};
//键盘编码表
const uchar KeyCode[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写命令函数
void CH451_WriteCommand(uint Command){
uchar i;
CH451_LOAD_LOW; //命令开始
for(i=0;i<12;i++){ //送入12位数据,低位在前
if(Command&0x0001) CH451_DIN_HIGH;//输出低位数据
else CH451_DIN_LOW;
CH451_DCLK_LOW;
Command>>=1; //数据右移一位
CH451_DCLK_HIGH; //上升沿有效
}
CH451_LOAD_HIGH; //加载数据
}
//CH451初始化函数
void CH451_Init(){
CH451_DIN_LOW; //低电平使能串行接口
CH451_DIN_HIGH;
CH451_WriteCommand(CH451_RESET); //复位CH451
CH451_WriteCommand(CH451_SYSON2);//开显示、键盘
DDRE&=~DOUT; //PE5输入方向
EICRB|=(1<
}
//CH451键值读取函数
void CH451_GetKeyValue(){
uchar i;
uchar GetKeyValue=0x00; //键盘代码
CH451_WriteCommand(CH451_KEY); //发送读取键值命令
for(i=0;i<7;i++){
GetKeyValue<<=1;
if(CH451_DOUT) GetKeyValue|=1;//从高到低读入451的数据
else GetKeyValue|=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是否显示小数点
void CH451_DisplayDig(uchar DIG,uchar Data,uchar Point){
if(Point==1) CH451_WriteCommand(DigCode[DIG]|(DSYCode[Data]|0x80));
else CH451_WriteCommand(DigCode[DIG]|DSYCode[Data]);
}
上一篇:PS/2键盘扫描实验C程序
下一篇:非接触式智能卡模块的原理
推荐阅读最新更新时间:2024-03-16 15:49