【AT-START-L021测评】串口中断通信
1,AT32 Work Bench v1.1.05 生成mdk代码
2,配置串口通信为发送中断,接收中断和中断允许。
代码如下
void wk_usart1_init(void)
{
/* add user code begin usart1_init 0 */
/* add user code end usart1_init 0 */
gpio_init_type gpio_init_struct;
gpio_default_para_init(&gpio_init_struct);
/* add user code begin usart1_init 1 */
/* add user code end usart1_init 1 */
/* configure the TX pin */
gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE9, GPIO_MUX_1);
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_MODERATE;
gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
gpio_init_struct.gpio_pins = GPIO_PINS_9;
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
gpio_init(GPIOA, &gpio_init_struct);
/* configure the RX pin */
gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE10, GPIO_MUX_1);
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_MODERATE;
gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
gpio_init_struct.gpio_pins = GPIO_PINS_10;
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
gpio_init(GPIOA, &gpio_init_struct);
/* config usart1 clock source */
crm_usart_clock_select(CRM_USART1, CRM_USART_CLOCK_SOURCE_PCLK);
/* configure param */
usart_init(USART1, 38400, USART_DATA_8BITS, USART_STOP_1_BIT);
usart_transmitter_enable(USART1, TRUE);
usart_receiver_enable(USART1, TRUE);
usart_parity_selection_config(USART1, USART_PARITY_NONE);
usart_hardware_flow_control_set(USART1, USART_HARDWARE_FLOW_NONE);
/**
* Users need to configure USART1 interrupt functions according to the actual application.
* 1. Call the below function to enable the corresponding USART1 interrupt.
* --usart_interrupt_enable(...)
* 2. Add the user's interrupt handler code into the below function in the at32l021_int.c file.
* --void USART1_IRQHandler(void)
*/
/* add user code begin usart1_init 2 */
<font color="#ff0000">usart_interrupt_enable(USART1, USART_RDBF_INT,TRUE),</font>
/* add user code end usart1_init 2 */
<font color="#8b0000">usart_enable(USART1, TRUE);</font>
/* add user code begin usart1_init 3 */
/* add user code end usart1_init 3 */
}
3,编写接收中断发送中断子程序。
extern uint8_t usart1_tx_buffer[];
//extern uint8_t usart2_rx_buffer[];
extern uint8_t usart1_rx_buffer[];
//extern uint8_t usart2_tx_counter;
extern uint8_t usart1_tx_counter;
//extern uint8_t usart2_rx_counter;
extern uint8_t usart1_rx_counter;
//extern uint8_t usart2_tx_buffer_size;
extern uint8_t usart1_tx_buffer_size;
/**
* [url=home.php?mod=space&uid=247401]@brief[/url] this function handles USART1 handler.
* @param none
* @retval none
*/
void USART1_IRQHandler(void)
{
/* add user code begin USART1_IRQ 0 */
/* add user code end USART1_IRQ 0 */
/* add user code begin USART1_IRQ 1 */
if(usart_interrupt_flag_get(USART1, USART_RDBF_FLAG) != RESET)
{
/* read one byte from the receive data register */
usart1_rx_buffer[usart1_rx_counter++] = usart_data_receive(USART1);
if(usart1_rx_counter == usart1_tx_buffer_size)
{
/* disable the usart1 receive interrupt */
// usart_interrupt_enable(USART1, USART_RDBF_INT, FALSE);
usart1_rx_counter =0;
}
}
if(usart_interrupt_flag_get(USART1, USART_TDBE_FLAG) != RESET)
{
/* write one byte to the transmit data register */
// usart_data_transmit(USART1, usart1_tx_buffer[usart1_tx_counter++]);
if(usart1_tx_counter == usart1_tx_buffer_size)
{
/* disable the usart1 transmit interrupt */
// usart_interrupt_enable(USART1, USART_TDBE_INT, FALSE);
usart1_tx_counter=0;
}
}
/* add user code end USART1_IRQ 1 */
}
4,编写主程序。
/* private includes ----------------------------------------------------------*/
/* add user code begin private includes */
#define COUNTOF(a) (sizeof(a) / sizeof(*(a)))
//#define USART2_TX_BUFFER_SIZE (COUNTOF(usart2_tx_buffer) - 1)
#define USART1_TX_BUFFER_SIZE (COUNTOF(usart1_tx_buffer) - 1)
//uint8_t usart2_tx_buffer[] = "usart transfer by interrupt: usart2 -> usart1 using interrupt";
uint8_t usart1_rx_buffer[100];
#define USART1_RX_BUFFER_SIZE (COUNTOF(usart1_rx_buffer) - 1)
uint8_t usart1_tx_buffer[] = "usart transfer by interrupt: usart1 using interrupt";
//uint8_t usart2_rx_buffer[USART1_TX_BUFFER_SIZE];
//volatile uint8_t usart2_tx_counter = 0x00;
volatile uint8_t usart1_tx_counter = 0x00;
//volatile uint8_t usart2_rx_counter = 0x00;
volatile uint8_t usart1_rx_counter = 0x00;
//uint8_t usart2_tx_buffer_size = USART2_TX_BUFFER_SIZE;
uint8_t usart1_tx_buffer_size = USART1_TX_BUFFER_SIZE;
uint8_t usart1_rx_buffer_size = USART1_RX_BUFFER_SIZE;
int main(void)
{
/* add user code begin 1 */
/* add user code end 1 */
/* system clock config. */
wk_system_clock_config();
/* config periph clock. */
wk_periph_clock_config();
/* nvic config. */
wk_nvic_config();
/* timebase config. */
wk_timebase_init();
/* init usart1 function. */
wk_usart1_init();
/* init gpio function. */
wk_gpio_config();
/* add user code begin 2 */
/* add user code end 2 */
while(1)
{
/* add user code begin 3 */
if((usart1_rx_counter>=4)&&(usart1_rx_buffer[0]=='s')&&(usart1_rx_buffer[1]=='e')&&(usart1_rx_buffer[2]=='n')&&(usart1_rx_buffer[3]=='d'))
{
int i =0;
gpio_bits_reset(led4_GPIO_PORT, led4_PIN);
gpio_bits_reset(led2_GPIO_PORT, led2_PIN | led3_PIN);
wk_delay_ms(100);//DelayMs(1000);
gpio_bits_set(led4_GPIO_PORT, led4_PIN);
gpio_bits_set(led2_GPIO_PORT, led2_PIN | led3_PIN);
wk_delay_ms(100);//DelayMs(1000);
usart1_tx_counter=0;
for(i =0;i<usart1_tx_buffer_size;i++)
{
usart_data_transmit(USART1, usart1_tx_buffer[usart1_tx_counter++]);
wk_delay_ms(1);
}
usart1_rx_counter=0;
usart1_rx_buffer[0]=0;
usart1_rx_buffer[1]=0;
usart1_rx_buffer[2]=0;
usart1_rx_buffer[3]=0;
}
/* add user code end 3 */
}
}
5,编译调试。
6,测试OK。
|