2324 views|0 replies

1w

Posts

16

Resources
The OP
 

MM32F031 Development Board Review 10: Hardware-Driven SPI [Copy link]

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
  1. 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);
  2. }
  3. void LCD_RS_CLR(void)
  4. {
  5.     GPIO_ResetBits(GPIOA,GPIO_Pin_11);
  6. }
  7. void LCD_RS_SET(void)
  8. {
  9.     GPIO_SetBits(GPIOA,GPIO_Pin_11);
  10. }
复制代码




然后在基本函数中加入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通讯为硬件:
  1. unsigned int SPIMReadWriteByte(unsigned char tx_data)
  2. {
  3.     SPI_SendData(SPI2, tx_data);        
  4.     while (1)
  5.     {
  6.         if(SPI_GetFlagStatus(SPI2, SPI_FLAG_RXAVL))        
  7.         {
  8.             return SPI_ReceiveData(SPI2);
  9.         }
  10.     }
  11. }
复制代码


在主函数只是清屏为蓝色:
  1. int main(void)
  2. {
  3.     delay_init();
  4.     uart_initwBaudRate(115200);                         //′ú3êˉa115200
  5.     SPIM_Init(8);
  6.           Lcd_Init();
  7.     Lcd_Clear(BLUE);        
  8.     while(1)                              //TT-·
  9.     {
  10.         
  11.     }
  12. }
复制代码


以下是运行时的照片:


Personal signaturehttp://shop34182318.taobao.com/
https://shop436095304.taobao.com/?spm=a230r.7195193.1997079397.37.69fe60dfT705yr
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list