#include "stm32f10x_spi.h"
#include "main.h"
#include "fonts.h"
#include "lcd.h"
本文引用地址:http://butianyuan.cn/article/201611/321618.htmvoid LCD_WriteReg(u8 LCD_RegValue);
void LCD_WriteDat(u8 LCD_DatValue);
staticvu16 TextColor = 0x0000, BackColor = 0xFFFF;
#defineGREEN0x07e0
#defineRED0xF800
#defineBLUE0x001f
#define XPIXES 8
#define YPIXES 16
//所有函數(shù)的目錄及函數(shù)功能
void STM3210B_LCD_Init(void)
{
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB| RCC_APB2Periph_GPIOD | RCC_APB1Periph_SPI2, ENABLE);
LCD_CtrlLinesConfig();
LCD_SPIConfig();
//Start
LCD_CtrlLinesWrite(GPIOB, CtrlPin_NCS, Bit_RESET);
LCD_CtrlLinesWrite(GPIOD, CtrlPin_NWR, Bit_SET);
Delay(1);
LCD_CtrlLinesWrite(GPIOD, CtrlPin_NWR, Bit_RESET);
Delay(10);
LCD_CtrlLinesWrite(GPIOD, CtrlPin_NWR, Bit_SET);
LCD_WriteReg(0x11);
Delay(10);
LCD_WriteReg(0x0c0);
LCD_WriteDat(0x05);
LCD_WriteReg(0x0c5);
LCD_WriteDat(0x0d1);
LCD_WriteReg(0x0c6);
LCD_WriteDat(0x17);
Delay(10);
LCD_WriteReg(0x0e0);
LCD_WriteDat(0x00);
LCD_WriteDat(0x01);
LCD_WriteDat(0x0c);
LCD_WriteDat(0x29);
LCD_WriteDat(0x27);
LCD_WriteDat(0x0b);
LCD_WriteDat(0x11);
LCD_WriteDat(0x06);
LCD_WriteDat(0x06);
LCD_WriteDat(0x05);
LCD_WriteDat(0x0e);
LCD_WriteDat(0x0f);
LCD_WriteDat(0x00);
LCD_WriteDat(0x03);
LCD_WriteDat(0x04);
LCD_WriteDat(0x08);
LCD_WriteReg(0x0e1);
LCD_WriteDat(0x06);
LCD_WriteDat(0x1f);
LCD_WriteDat(0x20);
LCD_WriteDat(0x16);
LCD_WriteDat(0x0a);
LCD_WriteDat(0x04);
LCD_WriteDat(0x04);
LCD_WriteDat(0x10);
LCD_WriteDat(0x06);
LCD_WriteDat(0x03);
LCD_WriteDat(0x03);
LCD_WriteDat(0x02);
LCD_WriteDat(0x0e);
LCD_WriteDat(0x0d);
LCD_WriteDat(0x05);
LCD_WriteDat(0x05);
LCD_WriteReg(0x2a);
LCD_WriteDat(0x00);
LCD_WriteDat(0x00);
LCD_WriteDat(0x00);
LCD_WriteDat(0x7f);
LCD_WriteReg(0x2b);//row address set
LCD_WriteDat(0x00);
LCD_WriteDat(0x00);
LCD_WriteDat(0x00);
LCD_WriteDat(0x9f);
LCD_WriteReg(0x3a);
LCD_WriteDat(0x55);
LCD_WriteReg(0x36);//memory data access control(MADCTR)
LCD_WriteDat(0x00);//MY=0; MX=0; MV=0; ML=1; RGB=0
LCD_WriteReg(0x29);//display on
Delay(20);
}
void LCD_WriteReg(u8 LCD_RegValue)
{
LCD_CtrlLinesWrite(GPIOB, CtrlPin_NCS, Bit_RESET);
LCD_CtrlLinesWrite(GPIOD, CtrlPin_RS, Bit_RESET);
while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE)==RESET);
SPI_I2S_SendData(SPI2, LCD_RegValue);
while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_BSY) != RESET)
{
}
}
void LCD_WriteDat(u8 LCD_DatValue)
{
LCD_CtrlLinesWrite(GPIOB, CtrlPin_NCS, Bit_RESET);
LCD_CtrlLinesWrite(GPIOD, CtrlPin_RS, Bit_SET);
while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE)==RESET);
SPI_I2S_SendData(SPI2, LCD_DatValue);
while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_BSY) != RESET)
{
}
}
void LCD_Display_Color(u16 ColorValue)
{
u8 i,j;
u16 tmp;
//column address set (CASET)
LCD_WriteReg(0x2a);//XS15-XS8
LCD_WriteDat(0x00);
LCD_WriteDat(0x00);//XS7-XS0(X start address)
LCD_WriteDat(0x00);//XE15-XE8
LCD_WriteDat(0x7f);//XE7-XE0(X end address)
//row address set (RASET)
LCD_WriteReg(0x2b); //YS15-YS8
LCD_WriteDat(0x00);//YS7-YS0(Y start
LCD_WriteDat(0x00);//YE15-YE8
LCD_WriteDat(0x00);
LCD_WriteDat(0x9f);//YE7-YE0(Y end address)
LCD_WriteReg(0x2c);//YE7-YE0(Y end address)
for(i=160;i>0;i--)
for(j=128;j>0;j--)
{
tmp=((ColorValue>>8)&0x00ff);
LCD_WriteDat(tmp);
tmp=(ColorValue&0x00ff);
LCD_WriteDat(tmp);
}
}
void LCD_CtrlLinesWrite(GPIO_TypeDef* GPIOx, u16 CtrlPins, BitAction BitVal)
{
GPIO_WriteBit(GPIOx, CtrlPins, BitVal);
}
void LCD_CtrlLinesConfig(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_13 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_15;
GPIO_Init(GPIOD, &GPIO_InitStructure);
LCD_CtrlLinesWrite(GPIOD, CtrlPin_NWR, Bit_SET);
LCD_CtrlLinesWrite(GPIOD, CtrlPin_RS, Bit_SET);
}
void LCD_SPIConfig(void)
{
SPI_InitTypeDefSPI_InitStructure;
GPIO_InitTypeDefGPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI2, &SPI_InitStructure);
SPI_Cmd(SPI2, ENABLE);
}
void LCD_DrawChar(u8 Ypos, u8 Xpos, uc8 *c)
{
u32 index = 0, i = 0;
LCD_SelectRegion(Xpos, Ypos, Xpos+XPIXES -1, Ypos+YPIXES-1);
for(index = 0;index{
for(i = 0; i < XPIXES; i++)
{
if((c[index]&(0x80>>i)) == 0x00)
{
LCD_WriteDat(BackColor>>8);
LCD_WriteDat(BackColor&0xff);
}
else
{
LCD_WriteDat(TextColor>>8);
LCD_WriteDat(TextColor&0xff);
}
}
}
}
void LCD_SetTextColor(vu16 Color)
{
TextColor = Color;
}
void LCD_SetBackColor(vu16 Color)
{
BackColor = Color;
}
void LCD_ClearLine(u8 Line)
{
LCD_DisplayStringLine(Line, 0, "");
}
void LCD_DisplayStringLine(u8 Line, u8 Column, u8 *ptr)
{
u32 i = 0;
u8 refcolumn = Column;//319;
Line = Line*YPIXES;
while ((*ptr != 0) & (i < 16)) //mary??16 chars one line.
{
LCD_DisplayChar(Line, refcolumn, *ptr);
refcolumn += XPIXES;
ptr++;
i++;
//add for change lines.
}
}
評(píng)論