| 
 
| void USART0_Init(void) {
 rcu_periph_clock_enable(RCU_USART0);
 
 
 /* configure USART Tx as alternate function push-pull */
 gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_9);
 gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9);
 
 /* configure USART Rx as alternate function push-pull */
 gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_10);
 gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_10);
 // PA10 - USART_RX (浮空输入)
 //gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_10);
 /* connect port to USARTx_Tx */
 gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_9);
 
 /* connect port to USARTx_Rx */
 gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_10);
 usart_deinit(USART0);
 usart_baudrate_set(USART0, 115200U);
 usart_word_length_set(USART0, USART_WL_8BIT); // 8位数据
 usart_stop_bit_set(USART0, USART_STB_1BIT); // 1位停止位
 usart_parity_config(USART0, USART_PM_NONE); // 无校验
 usart_receive_config(USART0, USART_RECEIVE_ENABLE);
 usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);
 
 usart_interrupt_enable(USART0, USART_INT_RBNE); // 使能USART0接收中断
 nvic_irq_enable(USART0_IRQn, 0);
 usart_enable(USART0);
 }
 void USART0_IRQHandler(void)
 {
 uint8_t ucChar;
 
 // 检查是否是接收中断
 if(usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE) != RESET)
 {
 // 读取接收到的数据
 ucChar = usart_data_receive(USART0);
 
 ms_comm.start_end = (ms_comm.start_end << 8) | ucChar;
 
 if (ms_comm.start_end == MasterStartRx)
 {
 ms_comm.rx_start_ok = 1;
 ms_comm.rx_index = 4;
 ms_comm.rx_len = 12;
 }
 else if (ms_comm.rx_start_ok == 1)
 {
 ms_comm.rx[ms_comm.rx_index] = ucChar;
 ms_comm.rx_index++;
 
 if (MsStartEnd16_t[0] == MsEnd)
 {
 if (ms_comm.rx_index == ms_comm.rx_len)
 {
 ms_comm.rx_start_ok = 0;
 Ms_Decode();
 }
 }
 if (ms_comm.rx_index >= sizeof (ms_cmd_t))
 ms_comm.rx_index = 4;
 }
 
 //usart_interrupt_flag_clear(USART0,USART_INT_FLAG_RBNE);
 }
 
 // 检查是否是发送中断(如果启用了发送中断)
 if (usart_interrupt_flag_get(USART0, USART_INT_FLAG_TC) != RESET)
 {
 
 if (ms_comm.tx_index < ms_comm.tx_len)
 {
 // Ms_SendByte(MsTxBuf[ms_comm.TxIndex++]);
 usart_data_transmit(USART0, MsTxBuf[ms_comm.tx_index++]);
 }
 else
 {
 Clr_Ms_INT_TC;//关闭中断
 ms_comm.tx_len = 0;
 ms_comm.wait_time = 10;
 // LCDm_CTL_L;
 }
 
 }
 
 }
 
 | 
 |