今天我们来搞下,用AT407板子的ardunio 接口的SPI来驱动LCD屏幕,它的芯片是XH8347D~诚如我在群里面说的,板子硬件设计时,一些引脚的复用,还是没考虑到位~
就如PA8引脚,可以选择一边可接到SPI FLASH 的NSS端,或者PA8_E端,然后PA8_E就是连接到ardunio接口上,但是他有把PA8_E又通过R64接到了
DM9162的REF_CLOCK_25M。
废话不多说,我用的LCD接口正好用到了PA8,只好焊掉了R64再调试~
屏幕ardunio接口:
我们配置好SPI先:
static void SPI1_GPIO_Configuration(void)
{
GPIO_InitType GPIO_InitStructure;
RCC_APB2PeriphClockCmd(SPI1_GPIO_CLK|RCC_APB2PERIPH_SPI1|RCC_APB2PERIPH_AFIO, ENABLE);
/* Configure SPI_FLASH pins*/
GPIO_InitStructure.GPIO_Pins = SPI1_PIN_MOSI;
GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(SPI1_GPIO, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pins = SPI1_PIN_MISO;
GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(SPI1_GPIO, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pins = SPI1_PIN_SCK;
GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(SPI1_GPIO, &GPIO_InitStructure);
//CS
GPIO_InitStructure.GPIO_Pins = LCD_CS_PIN;
GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT_PP;
GPIO_Init(LCD_CS_GPIO, &GPIO_InitStructure);
}
//SPI1 初始化
void SPI1_Init(void)
{
SPI_InitType SPI_InitStructure;
/* Enable SPIy clock and GPIO clock for SPIy and SPIz */
RCC_APB2PeriphClockCmd(SPI1_CLK , ENABLE);
/* 1st phase: SPI1 Master */
/* GPIO configuration ------------------------------------------------------*/
SPI1_GPIO_Configuration();
//CS 置高
__LCD_CS_SET();
/* SPI1 Config -------------------------------------------------------------*/
SPI_DefaultInitParaConfig(&SPI_InitStructure);
SPI_InitStructure.SPI_TransMode = SPI_TRANSMODE_FULLDUPLEX;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2EDGE;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_HIGH;
SPI_InitStructure.SPI_CPOLY = 0;
SPI_InitStructure.SPI_FirstBit = SPI_FIRSTBIT_MSB;
SPI_InitStructure.SPI_FrameSize = SPI_FRAMESIZE_8BIT;
SPI_InitStructure.SPI_MCLKP = SPI_MCLKP_8;
SPI_InitStructure.SPI_NSSSEL = SPI_NSSSEL_SOFT;
SPI_InitStructure.SPI_Mode = SPI_MODE_MASTER;
SPI_Init(SPI1, &SPI_InitStructure);
/* Enable SPI1 */
SPI_Enable(SPI1, ENABLE);
}
然后编写SPI发送读取已个字节的函数:
unsigned int SPI1ReadWriteByte(unsigned char tx_data)
{
/* Wait for SPIy Tx buffer empty */
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TE) == RESET);
/* Send SPIy data */
SPI_I2S_TxData(SPI1,tx_data);
/* Wait for SPIy data reception */
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RNE) == RESET);
/* Read SPIy received data */
return SPI_I2S_RxData(SPI1);
}
下面就对LCD的其他引脚进行配置:
这里用到PA15,PA15又关联到JTAG的JDI,引脚,所以首先要remap引脚,到关闭JTAG仅有SWD的功能配置上,这样就释放了PA15:
GPIO_PinsRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);//释放PA15 PB3 PB4
引脚配置:
void lcd_port_init(void)
{
GPIO_InitType GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_APB2PERIPH_GPIOA| RCC_APB2PERIPH_GPIOC||RCC_APB2PERIPH_AFIO , ENABLE);
//调试仅SWD
GPIO_PinsRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);//释放PA15 PB3 PB4
GPIO_StructInit(&GPIO_InitStructure);
//LCD-CS PA15
GPIO_InitStructure.GPIO_Pins = LCD_CS_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT_PP;
GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_50MHz;
GPIO_Init(LCD_CS_GPIO, &GPIO_InitStructure);
//LCD-DC PA8
GPIO_InitStructure.GPIO_Pins = LCD_DC_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT_PP;
GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_50MHz;
GPIO_Init(LCD_DC_GPIO, &GPIO_InitStructure);
//LCD-BKL PC7
GPIO_InitStructure.GPIO_Pins = LCD_BKL_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT_PP;
GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_50MHz;
GPIO_Init(LCD_BKL_GPIO, &GPIO_InitStructure);
}
其他的都是写LCD相关的函数,这里就不一一列出了。
下载调试:
程序(把库集在一起了):
SPI_LCD.rar
(5.24 MB)
|