说在开头
今天是2023/9/21
学了点灯 点流水灯 点蜂鸣器

工程创建
startup/arm
ST/STM32F10x
CM3/CoreSupport
STM32_Driver/inc
STM32_Driver/src
Project/template
低电平还是有较强的驱动能力的
高电平弱驱动
低电平强驱动
GPIO操作
- RCC开启GPIO时钟
 
- GPIO_init 初始化GPIO
- mode
 
- pin
 
- speed
 
 
- 输入输出函数控制GPIO口
- 8个输入输出
 
 
输入输出模式
https://zhuanlan.zhihu.com/p/361640739
输入模式
-输入浮空(GPIO_Mode_IN_FLOATING)
-输入上拉(GPIO_Mode_IPU)
-输入下拉(GPIO_Mode_IPD)
-模拟输入(GPIO_Mode_AIN)
输出模式
-开漏输出(GPIO_Mode_Out_OD)
-开漏复用功能(GPIO_Mode_AF_OD)
-推挽式输出(GPIO_Mode_Out_PP)
-推挽式复用功能(GPIO_Mode_AF_PP)
点灯
代码逻辑是这样
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
   | #include "stm32f10x.h"                   #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_0; 	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;      	GPIO_Init(GPIOA, &GPIO_InitStructure); 	 	while (1) 	{          		GPIO_ResetBits(GPIOA, GPIO_Pin_0); 		Delay_ms(500);          		GPIO_SetBits(GPIOA, GPIO_Pin_0); 		Delay_ms(500); 		 		GPIO_WriteBit(GPIOA, GPIO_Pin_0, Bit_RESET); 		Delay_ms(500); 		GPIO_WriteBit(GPIOA, GPIO_Pin_0, Bit_SET); 		Delay_ms(500); 		 	} }
   | 
 
点流水灯
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
   | #include "stm32f10x.h"                   #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);	 		Delay_ms(100); 		GPIO_Write(GPIOA, ~0x0002);	 		Delay_ms(100); 		GPIO_Write(GPIOA, ~0x0004);	 		Delay_ms(100); 		GPIO_Write(GPIOA, ~0x0008);	 		Delay_ms(100); 		GPIO_Write(GPIOA, ~0x0010);	 		Delay_ms(100); 		GPIO_Write(GPIOA, ~0x0020);	 		Delay_ms(100); 		GPIO_Write(GPIOA, ~0x0040);	 		Delay_ms(100); 		GPIO_Write(GPIOA, ~0x0080);	 		Delay_ms(100); 	} }
   | 
 
蜂鸣器
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
   | #include "stm32f10x.h"                   #include "Delay.h"
  int main(void) { 	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); 	} }
 
   |