找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3788|回复: 0
打印 上一主题 下一主题
收起左侧

ST7735驱动程序

[复制链接]
跳转到指定楼层
楼主
ID:329438 发表于 2018-5-14 11:02 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include "stm32f0xx.h"


#define LCD_SCK_PIN        GPIO_Pin_13
#define LCD_SCK_PORT       GPIOB
#define LCD_SCK_PIN_SCK    RCC_AHBPeriph_GPIOB
#define LCD_SCK_SOURCE     GPIO_PinSource13
#define LCD_SCK_AF         GPIO_AF_0


#define LCD_MISO_PIN       GPIO_Pin_14
#define LCD_MISO_PORT      GPIOB
#define LCD_MISO_PIN_SCK   RCC_AHBPeriph_GPIOB
#define LCD_MISO_SOURCE    GPIO_PinSource14
#define LCD_MISO_AF        GPIO_AF_0


#define LCD_MOSI_PIN       GPIO_Pin_15
#define LCD_MOSI_PORT      GPIOB
#define LCD_MOSI_PIN_SCK   RCC_AHBPeriph_GPIOB
#define LCD_MOSI_SOURCE    GPIO_PinSource15
#define LCD_MOSI_AF        GPIO_AF_0




#define LCD_SPI2               RCC_APB1Periph_SPI2
#define SPI_LCD_CS_LOW()       GPIO_ResetBits(GPIOB, GPIO_Pin_12)
#define SPI_LCD_CS_HIGH()      GPIO_SetBits(GPIOB, GPIO_Pin_12)
#define LCD_CS_PORT GPIOB
#define LCD_CS_PIN GPIO_Pin_12




#define SPI_LCD_REST()       GPIO_ResetBits(GPIOA, GPIO_Pin_6)
#define SPI_LCD_NO_REST()     GPIO_SetBits(GPIOA, GPIO_Pin_6)


#define SPI_LCD_DATA_W()       GPIO_ResetBits(GPIOA, GPIO_Pin_7)
#define SPI_LCD_DATA_R()       GPIO_SetBits(GPIOA, GPIO_Pin_7)




#define uchar unsigned char
#define uint  unsigned int
#define Height 128
#define Width   128
#define RED     0xf800
#define GREEN   0x07e0
#define BLUE   0x001f
#define YELLOW 0xffe0
#define WHITE   0xffff
#define BLACK   0x0000
#define PURPLE 0xf81f




/*******************************************************************************
* Function Name  : SPI_FLASH_Init
* Description    : Initializes the peripherals used by the SPI FLASH driver.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void lcd_Init(void)
{
   GPIO_InitTypeDef  GPIO_InitStruct;
   SPI_InitTypeDef   SPI_InitStruct;




   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA| RCC_AHBPeriph_GPIOB, ENABLE);

   RCC_APB1PeriphClockCmd(LCD_SPI2, ENABLE);

GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;                 
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);        


   /*!< Configure SD_SPI pins: SCK */
   GPIO_InitStruct.GPIO_Pin = LCD_SCK_PIN;
   GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
   GPIO_InitStruct.GPIO_Speed = GPIO_Speed_Level_3;
   GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
   GPIO_InitStruct.GPIO_PuPd  = GPIO_PuPd_UP;
   GPIO_Init(LCD_SCK_PORT, &GPIO_InitStruct);


   /*!< Configure SD_SPI pins: MISO */
   GPIO_InitStruct.GPIO_Pin = LCD_MISO_PIN;
   GPIO_Init(LCD_MISO_PORT, &GPIO_InitStruct);


   /*!< Configure SD_SPI pins: MOSI */
   GPIO_InitStruct.GPIO_Pin =LCD_MOSI_PIN;
   GPIO_Init(LCD_MOSI_PORT, &GPIO_InitStruct);

   /* Connect PXx to SD_SPI_SCK */
   GPIO_PinAFConfig(LCD_SCK_PORT, LCD_SCK_SOURCE, LCD_SCK_AF);


   /* Connect PXx to SD_SPI_MISO */
   GPIO_PinAFConfig(LCD_MISO_PORT, LCD_MISO_SOURCE, LCD_MISO_AF);


   /* Connect PXx to SD_SPI_MOSI */
   GPIO_PinAFConfig(LCD_MOSI_PORT, LCD_MOSI_SOURCE, LCD_MOSI_AF);


/*!< Configure SD_SPI_CS_PIN pin: SD Card CS pin */
   GPIO_InitStruct.GPIO_Pin =LCD_CS_PIN;
   GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
   GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
   GPIO_InitStruct.GPIO_Speed = GPIO_Speed_Level_3;
   GPIO_Init(LCD_CS_PORT, &GPIO_InitStruct);

   SPI_LCD_CS_HIGH();

   /*!< SD_SPI Config */
   SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
   SPI_InitStruct.SPI_Mode = SPI_Mode_Master;
   SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b;
   SPI_InitStruct.SPI_CPOL = SPI_CPOL_High;
   SPI_InitStruct.SPI_CPHA = SPI_CPHA_2Edge;
   SPI_InitStruct.SPI_NSS = SPI_NSS_Soft;
   SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
   SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;
   SPI_InitStruct.SPI_CRCPolynomial = 7;
   SPI_Init(SPI2, &SPI_InitStruct);
   SPI_RxFIFOThresholdConfig(SPI2, SPI_RxFIFOThreshold_QF);
   SPI_Cmd(SPI2, ENABLE); /*!< SD_SPI enable */
//  
// SPI_FLASH_CS_LOW();

}


/*******************************************************************************
* Function Name  : SPI_LCD_SendByte
* Description    : Sends a byte through the SPI interface and return the byte
*                  received from the SPI bus.
* Input          : byte : byte to send.
* Output         : None
* Return         : The value of the received byte.
*******************************************************************************/
uint8_t SPI_LCD_SendByte(uint8_t byte)
{
   /* Loop while DR register in not emplty */
   while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);


   /* Send byte through the SPI1 peripheral */
   SPI_SendData8(SPI2, byte);


   /* Wait to receive a byte */
   while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET);


   /* Return the byte read from the SPI bus */
   return SPI_ReceiveData8(SPI2);
}




//******************************************//
void delay(int count)   /* X10ms */
{
        unsigned int i,j;
         for(i=0;i<count;i++)        
               for(j=0;j<50;j++);            
}



//**********write register command***********//
void  LCD_WriteCommand(unsigned short c)
{
SPI_LCD_CS_LOW();
SPI_LCD_DATA_W();
SPI_LCD_SendByte(c);
SPI_LCD_CS_HIGH();
}



//***************write  data****************//
void LCD_WriteData(unsigned short dat)
{
SPI_LCD_CS_LOW();
SPI_LCD_DATA_R();
SPI_LCD_SendByte(dat);
SPI_LCD_CS_HIGH();
}




//******************************************//
void LCD_Write_Data(uint dat16)
{
   LCD_WriteData(dat16>>8);
   LCD_WriteData(dat16);
}



void  ST7735S_CPT144_Initial ()
{
//------------------------------------ST7735R Reset Sequence-----------------------------------------//
SPI_LCD_NO_REST();
delay (100);                                                              //Delay 1ms
SPI_LCD_REST();
delay (100);                                                              //Delay 1ms
SPI_LCD_NO_REST();
delay (300);                                                              //Delay 120ms
//--------------------------------End ST7735S Reset Sequence ---------------------------------------//
LCD_WriteCommand(0x11); //Sleep out
delay(120); //Delay 120ms
//------------------------------------ST7735S Frame rate-------------------------------------------------//
LCD_WriteCommand(0xB1); //Frame rate 80Hz
LCD_WriteData(0x02);
LCD_WriteData(0x35);
LCD_WriteData(0x36);
LCD_WriteCommand(0xB2); //Frame rate 80Hz
LCD_WriteData(0x02);
LCD_WriteData(0x35);
LCD_WriteData(0x36);
LCD_WriteCommand(0xB3); //Frame rate 80Hz
LCD_WriteData(0x02);
LCD_WriteData(0x35);
LCD_WriteData(0x36);
LCD_WriteData(0x02);
LCD_WriteData(0x35);
LCD_WriteData(0x36);
//------------------------------------End ST7735S Frame rate-------------------------------------------//
LCD_WriteCommand(0xB4); //Dot inversion
LCD_WriteData(0x03);
//------------------------------------ST7735S Power Sequence-----------------------------------------//
LCD_WriteCommand(0xC0);
LCD_WriteData(0xA2);
LCD_WriteData(0x02);
LCD_WriteData(0x84);
LCD_WriteCommand(0xC1);
LCD_WriteData(0xC5);
LCD_WriteCommand(0xC2);
LCD_WriteData(0x0D);
LCD_WriteData(0x00);
LCD_WriteCommand(0xC3);
LCD_WriteData(0x8D);
LCD_WriteData(0x2A);
LCD_WriteCommand(0xC4);
LCD_WriteData(0x8D);
LCD_WriteData(0xEE);
//---------------------------------End ST7735S Power Sequence---------------------------------------//
LCD_WriteCommand(0xC5); //VCOM
LCD_WriteData(0x0a);
LCD_WriteCommand(0x36); //MX, MY, RGB mode
LCD_WriteData(0xC8);
//------------------------------------ST7735S Gamma Sequence-----------------------------------------//
LCD_WriteCommand(0XE0);
LCD_WriteData(0x12);
LCD_WriteData(0x1C);
LCD_WriteData(0x10);
LCD_WriteData(0x18);
LCD_WriteData(0x33);
LCD_WriteData(0x2C);
LCD_WriteData(0x25);
LCD_WriteData(0x28);
LCD_WriteData(0x28);
LCD_WriteData(0x27);
LCD_WriteData(0x2F);
LCD_WriteData(0x3C);
LCD_WriteData(0x00);
LCD_WriteData(0x03);
LCD_WriteData(0x03);
LCD_WriteData(0x10);
LCD_WriteCommand(0XE1);
LCD_WriteData(0x12);
LCD_WriteData(0x1C);
LCD_WriteData(0x10);
LCD_WriteData(0x18);
LCD_WriteData(0x2D);
LCD_WriteData(0x28);
LCD_WriteData(0x23);
LCD_WriteData(0x28);
LCD_WriteData(0x28);
LCD_WriteData(0x26);
LCD_WriteData(0x2F);
LCD_WriteData(0x3B);
LCD_WriteData(0x00);
LCD_WriteData(0x03);
LCD_WriteData(0x03);
LCD_WriteData(0x10);
//------------------------------------End ST7735S Gamma Sequence-----------------------------------------//
LCD_WriteCommand(0x3A); //65k mode
LCD_WriteData(0x05);
LCD_WriteCommand(0x29); //Display on
}




void DISP_WINDOWS(void)
{
LCD_WriteCommand(0x2A);
   //设置列地址
LCD_WriteData(0x00);
LCD_WriteData(0x02);
LCD_WriteData(0x00);
LCD_WriteData(0x81);


LCD_WriteCommand(0x2B);
   //设置行地址
LCD_WriteData(0x00);
LCD_WriteData(0x03);
LCD_WriteData(0x00);
LCD_WriteData(0x82);


LCD_WriteCommand(0x2C);
}


void show_icon(st_icon ic)
{
short i,p=0;
short sx,ex,sy,ey;

sx=ic.x;
ex=ic.w+sx;


sy=ic.y;
ey=sy;


do
{
LCD_WriteCommand(0x2A);
   //设置列地址
LCD_WriteData(0x00);
LCD_WriteData(sx+1);
LCD_WriteData(0x00);
LCD_WriteData(ex+1);


LCD_WriteCommand(0x2B);
   //设置行地址
LCD_WriteData(0x00);
LCD_WriteData(sy+2);
LCD_WriteData(0x00);
LCD_WriteData(sy+2);


LCD_WriteCommand(0x2C);


for(i=ex;i>sx;i--)
{
LCD_WriteData(ic.dat[p++]);
LCD_WriteData(ic.dat[p++]);
}
sy++;
}
while(p<(ic.w*ic.h));
}




void show_any_icon(unsigned short sx,unsigned short sy,unsigned short w,unsigned short h,unsigned char dat[])
{
short i,p=0;
unsigned short ex,ey;


ex=w+sx;
ey=sy;


do
{
LCD_WriteCommand(0x2A);
   //设置列地址
LCD_WriteData(0x00);
LCD_WriteData(sx+1);
LCD_WriteData(0x00);
LCD_WriteData(ex+1);


LCD_WriteCommand(0x2B);
   //设置行地址
LCD_WriteData(0x00);
LCD_WriteData(sy+2);
LCD_WriteData(0x00);
LCD_WriteData(sy+2);


LCD_WriteCommand(0x2C);


for(i=ex;i>sx;i--)
{
LCD_WriteData(dat[p++]);
LCD_WriteData(dat[p++]);
}
sy++;
}
while(p<(w*h));
}






//************显示带彩色图片***************//
void disp_clear(void)
{
uint i,j;
unsigned short p=BACK_COLOR;


   DISP_WINDOWS();
for(j=Height;j>0;j--)
{
for(i=Width;i>0;i--)
{
LCD_WriteData((unsigned char)((p&0xff00)>>8));
LCD_WriteData((unsigned char)(p&0x00ff));
}
}


//HOLD_DISP ();
}


void disp_NumDot()
{
uint i,j;
uchar p = 0;
   DISP_WINDOWS();
for(j=Height;j>0;j--)
{
for(i=Width;i>0;i--)
{
LCD_WriteData(0x00);
LCD_WriteData(p);
delay(50);
}
p++;
}
}


//**********显示带白框的黑色图片************//
void Frame(void)
{
int i,j,k;
   DISP_WINDOWS();
   for (i=Width;i>0;i--)
{
     LCD_Write_Data(WHITE);
}
for (j=Height-2;j>0;j--)
{
     LCD_Write_Data(WHITE);
     for (k=Width-2;k>0;k--)
{
LCD_Write_Data(BLACK);
}
LCD_Write_Data(WHITE);
}
for (i=Width;i>0;i--)
{
     LCD_Write_Data(WHITE);
}
// HOLD_DISP ();
}


/*蓝牙图标*/

显示图标的代码如下:

typedef struct __st_icon
{
unsigned short x;//图标X轴起始点
unsigned short y;//图标Y轴起始点
unsigned short w;//图标宽度
unsigned short h;//图标高度
unsigned short len;
unsigned short color;//图标颜色
const unsigned char *dat;//图标取模得到的数据
} st_icon;


//蓝牙图片数据
#define BT_HIGHT 16
#define BT_WIGHT 16
const unsigned char gImage_blue[32] = { /* 0X00,0X01,0X10,0X00,0X10,0X00, */
0X01,0X00,0X01,0X80,0X01,0XC0,0X01,0XE0,0X0D,0XB0,0X0F,0XF0,0X07,0XE0,0X03,0XC0,
0X03,0XC0,0X07,0XE0,0X0F,0XF0,0X0D,0XB0,0X01,0XE0,0X01,0XC0,0X01,0X80,0X01,0X00,
};


st_icon g_st_bt_icon;


/*初始化蓝牙图片
设定起始地址、大小和数据

*/
void bt_init(void)
{
g_st_bt_icon.x=50;
g_st_bt_icon.y=2;
g_st_bt_icon.w=BT_WIGHT;
g_st_bt_icon.h=BT_HIGHT;
g_st_bt_icon.dat=gImage_blue;
}


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏2 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表