password
主页链接
type
Post
status
Published
date
Jan 31, 2025
slug
lanqiao
summary
tags
进行中
category
学习笔记
icon

一、新建工程

  1. ACCESS TO MCU SELECTOR
    1. notion image
  1. 搜索并双击选择芯片stm32g431rbt6
    1. notion image
  1. Pinout & Configuration
    1. System Core —— RCC —— HSE 选择Crystal/Ceramic Resonator
      1. notion image
    2. System Core —— SYS —— Debug 选择Serial Wire
    3. notion image
  1. Clock Configuration
    1. 外部晶振频率24MHz
    2. 选择HSE(外部晶振)
    3. 选择PLLCLK
    4. 修改HCLK(MHz)为80MHz
    5. notion image
  1. Project Manager
    1. Project Name
    2. Location
    3. Toolchain/IDE:MDK-ARM
    4. 不使用默认Firmware Location,选择自己下载的库文件夹
    5. notion image
  1. Code Generator
    1. notion image
  1. GENERATE CODE
  1. Keil魔术棒设置:
    1. Debug: CMSIS-DAP Debugger
      1. notion image
    2. Debug —— Settings —— Flash Download:勾选 Reset and Run
      1. notion image
9. 将自己创建的文件夹User在魔术棒C/C++处加入

二、基础驱动

1. 点亮一个LED

notion image

步骤:

  1. PD2输出高电平:U1(锁存器)使能端LE(引脚PD2)给高电平时,右侧1-10(引脚PC8-PC15)输入可以通过U1输出、
  1. PC8-PC15输出低电平:LED左侧连VDD(高电平),因此右侧给低电平灯亮。
  1. PD2输出低电平:防止lcd引脚冲突

Pinout配置:

  1. PC8-PC15、PD2配置为输出模式(GPID_OUTPUT)
    1. notion image
      notion image
  1. GPIO中可以看见默认输出低电平(Low),将其修改为默认高电平
    1. notion image

代码<按键控制led,点亮lcd>:

notion image

led.c

lcd_show.c

main.c

解决lcd和led引脚冲突:
在lcd.c用到的所有函数前,保存GPIOC→ODR到temp,函数体结束后将GPIOC→ODR还原

2.Tim定时器闪烁

notion image
notion image

步骤

  1. Timers-TIM2
    1. notion image
  1. 设置为内部时钟
    1. notion image
  1. 修改以下两项:
    1. notion image
  1. PSC = 8000 - 1 ARR = 10000 - 1
    1. notion image
  1. 中断使能
    1. notion image
  1. GENERATE CODE
  1. 编译,找到tim.c下的回调函数stm32g4xx_hal_tim.h
    1. notion image
  1. 2531行处找到回调函数callback
    1. notion image
  1. headfiles.h包含tim.h
  1. 定时器中断使能(注意使能函数位置)
    1. notion image
  1. 定时器中断函数
    1. 实现:count每秒+1
      notion image
  1. 实现:count每秒+1,led1间隔1s亮起(闪烁)
    1. notion image

3.实现长按键和短按键

  1. 配置TIM3(定时器中断这里就不打开了)
    1. PSC设置为8000-1,这样CNT = 10000时就对应1s
      notion image
  1. 使能TIM3
  1. 按下按键b1_key_state == 0 && b1_key_last_state == 1
    1. TIM3→CNT = 0;计数开始
  1. 松开按键b1_key_state == 1 && b1_key_last_state == 0
    1. 判断计数情况:
  1. 如果要实现长按期间连续改变
    1. 判断长按:b1_key_state == 0 && b1_key_last_state == 0
      notion image

4.lcd高亮显示

5.PWM波输出

notion image
notion image

6.输入捕获

notion image
notion image
notion image
相关文章
C++学习记录路径规划
Loading...