memcpy引起hardfault (Keil V5 + STM32)

Keil5中使用STM32F103的RS485通信,需要配置USART的中断函数。以下是一个示例代码: ``` #include "stm32f10x.h" volatile uint8_t rx_buf[256]; volatile uint8_t tx_buf[256]; volatile uint8_t rx_len = 0; volatile uint8_t tx_len = 0; void USART1_IRQHandler(void) { if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) { rx_buf[rx_len++] = USART_ReceiveData(USART1); if (rx_len >= 256) rx_len = 0; } if (USART_GetITStatus(USART1, USART_IT_TXE) != RESET) { if (tx_len > 0) { USART_SendData(USART1, tx_buf[0]); tx_buf++; tx_len--; } else { USART_ITConfig(USART1, USART_IT_TXE, DISABLE); } } } void USART1_Send(uint8_t *data, uint8_t len) { while (tx_len > 0) ; memcpy(tx_buf, data, len); tx_len = len; USART_ITConfig(USART1, USART_IT_TXE, ENABLE); } int main(void) { USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); USART_InitStructure.USART_BaudRate = 115200; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART1, &USART_InitStructure); USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); USART_Cmd(USART1, ENABLE); NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); while (1) { // do something } } ``` 在此示例中,我们使用`USART1`,并启用接收和发送中断。当接收到数据时,将数据存储在`rx_buf`缓冲区中,并增加`rx_len`计数器。当需要发送数据时,将数据复制到`tx_buf`缓冲区中,并将`tx_len`设置为数据长度。然后启用发送中断,并在中断处理程序中发送数据。当发送完成时,将禁用发送中断。 请注意,以上示例代码仅供参考,实际应用中需根据具体需求进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值