学习笔记——STM32F103的V3版本——3*3矩阵键盘控制数码管

一.硬件

1.数码管

2.3*3的矩阵键盘(自己做的模块(手残党一枚))

3.总体连接

二.在Keil5中的部分软代码

test.c中:

#include "sys.h"

#include "usart.h"

#include "delay.h"

#include "key.h"

#include "led.h"

#include "exti.h"

//MCU会在外部中断时发生自动处理

int main(void)

{

vu8 t;

Stm32_Clock_Init(9); //初始化系统时钟为72MHZ

delay_init(72); //初始化延时函数

uart_init(72, 9600); // 舒适化串口,波特率为9600

LED_Init(); // 初始化LED

EXTI_Init(); // 初始化外部中断

while(1)

{

t=KEY_Scan(); //获取按键值

printf("t的值为:%d\n",t);

switch(t)

{

case 1:

printf("你按下了:1\r\n");

LED_1();

break;

case 2:

printf("你按下了:2\r\n");

LED_2();

break;

case 3:

printf("你按下了:3\r\n");

LED_3();

break;

case 4:

printf("你按下了:4\r\n");

LED_4();

break;

case 5:

printf("你按下了:5\r\n");

LED_5();

break;

case 6:

printf("你按下了:6\r\n");

LED_6();

break;

case 7:

printf("你按下了:7\r\n");

LED_7();

break;

case 8:

printf("你按下了:8\r\n");

LED_8();

break;

case 9:

printf("你按下了:9\r\n");

LED_9();

break;

default:

printf("无按键按下\r\n");

delay_ms(1000);

break;

}

}

}

led.c中:

#include "sys.h"

#include "led.h"

void LED_Init(void)

{

RCC->APB2ENR |= 1 << 2; // Enable GPIOA clock

RCC->APB2ENR |= 1 << 5; // Enable GPIOD clock

// Configure GPIOA pins for LED control

GPIOA->CRL &= 0X00000000; // Clear configuration for GPIOA pins

GPIOA->CRL |= 0x33333333; // Set GPIOA pins as general purpose output push-pull

JTAG_Set(SWD_ENABLE); // Enable SWD for debugging

GPIOA->ODR &= 0X00000000; // Clear GPIOA output data register

}

void LED_1(void)

{

PAout(1) = 1;

PAout(2) = 1;

PAout(0) = 1;

PAout(5) = 0;

PAout(6) = 1;

PAout(7) = 1;

PAout(3) = 1;

PAout(4) = 0;

}

void LED_2(void)

{

PAout(1) = 0;

PAout(2) = 1;

PAout(0) = 0;

PAout(5) = 0;

PAout(6) = 0;

PAout(7) = 1;

PAout(3) = 0;

PAout(4) = 1;

}

void LED_3(void)

{

PAout(1) = 0;

PAout(0) = 0;

PAout(2) = 0;

PAout(5) = 1;

PAout(6) = 0;

PAout(7) = 1;

PAout(3) = 0;

PAout(4) = 1;

}

void LED_4(void)

{

PAout(1) = 0;

PAout(0) = 1;

PAout(5) = 1;

PAout(6) = 0;

PAout(7) = 1;

PAout(2) = 0;

PAout(3) = 1;

PAout(4) = 0;

}

void LED_5(void)

{

PAout(1) = 1;

PAout(0) = 0;

PAout(5) = 1;

PAout(6) = 0;

PAout(7) = 1;

PAout(3) = 0;

PAout(2) = 0;

PAout(4) = 0;

}

void LED_6(void)

{

PAout(1) = 1;

PAout(0) = 0;

PAout(5) = 0;

PAout(6) = 0;

PAout(7) = 1;

PAout(3) = 0;

PAout(4) = 0;

PAout(2) = 0;

}

void LED_7(void)

{

PAout(1) = 0;

PAout(0) = 0;

PAout(5) = 1;

PAout(6) = 1;

PAout(7) = 1;

PAout(2) = 0;

PAout(3) = 1;

PAout(4) = 1;

}

void LED_8(void)

{

PAout(1) = 0;

PAout(0) = 0;

PAout(2) = 0;

PAout(5) = 0;

PAout(6) = 0;

PAout(7) = 1;

PAout(3) = 0;

PAout(4) = 0;

}

void LED_9(void)

{

PAout(1) = 0;

PAout(0) = 0;

PAout(5) = 1;

PAout(6) = 0;

PAout(7) = 1;

PAout(3) = 0;

PAout(4) = 0;

PAout(2) = 0;

}

exti.c中:

#include "exti.h"

#include "led.h"

#include "key.h"

#include "delay.h"

#include "usart.h"

//外部中断初始化函数

void EXTI_Init(void)

{

//配置外部中断出发方式

AFIO->EXTICR3 &= ~(0xF << 8); //清除EXTI11-8位

AFIO->EXTICR3 |= 0x1000; //EXTI11配置端口

EXTI->FTSR |= (1 << 11); // 配置EXIT11为下降沿触发

EXTI->IMR |= (1 << 11); // 使能EXTI11中断

NVIC_EnableIRQ(EXTI15_10_IRQn); // 在NVIC中使能EXTI15_10中断

}

//外部中断处理函数

void EXTI_IRQHandler(void)

{

if (EXTI->PR & (1 << 11))

{

vu8 key = KEY_Scan();

switch(key)

{

case 1:

printf("你按下了:1\r\n");

LED_1();//ÏÔʾ¶ÔÓ¦Êý×Ö

break;

case 2:

printf("你按下了:2\r\n");

LED_2();

break;

case 3:

printf("你按下了:3\r\n");

LED_3();

break;

case 4:

printf("你按下了:4\r\n");

LED_4();

break;

case 5:

printf("你按下了:5\r\n");

LED_5();

break;

case 6:

printf("你按下了:6\r\n");

LED_6();

break;

case 7:

printf("你按下了:7\r\n");

LED_7();

break;

case 8:

printf("你按下了:8\r\n");

LED_8();

break;

case 9:

printf("你按下了:9\r\n");

LED_9();

break;

default:

printf("无按键按下\r\n");

delay_ms(1000);

break;

}

EXTI->PR |= (1 << 11); //清除EXTII挂起标志位

}

}

相关推荐
希和202710 小时前
江科大 51 单片机笔记:DS18B20 温度传感器 + LCD1602 显示代码详解
笔记·单片机·mongodb
电科_银尘13 小时前
【深度学习项目实战·22】灰度图像着色:用小型U-Net学习从灰度到RGB
人工智能·深度学习·学习
Sakura_j_h_13 小时前
MCP SDK 2.0 实战:用 MCPServer 搭本地笔记检索服务
开发语言·笔记·python·qt·ai工具·mcp·mcpserver
QuZhengRong14 小时前
【AI】Agent 全栈进阶|AgentScope 沙箱源码解析
python·学习·ai·agent·沙箱
前进的李工17 小时前
Agent 学习笔记 15:Agent 怎样响应事件与中途打断?
人工智能·笔记·agent·openclaw·工具设计·gpt-6 astra·原生异步
Zw-awa18 小时前
陀螺仪数据为什么看起来总在动:零偏、噪声和零漂
stm32
承渊政道19 小时前
【Python编程—从入门到实践】(用户账户:给Django网站装上登录系统,让每个人拥有自己的学习笔记)
python·学习·pycharm·django
诗句藏于尽头20 小时前
游戏《完美神话》-使用Ai-agent进行修复已停服游戏到本地成功部署以及GM端网页完整教程
人工智能·学习·游戏
希和202720 小时前
51 单片机学习笔记:LCD1602、独立按键、I2C 与 AT24C02 掉电存储(江科大课程小白笔记)
笔记·单片机·学习
北风toto20 小时前
Spring Boot 常用注解 · 概要笔记
spring boot·笔记·后端