接收的数据一直是 FCFC
程序文件
main.c
void gd_eval_com_init(void)
{
/* enable GPIO clock */
usart_deinit(USART1);
usart_disable(USART1);
rcu_periph_clock_enable(RCU_GPIOD);//使能GPIOD时钟
/* enable USART clock */
rcu_periph_clock_enable(RCU_USART1);//使能USART1时钟
gpio_af_set(GPIOD, GPIO_AF_7, GPIO_PIN_5|GPIO_PIN_6);//复用功能7
gpio_mode_set(GPIOD, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_5);//PD5配置成串口输出
gpio_output_options_set(GPIOD, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_5);
gpio_mode_set(GPIOD, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_6);//PD6配置成串口输入
gpio_output_options_set(GPIOD, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_6);
/* USART configure */
usart_interrupt_enable(USART1,USART_INT_RBNE);
usart_baudrate_set(USART1,115200);
usart_word_length_set(USART1, USART_WL_8BIT);
usart_stop_bit_set(USART1, USART_STB_1BIT);
usart_parity_config(USART1, USART_PM_NONE);
usart_hardware_flow_coherence_config(USART1,USART_HCM_NONE);//无硬件流控
usart_transmit_config(USART1,USART_TRANSMIT_ENABLE);//发送使能
usart_receive_config(USART1,USART_RECEIVE_ENABLE);//接收使能
usart_data_first_config(USART1,USART_MSBF_LSB);//LSB模式
usart_enable(USART1);//使能串口
nvic_irq_enable(USART1_IRQn, 0, 0);//使能USART1中断
}
//关闭串口
int32_t uart_stop(void)
{
usart_disable(USART1);
return 0;
}
//串口发送
int32_t uart_send(uint8_t *data,uint32_t len)
{
uint32_t i = 0;
while(len--)
{
usart_data_transmit(USART1,data);
while(usart_flag_get(USART1,USART_FLAG_TBE) == RESET);
i++;
}
return 0;
}
int main(void)
{
uint8_t buff[4]={0xaa,0xbb,0x11,0x22};
systick_config();//配置系统主频168M,外部8M晶振,配置在#define __SYSTEM_CLOCK_168M_PLL_8M_HXTAL (uint32_t)(168000000)
rcu_periph_clock_enable(RCU_GPIOG);//使能GPIOA时钟
gpio_mode_set(GPIOG, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLDOWN, GPIO_PIN_9);//PG9配置成串口输出
gpio_output_options_set(GPIOG, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_9);
gpio_bit_reset(GPIOG, GPIO_PIN_9);
fwdgt_write_enable();
fwdgt_config(100,FWDGT_PSC_DIV32);
fwdgt_enable();
//USART相关配置
gd_eval_com_init();
delay_1ms(7);
while(1)
{
uart_send(buff,4);
// fwdgt_counter_reload();
(void)gpio_bit_set(GPIOG,GPIO_PIN_9);
delay_1ms(500);
(void)gpio_bit_reset(GPIOG,GPIO_PIN_9);
}
}
void USART1_IRQHandler(void)
{
if( g_recv_positin >= sizeof(g_serial_recv_buf) )
{
memset(g_serial_recv_buf,0,sizeof(g_serial_recv_buf));
g_recv_positin = 0;
}
if(usart_interrupt_flag_get(USART1,USART_INT_FLAG_ERR_ORERR) != RESET)
{
usart_interrupt_flag_clear(USART1,USART_INT_FLAG_ERR_ORERR);
}
if(usart_interrupt_flag_get(USART1,USART_INT_FLAG_RBNE) != RESET)
{
usart_interrupt_flag_clear(USART1,USART_INT_FLAG_RBNE);
g_serial_recv_buf[g_recv_positin++] = usart_data_receive(USART1);
}
if(usart_interrupt_flag_get(USART1,USART_INT_FLAG_TBE) != RESET)
{
usart_interrupt_flag_clear(USART1,USART_INT_FLAG_TBE);
}
}
|