上次我们添加了软件SPI点亮我们的TFT液晶屏,今天我们测试下我们的硬件SPI完成我们的屏幕点亮,中间也遇到些问题,也参考了下网友分享的解决方案。 #define hardware_spi 0
#define software_spi 1
#define LCD_SPI hardware_spi
void LCD_GPIO_Init(void)
{
GPIO_InitType GPIO_InitStructure;
RCC_AHB_Peripheral_Clock_Enable(RCC_AHB_PERIPH_GPIOA); //使能A端口时钟
GPIO_Structure_Initialize(&GPIO_InitStructure);
#if LCD_SPI==software_spi
/* Select the GPIO pin to control */
GPIO_InitStructure.Pin = GPIO_PIN_5|GPIO_PIN_7|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4;
/* Set pin mode to general push-pull output */
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUT_PP;
/* Set the pin drive current to 4MA*/
GPIO_InitStructure.GPIO_Current = GPIO_DS_4MA;
/* Initialize GPIO */
GPIO_Peripheral_Initialize(GPIOA, &GPIO_InitStructure);
GPIO_Pins_Set(GPIOA,GPIO_PIN_5|GPIO_PIN_7|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4);
#elif LCD_SPI==hardware_spi
SPI_InitType SPI_InitStructure;
GPIO_Structure_Initialize(&GPIO_InitStructure);
RCC_APB2_Peripheral_Clock_Enable(RCC_APB2_PERIPH_SPI1);
/* Configure master pins: NSS, SCK, MISO and MOSI */
/* Confugure SPI pins as Alternate Function Push Pull */
GPIO_InitStructure.Pin = GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AF_PP;
GPIO_InitStructure.GPIO_Slew_Rate = GPIO_SLEW_RATE_FAST;
GPIO_InitStructure.GPIO_Alternate = GPIO_AF1_SPI1;
GPIO_Peripheral_Initialize(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.Pin = GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4;
/* Set pin mode to general push-pull output */
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUT_PP;
/* Set the pin drive current to 4MA*/
GPIO_InitStructure.GPIO_Current = GPIO_DS_4MA;
/* Initialize GPIO */
GPIO_Peripheral_Initialize(GPIOA, &GPIO_InitStructure);
GPIO_Pins_Set(GPIOA,GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4);
// SPI_I2S_Reset(SPI1);
/* SPI_MASTER configuration ------------------------------------------------------*/
SPI_Initializes_Structure(&SPI_InitStructure);
SPI_InitStructure.DataDirection = SPI_DIR_DOUBLELINE_FULLDUPLEX;
SPI_InitStructure.SpiMode = SPI_MODE_MASTER;
SPI_InitStructure.DataLen = SPI_DATA_SIZE_8BITS;
SPI_InitStructure.CLKPOL = SPI_CLKPOL_HIGH;
SPI_InitStructure.CLKPHA = SPI_CLKPHA_SECOND_EDGE;
SPI_InitStructure.NSS = SPI_NSS_SOFT;
/* It is recommended that the SPI master mode of the C version chips should not exceed 18MHz */
SPI_InitStructure.BaudRatePres = SPI_BR_PRESCALER_2;
SPI_InitStructure.FirstBit = SPI_FB_MSB;
SPI_InitStructure.CRCPoly = 7;
SPI_Initializes(SPI1, &SPI_InitStructure);
SPI_Set_Nss_Level(SPI1, SPI_NSS_HIGH);
/* Enable SPI_MASTER */
SPI_ON(SPI1);
#endif
}
/******************************************************************************
函数说明:LCD串行数据写入函数
入口数据:dat 要写入的串行数据
返回值: 无
******************************************************************************/
void LCD_Writ_Bus(u8 dat)
{
#if LCD_SPI==software_spi
u8 i;
for(i=0;i<8;i++)
{
LCD_SCLK_Clr();
if(dat&0x80)
{
LCD_MOSI_Set();
}
else
{
LCD_MOSI_Clr();
}
LCD_SCLK_Set();
dat<<=1;
}
#elif LCD_SPI==hardware_spi
while(SPI_I2S_Flag_Status_Get(SPI1, SPI_I2S_FLAG_TE) == RESET);
SPI_I2S_Data_Transmit(SPI1,dat);
while (SPI_I2S_Flag_Status_Get(SPI1, SPI_I2S_FLAG_RNE) == RESET);
SPI_I2S_Data_Get(SPI1);
#endif
}
看下代码就是上面的,初始化IO和发送函数里面我们添加了区分,其余地方软件和硬件SPI都一样。 原本我只使用发送的,结果屏幕就是没有亮,加上读取SPI数据后就能点亮了,应该就这块要注意下。 SHT20_GetValue();
LCD_ShowChinese(0,0,"国民技术",RED,WHITE,32,0);
//温湿度显示
LCD_ShowString(0,60,"TEMP:",BLACK,WHITE,24,0);
LCD_ShowFloatNum1(100,60,sht20Info.tempreture,4,BLACK,WHITE,24);
LCD_ShowString(150,60,"C",BLACK,WHITE,24,0);
LCD_ShowString(0,90,"HUMI:",BLACK,WHITE,24,0);
LCD_ShowFloatNum1(100,90,sht20Info.humidity,4,BLACK,WHITE,24);
LCD_ShowString(150,90,"%",BLACK,WHITE,24,0);
然后我们加如温湿度显示数据,这样屏幕上就能获取到我们温湿度传感器数据然后显示出来了。非常的直观。 下面看下硬件接线。 后续我们也可以制作中文字体替换。
|