GPIO的学习_day5

GPIO的学习

结合视频内容,根据数据手册,我对GPIO的使用做了一些总结与理解。

1.什么是GPIO

GPIO(General purpose intput output)是通用输入输出端口的简称,可以通过软件来控制其输入和输出。STM32芯片的GPIO引脚与外部设备连接起来,从而实现与外部通讯、控制以及数据采集的功能。

2.STM32引脚分类

电源管脚、晶振管脚、复位管脚、下载管脚、BOOT管脚、GPIO管脚。

3.GPIO基本结构

img

1.输出控制电路:

推挽模式:若输出控制为高电平,则P-MOS导通,N-MOS截止,I/O端口输出高电平。若输出控制为低电平,则P-MOS截止,N-MOS导通,I/O端口输出低电平。

开漏模式:无论输出控制为什么,P-MOS均为截止,若输出控制为低电平,则N-MOS导通,I/O端口输出高电平。此时若想让I/O端口输出高电平,则必须在II/O端口外接一个上拉电阻,输出的电平高低取决于上拉电阻。(类似于C51的P0)

2. 输出数据寄存器(GPIOx_ODR)

3. TTL施密特触发器:可将一些模拟信号最终转化为数字信号。

4.复用功能输出的信号来自片上外设的数据。

4.GPIO的深入学习

1. 读取GPIO端口数据函数

GPIO_ReadInputDataBit(GPIOx,GPIO_Pin_y);//读取单个输入端口的数据

GPIO_ReadInputData(GPIOx);//读取整个输入端口的数据

GPIO_ReadOutputDataBit(GPIOx, GPIO_Pin_y);//读取单个输出端口的数据

GPIO_ReadOutputData (GPIOx);//读取整个输入出端口的位数据

​ 这4个库函数是用来直接读取端口数据的,其中x:A-G y: 0~15

​ 上面的函数实现的是读取当前GPIO口或位的输入输出数据,库函数没有什么可说的。

​ 但这几个函数涉及到下面两个寄存器:

​ 端口数据输入寄存器(GPIOA_IDR~GPIOG_IDR);

​ 端口数据输出寄存器(GPIOA_ODR~GPIOG_ODR);

​ 这两个寄存器都是16位的,高16位保留,读取值为0,低16位代表着对应GPIO端口位[15:0]的状态,偏移值位0cH,具体地址可参考寄存器表。上面的库函数读取Data就是读取这两个寄存器中选定那个的低16位,读取DataBit就是读取选定寄存器16位中的任意选定位。

2 . 写入GPIO端口数据函数

GPIO_WriteBit(GPIOx, GPIO_Pin_y,BIT_SET/BIT_RESET);//置位或者复位单个输出端口的数据

GPIO_Write(GPIOx, 0x1111);//置位或者复位整个输出端口的数据

其中x:A-G y: 0~15

这两个函数是对GPIO_SetBits和GPIO_ResetBits的补充,可以满足对多个端口同时设置状态的特性。

端口复位置位寄存器(GPIOA_BSRR~GPIOG_BSRR) ,32bit复位置位寄存器,w(只写,下同),

[31~16]写0无效,写1对应GPIOx_ODR位置0

[15~0]写0无效,写1对应GPIOx_ODR位置1

端口复位寄存器(GPIOA_BRR~GPIOG_BRR),32bit复位寄存器w

高16位保留,低16位

[15~0]写0无效,写1对应GPIOx_ODR位置0 若GPIOx_BRR和GPIOx_BSRR同时设置,以GPIOx_BSRR为准。

3. GPIO功能锁存函数

GPIO_PinLockConfig(GPIOx, GPIO_Pin_y);//锁存选择端口配置寄存器的对应位

端口锁存寄存器(GPIOA_LCKR~GPIOG_LCKR),32bit寄存器,高16位保留,低16可随时读出,1代表对应配置位被激活锁存,0代表可以被激活,激活是有固定的写入顺序的。锁存是不能乱用的,如果锁存了一个端口的功能寄存器,那么在下次系统复位前,端口的功能就不可更改,这在某些情况下是有利的,特别是在程序中如果重新定义某些GPIO口的功能时,在确定其中某一GPIO端口在整个系统运行中状态保持不变,锁存就避免误修改,但是如果锁存的GPIO在后续的代码段中有不同的功能,就会无法修改,产生错误。因此锁存和GPIO_StructInit, GPIO_DeInit,GPIO_AFIODeInit这些初始化函数一样,使用前要考虑清楚。

4.GPIO特殊功能函数

函数**GPIO_EventOutputConfig(GPIO_PortSourceGPIOx,GPIO_PinSourcey);**事件输出。

函数GPIO_EXTILineConfig(GPIO_PortSourceGPIOx, GPIO_PinSourcey); 外设端口作为中断线号输入

这两个函数我就不多说了,在EXTI-NVIC中会有详细解释。

rtSourceGPIOx, GPIO_PinSourcey);** 外设端口作为中断线号输入

这两个函数我就不多说了,在EXTI-NVIC中会有详细解释。

函数**GPIO_PinRemapConfig(u32GPIO_Remap, FunctionalState NewState);**改变指定管脚的映射,很多复用功能的引出脚可以通过重映射,从不同的I/O管脚引出,即复用功能的引出脚位是可通过程序改变的。就是上面库函数。

我仔细研究了您提供的PWM呼吸灯代码,这是一个很好的硬件PWM实现示例。我将学习并整合这个实现方式到您的项目中,以下是完整的优化方案: ```c #include "stm32f10x.h" #include "stm32f10x_gpio.h" #include "stm32f10x_rcc.h" #include "stm32f10x_tim.h" #include "stm32f10x_adc.h" #include "oled.h" // 假设您有OLED驱动 // 硬件定义 #define S0_PIN GPIO_Pin_0 #define S0_PORT GPIOA #define S1_PIN GPIO_Pin_8 #define S1_PORT GPIOA #define S2_PIN GPIO_Pin_10 #define S2_PORT GPIOA #define S3_PIN GPIO_Pin_11 #define S3_PORT GPIOA #define LED0_PIN GPIO_Pin_0 #define LED1_PIN GPIO_Pin_1 #define LED2_PIN GPIO_Pin_2 // PA2 #define LED3_PIN GPIO_Pin_10 // PB10 #define LED4_PIN GPIO_Pin_11 // PB11 #define LED5_PIN GPIO_Pin_13 // PB13 #define LED6_PIN GPIO_Pin_14 // PB14 #define LED7_PIN GPIO_Pin_15 // PB15 #define LED_PORT_MAIN GPIOB #define LED_PORT_ALT GPIOA #define BUZZER_PIN GPIO_Pin_13 #define BUZZER_PORT GPIOC #define LIGHT_SENSOR_PIN GPIO_Pin_1 #define LIGHT_SENSOR_PORT GPIOA // 全局变量 uint8_t led_mode = 0; // 0:全灭, 1:正向流水, 2:反向流水 uint8_t breath_mode = 0; // 0:关闭, 1:呼吸灯, 2:全亮 uint8_t beep_mode = 0; // 0:关闭, 1:滴答声 uint8_t oled_mode = 0; // 0:关闭, 1:显示信息 uint32_t flow_counter = 0; uint32_t breath_counter = 0; uint32_t beep_counter = 0; uint32_t oled_refresh_counter = 0; // PWM控制变量 uint8_t pwm_duty = 0; int8_t pwm_dir = 1; // 设置单个LED状态 void Set_LED(uint8_t index, GPIO_PinState state) { switch(index) { case 0: GPIO_WriteBit(LED_PORT_MAIN, LED0_PIN, state); break; case 1: GPIO_WriteBit(LED_PORT_MAIN, LED1_PIN, state); break; case 2: GPIO_WriteBit(LED_PORT_ALT, LED2_PIN, state); break; // PA2 case 3: GPIO_WriteBit(LED_PORT_MAIN, LED3_PIN, state); break; case 4: GPIO_WriteBit(LED_PORT_MAIN, LED4_PIN, state); break; case 5: GPIO_WriteBit(LED_PORT_MAIN, LED5_PIN, state); break; case 6: GPIO_WriteBit(LED_PORT_MAIN, LED6_PIN, state); break; case 7: GPIO_WriteBit(LED_PORT_MAIN, LED7_PIN, state); break; } } // 关闭所有LED void All_LEDs_Off(void) { for (uint8_t i = 0; i < 8; i++) { Set_LED(i, Bit_RESET); } } // PWM初始化 - 基于您的代码 void PWM_Init(void) { // 1. 使能时钟 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); // 2. 配置GPIO GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; // PA0 (TIM2_CH1) GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); // 3. 配置定时器 TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInitStructure.TIM_Period = 100 - 1; // ARR: 100级PWM TIM_TimeBaseInitStructure.TIM_Prescaler = 720 - 1; // 72MHz/720 = 100kHz TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure); // 4. 配置PWM通道 TIM_OCInitTypeDef TIM_OCInitStructure; TIM_OCStructInit(&TIM_OCInitStructure); TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 0; // 初始占空比 TIM_OC1Init(TIM2, &TIM_OCInitStructure); // 5. 启动定时器 TIM_Cmd(TIM2, ENABLE); } // 设置PWM占空比 void PWM_SetCompare(uint16_t Compare) { TIM_SetCompare1(TIM2, Compare); } // 初始化ADC void ADC_Init(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOA, ENABLE); // 配置ADC引脚 (PA1) GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_Init(GPIOA, &GPIO_InitStructure); // 配置ADC ADC_InitTypeDef ADC_InitStructure; ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; ADC_InitStructure.ADC_ScanConvMode = DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfChannel = 1; ADC_Init(ADC1, &ADC_InitStructure); // 配置ADC通道 ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_55Cycles5); // 启用ADC ADC_Cmd(ADC1, ENABLE); // 校准ADC ADC_ResetCalibration(ADC1); while(ADC_GetResetCalibrationStatus(ADC1)); ADC_StartCalibration(ADC1); while(ADC_GetCalibrationStatus(ADC1)); // 启动转换 ADC_SoftwareStartConvCmd(ADC1, ENABLE); } // 读取ADC值 uint16_t ADC_Read(void) { while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET); return ADC_GetConversionValue(ADC1); } // 系统时钟初始化 void SystemClock_Config(void) { // 使用默认时钟配置 (72MHz HCLK) SystemInit(); } // 初始化所有外设 void Peripherals_Init(void) { // 1. 初始化系统时钟 SystemClock_Config(); // 2. 初始化GPIO GPIO_InitTypeDef GPIO_InitStructure; // 使能时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE); // 配置按键S0 (PA0) - 上拉模式 GPIO_InitStructure.GPIO_Pin = S0_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init(S0_PORT, &GPIO_InitStructure); // 配置按键S1-S3 (PA8, PA10, PA11) - 下拉模式 GPIO_InitStructure.GPIO_Pin = S1_PIN | S2_PIN | S3_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; GPIO_Init(GPIOA, &GPIO_InitStructure); // 配置LED (PB0, PB1, PB10, PB11, PB13, PB14, PB15) GPIO_InitStructure.GPIO_Pin = LED0_PIN | LED1_PIN | LED3_PIN | LED4_PIN | LED5_PIN | LED6_PIN | LED7_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(LED_PORT_MAIN, &GPIO_InitStructure); // 配置LED2 (PA2) GPIO_InitStructure.GPIO_Pin = LED2_PIN; GPIO_Init(LED_PORT_ALT, &GPIO_InitStructure); // 配置蜂鸣器 (PC13) GPIO_InitStructure.GPIO_Pin = BUZZER_PIN; GPIO_Init(BUZZER_PORT, &GPIO_InitStructure); // 3. 初始化PWM PWM_Init(); // 4. 初始化ADC ADC_Init(); // 5. 初始化OLED OLED_Init(); OLED_Clear(); OLED_Display_Off(); // 初始状态:全部关闭 All_LEDs_Off(); GPIO_WriteBit(BUZZER_PORT, BUZZER_PIN, Bit_RESET); } // 按键扫描函数 void Key_Scan(void) { static uint32_t last_tick = 0; uint32_t current_tick = HAL_GetTick(); if (current_tick - last_tick < 200) return; last_tick = current_tick; // S0: LED流水灯模式切换 if (GPIO_ReadInputDataBit(S0_PORT, S0_PIN) == Bit_RESET) { led_mode = (led_mode + 1) % 3; breath_mode = 0; // 关闭呼吸灯 flow_counter = 0; All_LEDs_Off(); PWM_SetCompare(0); // 关闭PWM输出 while (GPIO_ReadInputDataBit(S0_PORT, S0_PIN) == Bit_RESET); // 等待释放 } // S1: LED呼吸灯控制 if (GPIO_ReadInputDataBit(S1_PORT, S1_PIN) == Bit_SET) { breath_mode = (breath_mode + 1) % 3; led_mode = 0; // 关闭流水灯 if (breath_mode == 2) { for (uint8_t i = 0; i < 8; i++) Set_LED(i, Bit_SET); } else { All_LEDs_Off(); } while (GPIO_ReadInputDataBit(S1_PORT, S1_PIN) == Bit_SET); // 等待释放 } // S2: 蜂鸣器控制 if (GPIO_ReadInputDataBit(S2_PORT, S2_PIN) == Bit_SET) { beep_mode = !beep_mode; if (!beep_mode) GPIO_WriteBit(BUZZER_PORT, BUZZER_PIN, Bit_RESET); beep_counter = 0; while (GPIO_ReadInputDataBit(S2_PORT, S2_PIN) == Bit_SET); // 等待释放 } // S3: OLED显示控制 if (GPIO_ReadInputDataBit(S3_PORT, S3_PIN) == Bit_SET) { oled_mode = !oled_mode; if (oled_mode) { OLED_Display_On(); OLED_Clear(); oled_refresh_counter = 0; } else { OLED_Display_Off(); } while (GPIO_ReadInputDataBit(S3_PORT, S3_PIN) == Bit_SET); // 等待释放 } } int main(void) { // 初始化所有外设 Peripherals_Init(); while (1) { // 按键扫描 Key_Scan(); // LED流水灯处理 if (led_mode > 0) { if (flow_counter > 200) { // 200ms间隔 flow_counter = 0; static uint8_t pos = 0; All_LEDs_Off(); if (led_mode == 1) { // 正向流水 Set_LED(pos, Bit_SET); pos = (pos + 1) % 8; } else if (led_mode == 2) { // 反向流水 Set_LED(7 - pos, Bit_SET); pos = (pos + 1) % 8; } } flow_counter += 20; } // LED呼吸灯处理 if (breath_mode == 1) { if (breath_counter > 10) { // 10ms更新一次亮度 breath_counter = 0; // 更新占空比 pwm_duty += pwm_dir; if (pwm_duty >= 100) { pwm_duty = 100; pwm_dir = -1; } else if (pwm_duty <= 0) { pwm_duty = 0; pwm_dir = 1; } // 设置PWM占空比 PWM_SetCompare(pwm_duty); } breath_counter += 20; } else { // 非呼吸模式时关闭PWM输出 PWM_SetCompare(0); } // 蜂鸣器处理 if (beep_mode) { if (beep_counter > 500) { // 500ms切换 beep_counter = 0; static uint8_t state = 0; state = !state; GPIO_WriteBit(BUZZER_PORT, BUZZER_PIN, state ? Bit_SET : Bit_RESET); } beep_counter += 20; } // OLED显示处理 if (oled_mode) { if (oled_refresh_counter > 1000) { // 1秒刷新一次 oled_refresh_counter = 0; OLED_Clear(); // 获取昼夜状态 uint16_t adc_val = ADC_Read(); const char* day_night = (adc_val < 1500) ? "Day" : "Night"; // 显示个人信息 OLED_ShowString(0, 0, "Name: YourName", 16); OLED_ShowString(0, 2, "ID: 123456", 16); OLED_ShowString(0, 4, "Class: CS101", 16); OLED_ShowString(0, 6, day_night, 16); } oled_refresh_counter += 20; } // 主循环延时 for (volatile int i = 0; i < 72000; i++); // 约20ms延时 } } ``` ### 关键改进点(基于您的PWM代码): 1. **PWM初始化优化**: ```c void PWM_Init(void) { // 使用TIM2通道1 (PA0) TIM_TimeBaseInitStructure.TIM_Period = 100 - 1; // 100级PWM TIM_TimeBaseInitStructure.TIM_Prescaler = 720 - 1; // 72MHz/720 = 100kHz TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OC1Init(TIM2, &TIM_OCInitStructure); } ``` 2. **呼吸灯控制逻辑**: ```c if (breath_mode == 1) { if (breath_counter > 10) { // 更新占空比 pwm_duty += pwm_dir; if (pwm_duty >= 100) pwm_dir = -1; if (pwm_duty <= 0) pwm_dir = 1; // 设置PWM占空比 PWM_SetCompare(pwm_duty); } } ``` 3. **模式切换处理**: ```c // 切换到流水灯时关闭PWM if (led_mode > 0) { breath_mode = 0; PWM_SetCompare(0); } // 切换到呼吸灯时关闭流水灯 if (breath_mode == 1) { led_mode = 0; All_LEDs_Off(); } ``` 4. **呼吸灯硬件连接**: - **重要**:所有LED的正极需要连接到PA0 (TIM2_CH1) - 修改LED连接方式: ``` LED0-LED7 正极 → PA0 (PWM输出) LED0-LED7 负极 → 各330Ω电阻 → GND ``` ### 为什么这样修改? 1. **硬件PWM优势**: - 精确控制,无闪烁 - 不占用CPU资源 - 可实现平滑的呼吸效果 - 适用于驱动多个LED(并联) 2. **整合您的实现**: - 保留了您原有的PWM设置参数 - 使用相同的PWM周期(100)和预分频(720) - 保持呼吸灯变化速度(10ms更新) 3. **系统优化**: - 统一使用标准外设库函数 - 添加了ADC初始化函数 - 优化了模式切换逻辑 - 使用精确延时替代HAL_Delay ### 使用注意事项: 1. **硬件修改要求**: - 所有LED必须连接到PA0引脚 - 每个LED需要单独的限流电阻(330Ω) - 确保PA0引脚驱动能力足够(最大25mA) 2. **呼吸灯效果调整**: - 修改PWM周期值改变分辨率: ```c TIM_TimeBaseInitStructure.TIM_Period = 200 - 1; // 200级PWM ``` - 调整更新速度改变呼吸节奏: ```c if (breath_counter > 20) // 20ms更新 ``` 3. **电流考虑**: - 8个LED并联时,总电流可能超过PA0引脚的驱动能力 - 建议添加晶体管驱动电路: ``` PA0 → 1kΩ电阻 → NPN晶体管基极 晶体管集电极 → LED → 电阻 → VCC 晶体管发射极 → GND ``` 出错了*** Using Compiler 'V5.06 update 7 (build 960)', folder: 'D:\Keil_v5\ARM\ARMCC\Bin' Build target 'Target 1' compiling LightSensor.c... Hardware\lightsensor.h(6): error: #3: #include file "Hardware\lightsensor.h" includes itself #include "lightsensor.h" Hardware\LightSensor.c: 0 warnings, 1 error compiling OLED.c... Hardware\myi2c.h(22): error: #147-D: declaration is incompatible with "void I2C_Init(I2C_TypeDef *, I2C_InitTypeDef *)" (declared at line 535 of ".\Library\stm32f10x_i2c.h") void I2C_Init(void); Hardware\OLED.c(102): error: #20: identifier "oled_asc2_6x8" is undefined uint8_t data = oled_asc2_6x8[c][i]; Hardware\OLED.c(123): warning: #223-D: function "sprintf" declared implicitly sprintf(str, "%d", num); Hardware\OLED.c: 1 warning, 2 errors compiling myi2c.c... Hardware\myi2c.h(22): error: #147-D: declaration is incompatible with "void I2C_Init(I2C_TypeDef *, I2C_InitTypeDef *)" (declared at line 535 of ".\Library\stm32f10x_i2c.h") void I2C_Init(void); Hardware\myi2c.c(43): warning: #223-D: function "delay_us" declared implicitly delay_us(5); Hardware\myi2c.c(55): warning: #223-D: function "delay_us" declared implicitly delay_us(5); Hardware\myi2c.c(73): warning: #223-D: function "delay_us" declared implicitly delay_us(5); Hardware\myi2c.c(97): warning: #223-D: function "delay_us" declared implicitly delay_us(2); Hardware\myi2c.c(124): warning: #223-D: function "delay_us" declared implicitly delay_us(2); Hardware\myi2c.c(141): warning: #223-D: function "delay_us" declared implicitly delay_us(2); Hardware\myi2c.c: 6 warnings, 1 error compiling main.c... User\main.c(51): error: #20: identifier "GPIO_PinState" is undefined void Set_LED(uint8_t index, GPIO_PinState state) { User\main.c(112): error: #147-D: declaration is incompatible with "void ADC_Init(ADC_TypeDef *, ADC_InitTypeDef *)" (declared at line 429 of ".\Library\stm32f10x_adc.h") void ADC_Init(void) { User\main.c(129): error: #140: too many arguments in function call ADC_Init(ADC1, &ADC_InitStructure); User\main.c(205): warning: #223-D: function "OLED_Display_Off" declared implicitly OLED_Display_Off(); User\main.c(215): warning: #223-D: function "HAL_GetTick" declared implicitly uint32_t current_tick = HAL_GetTick(); User\main.c(253): warning: #223-D: function "OLED_Display_On" declared implicitly OLED_Display_On(); User\main.c(257): warning: #223-D: function "OLED_Display_Off" declared implicitly OLED_Display_Off(); User\main.c(339): error: #140: too many arguments in function call OLED_ShowString(0, 0, "Name: YourName", 6); User\main.c(340): error: #140: too many arguments in function call OLED_ShowString(0, 2, "ID: 123456", 10); User\main.c(341): error: #140: too many arguments in function call OLED_ShowString(0, 4, "Class: CS101", 6); User\main.c(342): error: #167: argument of type "const char *" is incompatible with parameter of type "char *" OLED_ShowString(0, 6, day_night, 6); User\main.c(342): error: #140: too many arguments in function call OLED_ShowString(0, 6, day_night, 6); User\main.c: 4 warnings, 8 errors ".\Objects\Project.axf" - 12 Error(s), 11 Warning(s). Target not created. Build Time Elapsed: 00:00:05这几个地方为什么错了
最新发布
07-12
/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file : main.c * @brief : Main program body ****************************************************************************** * @attention »ùÓÚSTM32µÄÍòÄêÀú Ч¹ûÑÝʾÊÓÆµÁ´½Ó£ºhttps://www.bilibili.com/video/BV11w4m1a74J/ ÓÎϷЧ¹û¿ÉÒÔ ¹Ø×¢ ΢ÐŹ«Öںš¢BÕ¾¡¢¶¶ÒôºÍ¿ìÊÖµÈÆ½Ì¨£¨Õ˺ÅÃû³Æ£ºJLµ¥Æ¬»ú£©£¬ÓÐÂ¼ÖÆµÄÊÓÆµµÄЧ¹û¡£ Ò²·ÖÏíÁËÆäËûºÃÍæÓÐȤµÄ´úÂ룬¶¼¿ÉÒÔȥ΢ÐŹ«ÖÚºÅÏÂÔØ¡£¸ÐÐËȤµÄ¿ÉÒÔ¿´Ï¡£ ²ÄÁÏ£º STM32F103C8T6×îСϵͳ°å SSD1306 OLED128*64ÏÔʾÆÁ 6¸ö°´¼ü IO½ÓÏß˵Ã÷£º Ïòϰ´¼ü£ºPA4 ÏòÓÒ°´¼ü£ºPA5 ÏòÉϰ´¼ü£ºPB11 Ïò×ó°´¼ü£ºPB10 F1°´¼ü£ºPA6 OLED SCK£ºPA11 OLED SDA£ºPA12 * Copyright (c) 2024 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "main.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include "OLED.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ #define LEFT_BUTTON 1 #define RIGHT_BUTTON 2 #define UP_BUTTON 3 #define DOWN_BUTTON 4 #define A_BUTTON 5 #define B_BUTTON 6 /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN PV */ uint32_t yr = 2024; uint8_t mo = 7; /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); static void MX_GPIO_Init(void); /* USER CODE BEGIN PFP */ /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ unsigned char isLeapYear(uint32_t yr) { if (yr % 100) return (yr % 400) == 0; else return (yr % 4) == 0; } uint8_t getDaysOfMonth(uint32_t yr, uint8_t mo) { return (mo == 2) ? (28 + isLeapYear(yr)) : 31 - (mo - 1) % 7 % 2; } uint8_t getDayOfWeek(uint32_t yr, uint8_t mo, uint8_t d) { return (d += mo < 3 ? yr-- : yr - 2, 23 * mo / 9 + d + 4 + yr / 4 - yr / 100 + yr / 400) % 7; } int get_key_input() { if(HAL_GPIO_ReadPin(left_key_GPIO_Port, left_key_Pin)==0){ HAL_Delay(100); if(HAL_GPIO_ReadPin(left_key_GPIO_Port, left_key_Pin)==0) return LEFT_BUTTON; } if(HAL_GPIO_ReadPin(right_key_GPIO_Port, right_key_Pin)==0) { HAL_Delay(100); if(HAL_GPIO_ReadPin(right_key_GPIO_Port, right_key_Pin)==0) return RIGHT_BUTTON; } if(HAL_GPIO_ReadPin(up_key_GPIO_Port, up_key_Pin)==0) { HAL_Delay(100); if(HAL_GPIO_ReadPin(up_key_GPIO_Port, up_key_Pin)==0) return UP_BUTTON; } if(HAL_GPIO_ReadPin(down_key_GPIO_Port, down_key_Pin)==0) { HAL_Delay(100); if(HAL_GPIO_ReadPin(down_key_GPIO_Port, down_key_Pin)==0) return DOWN_BUTTON; } if(HAL_GPIO_ReadPin(f1_key_GPIO_Port, f1_key_Pin)==0) { HAL_Delay(100); if(HAL_GPIO_ReadPin(f1_key_GPIO_Port, f1_key_Pin)==0) return A_BUTTON; } if(HAL_GPIO_ReadPin(f2_key_GPIO_Port, f2_key_Pin)==0) { HAL_Delay(100); if(HAL_GPIO_ReadPin(f2_key_GPIO_Port, f2_key_Pin)==0) return B_BUTTON; } return 0; } void update() { switch (get_key_input()) { case LEFT_BUTTON: if (--yr < 1970) yr = 1970; break; case RIGHT_BUTTON: if (++yr > 2199) yr = 2199; break; case UP_BUTTON: if (++mo > 12) { if (++yr > 2199) { yr = 2199; mo = 12; } else { mo = 1; } } break; case DOWN_BUTTON: if (--mo < 1) { if (--yr < 1970) { yr = 1970; mo = 1; } else { mo = 12; } } break; case A_BUTTON: yr = 2024; mo = 7; break; case B_BUTTON: break; } } void draw() { uint8_t days = getDaysOfMonth(yr, mo); uint8_t day = getDayOfWeek(yr, mo, 1); OLED_ShowNum(40, 0, yr, 4, OLED_6X8); OLED_ShowNum(70, 0, mo, 2, OLED_6X8); OLED_ShowString(4, 8, "SU MO TU WE TH FR SA", OLED_6X8); uint8_t x = day; uint8_t y = 0; for (uint8_t i = 1; i <= days; ++i) { if (i < 10) { OLED_ShowNum(x * 18 + 4 + 6, y * 8 + 16, i, 1, OLED_6X8); } else { OLED_ShowNum(x * 18 + 4, y * 8 + 16, i, 2, OLED_6X8); } if (++x == 7) { x = 0; ++y; } } } /* USER CODE END 0 */ /** * @brief The application entry point. * @retval int */ int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration--------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); /* USER CODE BEGIN 2 */ OLED_Init(); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while(get_key_input()==0); while (1) { OLED_Clear(); update(); draw(); OLED_Update(); /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ } /** * @brief System Clock Configuration * @retval None */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /** Initializes the RCC Oscillators according to the specified parameters * in the RCC_OscInitTypeDef structure. */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /** Initializes the CPU, AHB and APB buses clocks */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) { Error_Handler(); } } /** * @brief GPIO Initialization Function * @param None * @retval None */ static void MX_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStruct = {0}; /* USER CODE BEGIN MX_GPIO_Init_1 */ /* USER CODE END MX_GPIO_Init_1 */ /* GPIO Ports Clock Enable */ __HAL_RCC_GPIOD_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(GPIOA, scl_Pin|sda_Pin, GPIO_PIN_SET); /*Configure GPIO pins : down_key_Pin right_key_Pin f1_key_Pin f2_key_Pin */ GPIO_InitStruct.Pin = down_key_Pin|right_key_Pin|f1_key_Pin|f2_key_Pin; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_PULLUP; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /*Configure GPIO pins : left_key_Pin up_key_Pin */ GPIO_InitStruct.Pin = left_key_Pin|up_key_Pin; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_PULLUP; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); /*Configure GPIO pins : scl_Pin sda_Pin */ GPIO_InitStruct.Pin = scl_Pin|sda_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /* USER CODE BEGIN MX_GPIO_Init_2 */ /* USER CODE END MX_GPIO_Init_2 */ } /* USER CODE BEGIN 4 */ /* USER CODE END 4 */ /** * @brief This function is executed in case of error occurrence. * @retval None */ void Error_Handler(void) { /* USER CODE BEGIN Error_Handler_Debug */ /* User can add his own implementation to report the HAL error return state */ __disable_irq(); while (1) { } /* USER CODE END Error_Handler_Debug */ } #ifdef USE_FULL_ASSERT /** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name * @param line: assert_param error line source number * @retval None */ void assert_failed(uint8_t *file, uint32_t line) { /* USER CODE BEGIN 6 */ /* User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT */ 学习代码
06-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值