Zen的小站

小舟从此逝,江海寄余生

0%

【模块】数码管

文章概览

这篇写得超级好(但是有个错误:五、代码分析2.代码块PB15和PB13位置写反了,我这里改过来了)

四位数码管,背面有两个芯片。

除了VCC和GND,其余引脚包括 DIO、LCLK、SCLK

控制原理简单,不需要设置I2C,用代码模拟就行了

原理

数码管背面芯片是 74HC595,串行输入——并行输出

操作流程

  1. 发送字节到74HC595芯片的移位寄存器中
  2. 继续发送数码管位置到移位寄存器中
  3. 从移位寄存器转存带存储寄存器

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/************************
VCC------------>供电
DIO------------>PB13
RCLK------------>PB12
SCLK------------>PB15
GND------------>接地
**************************/

void HC595_GPIO_Configuration(void);
void HC595_Send_Data(unsigned char num, unsigned char show_bit);
void HC595_Send_Byte(unsigned char byte)
void display(unsigned int n);

unsigned int num[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90, 0xff, 0x00};
//创建一个数组,0-9所对应的十六进制数


//串入
void HC595_Send_Byte(unsigned char byte)
{
unsigned int i;

for(i = 0; i < 8; i++)
{
if(byte & 0x80)
GPIO_WriteBit(GPIOB, GPIO_Pin_15, Bit_SET);
else
GPIO_WriteBit(GPIOB, GPIO_Pin_15, Bit_RESET);

GPIO_WriteBit(GPIOB, GPIO_Pin_13, Bit_RESET);
Delay_us(10);
GPIO_WriteBit(GPIOB, GPIO_Pin_13, Bit_SET);
Delay_us(10);

byte <<= 1;
}
}


//并出
void HC595_Send_Data(unsigned char num, unsigned char show_bit)
{
HC595_Send_Byte(num);
HC595_Send_Byte(1 << show_bit);

GPIO_WriteBit(GPIOB, GPIO_Pin_12, Bit_RESET);
Delay_us(10);
GPIO_WriteBit(GPIOB, GPIO_Pin_12, Bit_SET);
Delay_us(10);
}



// 显示四位数字
void display(unsigned int n)
{
static unsigned int thousand_bit, hundred_bit, ten_bit, single_bit;

thousand_bit = n / 1000;
hundred_bit = (n % 1000) / 100;
ten_bit = n % 1000 % 100 / 10;
single_bit = n % 10;

HC595_Send_Data(num[thousand_bit], 3);
HC595_Send_Data(num[hundred_bit], 2);
HC595_Send_Data(num[ten_bit], 1);
HC595_Send_Data(num[single_bit], 0);
}

多想多做,发篇一作

-------------本文结束感谢您的阅读-------------
// 在最后添加