STM32串行通信驱动LCD12864(使用5V电源,驱动信号用5VFT的引脚(3.3V MCU))

#include "spi.h"
 

void SPI_GPIO_Init(void)
{
    //hardware make PSB low to enable serial communication
    GPIO_InitTypeDef   GPIO_InitStruct;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_Init(GPIOB, &GPIO_InitStruct);

    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_11;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_Init(GPIOB, &GPIO_InitStruct);

    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;

    
    GPIO_Init(GPIOB, &GPIO_InitStruct);
}

void SPI_SID_IN(void)
{
    GPIO_InitTypeDef   GPIO_InitStruct;

    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12;
    

    GPIO_Init(GPIOB, &GPIO_InitStruct);
}

void SPI_SID_OUT(void)
{
    GPIO_InitTypeDef   GPIO_InitStruct;

    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_Init(GPIOB, &GPIO_InitStruct);
}
void SPI_Start(void)
{
    SPI_SID_L;
    Delayus(10);
    SPI_CLK_L;
    Delayus(10);
    SPI_CS_L;
    Delayus(10);
    SPI_CS_H;
}
void SPI_Stop(void)
{
    SPI_CS_L;
    Delayus(10);
//    SPI_SID_H;
//    Delayus(10);
//    SPI_CLK_H;
}
void SPI_Write(unsigned char data)
{
    uint8_t i = 0;
    for(i = 0; i < 8; i ++)
    {
        if((data&0x80) == 0x80)                             
        {                                                  
            SPI_SID_H;                                     
        }                                                  
        else                                              
        {                                                  
            SPI_SID_L;                                      
        }                                                  
        Delayus(10);                                      
                                                          
        SPI_CLK_H;
        Delayus(10);
        

        SPI_CLK_L;
        data = data << 1;
        Delayus(10);
    }
    
     
}

unsigned char SPI_Read(void)
{
    uint8_t i = 0;
    unsigned char val = 0;
    SPI_SID_IN();
    for(i = 0; i < 8; i ++)                                                   
    {
        val = val << 1;                                              
        
        SPI_CLK_H;                                                  
        Delayus(10);
                                                                  
        if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_12))                             
        {                                                            
            val |= 0x01;                                           
        }                                                            
        else                                                        
        {                                                            
            val |= 0x00;                                      
        }                                                  
        Delayus(10);

        SPI_CLK_L;
        
        Delayus(10);
    }
    return val;

}
void SPI_WriteCmd(unsigned char cmd)//0xf8 write cmd;0xfa write data;
{
    unsigned char high4bit = 0, low4bit = 0;
    SPI_Start();
    high4bit = (cmd & 0xF0);
    low4bit = ((cmd << 4)&0xF0);
    SPI_Write(0xF8);
    SPI_Write(high4bit);
    SPI_Write(low4bit);


    SPI_Stop();


}

void SPI_WriteData(unsigned char data)
{
    unsigned char high4bit = 0, low4bit = 0;
    SPI_Start();
    high4bit = (data & 0xF0);
    low4bit = ((data << 4)&0xF0);
    SPI_Write(0xFA);
    SPI_Write(high4bit);
    SPI_Write(low4bit);

    SPI_Stop();

}

uint8_t SPI_ReadCmd(void)//0xfc read cmd;0xfe read data;
{
    unsigned char high4bit = 0, low4bit = 0, val = 0;
    SPI_Start();
    
    SPI_Write(0xFC);
    high4bit = SPI_Read();
    low4bit =  SPI_Read();

    SPI_Stop();

    val = (high4bit+(low4bit >> 4));

    
    return val;

}

uint8_t SPI_ReadData(void)
{
    unsigned char high4bit = 0, low4bit = 0, val = 0;
    SPI_Start();
    
    SPI_Write(0xFE);
    high4bit = SPI_Read();
    low4bit =  SPI_Read();

    SPI_Stop();

    val = (high4bit+(low4bit >> 4));

    
    return val;
}

void WriteStr(unsigned char *s)
{
    while(*s != '\0')
    {
        SPI_WriteData(*s);
        s ++;

    }
}

void LCD_PutGraphic(const unsigned char *img)
{ 
 int i,j;
//显示上半屏内容设置
   for(i=0;i<32;i++)            
    { 
      SPI_WriteCmd(0x80 + i); //SET  垂直地址 VERTICAL ADD
      SPI_WriteCmd(0x80);     //SET  水平地址 HORIZONTAL ADD
      for(j=0;j<16;j++)
       {
         SPI_WriteData(*img);
         img++;
       }
    }
//显示下半屏内容设置
   for(i=0;i<32;i++)            
    {
      SPI_WriteCmd(0x80 + i); //SET 垂直地址 VERTICAL ADD
      SPI_WriteCmd(0x88);     //SET 水平地址 HORIZONTAL ADD
      for(j=0;j<16;j++)
       {
         SPI_WriteData(*img);
         img++;
       }
    }  
           
}
#include "stm32f10x.h"

#include "delay.h"

#include "led.h"
#include "spi.h"

//12864是5V电源供电才行;数据时钟用3.3VMCU输出即可,用FT5V引脚

const unsigned char pic1[]=      // 图片代码
{
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,
0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,
0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,
0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,
0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,
0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,
0xC0,0x1F,0xFF,0x81,0xFF,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,
0xC0,0x1F,0xFF,0x81,0xFF,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,
0xC0,0x18,0x01,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x08,0x00,0x00,0x00,0x03,
0xC0,0x1F,0xFF,0x80,0x00,0x00,0x04,0x07,0x81,0xE0,0x10,0x10,0x03,0xC0,0x30,0x03,
0xC0,0x1F,0xFF,0x80,0x00,0x00,0x0C,0x0C,0xC3,0x30,0x08,0x20,0x0C,0xE0,0x70,0x03,
0xC0,0x18,0x01,0x87,0xFF,0xFE,0x3C,0x18,0x66,0x18,0x04,0x40,0x0C,0x60,0x70,0x03,
0xC0,0x1F,0xFF,0x87,0xFF,0xFE,0x0C,0x18,0x66,0x18,0x02,0x80,0x18,0x00,0xB0,0x03,
0xC0,0x1F,0xFF,0x80,0x06,0x00,0x0C,0x18,0x66,0x18,0x01,0x00,0x1B,0x81,0x30,0x03,
0xC0,0x01,0x98,0x00,0x06,0x00,0x0C,0x00,0xE3,0x30,0x02,0x80,0x1C,0xC1,0x30,0x03,
0xC0,0x19,0x99,0x80,0x66,0x60,0x0C,0x00,0xC1,0xE0,0x04,0x40,0x18,0x62,0x30,0x03,
0xC0,0x19,0x99,0x80,0x66,0x30,0x0C,0x01,0x83,0x30,0x08,0x20,0x18,0x66,0x30,0x03,
0xC0,0x0D,0x9B,0x00,0xC6,0x38,0x0C,0x03,0x06,0x18,0x10,0x10,0x18,0x67,0xFC,0x03,
0xC0,0x0D,0x9B,0x01,0xC6,0x1C,0x0C,0x06,0x06,0x18,0x20,0x08,0x18,0x60,0x30,0x03,
0xC0,0x01,0x98,0x03,0x86,0x0C,0x0C,0x0C,0x06,0x18,0x00,0x00,0x08,0x60,0x30,0x03,
0xC0,0x7F,0xFF,0xE3,0x06,0x00,0x0C,0x18,0x03,0x30,0x00,0x00,0x0C,0xC0,0x30,0x03,
0xC0,0x7F,0xFF,0xE0,0x1E,0x00,0x0C,0x1F,0xE1,0xE0,0x00,0x00,0x07,0x80,0x30,0x03,
0xC0,0x00,0x00,0x00,0x1C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,
0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,
0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,
0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,
0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,
0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,
0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,
0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,
0xC0,0x00,0x00,0x00,0x00,0x00,0x0C,0x00,0x00,0x18,0x00,0x00,0x00,0x06,0x0C,0x03,
0xC3,0xFF,0xFC,0x3F,0xF0,0x80,0x0C,0x00,0x3F,0x18,0x01,0xFF,0xFE,0x06,0x0C,0x03,
0xC3,0xFF,0xFC,0x3F,0xF1,0x80,0x0F,0xFC,0x3F,0x18,0x01,0xFF,0xFE,0x06,0x0C,0x03,
0xC3,0x0C,0x0C,0x0C,0xC3,0x00,0x0F,0xFC,0x33,0x7F,0xE1,0x86,0x06,0x06,0x0C,0x03,
0xC3,0x1F,0xCC,0x0C,0xC6,0x00,0x0C,0x00,0x33,0x7F,0xE1,0x8F,0xE6,0x06,0x0C,0x03,
0xC3,0x3F,0xCC,0x0C,0xCC,0x00,0x0C,0x00,0x36,0x30,0x01,0x9F,0xE6,0x07,0xFF,0xE3,
0xC3,0x79,0x8C,0x0C,0xC8,0x00,0xFF,0xF0,0x36,0x36,0x01,0xBC,0xC6,0x07,0xFF,0xE3,
0xC3,0x6F,0x0C,0x7F,0xF0,0xC0,0xFF,0xF0,0x36,0x66,0x01,0xB7,0x86,0x06,0x00,0x03,
0xC3,0x0F,0x0C,0x7F,0xF1,0x80,0xC0,0x30,0x36,0x7F,0xC1,0x87,0x86,0x06,0x00,0x03,
0xC3,0x79,0xEC,0x0C,0xC3,0x00,0xC0,0x30,0x33,0x7F,0xC1,0xBC,0xF6,0x06,0x00,0x03,
0xC3,0x76,0xEC,0x0C,0xC6,0x00,0xC0,0x30,0x33,0x06,0x01,0xBB,0x76,0x07,0xFF,0x03,
0xC3,0x07,0x0C,0x0C,0xCC,0x20,0xFF,0xF0,0x33,0x06,0x01,0x83,0x86,0x07,0xFF,0x03,
0xC3,0x03,0x0C,0x18,0xC8,0x60,0xFF,0xF0,0x3F,0x06,0x01,0x81,0x86,0x06,0x03,0x03,
0xC3,0x1C,0x0C,0x18,0xC0,0xC0,0x00,0x00,0x3E,0xFF,0xE1,0x8E,0x06,0x06,0x03,0x03,
0xC3,0x1F,0x0C,0x18,0xC1,0x81,0xB1,0x8C,0x30,0xFF,0xE1,0x8F,0x86,0x06,0x03,0x03,
0xC3,0x03,0x0C,0x30,0xC7,0x01,0xB1,0x8C,0x30,0x06,0x01,0x81,0x86,0x0C,0x03,0x03,
0xC3,0xFF,0xFC,0x70,0xDE,0x03,0x18,0xC6,0x30,0x06,0x01,0xFF,0xFE,0x0C,0x03,0x03,
0xC3,0xFF,0xFC,0x20,0x18,0x03,0x18,0xC6,0x30,0x06,0x01,0xFF,0xFE,0x18,0x03,0x03,
0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,
0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,
0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,
0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,
0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,
0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,
0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,
0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,
0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,
0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,
0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,
0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,

};
int main(void)
{
    Delay_Init();
    LED_GPIO_Init();
    SPI_GPIO_Init();
    
    Delayus(10000);
    SPI_WriteCmd(0x30);
    Delayus(1000);
    SPI_WriteCmd(0x30);
    Delayus(1000);
    SPI_WriteCmd(0x0c);
    Delayus(10000);
    SPI_WriteCmd(0x01);
    Delayus(10000);

    SPI_WriteCmd(0x06);
    Delayus(10000);

//    SPI_WriteCmd(0x80);
//    WriteStr("你好");

    SPI_WriteCmd(0x36);
    Delayus(10000);

    LCD_PutGraphic(pic1);
    while(1)
    {
                
    }
    
}

 

posted @ 2024-04-05 21:26  了解2号  阅读(264)  评论(0编辑  收藏  举报