在线时间858 小时
UID2073122
ST金币0
蝴蝶豆27
注册时间2015-5-22
论坛元老
- 最后登录
- 2020-12-9
|
a0a.1 32b0c
本帖最后由 原来的你 于 2016-8-7 20:31 编辑
int main(void)
{
/* This sample code shows how to use GPIO HAL API to toggle LED3 IO
in an infinite loop. */
/* STM32L4xx HAL library initialization:
- Configure the Flash prefetch
- Systick timer is configured by default as source of time base, but user
can eventually implement his proper time base source (a general purpose
timer for example or other time source), keeping in mind that Time base
duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
handled in milliseconds basis.
- Set NVIC Group Priority to 4
- Low Level Initialization
*/
HAL_Init();
/* Configure the system clock to 80 MHz */
SystemClock_Config();
/* -1- Enable GPIO Clock (to be able to program the configuration registers) */
LED3_GPIO_CLK_ENABLE();
/* -2- Configure IO in output push-pull mode to drive external LEDs */
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Pin = LED3_PIN;
HAL_GPIO_Init(LED3_GPIO_PORT, &GPIO_InitStruct);
/* -3- Toggle IO in an infinite loop */
while (1)
{
HAL_GPIO_TogglePin(LED3_GPIO_PORT, LED3_PIN);
/* Insert delay 100 ms */
HAL_Delay(100);
}
}
先附上官方的GPIO程序,一般来说对于一个单片机来说,我们最新接触就是用GPIO点个灯,这个我也一样,先看下官方的GPIO程序,官方是用HAL库,跟以前的标准库不一样,新的芯片都只有HAL,这是趋势,幸好我对ST特别熟悉,早就把HAL玩熟练了,所以上手也是很容易,先分析一下程序的逻辑。
HAL_Init();//使用HAL开始必要的初始化函数;
SystemClock_Config();//系统时钟配置函数,把系统时钟配置到最高80MHz;
LED3_GPIO_CLK_ENABLE();//使能驱动LED3的IO口时钟;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;//IO口作输出
GPIO_InitStruct.Pull = GPIO_PULLUP;//IO口上拉
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;//IO口输出速度为最高
GPIO_InitStruct.Pin = LED3_PIN;//选择GPIO引脚序号
HAL_GPIO_Init(LED3_GPIO_PORT, &GPIO_InitStruct);//初始化GPIO引脚
HAL_GPIO_TogglePin(LED3_GPIO_PORT, LED3_PIN);//翻转IO口电平
HAL_Delay(100);//延时100ms
先简单分析下GPIO的程序,大家可以跟着官方程序学习,后面会继续介绍各个自己在实际工程使用的程序细节分析
|
|