给我一份STM32驱动74HC164驱动四位数码管的C语言程序。
时间: 2023-08-10 07:08:23 浏览: 111
好的,以下是一份基于STM32驱动74HC164驱动四位数码管的C语言程序,供您参考:
```c
#include "stm32f10x.h"
#define CLK_PIN GPIO_Pin_0
#define CLK_PORT GPIOB
#define DATA_PIN GPIO_Pin_1
#define DATA_PORT GPIOB
void HC164_WriteByte(uint8_t data)
{
uint8_t i;
for(i=0;i<8;i++)
{
GPIO_ResetBits(CLK_PORT, CLK_PIN);
if(data&0x80)
GPIO_SetBits(DATA_PORT, DATA_PIN);
else
GPIO_ResetBits(DATA_PORT, DATA_PIN);
data <<= 1;
GPIO_SetBits(CLK_PORT, CLK_PIN);
}
}
void Display_Digit(uint8_t digit, uint8_t data)
{
switch(digit)
{
case 0:
HC164_WriteByte(0x3f);
break;
case 1:
HC164_WriteByte(0x06);
break;
case 2:
HC164_WriteByte(0x5b);
break;
case 3:
HC164_WriteByte(0x4f);
break;
case 4:
HC164_WriteByte(0x66);
break;
case 5:
HC164_WriteByte(0x6d);
break;
case 6:
HC164_WriteByte(0x7d);
break;
case 7:
HC164_WriteByte(0x07);
break;
case 8:
HC164_WriteByte(0x7f);
break;
case 9:
HC164_WriteByte(0x6f);
break;
default:
HC164_WriteByte(0x00);
break;
}
HC164_WriteByte(data);
}
int main(void)
{
uint8_t data[4] = {0x01, 0x02, 0x04, 0x08};
uint8_t i, j;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = CLK_PIN | DATA_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
while(1)
{
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
Display_Digit(j, data[j]);
}
data[i] <<= 1;
if(data[i] == 0)
data[i] = 1;
delay_ms(1);
}
}
}
```
其中,`HC164_WriteByte()`函数用于写入一个字节的数据到74HC164芯片;`Display_Digit()`函数用于显示一个数字到数码管上;`main()`函数中的循环用于不断地在四位数码管上显示数字。请注意,该程序中的`delay_ms()`函数需要根据实际情况进行实现。
阅读全文