【AT32A403A 车规MCU开发板】 SPI-DMA方式驱动LCD
<p><span style="font-size:16px;">测试SPI-DMA方式驱动LCD显示屏。</span></p><p> </p>
<p><span style="font-size:16px;"><strong>一、硬件部分</strong></span></p>
<p> </p>
<p><span style="font-size:16px;">测试使用SPI1接口</span></p>
<p><span style="font-size:16px;"></span></p>
<p> </p>
<p><strong><span style="font-size:16px;">二、程序部分</span></strong></p>
<p> </p>
<p><span style="font-size:16px;">2.1、SPI初始化部分</span></p>
<pre>
<code>#include "main.h"
uint16_t *lcd_data_buf;
//LCD_RS--> D7
//LCD-RST --> D6
//LCD-CS--> D10
//LCD-BK--> A3
//TP-CS --> D8
//TP-INT--> D9
/**
* @brief initialize lcd spi1
* @paramnone
* @retval none
*/
void lcd_hw_init(void)
{
gpio_init_type gpio_init_struct;
dma_init_type dma_init_struct;
spi_init_type spi_init_struct;
//时钟使能
crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
crm_periph_clock_enable(CRM_SPI1_PERIPH_CLOCK, TRUE);
crm_periph_clock_enable(CRM_DMA1_PERIPH_CLOCK, TRUE);
// crm_periph_clock_enable(CRM_IOMUX_PERIPH_CLOCK, TRUE);
//端口初始化
gpio_default_para_init(&gpio_init_struct);
/* configure the SCK pin */
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 = LCD_SPI_SCK_PIN;
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
gpio_init(LCD_SPI_SCK_PORT, &gpio_init_struct);
/* configure the MISO pin */
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_INPUT;
gpio_init_struct.gpio_pins = LCD_SPI_MISO_PIN;
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
gpio_init(LCD_SPI_MISO_PORT, &gpio_init_struct);
/* configure the MOSI pin */
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 = LCD_SPI_MOSI_PIN;
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
gpio_init(LCD_SPI_MOSI_PORT, &gpio_init_struct);
//SPI初始化
spi_default_para_init(&spi_init_struct);
spi_init_struct.transmission_mode = SPI_TRANSMIT_FULL_DUPLEX;
spi_init_struct.master_slave_mode = SPI_MODE_MASTER;
spi_init_struct.frame_bit_num = SPI_FRAME_8BIT;
spi_init_struct.first_bit_transmission = SPI_FIRST_BIT_MSB;
spi_init_struct.mclk_freq_division = SPI_MCLK_DIV_4;
spi_init_struct.clock_polarity = SPI_CLOCK_POLARITY_LOW;
spi_init_struct.clock_phase = SPI_CLOCK_PHASE_1EDGE;
spi_init_struct.cs_mode_selection = SPI_CS_SOFTWARE_MODE;
spi_init(LCD_SPI_SELECTED, &spi_init_struct);
spi_i2s_dma_transmitter_enable(LCD_SPI_SELECTED, TRUE);
spi_enable(LCD_SPI_SELECTED, TRUE);
/* lcd_spi_master_tx_dma_channel configuration */
dma_reset(LCD_SPI_MASTER_Tx_DMA_Channel);
dma_default_para_init(&dma_init_struct);
dma_init_struct.buffer_size = 0xFFFE;
dma_init_struct.direction = DMA_DIR_MEMORY_TO_PERIPHERAL;
dma_init_struct.memory_base_addr = (uint32_t)lcd_data_buf;
dma_init_struct.memory_data_width = DMA_MEMORY_DATA_WIDTH_HALFWORD;
dma_init_struct.memory_inc_enable = TRUE;
dma_init_struct.peripheral_base_addr = (uint32_t)LCD_SPI_MASTER_DR_Base;
dma_init_struct.peripheral_data_width = DMA_PERIPHERAL_DATA_WIDTH_HALFWORD;
dma_init_struct.peripheral_inc_enable = FALSE;
dma_init_struct.priority = DMA_PRIORITY_HIGH;
dma_init_struct.loop_mode_enable = FALSE;
dma_init(LCD_SPI_MASTER_Tx_DMA_Channel, &dma_init_struct);
dma_flexible_config(DMA1, FLEX_CHANNEL3, DMA_FLEXIBLE_SPI1_TX);
}
/**
* @brieflcd spi1 receive data
* @paramnone
* @retval receive data
*/
uint8_t lcd_spi1_data_receive(void)
{
uint32_t retry = 0;
spi_i2s_flag_clear(LCD_SPI_SELECTED, SPI_I2S_RDBF_FLAG);
spi_i2s_data_transmit(LCD_SPI_SELECTED, 0);
while (spi_i2s_flag_get(LCD_SPI_SELECTED, SPI_I2S_RDBF_FLAG) == RESET)
{
retry++;
if(retry > 0xFFFFF)
{
return 0;
}
}
return (uint8_t)spi_i2s_data_receive(LCD_SPI_SELECTED);
}
/**
* @brieflcd spi1 write
* @paramdata: write data
* @retval none
*/
void lcd_spi1_write(uint8_t data)
{
uint32_t retry = 0;
while (spi_i2s_flag_get(SPI1, SPI_I2S_TDBE_FLAG) == RESET)
{
retry++;
if(retry > 10000)
return;
}
spi_i2s_data_transmit(SPI1, data);
retry = 0;
while (spi_i2s_flag_get(SPI1, SPI_I2S_BF_FLAG) == SET)
{
retry++;
if(retry > 0xFFFFF)
return;
}
}
/**
* @briefspi mode switch between lcd and touch
* @parammode
* 1:touch, 0:lcd
* @retval none
*/
void spi_switch(uint8_t mode)
{
if(mode)
{
//LCD_CS1_SET;
spi_enable(LCD_SPI_SELECTED, FALSE);
LCD_SPI_SELECTED->ctrl1_bit.mdiv_l = SPI_MCLK_DIV_32;
spi_enable(LCD_SPI_SELECTED, TRUE);
//LCD_CS2_CLR;
}
else
{
//LCD_CS2_SET;
spi_enable(LCD_SPI_SELECTED, FALSE);
LCD_SPI_SELECTED->ctrl1_bit.mdiv_l = SPI_MCLK_DIV_4;
spi_enable(LCD_SPI_SELECTED, TRUE);
//LCD_CS1_CLR;
}
}
</code></pre>
<p> </p>
<p><span style="font-size:16px;">2.2、lcd驱动程序</span></p>
<pre>
<code>#include "main.h"
_lcd_dev lcddev;
void init_lcd_port(void)
{
gpio_init_type gpio_init_struct;
crm_periph_clock_enable(LCD_RST_GPIO_CLK, TRUE);
crm_periph_clock_enable(LCD_BLK_GPIO_CLK, TRUE);
crm_periph_clock_enable(LCD_RS_GPIO_CLK, TRUE);
crm_periph_clock_enable(LCD_CS_GPIO_CLK, TRUE);
crm_periph_clock_enable(CRM_IOMUX_PERIPH_CLOCK, TRUE);
gpio_pin_remap_config(SWJTAG_GMUX_010, TRUE);
gpio_default_para_init(&gpio_init_struct);
//LCD-RS
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_OUTPUT;
gpio_init_struct.gpio_pins = LCD_RS_PIN;
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
gpio_init(LCD_RS_PORT, &gpio_init_struct);
//LCD-RST
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_OUTPUT;
gpio_init_struct.gpio_pins = LCD_RST_PIN;
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
gpio_init(LCD_RST_PORT, &gpio_init_struct);
//LCD-CS
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_OUTPUT;
gpio_init_struct.gpio_pins = LCD_CS_PIN;
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
gpio_init(LCD_CS_PORT, &gpio_init_struct);
//LCD-BK
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_OUTPUT;
gpio_init_struct.gpio_pins = LCD_BLK_PIN;
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
gpio_init(LCD_BLK_PORT, &gpio_init_struct);
}
static void LCD_RESET(void)
{
LCD_RES_CLR;
DELAY(100);
LCD_RES_SET;
DELAY(100);
}
static void LCD_WR_REG(uint8_t reg)
{
LCD_DC_CLR;
LCD_CS_CLR;
lcd_spi1_write(reg);
LCD_CS_SET;
LCD_DC_SET;
}
static void LCD_WR_DATA(uint8_t data)
{
LCD_DC_SET;
LCD_CS_CLR;
lcd_spi1_write(data);
LCD_CS_SET;
}
static void LCD_WriteReg(uint8_t reg, uint16_t regdata)
{
LCD_WR_REG(reg);
LCD_WR_DATA(regdata);
}
static void LCD_WriteRAM_Prepare(void)
{
LCD_WR_REG(lcddev.wramcmd);
}
static void LCD_WriteData_16Bit(uint16_t Data)
{
LCD_DC_SET;
LCD_CS_CLR;
lcd_spi1_write(Data >> 8);
lcd_spi1_write(Data & 0xFF);
LCD_CS_SET;
}
void LCD_direction(uint8_t direction)
{
lcddev.setxcmd = 0x2A;
lcddev.setycmd = 0x2B;
lcddev.wramcmd = 0x2C;
switch (direction)
{
case 0:
lcddev.width = LCD_W;
lcddev.height = LCD_H;
LCD_WriteReg(0x36, (1 << 3) | (0 << 6) | (0 << 7)); /* BGR==1,MY==0,MX==0,MV==0 */
break;
case 1:
lcddev.width = LCD_H;
lcddev.height = LCD_W;
LCD_WriteReg(0x36, (1 << 3) | (0 << 7) | (1 << 6) | (1 << 5)); /* BGR==1,MY==1,MX==0,MV==1 */
break;
case 2:
lcddev.width = LCD_W;
lcddev.height = LCD_H;
LCD_WriteReg(0x36, (1 << 3) | (1 << 6) | (1 << 7)); /* BGR==1,MY==0,MX==0,MV==0 */
break;
case 3:
lcddev.width = LCD_H;
lcddev.height = LCD_W;
LCD_WriteReg(0x36, (1 << 3) | (1 << 7) | (1 << 5)); /* BGR==1,MY==1,MX==0,MV==1 */
break;
default:
break;
}
}
void LCD_SetWindows(uint16_t xStar, uint16_t yStar, uint16_t xEnd, uint16_t yEnd)
{
LCD_WR_REG(lcddev.setxcmd);
LCD_WR_DATA(xStar >> 8);
LCD_WR_DATA(0x00FF & xStar);
LCD_WR_DATA(xEnd >> 8);
LCD_WR_DATA(0x00FF & xEnd);
LCD_WR_REG(lcddev.setycmd);
LCD_WR_DATA(yStar >> 8);
LCD_WR_DATA(0x00FF & yStar);
LCD_WR_DATA(yEnd >> 8);
LCD_WR_DATA(0x00FF & yEnd);
LCD_WriteRAM_Prepare();
}
void LCD_SetCursor(uint16_t Xpos, uint16_t Ypos)
{
LCD_SetWindows(Xpos, Ypos, Xpos, Ypos);
}
void LCD_Clear(uint16_t Color)
{
#if 0 /* not use dma */
unsigned int i, m, j;
uint8_t buf;
for (i = 0; i < 40; i++)
{
buf = Color >> 8;
buf = Color & 0xff;
}
LCD_SetWindows(0, 0, lcddev.width - 1, lcddev.height - 1);
for (i = 0; i < lcddev.height; i++)
{
for (m = 0; m < lcddev.width;)
{
m += 40;
LCD_CS_CLR;
//spi_send(buf, 80);
for(j=0;j<80;j++)
{
lcd_spi1_write(buf);
}
LCD_CS_SET;
}
}
#else /* use dma */
uint32_t num,num1;
//lcd_set_window(0, 0, LCD_WIDTH - 1, LCD_HEIGHT - 1);
LCD_SetWindows(0, 0, lcddev.width - 1, lcddev.height - 1);
LCD_DC_SET;
LCD_CS_CLR;
spi_frame_bit_num_set(LCD_SPI_SELECTED, SPI_FRAME_16BIT);
LCD_SPI_MASTER_Tx_DMA_Channel->ctrl_bit.mincm = FALSE;
num = lcddev.width * lcddev.height;
while(num)
{
if(num >= 0xFFFE)
{
num -= 0xFFFE;
num1 = 0xFFFE;
}
else
{
num1 = num+2;
num = 0;
}
LCD_SPI_MASTER_Tx_DMA_Channel->ctrl_bit.chen = FALSE;
LCD_SPI_MASTER_Tx_DMA_Channel->dtcnt_bit.cnt = num1;
LCD_SPI_MASTER_Tx_DMA_Channel->maddr= (uint32_t)&Color;
LCD_SPI_MASTER_Tx_DMA_Channel->ctrl_bit.chen = TRUE;
while(dma_flag_get(LCD_SPI_MASTER_Tx_DMA_FLAG) != SET);
dma_flag_clear(LCD_SPI_MASTER_Tx_DMA_FLAG);
}
LCD_CS_SET;
LCD_SPI_MASTER_Tx_DMA_Channel->ctrl_bit.mincm = TRUE;
spi_frame_bit_num_set(LCD_SPI_SELECTED, SPI_FRAME_8BIT);
#endif
}
void LCD_Fill(uint16_t xsta, uint16_t ysta, uint16_t xend, uint16_t yend, uint16_t color)
{
// uint16_t i, j;
// LCD_SetWindows(xsta, ysta, xend - 1, yend - 1);
// LCD_DC_SET;
// for (i = ysta; i < yend; i++)
// {
// for (j = xsta; j < xend; j++)
// {
// LCD_WriteData_16Bit(color);
// }
// }
#if 0 /* not use dma */
//uint16_t i, j;
//for(i = sy; i <= ey; i++)
//{
// lcd_set_cursor(sx, i);
// for(j = sx; j < ex; j++)
// {
// lcd_wr_data16(*color++);
// }
//}
#else /* use dma */
uint32_t num,num1;
//lcd_set_window(sx, sy, ex - 1, ey - 1);
LCD_SetWindows(xsta, ysta, xend - 1, yend - 1);
LCD_CS_CLR;
LCD_DC_SET;
//num = (ex - sx) * (ey - sy);
num = (xend - xsta) * (yend - ysta);
spi_frame_bit_num_set(LCD_SPI_SELECTED, SPI_FRAME_16BIT);
while(num)
{
if(num >= 0xFFFE)
{
num -= 0xFFFE;
num1 = 0xFFFE;
}
else
{
num1 = num;
num = 0;
}
LCD_SPI_MASTER_Tx_DMA_Channel->ctrl_bit.chen = FALSE;
LCD_SPI_MASTER_Tx_DMA_Channel->dtcnt_bit.cnt = num1;
LCD_SPI_MASTER_Tx_DMA_Channel->maddr= (uint32_t)color;
LCD_SPI_MASTER_Tx_DMA_Channel->ctrl_bit.chen = TRUE;
while(dma_flag_get(LCD_SPI_MASTER_Tx_DMA_FLAG) != SET);
dma_flag_clear(LCD_SPI_MASTER_Tx_DMA_FLAG);
color += num1;
}
spi_frame_bit_num_set(LCD_SPI_SELECTED, SPI_FRAME_8BIT);
LCD_CS_SET;
#endif
}
void lcd_fill_array_spi(uint16_t Xstart, uint16_t Ystart, uint16_t Xend, uint16_t Yend, void *Image)
{
uint32_t size = 0;
size = (Xend - Xstart + 1) * (Yend - Ystart + 1) * 2;/*16bit*/
LCD_SetWindows(Xstart, Ystart, Xend, Yend);
LCD_DC_SET;
LCD_CS_CLR;
//spi_send(Image, size);
LCD_CS_SET;
}
static void _ili9341_init(void)
{
LCD_WR_REG(0xCF);
LCD_WR_DATA(0x00);
LCD_WR_DATA(0X83);
LCD_WR_DATA(0X30);
LCD_WR_REG(0xED);
LCD_WR_DATA(0x64);
LCD_WR_DATA(0x03);
LCD_WR_DATA(0X12);
LCD_WR_DATA(0X81);
LCD_WR_REG(0xE8);
LCD_WR_DATA(0x85);
LCD_WR_DATA(0x00);
LCD_WR_DATA(0x79);
LCD_WR_REG(0xCB);
LCD_WR_DATA(0x39);
LCD_WR_DATA(0x2C);
LCD_WR_DATA(0x00);
LCD_WR_DATA(0x34);
LCD_WR_DATA(0x02);
LCD_WR_REG(0xF7);
LCD_WR_DATA(0x20);
LCD_WR_REG(0xEA);
LCD_WR_DATA(0x00);
LCD_WR_DATA(0x00);
LCD_WR_REG(0xC0); /* Power control */
LCD_WR_DATA(0x26);/* VRH */
LCD_WR_REG(0xC1); /* Power control */
LCD_WR_DATA(0x11);/* SAP;BT */
LCD_WR_REG(0xC5); /* VCM control */
LCD_WR_DATA(0x35);
LCD_WR_DATA(0x3E);
LCD_WR_REG(0xC7); /* VCM control2 */
LCD_WR_DATA(0XBE);
LCD_WR_REG(0x36); /* Memory Access Control */
LCD_WR_DATA(0x28);
LCD_WR_REG(0x3A);
LCD_WR_DATA(0x55);
LCD_WR_REG(0xB1);
LCD_WR_DATA(0x00);
LCD_WR_DATA(0x1B);
LCD_WR_REG(0xB6); /* Display Function Control */
LCD_WR_DATA(0x0A);
LCD_WR_DATA(0xA2);
LCD_WR_REG(0xF2); /* 3Gamma Function Disable */
LCD_WR_DATA(0x08);
LCD_WR_REG(0x26); /* Gamma curve selected */
LCD_WR_DATA(0x01);
LCD_WR_REG(0xE0); /* set Gamma */
LCD_WR_DATA(0X1F);
LCD_WR_DATA(0X1A);
LCD_WR_DATA(0X18);
LCD_WR_DATA(0X0A);
LCD_WR_DATA(0X0F);
LCD_WR_DATA(0X06);
LCD_WR_DATA(0X45);
LCD_WR_DATA(0X87);
LCD_WR_DATA(0X32);
LCD_WR_DATA(0X0A);
LCD_WR_DATA(0X07);
LCD_WR_DATA(0X02);
LCD_WR_DATA(0X07);
LCD_WR_DATA(0X05);
LCD_WR_DATA(0X00);
LCD_WR_REG(0xE1); /* set Gamma */
LCD_WR_DATA(0X00);
LCD_WR_DATA(0X25);
LCD_WR_DATA(0X27);
LCD_WR_DATA(0X05);
LCD_WR_DATA(0X10);
LCD_WR_DATA(0X09);
LCD_WR_DATA(0X3A);
LCD_WR_DATA(0X78);
LCD_WR_DATA(0X4D);
LCD_WR_DATA(0X05);
LCD_WR_DATA(0X18);
LCD_WR_DATA(0X0D);
LCD_WR_DATA(0X38);
LCD_WR_DATA(0X3A);
LCD_WR_DATA(0X2F);
LCD_WR_REG(0x29);
}
static uint16_t color_array[] =
{
WHITE, BLACK, BLUE, BRED,
GRED, GBLUE, RED, YELLOW
};
void lcd_spi_test(void)
{
uint8_t index = 0;
for (index = 0; index < sizeof(color_array) / sizeof(color_array); index++)
{
LCD_Clear(color_array);
DELAY(200);
}
}
void lcd_draw_point(uint16_t x, uint16_t y, uint32_t color)
{
LCD_SetCursor(x, y);
LCD_WriteRAM_Prepare();
LCD_WriteData_16Bit(color);
}
void init_lcd(void)
{
init_lcd_port();
lcd_hw_init();
LCD_RESET(); /* LCD Hardware Reset */
LCD_WR_REG(0x11); /* Sleep out */
DELAY(120); /* Delay 120ms */
_ili9341_init(); /* IlI9341 init */
LCD_BLK_CLR; /* Open Backlight */
LCD_direction(USE_DIRECTION);
}
</code></pre>
<p> </p>
<p><span style="font-size:16px;">2.3、main.c</span></p>
<pre>
<code>#include "main.h"
int main(void)
{
uint8_t spi1_tx_buf;
system_clock_config();
nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
delay_init();
init_usart(115200);
init_led();
init_key();
init_i2c();
init_lcd();
lcd_spi_test();
LCD_Clear(WHITE);
g_point_color = WHITE;
lcd_show_string(10, 40, 240, 32, 32, "AT32A403A", RED);
lcd_show_string(10, 80, 240, 24, 24, "LCD TEST", RED);
lcd_show_string(10, 110, 240, 16, 16, "https://bbs.eeworld.com.cn/", RED);
while(1)
{
led2_tog();
delay_ms(100);
}
}</code></pre>
<p> </p>
<p><strong><span style="font-size:16px;">三、程序运行</span></strong></p>
<p> </p>
<p><span style="font-size:16px;">下载程序后,复位开发板</span></p>
<p>fb9f6920281aa69c8a1a56c3ed8f4c65<br />
</p>
<p> </p>
<p>都是代码,SPI-DMA方式驱动LCD看结果是行的</p>
<p> </p>
<p>请问你的system_clock_config()是怎样的呢?我调用了system_clock_config就没显示了,时钟有什么要求呢?</p>
页:
[1]