STM32——从点灯到传感器控制
STM32基础外设开发:从点灯到传感器控制
一、前言
本篇文章总结STM32F10x系列基础外设开发实例,涵盖GPIO控制、按键检测、传感器应用等。所有代码基于标准库开发,适合STM32初学者参考。
二、硬件准备
- STM32F10x系列开发板
- LED模块
- 有源蜂鸣器
- 光敏电阻模块
- 按键模块
三、基础GPIO控制
1. 点灯实验
// 核心代码片段 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); while(1) { GPIO_WriteBit(GPIOA, GPIO_Pin_0, Bit_RESET); Delay_ms(500); GPIO_WriteBit(GPIOA, GPIO_Pin_0, Bit_SET); Delay_ms(500); }
关键点:
必须开启对应GPIO时钟 :选择你面包板插入对应的端口
推挽输出模式适合驱动LED
使用Delay函数实现定时控制
#include "stm32f10x.h" /** * @brief 微秒级延时 * @param xus 延时时长,范围:0~233015 * @retval 无 */ void Delay_us(uint32_t xus) { SysTick->LOAD = 72 * xus; //设置定时器重装值 SysTick->VAL = 0x00; //清空当前计数值 SysTick->CTRL = 0x00000005; //设置时钟源为HCLK,启动定时器 while(!(SysTick->CTRL & 0x00010000)); //等待计数到0 SysTick->CTRL = 0x00000004; //关闭定时器 } /** * @brief 毫秒级延时 * @param xms 延时时长,范围:0~4294967295 * @retval 无 */ void Delay_ms(uint32_t xms) { while(xms--) { Delay_us(1000); } } /** * @brief 秒级延时 * @param xs 延时时长,范围:0~4294967295 * @retval 无 */ void Delay_s(uint32_t xs) { while(xs--) { Delay_ms(1000); } }
2. 流水灯实验
#include "stm32f10x.h" // Device header #include "Delay.h" int main(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); while (1) { GPIO_Write(GPIOA, ~0x0001); //0000 0000 0000 0001 Delay_ms(100); GPIO_Write(GPIOA, ~0x0002); //0000 0000 0000 0010 Delay_ms(100); GPIO_Write(GPIOA, ~0x0004); //0000 0000 0000 0100 Delay_ms(100); GPIO_Write(GPIOA, ~0x0008); //0000 0000 0000 1000 Delay_ms(100); GPIO_Write(GPIOA, ~0x0010); //0000 0000 0001 0000 Delay_ms(100); GPIO_Write(GPIOA, ~0x0020); //0000 0000 0010 0000 Delay_ms(100); GPIO_Write(GPIOA, ~0x0040); //0000 0000 0100 0000 Delay_ms(100); GPIO_Write(GPIOA, ~0x0080); //0000 0000 1000 0000 Delay_ms(100); } } /*优化 void LED_Flow() { uint16_t ledPins = 0x0001; // 从PA0开始 while (1) { GPIO_Write(GPIOA, ~ledPins); // 取反实现低电平点亮 Delay_ms(200); ledPins RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); while (1) { GPIO_ResetBits(GPIOB, GPIO_Pin_12); Delay_ms(100); GPIO_SetBits(GPIOB, GPIO_Pin_12); Delay_ms(100); GPIO_ResetBits(GPIOB, GPIO_Pin_12); Delay_ms(100); GPIO_SetBits(GPIOB, GPIO_Pin_12); Delay_ms(700); } } RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_SetBits(GPIOA, GPIO_Pin_1 | GPIO_Pin_2); } void LED1_ON(void) { GPIO_ResetBits(GPIOA, GPIO_Pin_1); } void LED1_OFF(void) { GPIO_SetBits(GPIOA, GPIO_Pin_1); } void LED1_Turn(void) { if (GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_1) == 0) { GPIO_SetBits(GPIOA, GPIO_Pin_1); } else { GPIO_ResetBits(GPIOA, GPIO_Pin_1); } } void LED2_ON(void) { GPIO_ResetBits(GPIOA, GPIO_Pin_2); } void LED2_OFF(void) { GPIO_SetBits(GPIOA, GPIO_Pin_2); } void LED2_Turn(void) { if (GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_2) == 0) { GPIO_SetBits(GPIOA, GPIO_Pin_2); } else { GPIO_ResetBits(GPIOA, GPIO_Pin_2); } } RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//上拉输入,默认高电平 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_11; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); } uint8_t Key_GetNum(void) { uint8_t KeyNum = 0; if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 0) //按下按键,低电平 { Delay_ms(20);//延时消抖 while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 0);//如果不松开,循环卡住 Delay_ms(20);//松开消抖 KeyNum = 1;//控制led1,赋值 } if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_11) == 0) { Delay_ms(20); while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_11) == 0); Delay_ms(20); KeyNum = 2;控制led2,赋值 } return KeyNum; } LED_Init(); Key_Init(); while (1) { KeyNum = Key_GetNum(); if (KeyNum == 1) { LED1_Turn(); } if (KeyNum == 2) { LED2_Turn(); } } } RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_SetBits(GPIOB, GPIO_Pin_12); } void Buzzer_ON(void) { GPIO_ResetBits(GPIOB, GPIO_Pin_12); } void Buzzer_OFF(void) { GPIO_SetBits(GPIOB, GPIO_Pin_12); } void Buzzer_Turn(void) { if (GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_12) == 0) { GPIO_SetBits(GPIOB, GPIO_Pin_12); } else { GPIO_ResetBits(GPIOB, GPIO_Pin_12); } } RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); } uint8_t LightSensor_Get(void) { return GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_13); } Buzzer_Init(); LightSensor_Init(); while (1) { if (LightSensor_Get() == 1) { Buzzer_ON(); } else { Buzzer_OFF(); } } }
免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们。