#include "uart0.h"
/*!
\brief cofigure the USART0 GPIO ports
\param[in] none
\param[out] none
\retval none
*/
void usart0_gpio_config(void)
{
/* enable COM GPIO clock */
rcu_periph_clock_enable(RCU_GPIOA);
/* 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);
/* 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_10MHZ, 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_10MHZ, GPIO_PIN_10);
}
/*!
\brief cofigure the USART0
\param[in] none
\param[out] none
\retval none
*/
void usart0_config(void)
{
/* enable USART clock */
rcu_periph_clock_enable(RCU_USART0);
/* USART configure */
usart_deinit(USART0);
usart_baudrate_set(USART0, 460800U);
usart_receive_config(USART0, USART_RECEIVE_ENABLE);
usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);
usart_enable(USART0);
}
void usart0_int_init(void)
{
/* USART interrupt configuration */
nvic_irq_enable(USART0_IRQn, 0, 1);
usart_interrupt_enable(USART0, USART_INT_RBNE);
}
void usart0_init(void)
{
usart0_gpio_config();
usart0_config();
usart0_int_init();
}
void usart0_send_ch(uint8_t ch)
{
usart_data_transmit(USART0, (uint8_t) ch);
while(RESET == usart_flag_get(USART0, USART_FLAG_TBE));
}
void uart0_Send_Array(unsigned char send_array[],unsigned char num)
{
//串口发送
unsigned char i=0;
for(i=0 ;i< num ; i++)
{
usart0_send_ch(send_array);
}
}
/* retarget the C library printf function to the USART */
int fputc(int ch, FILE *f)
{
usart_data_transmit(USART0, (uint8_t) ch);
while(RESET == usart_flag_get(USART0, USART_FLAG_TBE));
return ch;
}
中断
void USART0_IRQHandler(void)
{
uint16_t re = 0;
if(RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE)){
/* receive data */
re = usart_data_receive(USART0);
if( re == 13)
{
usart0_send_ch('\r');
usart0_send_ch('\n');
}else
usart0_send_ch(re);
}
}
————————————————
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/liuhfeng21/article/details/135642134
|