本帖最后由 yuyy1989 于 2023-7-11 17:40 编辑
#申请原创# @21小跑堂
5.UART通讯+SPI驱动LCD12864+I2C读取DHTC12温湿度
5.1 UART串口通讯
UART在嵌入式领域应用的非常广泛,APM32F407拥有4个USART和2个UART,板子上引出的串口时USART1 TX:PA9 RX:PA10
简单做一个将接收到的数据原样返回的程序,代码示例
#include "Board_APM32F407_TINY.h"
#include "bsp_delay.h"
#include "apm32f4xx_tmr.h"
#define UART_BUFFER_LEN 30
uint8_t uart_buffer[UART_BUFFER_LEN] = {0};
uint8_t uart_rxindex = 0;
uint8_t uart_rxlen = 0;
uint8_t uart_txindex = 0;
uint8_t uart_txlen = 0;
void uart_init()
{
USART_Config_T usartConfigStruct;
usartConfigStruct.baudRate = 115200;
usartConfigStruct.hardwareFlow = USART_HARDWARE_FLOW_NONE;
usartConfigStruct.mode = USART_MODE_TX_RX;
usartConfigStruct.parity = USART_PARITY_NONE;
usartConfigStruct.stopBits = USART_STOP_BIT_1;
usartConfigStruct.wordLength = USART_WORD_LEN_8B;
APM_TINY_COMInit(COM1, &usartConfigStruct);
USART_EnableInterrupt(TINY_COM1, USART_INT_RXBNE);
USART_EnableInterrupt(TINY_COM1, USART_INT_IDLE);
USART_ClearStatusFlag(TINY_COM1, USART_FLAG_RXBNE);
NVIC_EnableIRQRequest(TINY_COM1_IRQn,0,0);
}
void readrxtotxbuffer()
{
uart_txlen += uart_rxlen;
uart_rxlen = 0;
USART_EnableInterrupt(TINY_COM1, USART_INT_TXBE);
}
int main(void)
{
APM_DelayInit();
uart_init();
while (1)
{
}
}
void SysTick_Handler(void)
{
APM_DelayTickDec();
}
void USART1_IRQHandler(void)
{
if(USART_ReadStatusFlag(TINY_COM1, USART_FLAG_RXBNE) == SET){
uart_buffer[uart_rxindex++] = (uint8_t)USART_RxData(TINY_COM1);
if(uart_rxindex == UART_BUFFER_LEN)
uart_rxindex = 0;
uart_rxlen++;
}
if(USART_ReadStatusFlag(TINY_COM1, USART_FLAG_IDLE) == SET|| uart_rxlen == 10) {
USART_ClearStatusFlag(TINY_COM1, USART_FLAG_IDLE);
readrxtotxbuffer();
}
if(USART_ReadStatusFlag(TINY_COM1, USART_FLAG_TXBE) == SET){
if(uart_txlen > 0)
{
USART_TxData(TINY_COM1,uart_buffer[uart_txindex++]);
if(uart_txindex == UART_BUFFER_LEN)
uart_txindex = 0;
uart_txlen--;
}
else
{
USART_DisableInterrupt(TINY_COM1, USART_INT_TXBE);
}
}
}
编译烧录查看效果
板子上的Geehy LINK没有串口功能,还要额外找个USB转串口工具测试,按顺序连接好串口接线
打开串口工具,发送任意字符串
5.2 SPI驱动12864LCD
SPI是一种高速的全双工同步的通信总线,需要至少SCK MOSI MISO三根线实现数据双向传输,可以通过片选线来实现对不同设备的访问
APM32F407拥有3个SPI,手上有个12864的LCD屏,用SPI驱动的,尝试用这块板子点亮一下
通讯需要用到5个IO:SCK MOSI CS RST A0都是输出模式,SCK用PA5 MOSI用PA7,其它的就随意了
数据是单向传输的因此SPI配置成仅发送就行了,代码示例
#include "Board_APM32F407_TINY.h"
#include "bsp_delay.h"
#include "apm32f4xx_tmr.h"
#include "apm32f4xx_spi.h"
#include "yuyy_project_config.h"
#include "yuyy_hs12864g18b.h"
Yuyy_Hs12864g18b_Gpio_Config_t hs12864g18b_gpio_config;
void hs12864g18b_init()
{
GPIO_Config_T GPIO_InitStructure;
SPI_Config_T SPI1_InitStructure;
RCM_EnableAHB1PeriphClock (RCM_AHB1_PERIPH_GPIOA);
RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_SPI1);
RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_SYSCFG);
hs12864g18b_gpio_config.cs.gpio = GPIOA;
hs12864g18b_gpio_config.cs.pin = GPIO_PIN_3;
hs12864g18b_gpio_config.rst.gpio = GPIOA;
hs12864g18b_gpio_config.rst.pin = GPIO_PIN_4;
hs12864g18b_gpio_config.a0.gpio = GPIOA;
hs12864g18b_gpio_config.a0.pin = GPIO_PIN_6;
hs12864g18b_gpio_config.sck.gpio = GPIOA;
hs12864g18b_gpio_config.sck.pin = GPIO_PIN_5;
hs12864g18b_gpio_config.mo.gpio = GPIOA;
hs12864g18b_gpio_config.mo.pin = GPIO_PIN_7;
GPIO_ConfigStructInit(&GPIO_InitStructure);
GPIO_InitStructure.pin = GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_6;
GPIO_InitStructure.speed = GPIO_SPEED_100MHz;
GPIO_InitStructure.mode = GPIO_MODE_OUT;
GPIO_InitStructure.otype = GPIO_OTYPE_PP;
GPIO_InitStructure.pupd = GPIO_PUPD_NOPULL;
GPIO_Config(GPIOA, &GPIO_InitStructure);
#if(YUYY_HS12864G18B_USE_SOFT_SPI)
GPIO_InitStructure.pin = GPIO_PIN_5 | GPIO_PIN_7;
GPIO_InitStructure.mode = GPIO_MODE_OUT;
GPIO_Config(GPIOA, &GPIO_InitStructure);
yuyy_hs12864g18b_set_spigpio(NULL,&hs12864g18b_gpio_config);
#else
GPIO_ConfigPinAF(GPIOA, GPIO_PIN_SOURCE_5, GPIO_AF_SPI1);
GPIO_ConfigPinAF(GPIOA, GPIO_PIN_SOURCE_7, GPIO_AF_SPI1);
GPIO_InitStructure.pin = GPIO_PIN_5 | GPIO_PIN_7;
GPIO_InitStructure.mode = GPIO_MODE_AF;
GPIO_Config(GPIOA, &GPIO_InitStructure);
SPI_ConfigStructInit(&SPI1_InitStructure);
SPI1_InitStructure.direction = SPI_DIRECTION_1LINE_TX; //只需要发送
SPI1_InitStructure.mode = SPI_MODE_MASTER;
SPI1_InitStructure.length = SPI_DATA_LENGTH_8B; //数据长度8bit
SPI1_InitStructure.polarity = SPI_CLKPOL_HIGH; //空闲时SCK高电平
SPI1_InitStructure.phase = SPI_CLKPHA_2EDGE; //第二个边沿采样
SPI1_InitStructure.nss = SPI_NSS_SOFT; //软件控制片选
SPI1_InitStructure.baudrateDiv = SPI_BAUDRATE_DIV_8; //时钟分频系数
SPI1_InitStructure.firstBit = SPI_FIRSTBIT_MSB;
SPI_Config(SPI1, &SPI1_InitStructure);
SPI_DisableCRC(SPI1);
SPI_Enable(SPI1);
yuyy_hs12864g18b_set_spigpio(SPI1,&hs12864g18b_gpio_config);
#endif
yuyy_hs12864g18b_init();
yuyy_hs12864g18b_clear_screen();
}
void yuyy_hard_spi_writebyte(YUYY_SPI_TYPE spix,uint8_t dat)
{
while (SPI_I2S_ReadStatusFlag(spix, SPI_FLAG_TXBE) == RESET);
SPI_I2S_TxData(spix, dat);
}
int main(void)
{
APM_DelayInit();
hs12864g18b_init();
yuyy_hs12864g18b_display_string_8x16(0,0,0,(uint8_t *)"APM32F407IG TEST");
yuyy_hs12864g18b_display_string_8x16(0,2,0,(uint8_t *)"Tiny Board v1.0");
yuyy_hs12864g18b_display_string_8x16(0,4,0,(uint8_t *)"Code by yuyy1989");
while (1)
{
}
}
void SysTick_Handler(void)
{
APM_DelayTickDec();
}
编译烧录查看效果
5.3 I2C读取DHTC12温湿度
I2C属于两线式串行总线,属于一主多从的总线结构,总线上的每个设备都有一个特定的设备地址,以区分同一I2C总线上的其他设备。
APM32F407拥有3个I2C,很多传感器都是用I2C进行通讯的,接下来尝试用I2C读取DHTC12温湿度,选用PF0 PF1
尝试用硬件I2C,结果在调用I2C_EnableGenerateStart后无论等多久I2C_ReadEventStatus(iicx, I2C_EVENT_MASTER_MODE_SELECT)返回的都是0
想参考bsp_i2c.c的驱动代码,结果用的是软件I2C,索性直接用软件I2C了,通讯代码可以直接参考bsp_i2c.c
运行效果
|
赞,学习一下