新手上路
- 积分
- 32
- 金钱
- 32
- 注册时间
- 2019-3-27
- 在线时间
- 11 小时
|
5金钱
我照着原子哥的视频把串口1换成了串口2,结果无法返回发送的数据,自己找了半天也找不到哪里出了错,大佬们帮忙看看程序代码。
[mw_shl_code=c,true]#include "sys.h"
#include "delay.h"
#include "usart.h"
u8 rdata[1];
//UART_HandleTypeDef usart1_handler;
UART_HandleTypeDef usart2_handler;
/*void uart1_init() //串口1初始化
{
usart1_handler.Instance=USART1;
usart1_handler.Init.BaudRate=115200;
usart1_handler.Init.WordLength=UART_WORDLENGTH_8B;
usart1_handler.Init.StopBits=UART_STOPBITS_1;
usart1_handler.Init.HwFlowCtl=UART_HWCONTROL_NONE;
usart1_handler.Init.Mode=UART_MODE_TX_RX;
usart1_handler.Init.Parity=UART_PARITY_NONE;
HAL_UART_Init(&usart1_handler);
}*/
void uart2_init() //串口2初始化
{
usart2_handler.Instance=USART2;
usart2_handler.Init.BaudRate=115200;
usart2_handler.Init.WordLength=UART_WORDLENGTH_8B;
usart2_handler.Init.StopBits=UART_STOPBITS_1;
usart2_handler.Init.HwFlowCtl=UART_HWCONTROL_NONE;
usart2_handler.Init.Mode=UART_MODE_TX_RX;
usart2_handler.Init.Parity=UART_PARITY_NONE;
HAL_UART_Init(&usart2_handler);
}
void HAL_UART_MspInit(UART_HandleTypeDef *huart)
{
GPIO_InitTypeDef GPIO_Initure;
/* if(huart->Instance==USART1)
{
__HAL_RCC_GPIOA_CLK_ENABLE(); //使能GPIOA时钟
__HAL_RCC_USART1_CLK_ENABLE(); //使能USART1时钟
GPIO_Initure.Pin=GPIO_PIN_9; //PA9
GPIO_Initure.Mode=GPIO_MODE_AF_PP; //复用推挽输出
GPIO_Initure.Pull=GPIO_PULLUP; //上拉
GPIO_Initure.Speed=GPIO_SPEED_FAST; //高速
GPIO_Initure.Alternate=GPIO_AF7_USART1; //复用为USART1
HAL_GPIO_Init(GPIOA,&GPIO_Initure); //初始化PA9
GPIO_Initure.Pin=GPIO_PIN_10; //PA10
HAL_GPIO_Init(GPIOA,&GPIO_Initure);
HAL_NVIC_SetPriority(USART1_IRQn,3,3);
HAL_NVIC_EnableIRQ(USART1_IRQn);
}*/
if(huart->Instance==USART2)
{
__HAL_RCC_GPIOA_CLK_ENABLE(); //使能GPIOA时钟
__HAL_RCC_USART2_CLK_ENABLE(); //使能USART2时钟
GPIO_Initure.Pin=GPIO_PIN_2; //PA2
GPIO_Initure.Mode=GPIO_MODE_AF_PP; //复用推挽输出
GPIO_Initure.Pull=GPIO_PULLUP; //上拉
GPIO_Initure.Speed=GPIO_SPEED_FAST; //高速
GPIO_Initure.Alternate=GPIO_AF7_USART2; //复用为USART2
HAL_GPIO_Init(GPIOA,&GPIO_Initure); //初始化PA2
GPIO_Initure.Pin=GPIO_PIN_3; //PA3
HAL_NVIC_SetPriority(USART2_IRQn,3,3);
HAL_NVIC_EnableIRQ(USART2_IRQn);
}
}
/*void USART1_IRQHandler(void)
{
HAL_UART_IRQHandler(&usart1_handler);
HAL_UART_Receive_IT(&usart1_handler,rdata,sizeof(rdata));
}*/
void USART2_IRQHandler(void)
{
HAL_UART_IRQHandler(&usart2_handler);
HAL_UART_Receive_IT(&usart2_handler,rdata,sizeof(rdata));
}
/*void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
u8 rec;
if(huart->Instance==USART1)
{
rec=*(--(huart->pRxBuffPtr));
HAL_UART_Transmit(&usart1_handler,&rec,sizeof(rec),1000);
}
}*/
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
u8 rec;
if(huart->Instance==USART2)
{
rec=*(--(huart->pRxBuffPtr));
HAL_UART_Transmit(&usart2_handler,&rec,1,1000);
}
}
int main(void)
{
//u8 buff[]="test";
HAL_Init(); //初始化HAL库
Stm32_Clock_Init(360,25,2,8); //设置时钟,180Mhz
delay_init(180);
//uart1_init();
uart2_init();
HAL_UART_Receive_IT(&usart2_handler,rdata,sizeof(rdata));
//HAL_UART_Receive_IT(&usart1_handler,rdata,sizeof(rdata));
while(1)
{
//HAL_UART_Transmit(&usart2_handler,buff,sizeof(buff),1000);
//delay_ms(300);
}
}
[/mw_shl_code]
|
最佳答案
查看完整内容[请看2#楼]
参考下这个767的8个串口:http://www.openedv.com/forum.php?mod=viewthread&tid=278777
|