资源描述:
《基于CH451的键盘扫描程序设计》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、基于CH451的键盘扫描程序设计简介:基于CH451的键盘扫描程序设计采用AVR单片机ATmega16进行读取CH451的接收到的按键值,CH451可以实现8*8矩阵键盘输入,且带有中断提醒,接收到的按键值在12864液晶上显示。程序中采用的是2*3矩阵。系统采用AVRStudio+WinAVR构成的AVRGCC编译环境。系统源程序如下:#defineF_CPU1000000UL#include#include#include#include#defineno
2、p()asm("NOP")/*液晶显示引脚定义*//*对rs引脚进行定义与设置*/#definers_outDDRD
3、=_BV(PD5)#definers_inDDRD&=~_BV(PD5)#definers_1PORTD
4、=_BV(PD5)#definers_0PORTD&=~_BV(PD5)/*对rw引脚进行定义与设置*/#definerw_outDDRD
5、=_BV(PD6)#definerw_inDDRD&=~_BV(PD6)#definerw_1PORTD
6、=_BV(PD6)#definerw_0PORTD&=~_BV(PD6)/*对使能en引脚进
7、行定义与设置*/#defineen_outDDRD
8、=_BV(PD7)#defineen_inDDRD&=~_BV(PD7)#defineen_1PORTD
9、=_BV(PD7)#defineen_0PORTD&=~_BV(PD7)#definebusy_1DDRB
10、=_BV(PB7)#definebusy_0DDRB&=~_BV(PB7)/*ch451引脚定义*//*数据输出与键盘中断*/#definedout_outDDRA
11、=_BV(PA2)#definedout_inDDRA&=~_BV(PA2)#definedout_1PORTA
12、=_BV(PA2
13、)#definedout_0PORTA&=~_BV(PA2)#definedout_read(PINA&_BV(PA2))/*串行数据加载引脚*/#defineload_outDDRA
14、=_BV(PA3)#defineload_inDDRA&=~_BV(PA3)#defineload_1PORTA
15、=_BV(PA3)#defineload_0PORTA&=~_BV(PA3)/*数据输入引脚*/#definedin_outDDRA
16、=_BV(PA4)#definedin_inDDRA&=~_BV(PA4)#definedin_1PORTA
17、=_BV(PA4)
18、#definedin_0PORTA&=~_BV(PA4)/*数据时钟线*/#definedclk_outDDRA
19、=_BV(PA5)#definedclk_inDDRA&=~_BV(PA5)#definedclk_1PORTA
20、=_BV(PA5)#definedclk_0PORTA&=~_BV(PA5)constunsignedchartabn[]={"0123456789"};unsignedchartemp=0;/*检查引脚是否忙*/voidcheckbusy(){rs_0;rw_1;en_1;_delay_us(200);busy_0;while((
21、PINB&0x80)==1);en_0;busy_1;}/*写命令*/voidwrite_com(unsignedcharcmd){checkbusy();rs_0;rw_0;en_1;_delay_us(1);PORTB=cmd;_delay_us(2);en_0;}/*写数据*/voidwrite_date(unsignedchardat){checkbusy();rs_1;rw_0;en_1;_delay_us(2);PORTB=dat;_delay_us(2);en_0;}/*液晶初始化*/voidinit_lcd(){DDRB=0xff;PORT
22、B=0xff;DDRD=0xff;PORTB=0xff;write_com(0x30);_delay_us(2);write_com(0x0c);_delay_us(2);write_com(0x01);_delay_us(2);write_com(0x06);_delay_us(2);}/*向lcd写入一个字符串*/voidwrite_string(constunsignedchar*s){while(*s>0){write_date(*s);s++;_delay_us(1);}_delay_ms(3);}/*向lcd某个地址写入一个字符串*/voidw
23、rite_addstr(unsignedcharadd,const