lm3s811配合ch451控制数码管的显示示例,昨天刚调出来的
// 定义全局的系统时钟变量
unsigned long TheSysClock = 12000000UL;
// 定义引脚
#define CH451_PERIPH SYSCTL_PERIPH_GPIOD
#define CH451_PORT GPIO_PORTD_BASE
//PD0-LOAD(上升沿发送命令) PD1-DIN(输入) PD2-DCLK(时钟) PD3-DOUT(中断引脚EINT1)若只显示数码管不用键盘可不接这个引脚
void ch451write(unsigned int command)
{
int i;
signed int a;
SysCtlDelay(50 * (TheSysClock / 3000)); // 延时约50ms
GPIOPinWrite(CH451_PORT, GPIO_PIN_0 , 0);
for(i=0;i<12;i++)
{
a=command&1;
if(a==1)
{
GPIOPinWrite(CH451_PORT, GPIO_PIN_1 , 0xff);
}
else
{
GPIOPinWrite(CH451_PORT, GPIO_PIN_1 , 0x0); //Din
}
GPIOPinWrite(CH451_PORT, GPIO_PIN_2 , 0x0);
command>>=1;
GPIOPinWrite(CH451_PORT, GPIO_PIN_2 , 0xff); //Dclk
}
GPIOPinWrite(CH451_PORT, GPIO_PIN_0 , 0x01); //Load 重要
}
void ch451_init()
{
SysCtlPeripheralEnable(CH451_PERIPH); // 使能CH451所在的GPIO端口
GPIOPinTypeGPIOOutput(CH451_PORT, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2); // 设置CH451所在管脚为输出
GPIOPinTypeGPIOInput(CH451_PORT, GPIO_PIN_3); // 设置CH451所在管脚为输入
GPIOPinWrite(CH451_PORT, GPIO_PIN_1 , 0x00); //先低后高选择4线输入
GPIOPinWrite(CH451_PORT, GPIO_PIN_1 , 0xff);
ch451write(0x201); //复位
ch451write(0x401); //开显示
ch451write(0x580); //BCD译码方式
}
void show(int value)
{
ch451write(0x0800|(value%10)); //个位
ch451write(0x0900|(value/10%10)); //十位
ch451write(0x0a00|(value/100%10)); //百位
ch451write(0x0b00|(value/1000)); //千位
}
主函数中调用ch451_init() 初始化和show(val)显示数值