hal_init(哈利你特魔法觉醒不能换发型了)

http://www.itjxue.com  2023-01-28 00:24  来源:未知  点击次数: 

如何实现F411RE使用HAL库实现IO中断

各位:

我在NUCLEO-F411RE的板子平台上基于HAL库调试程序,想实现按键中断(PC13),但试验了多次都失败了,麻烦高手帮忙看下哪里出了问题,谢谢!代码如下:

IO初始化部分,位于main.c中

HAL_Init();

SystemClock_Config();

__HAL_RCC_GPIOA_CLK_ENABLE();

GPIO_InitStruct.Pin = GPIO_PIN_5;

GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

GPIO_InitStruct.Pull = GPIO_PULLUP;

GPIO_InitStruct.Speed = GPIO_SPEED_FAST;

HAL_GPIO_Init(GPIOA, GPIO_InitStruct);

__HAL_RCC_GPIOC_CLK_ENABLE();

GPIO_InitStruct.Pin = GPIO_PIN_13;

GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;

GPIO_InitStruct.Pull = GPIO_NOPULL;

HAL_GPIO_Init(GPIOC, GPIO_InitStruct);

HAL_NVIC_SetPriority(EXTI0_IRQn, 2, 0);

HAL_NVIC_EnableIRQ(EXTI0_IRQn);

中断stm32f4xx_it.c中程序

void EXTI0_IRQHandler(void)

{

HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_13);

}

HAL_GPIO_EXTI_Callback函数,位于main.c中

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)

{

if(GPIO_Pin == GPIO_PIN_13)

{

}

HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);

HAL_Delay(50)

配置PB15成推挽输出,写出相应的初始化函数,并指出哪个是结构体变量?

下面是使用 STM32CubeMX 软件生成的 PB15 推挽输出的初始化函数的示例:

void MX_GPIO_Init(void)

{

GPIO_InitTypeDef GPIO_InitStruct = {0};

/* GPIO Ports Clock Enable */

__HAL_RCC_GPIOC_CLK_ENABLE();

__HAL_RCC_GPIOH_CLK_ENABLE();

__HAL_RCC_GPIOB_CLK_ENABLE();

/*Configure GPIO pin Output Level */

HAL_GPIO_WritePin(GPIOC, LD4_Pin|LD3_Pin, GPIO_PIN_RESET);

/*Configure GPIO pin Output Level */

HAL_GPIO_WritePin(GPIOB, PB15_Pin, GPIO_PIN_RESET);

/*Configure GPIO pin : B1_Pin */

GPIO_InitStruct.Pin = B1_Pin;

GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;

GPIO_InitStruct.Pull = GPIO_NOPULL;

HAL_GPIO_Init(B1_GPIO_Port, GPIO_InitStruct);

/*Configure GPIO pins : LD4_Pin LD3_Pin */

GPIO_InitStruct.Pin = LD4_Pin|LD3_Pin;

GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

GPIO_InitStruct.Pull = GPIO_NOPULL;

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

HAL_GPIO_Init(GPIOC, GPIO_InitStruct);

/*Configure GPIO pin : PB15_Pin */

GPIO_InitStruct.Pin = PB15_Pin;

GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

GPIO_InitStruct.Pull = GPIO_NOPULL;

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

HAL_GPIO_Init(PB15_GPIO_Port, GPIO_InitStruct);

}

在上面的代码中,GPIO_InitTypeDef 是一个结构体类型,GPIO_InitStruct 是一个 GPIO_InitTypeDef 类型的结构体变量。

stm32f_open失败

STM32用CubeMX创建SDIO+FatFs,f_Open失败

如题,具体环境是CubeMX最新版,HAL库最新版,MDK5.24a,STLINKv2-1,板子是STM32F407Vet6核心板(某宝四五十块钱)。

SDIO单独测试TF卡(4G卡肯定不是正版)成功,可以读出CSD,CID,卡的状态,卡的容量等,SDIO四线无DMA读写正常。

本人前前后后试过无数次,好几个月,现在不得不弄好!感谢大佬的帮助!

CubeMX:SDIO四线,无DMA,无SDIO全局中断,勾选FatFS文件系统,文件系统加入长名STACK,单片机HEAP-0x800,STACK-0x1000

具体代码:

主程序:

/* USER CODE BEGIN PV */

/* Private variables ---------------------------------------------------------*/

FRESULT res; /* FatFs function common retSDult code */

uint32_t byteswritten, bytesread; /* File write/read counts */

uint8_t wtext[] = "This is STM32 working with FatFs"; /* File write buffer */

uint8_t rtext[100]; /* File read buffer */

/* USER CODE END PV */

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();

MX_SDIO_SD_Init();

MX_USART2_UART_Init();

MX_FATFS_Init();

/* USER CODE BEGIN 2 */

****************************************************

从HAL库中F4Disco里抄来的代码

****************************************************

if(f_mount(SDFatFS, (TCHAR const*)SDPath, 0) != FR_OK)

{

/* FatFs Initialization Error */

Error_Handler();

}

else

{

/* Create and Open a new text file object with write access */

if(f_open(SDFile, "STM32.TXT", FA_CREATE_ALWAYS | FA_WRITE) != FR_OK)

{

/* 'STM32.TXT' file Open for write Error */

Error_Handler();

}

else

{

/* Write data to the text file */

res = f_write(SDFile, wtext, sizeof(wtext), (void *)byteswritten);

if((byteswritten == 0) || (retSD != FR_OK))

{

/* 'STM32.TXT' file Write or EOF Error */

Error_Handler();

}

else

{

/* Close the open text file */

f_close(SDFile);

/* Open the text file object with read access */

if(f_open(SDFile, "STM32.TXT", FA_READ) != FR_OK)

{

/* 'STM32.TXT' file Open for read Error */

Error_Handler();

}

else

{

/* Read data from the text file */

res = f_read(SDFile, rtext, sizeof(rtext), (void *)bytesread);

if((bytesread == 0) || (retSD != FR_OK))

{

/* 'STM32.TXT' file Read or EOF Error */

Error_Handler();

}

else

{

/* Close the open text file */

f_close(SDFile);

/* Compare read data with the expected data */

if((bytesread != byteswritten))

{

/* Read data is different from the expected data */

Error_Handler();

}

else

{

/* Success of the demo: no error occurrence */

HAL_GPIO_WritePin(GPIOA, D2_Pin|D3_Pin, GPIO_PIN_SET);

}

}

}

}

}

}

/* Unlink the USB disk I/O driver */

FATFS_UnLinkDriver(SDPath);

/* USER CODE END 2 */

/* Infinite loop */

/* USER CODE BEGIN WHILE */

while (1)

{

/* USER CODE END WHILE */

/* USER CODE BEGIN 3 */

}

/* USER CODE END 3 */

}

****************************************************

单步调试结果 Sd_diskio.c中死循环

****************************************************

DRESULT SD_read(BYTE lun, BYTE *buff, DWORD sector, UINT count)

{

DRESULT res = RES_ERROR;

ReadStatus = 0;

uint32_t timeout;

#if (ENABLE_SD_DMA_CACHE_MAINTENANCE == 1)

uint32_t alignedAddr;

#endif

if(BSP_SD_ReadBlocks_DMA((uint32_t*)buff,

(uint32_t) (sector),

count) == MSD_OK)

{

/* Wait that the reading process is completed or a timeout occurs */

timeout = HAL_GetTick();

while((ReadStatus == 0) ((HAL_GetTick() - timeout) SD_TIMEOUT))//在此处无限循环

{

}

/* incase of a timeout return error */

if (ReadStatus == 0)

{

res = RES_ERROR;

}

else

{

ReadStatus = 0;

timeout = HAL_GetTick();

while((HAL_GetTick() - timeout) SD_TIMEOUT)

{

if (BSP_SD_GetCardState() == SD_TRANSFER_OK)

{

res = RES_OK;

#if (ENABLE_SD_DMA_CACHE_MAINTENANCE == 1)

/*

the SCB_InvalidateDCache_by_Addr() requires a 32-Byte aligned address,

adjust the address and the D-Cache size to invalidate accordingly.

*/

alignedAddr = (uint32_t)buff ~0x1F;

SCB_InvalidateDCache_by_Addr((uint32_t*)alignedAddr, count*BLOCKSIZE + ((uint3

STM32L476使用stop2模式进入低功耗后功3uA,RTC唤醒后功耗立马增大到140mA,请问这是哪里的原因?

功耗来源:

1 设备工作,比如你的LED 灯如果打开了,会产生多少功耗。可以通过开关这个LED来测量。

2 GPIO 工作模式, 印象中 浮空输入功耗最低,其他模式可能会略高。

3 时钟频率越高,功耗越大,你这里好像没有动时钟,应该不会是这里。

4 如果时候瞬时升高后又降下来,考虑一下电路设计,是否产生浪涌电流,

能想到就这么多了。

dsp csl_init调用卡死

需要手动加入库文件:C:/CCStudio_v3.1/C6000/csl/lib/cslDM642.lib

/*

* 功能: CSL中cache module和timer module的使用示例

* 说明: 需要手动加入库文件:C:/CCStudio_v3.1/C6000/csl/lib/cslDM642.lib,建议到TI网站下载最新的CSL库更新,否则有些模块可能会出问题

* 设计者: 3881

* 日期: 2010-5-28

*/

#include csl.h //顶层应用程序模块,用于初始化CSL。

main初始化部分,分三部分代码麻烦分析,main调用,BSP部分,GUI初始化部分

div class="blockcode"blockquoteint main(void)

{

/* STM32F4xx HAL library initialization:

- Configure the Flash prefetch, instruction and Data caches

- Configure the Systick to generate an interrupt each 1 msec

- Set NVIC Group Priority to 4

- Global MSP (MCU Support Package) initialization

*/

HAL_Init();

/* Configure the system clock to 180 MHz */

SystemClock_Config();

BSP_Config();

/* Configure LED1 and LED3 */

BSP_LED_Init(LED1);

BSP_LED_Init(LED3);

file_isok = FatFS_Init();

/* Thread 1 definition */

osThreadDef(LED1, LED_Thread1, osPriorityNormal, 0, configMINIMAL_STACK_SIZE);

/* Thread 2 definition */

osThreadDef(LED3, LED_Thread2, osPriorityNormal, 0, configMINIMAL_STACK_SIZE);

osThreadDef(GUI_TASKID, GUI_Thread, osPriorityNormal, 0, 2048);

/* Start thread 1 */

LEDThread1Handle = osThreadCreate (osThread(LED1), NULL);

/* Start thread 2 */

LEDThread2Handle = osThreadCreate (osThread(LED3), NULL);

GUIThreadHandle = osThreadCreate (osThread(GUI_TASKID), NULL);

//init_gui();

/* Start scheduler */

osKernelStart();

/* We should never get here as control is now taken by the scheduler */

for(;;);

}

void BSP_Config(void)

{

/* Initializes the SDRAM device */

BSP_SDRAM_Init();

/* Initialize the Touch screen */

BSP_TS_Init(800, 480);

//BSP_TS_Init(320, 240);

/* Enable the CRC Module */

__HAL_RCC_CRC_CLK_ENABLE();

__HAL_RCC_BKPSRAM_CLK_ENABLE();

/* Compute the prescaler value to have TIM3 counter clock equal to 10 KHz */

uwPrescalerValue = (uint32_t) ((SystemCoreClock /2) / 10000) - 1;

/* Set TIMx instance */

TimHandle.Instance = TIM3;

/* Initialize TIM3 peripheral as follows:

+ Period = 500 - 1

+ Prescaler = ((SystemCoreClock/2)/10000) - 1

+ ClockDivision = 0

+ Counter direction = Up

*/

TimHandle.Init.Period = 500 - 1;

TimHandle.Init.Prescaler = uwPrescalerValue;

TimHandle.Init.ClockDivision = 0;

TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;

if(HAL_TIM_Base_Init(TimHandle) != HAL_OK)

{

while(1)

{

}

}

/*##-2- Start the TIM Base generation in interrupt mode ####################*/

/* Start Channel1 */

if(HAL_TIM_Base_Start_IT(TimHandle) != HAL_OK)

{

while(1)

{

}

}

}

外部中断包含io口引脚中断吗

包含

IO口外部中断:

(1) STM32的每个IO都可以作为外部中断输入。

(2) 每个外部中断线可以独立的配置触发方式(上升沿,下降沿或者双边沿触发),触发/屏蔽,专用的状态位。

STM32供IO使用的中断线只有16个,但是STM32F系列的IO口多达上百个,STM32F103ZGT6(112),那么中断线怎么跟IO口对应呢?

SYSCFG_EXTICR1 寄存器中有EXTI0~EXTI4,EXTI0[3:0] 有四位,可以有16个序号为0的IO引脚与EXTI0相连。其他依次类推。

16个中断线分配7个中断函数

二、IO口外部中断的一般配置步骤:

(1) 在用户定义的EXTI_Init( )函数中,使能IO口时钟。

(2) 调用HAL_GPIO_Init( )初始化IO口,设置触发方式。

(3) 设置中断优先级,并使能中断通道。

(4) 在中断服务函数void EXTI0~4_IRQHandler( ) 中调用外部中断通用处理函数HAL_GPIO_EXTI_IRQHandler( )。

(5) 在外部中断回调函数:HAL_GPIO_EXTI_Callback( )中编写我们真正的控制逻辑。

(责任编辑:IT教学网)

更多

相关微软认证文章

推荐微软认证文章