CH451能够一次性驱动8位数码管,与单片机串行通讯。支撑数码管的左移,右移,左循环,右循环,独立闪耀,16级亮度操控等功能。是单片机操控多个数码管的得力助手,现将MEGA16单片机(MEGA16是AVR单片机,改程序稍加修正能够应用到8051单片机,PIC单片机,MSP430单片机上)驱动CH451的程序发布,期望能有所用。CH451的详细资料可去官方网站下载。
//包括CH451所需头文件
#i nclude
#i nclude
/*——宏界说——*/
#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 ch451dout0 PORTB&=~BIT(PB3)
#define ch451dout1 PORTB|=BIT(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=1;
}
//输出指令子程序
//界说一个无符号整型变量存储12字节的指令字
void ch451_write(uint command)
{
uchar i;
ch451load0;
for(i=0;i<12;i++)
{
ch451din=command&1;
ch451dclk0;
ch451dclk1;
command>>=1;
}
ch451load1;
}
//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)
{
port_init();
ch451_init();
ch451_write(CH451_RESET);
ch451_write(0x401);
ch451_write(0x580);
ch451_write(0x600);
ch451_write(CH451_DIG0|0x08);
while(1)
{}
}