|
#include <ioM16v.h>
#include <macros.h>
#include "delayms.h"
/*------宏定义------*/
#define uchar unsigned char
#define uint unsigned int
#define ch451din PORTB
#define ch451dclk0 PORTB&=~BIT(PB1)
#define ch451dclk1 PORTB|=BIT(PB1)
#define ch451load0 PORTB&=~BIT(PB2)
#define ch451load1 PORTB|=BIT(PB2)
#define ch451dout PB3
//CH451复位
#define CH451_RESET 0x0201
//设置移动方式-作移、左循、右移、右循
#define CH451_LEFTMOV 0x0300
#define CH451_LEFTCYC 0x0301
#define CH451_RIGHTMOV 0x0302
#define CH451_RIGHTCYC 0x0303
//设置BCD译码方式
#define CH451_BCD 0x0580
//设置数码管闪烁控制
#define CH451_TWINKLE 0x0600
//数码管各位显示
#define CH451_DIG0 0x0800
#define CH451_DIG1 0x0900
#define CH451_DIG2 0x0a00
#define CH451_DIG3 0x0b00
#define CH451_DIG4 0x0c00
#define CH451_DIG5 0x0d00
#define CH451_DIG6 0x0e00
#define CH451_DIG7 0x0f00
//CH451初始化
void ch451_init()
{
ch451din=0; //ch451din为PB0
ch451din=1;
}
//输出命令子程序
//定义一个无符号整型变量存储12字节的命令字
void ch451_write(uint command)
{
uchar i;
ch451load0;
for(i=0;i<12;i++)
{
ch451din=command&1;
ch451dclk0;
ch451dclk1;
command>>=1;
}
ch451load1;
}
//读取按键函数
uint ReadKey(void)
{
uchar i;
uint key=0;
uint command=0x700;
ch451load0;
for(i=0;i<12;i++)
{
ch451din=command&1;
ch451dclk0;
ch451dclk1;
command>>=1;
}
ch451load1;
key=ch451dout;
for(i=0;i<6;i++)
{
ch451dclk0;
key<<=1;
key|=ch451dout;
ch451dclk1;
}
return key;
}
//CH451端口初始化
void port_init(void)
{
PORTA = 0x00;
DDRA = 0x00;
PORTB = 0x07;
DDRB = 0x07;
PORTC = 0x00;
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
}
//单片机C语言主函数
void main(void)
{
uint key1;
port_init();
ch451_write(CH451_RESET); //复位
ch451_init();
ch451_write(0x403); //关闭闪烁,关闭看门狗,打开键盘扫描 ,打开显示
ch451_write(0x500); //不译码方式,扫描8个管,亮度默认值
ch451_write(0x600); //闪烁功能关闭
//ch451_write(CH451_DIG0|0xFF); //不译码方式,显示8,无小数点
//ch451_write(CH451_DIG1|0xFF);
//ch451_write(CH451_DIG2|0xFF);
//ch451_write(CH451_DIG3|0xFF);
//ch451_write(CH451_DIG4|0xFF);
//ch451_write(CH451_DIG5|0xFF);
//ch451_write(CH451_DIG6|0xFF);
ch451_write(CH451_DIG7|0xFF);
while(1)
{
if((PINB&0x08)==0)
key1=ReadKey();
switch(key1)
{
case 0x40:
{
ch451_write(0x0800); //在第0个数码管显示0
break;
}
case 0x41:
{
ch451_write(0x0906);//在第1个数码管显示1
break;
}
case 0x42:
{
ch451_write(0x0A5B);//在第2个数码管显示2
break;
}
case 0x43:
{
ch451_write(0x0B4F);//在第3个数码管显示3
break;
}
case 0x48:
{
ch451_write(0x0C66);//在第4个数码管显示4
break;
}
case 0x49:
{
ch451_write(0x0D6D);//在第5个数码管显示5
break;
}
case 0x4A:
{
ch451_write(0x0E7D);//在第6个数码管显示6
break;
}
case 0x4B:
{
ch451_write(0x0F07);//在第7个数码管显示7
break;
}
default:break;
}
}
}
这是我们的一个驱动程序,显示没有问题,但是读键值函数ReadKey()中总是返回0xff,此函数中key=ch451dout;指令后key为0x03,请教为什么会出现这种错误,谢谢,或者各位能否发个mega16的ch451驱动程序参考一下 谢谢 |
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|