[AT-START-F425测评] GPIO控制LCD12864显示 + USART收发数据
<p>本次测试模块包括: GPIO, USART</p><p> </p>
<p>测试内容如下:</p>
<p>1. 使用GPIO控制LCD12864液晶显示</p>
<p>2. 初始化usart2,使用中断模式 接收串口数据,当接收数量超过10个时,显示在LCD上,并将数据通过串口发送出去</p>
<p> </p>
<p>测试效果:</p>
<p> </p>
<p>源码如下:</p>
<p>uart.c</p>
<pre>
<code>
#include "uart.h"
uint8_t *rx_buffer;
uint32_trx_cnt = 0U;
uint8_t *tx_buffer;
uint32_ttx_cnt = 0U;
uint32_tnbr_data_to_send = 0U;
uint8_t uart_send_finish_flag = 0U;
void uart_nvic_irq_enable(usart_type* UARTx)
{
if (USART1 == UARTx)
{
nvic_irq_enable(USART1_IRQn, 0, 0);
}
else if (USART2 == UARTx)
{
nvic_irq_enable(USART2_IRQn, 0, 0);
}
else if ((USART3 == UARTx)||(USART4 == UARTx))
{
nvic_irq_enable(USART4_3_IRQn, 0, 0);
}
else
{
}
}
void uart2_init(uint32_t bps)
{
gpio_init_type gpio_init_struct;
/* enable the usart2 and gpio clock */
crm_periph_clock_enable(CRM_USART2_PERIPH_CLOCK, TRUE);
crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
gpio_default_para_init(&gpio_init_struct);
/* configure the usart2 tx/rx pin */
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
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_2 | GPIO_PINS_3;
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
gpio_init(GPIOA, &gpio_init_struct);
/* config usart2 iomux */
gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE2, GPIO_MUX_1);
gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE3, GPIO_MUX_1);
/* config usart2 nvic interrupt */
nvic_irq_enable(USART2_IRQn, 0, 0);
/* configure usart2 param */
usart_init(USART2, 115200, USART_DATA_8BITS, USART_STOP_1_BIT);
usart_transmitter_enable(USART2, TRUE);
usart_receiver_enable(USART2, TRUE);
/* enable usart2 interrupt */
usart_interrupt_enable(USART2, USART_RDBF_INT, TRUE);
usart_enable(USART2, TRUE);
//usart_interrupt_enable(USART2, USART_TDBE_INT, TRUE);
}
void uart_send_1byte(usart_type* UARTx, uint8_t data)
{
/* write one byte to the transmit data register */
usart_data_transmit(UARTx, data);
}
void uart_send_data(usart_type* UARTx, uint8_t *buff, uint32_t len)
{
uart_send_finish_flag = 0;
tx_buffer = buff;
nbr_data_to_send = len;
tx_cnt = 0;
usart_interrupt_enable(UARTx, USART_TDBE_INT, TRUE);
uart_nvic_irq_enable(UARTx);
}
void uart_receive_data(usart_type* UARTx, uint8_t *buff)
{
rx_cnt = 0;
rx_buffer = buff;
usart_interrupt_enable(UARTx, USART_RDBF_INT, TRUE);
// uart_nvic_irq_enable(UARTx);
}
void get_uart_rx_data(uint8_t *buff)
{
uint32_t i = 0;
for(i=0; i<rx_cnt; i++) {
buff = rx_buffer;
}
rx_cnt=0;
}
uint32_t get_uart_rx_len(void)
{
return rx_cnt;
}
/**
* @brief this function handles usart2 handler.
* @paramnone
* @retval none
*/
void USART2_IRQHandler(void)
{
if(usart_flag_get(USART2, USART_RDBF_FLAG) != RESET)
{
/* read one byte from the receive data register */
rx_buffer = (uint8_t)usart_data_receive(USART2);
/* disable the usart2 receive interrupt */
//usart_interrupt_enable(USART2, USART_RDBF_INT, FALSE);
}
if(usart_flag_get(USART2, USART_TDBE_FLAG) != RESET)
{
/* write one byte to the transmit data register */
usart_data_transmit(USART2, tx_buffer);
if(tx_cnt >= nbr_data_to_send) {
/* disable the USART2 transmit interrupt */
usart_interrupt_enable(USART2, USART_TDBE_INT, FALSE);
uart_send_finish_flag = 1;
}
}
}
</code></pre>
<p>uart.h</p>
<pre>
<code>#ifndef __UART_H__
#define __UART_H__
#include <stdint.h>
#include "at32f425_usart.h"
extern uint8_t *rx_buffer;
extern uint32_trx_cnt;
extern uint8_t uart_send_finish_flag;
void uart2_init(uint32_t bps);
void uart_send_1byte(usart_type* UARTx, uint8_t data);
void uart_send_data(usart_type* UARTx, uint8_t *buff, uint32_t len);
void uart_receive_data(usart_type* UARTx, uint8_t *buff);
uint32_t get_uart_rx_len(void);
void get_uart_rx_data(uint8_t *buff);
#endif
</code></pre>
<p>测试代码</p>
<pre>
<code>
#define SPP_COM USART2
#define SPP_BUF_LEN100
uint8_t spp_txbuf;
uint8_t spp_rxbuf;
uint32_t spp_rxlen=0;
void uart_test()
{
uint8_t i;
uart2_init(115200);
for(i=0; i<SPP_BUF_LEN; i++)
{
spp_rxbuf = 0;
spp_txbuf = 0;
}
uart_receive_data(SPP_COM, spp_rxbuf);//开启中断接收,并确定接收数组
i=0;
while(1)
{
spp_rxlen = get_uart_rx_len();
if (spp_rxlen >= 10)
{
get_uart_rx_data(spp_rxbuf);
LCD_printf_line(6,"%s %d",spp_rxbuf,i);
uart_send_data(SPP_COM, spp_rxbuf, spp_rxlen);//开启中断发送
i++;
}
}
}</code></pre>
<p> </p>
<p> USART的测试看到收发正确就可以了</p>
结合LCD显示,两个项目一起,很棒,期待更精彩测试! <p>谢谢分享,期待后续。</p>
页:
[1]