Today I worked on the hardware driver SPI. I originally wanted to give it a try because I didn't get it done on Sunday. I just wanted to give it a try. Maybe I have some basic knowledge of the simulated SPI I worked on a few days ago. This time it was done in one go. First, use the SPI initialization in the example
- void SPIM_Init(unsigned short spi_baud_div) { SPI_InitTypeDef SPI_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE); //SPI2 clk enable SPIM_CSHigh(); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA|RCC_AHBPeriph_GPIOB, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //spi2_cs pb12 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // ía′óê3 GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; //spi2_sck pb13 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // ía′óê3 GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15; //spi2_mosi pb15 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // ía′óê3 GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14; //spi2_miso pb14 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //éà-êè GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11|GPIO_Pin_12; //PA11 RS PA12 REST GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // ía′óê3 GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOB,GPIO_PinSource13,GPIO_AF_0);//DèòaSPIμIO GPIO_PinAFConfig(GPIOB,GPIO_PinSource14,GPIO_AF_0); GPIO_PinAFConfig(GPIOB,GPIO_PinSource15,GPIO_AF_0); SPI_Mode_Master; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; SPI_InitStructure.SPI_DataWidth = SPI_DataWidth_8b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; // mode0 SPI_CPOL_Low, SPI_CPHA_1Edge; SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; // mode3 SPI_CPOL_High,SPI_CPHA_2Edge SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = spi_baud_div; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_Init(SPI2, &SPI_InitStructure); SPIM_TXEn(); SPIM_RXEn(); SPI_Cmd(SPI2, ENABLE); //Enables the specified SPI peripheral SPIê1ü Then, use two lines to control RS and REST. I defined PA11 and PA12. Of course, the PA port clock must be enabled during initialization and defined as output [code] void LCD_RST_CLR(void) { GPIO_ResetBits(GPIOA,GPIO_Pin_12); } void LCD_RST_SET(void) { GPIO_SetBits(GPIOA,GPIO_Pin_12);
- }
- void LCD_RS_CLR(void)
- {
- GPIO_ResetBits(GPIOA,GPIO_Pin_11);
- }
- void LCD_RS_SET(void)
- {
- GPIO_SetBits(GPIOA,GPIO_Pin_11);
- }
复制代码
然后在基本函数中加入RS函数
void Lcd_WriteIndex(u8 Index)
{
LCD_RS_CLR();
SPIMReadWriteByte(Index);
}
void Lcd_WriteData(u8 Data)
{
//LCD_CS_CLR();
LCD_RS_SET();
SPIMReadWriteByte(Data);
///LCD_CS_SET();
}
void Lcd_WriteData_16Bit(u16 Data)
{
Lcd_WriteData(Data>>8);
Lcd_WriteData(Data);
}
void LCD_WriteReg(u8 Index,u16 Data)
{
Lcd_WriteIndex(Index);
Lcd_WriteData_16Bit(Data);
}
void Lcd_Reset(void)
{
LCD_RST_CLR();
delay_ms(100);
LCD_RST_SET();
delay_ms(50);
}[/code]
而SPI通讯为硬件:
- unsigned int SPIMReadWriteByte(unsigned char tx_data)
- {
- SPI_SendData(SPI2, tx_data);
- while (1)
- {
- if(SPI_GetFlagStatus(SPI2, SPI_FLAG_RXAVL))
- {
- return SPI_ReceiveData(SPI2);
- }
- }
- }
复制代码
在主函数只是清屏为蓝色:
- int main(void)
- {
- delay_init();
- uart_initwBaudRate(115200); //′ú3êˉa115200
- SPIM_Init(8);
- Lcd_Init();
- Lcd_Clear(BLUE);
- while(1) //TT-·
- {
-
- }
- }
复制代码
以下是运行时的照片: