74LS595控制数码管显示
时间: 2023-05-30 17:02:56 浏览: 455
74LS595是一个8位移位寄存器,可以用来控制数码管的显示。以下是一个使用74LS595控制4位共阴数码管显示0~9的简单示例:
1.连接电路
将74LS595的引脚连接到数码管的引脚,具体连接方式如下:
- Q0~Q3连接到数码管的a~d引脚
- Q4~Q7连接到数码管的e~h引脚
- Q7'连接到数码管的com引脚
- SER连接到单片机的一个IO口
- SRCLK连接到单片机的另一个IO口
- RCLK连接到单片机的另一个IO口
2.编写程序
使用单片机控制74LS595输出数据的顺序,即可实现数码管的显示。以下是一段使用STM32控制74LS595显示数字0~9的示例代码:
```
#include "stm32f10x.h"
#define SER_GPIO GPIOB
#define SER_PIN GPIO_Pin_0
#define SRCLK_GPIO GPIOB
#define SRCLK_PIN GPIO_Pin_1
#define RCLK_GPIO GPIOB
#define RCLK_PIN GPIO_Pin_2
void delay_us(uint32_t us)
{
while (us--)
{
__nop();
__nop();
__nop();
__nop();
}
}
void shift_out(uint8_t data)
{
for (int i = 0; i < 8; i++)
{
if (data & 0x80)
GPIO_SetBits(SER_GPIO, SER_PIN);
else
GPIO_ResetBits(SER_GPIO, SER_PIN);
data <<= 1;
GPIO_SetBits(SRCLK_GPIO, SRCLK_PIN);
delay_us(1);
GPIO_ResetBits(SRCLK_GPIO, SRCLK_PIN);
delay_us(1);
}
}
void latch()
{
GPIO_SetBits(RCLK_GPIO, RCLK_PIN);
delay_us(1);
GPIO_ResetBits(RCLK_GPIO, RCLK_PIN);
delay_us(1);
}
void display(uint8_t num)
{
static const uint8_t DIGITS[] = {
0x3f, // 0
0x06, // 1
0x5b, // 2
0x4f, // 3
0x66, // 4
0x6d, // 5
0x7d, // 6
0x07, // 7
0x7f, // 8
0x6f, // 9
};
shift_out(DIGITS[num % 10]);
shift_out(DIGITS[num / 10]);
shift_out(0xff);
latch();
}
int main()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = SER_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(SER_GPIO, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = SRCLK_PIN;
GPIO_Init(SRCLK_GPIO, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = RCLK_PIN;
GPIO_Init(RCLK_GPIO, &GPIO_InitStructure);
while (1)
{
for (int i = 0; i <= 9; i++)
{
display(i);
for (int j = 0; j < 50000; j++)
__nop();
}
}
}
```
3.运行程序
将程序下载到单片机,并将数码管连接到74LS595的引脚上,即可看到数码管显示0~9的数字。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)