请使用 STM32设计具有以下要求的应用控制系统: 1、功能要求: 利用该应用系统的 4X4 矩阵式键盘,来控制八个数码管; 1) 板子上电后,第一行键盘按下,数码管各自显示 1,3,5,7; 2)第二行键盘按下,数码管各自显示 2,4,6,8; 3)第三行键盘按下,数码管各自显示 12,34,56,78; 4)第四行键盘按下,数码管各自显示 1234, 5678,12345678,全 部熄灭。 2、硬件设计要求: 通过杜邦线连接按键及数码管电路, 3、软件编制要求: 在硬件设计基础上,运用 kei14 软件编写出相应软件代码,并完成系统调试,实现系统控制要求;请给出详细相应软件代码
时间: 2024-03-05 13:52:55 浏览: 24
好的,下面是根据您的要求编写的代码:
```c
#include "stm32f10x.h"
#define ROW1 GPIO_Pin_0
#define ROW2 GPIO_Pin_1
#define ROW3 GPIO_Pin_2
#define ROW4 GPIO_Pin_3
#define COL1 GPIO_Pin_4
#define COL2 GPIO_Pin_5
#define COL3 GPIO_Pin_6
#define COL4 GPIO_Pin_7
void delay_ms(uint32_t ms)
{
uint32_t i;
for(i=0;i<ms*1000;i++);
}
void display(uint8_t num)
{
uint8_t i;
uint8_t digit[8];
digit[0] = num % 10;
digit[1] = (num/10) % 10;
digit[2] = (num/100) % 10;
digit[3] = (num/1000) % 10;
digit[4] = (num/10000) % 10;
digit[5] = (num/100000) % 10;
digit[6] = (num/1000000) % 10;
digit[7] = (num/10000000) % 10;
for(i=0;i<8;i++)
{
GPIO_SetBits(GPIOB, 0xFF);
GPIO_ResetBits(GPIOA, 1<<i);
GPIO_SetBits(GPIOB, digit[i]);
delay_ms(1);
}
}
int main(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = ROW1|ROW2|ROW3|ROW4;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = COL1|COL2|COL3|COL4;
GPIO_Init(GPIOB, &GPIO_InitStructure);
while(1)
{
// 第一行键盘按下,数码管各自显示 1,3,5,7
GPIO_SetBits(GPIOA, ROW1);
GPIO_ResetBits(GPIOA, ROW2|ROW3|ROW4);
if(GPIO_ReadInputDataBit(GPIOB, COL1)==0)
{
display(1);
}
if(GPIO_ReadInputDataBit(GPIOB, COL2)==0)
{
display(3);
}
if(GPIO_ReadInputDataBit(GPIOB, COL3)==0)
{
display(5);
}
if(GPIO_ReadInputDataBit(GPIOB, COL4)==0)
{
display(7);
}
// 第二行键盘按下,数码管各自显示 2,4,6,8
GPIO_SetBits(GPIOA, ROW2);
GPIO_ResetBits(GPIOA, ROW1|ROW3|ROW4);
if(GPIO_ReadInputDataBit(GPIOB, COL1)==0)
{
display(2);
}
if(GPIO_ReadInputDataBit(GPIOB, COL2)==0)
{
display(4);
}
if(GPIO_ReadInputDataBit(GPIOB, COL3)==0)
{
display(6);
}
if(GPIO_ReadInputDataBit(GPIOB, COL4)==0)
{
display(8);
}
// 第三行键盘按下,数码管各自显示 12,34,56,78
GPIO_SetBits(GPIOA, ROW3);
GPIO_ResetBits(GPIOA, ROW1|ROW2|ROW4);
if(GPIO_ReadInputDataBit(GPIOB, COL1)==0)
{
display(12);
}
if(GPIO_ReadInputDataBit(GPIOB, COL2)==0)
{
display(34);
}
if(GPIO_ReadInputDataBit(GPIOB, COL3)==0)
{
display(56);
}
if(GPIO_ReadInputDataBit(GPIOB, COL4)==0)
{
display(78);
}
// 第四行键盘按下,数码管各自显示 1234, 5678,12345678,全部熄灭
GPIO_SetBits(GPIOA, ROW4);
GPIO_ResetBits(GPIOA, ROW1|ROW2|ROW3);
if(GPIO_ReadInputDataBit(GPIOB, COL1)==0)
{
display(1234);
}
if(GPIO_ReadInputDataBit(GPIOB, COL2)==0)
{
display(5678);
}
if(GPIO_ReadInputDataBit(GPIOB, COL3)==0)
{
display(12345678);
}
if(GPIO_ReadInputDataBit(GPIOB, COL4)==0)
{
GPIO_SetBits(GPIOB, 0xFF);
delay_ms(1);
}
delay_ms(10);
}
}
```
这个程序使用了4x4矩阵键盘和8个数码管。在按下键盘时,会根据要求在数码管上显示不同的数字。在程序中,我们使用了`display`函数来控制数码管的显示,该函数将一个数字转换为8位数字,然后依次控制每个数码管进行显示。您可以根据需要修改此函数以适应您的具体需求。
相关推荐
![doc](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_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)