#include "12864.h"
#include "delay.h"
#define uchar unsigned char
#define uint unsigned int
uchar const TABLE[]=
{
0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,
0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,
0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f,
0x98,0x99,0x9a,0x9b,0x9c,0x9d,0x9e,0x9f,
};
void SendByte(uchar Dbyte)
{
uchar i;
for(i=0;i<8;i++)
{
GPIO_ResetBits(LCD_CLK);
if(Dbyte&0x80)
GPIO_SetBits(LCD_DATA);
else
GPIO_ResetBits(LCD_DATA);
Dbyte=Dbyte<<1;
delay_us(10);
GPIO_SetBits(LCD_CLK);
delay_us(10);
GPIO_ResetBits(LCD_CLK);
delay_us(10);
}
}
uchar ReceiveByte(void)
{
uchar i,temp1,temp2;
temp1 = 0;
temp2 = 0;
for(i=0;i<8;i++)
{
temp1=temp1<<1;
GPIO_ResetBits(LCD_CLK);
delay_us(10);
GPIO_SetBits(LCD_CLK);
delay_us(10);
GPIO_ResetBits(LCD_CLK);
if(GPIO_ReadInputDataBit(LCD_DATA)) temp1++;
}
for(i=0;i<8;i++)
{
temp2=temp2<<1;
GPIO_ResetBits(LCD_CLK);
delay_us(5);
GPIO_SetBits(LCD_CLK);
delay_us(5);
GPIO_ResetBits(LCD_CLK);
if(GPIO_ReadInputDataBit(LCD_DATA)) temp2++;
}
return ((0xf0&temp1)+(0x0f&temp2));
}
void CheckBusy(void)
{
do SendByte(0xfc);
while(0x80&ReceiveByte());
}
void WriteCommand(uchar Cbyte )
{
GPIO_SetBits(LCD_CS);
delay_ms(1);
SendByte(0xf8);
SendByte(0xf0&Cbyte);
SendByte(0xf0&Cbyte<<4);
GPIO_ResetBits(LCD_CS);
delay_ms(2);
}
void WriteData(uchar Dbyte )
{
GPIO_SetBits(LCD_CS);
delay_ms(1);
SendByte(0xfa);
SendByte(0xf0&Dbyte);
SendByte(0xf0&Dbyte<<4);
GPIO_ResetBits(LCD_CS);
delay_ms(2);
}
void LCD_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure; //设一个 初始化GPIO口时候用的 结构体
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA , ENABLE); //使能PA口的时钟,否则端口是不工作的
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2| GPIO_Pin_3| GPIO_Pin_4; //对结构体的GPIO_Pin对象赋值,声明要操作的是1,2,3,4端口
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//对结构体的GPIO_Mode对象赋值,声明IO口的模式是输出
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//对结构体的GPIO_OType对象赋值,声明IO口的结构是推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//对结构体的GPIO_Speed对象赋值,声明速度是50MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //对结构体的GPIO_PuPd对象赋值,声明内部上拉
GPIO_Init(GPIOA, &GPIO_InitStructure);//将结构体带入初始化函数中,执行初始化,否则之前设置都是无效的
}
void lcd_init(void)
{
LCD_GPIO_Config();
delay_ms(5);
GPIO_ResetBits(LCD_PSB);
GPIO_ResetBits(LCD_RST);
delay_ms(100);
GPIO_SetBits(LCD_RST);
delay_ms(5);
WriteCommand(0x30);
WriteCommand(0x30);
WriteCommand(0x0C);
WriteCommand(0x01);
delay_ms(20);
WriteCommand(0x06);
}
void LcmClearTXT( void )
{
uchar i;
WriteCommand(0x30);
WriteCommand(0x80);
for(i=0;i<64;i++)
WriteData(0x20);
}
void LcmClearBMP( void )
{
uchar i,j;
WriteCommand(0x34);
WriteCommand(0x36);
for(i=0;i<32;i++)
{
WriteCommand(0x80|i);
WriteCommand(0x80);
for(j=0;j<32;j++)
WriteData(0);
}
}
void PutStr(uchar row,uchar col,uchar *puts)
{
WriteCommand(0x30);
WriteCommand(TABLE[8*row+col]);
while(*puts != '\0')
{
if(col==8)
{
col=0;
row++;
}
if(row==4) row=0;
WriteCommand(TABLE[8*row+col]);
WriteData(*puts);
puts++;
WriteData(*puts);
puts++;
col++;
}
}
void PutBMP(uchar *puts)
{
uint x=0;
uchar i,j;
WriteCommand(0x34);
WriteCommand(0x36);
for(i=0;i<32;i++)
{
WriteCommand(0x80|i);
WriteCommand(0x80);
for(j=0;j<32;j++)
{
WriteData(puts[x]);
x++;
}
}
}
void DisplayDots(uchar DotByte)
{
uchar i,j;
WriteCommand(0x34);
WriteCommand(0x36);
for(i=0;i<32;i++)
{
WriteCommand(0x80|i);
WriteCommand(0x80);
for(j=0;j<32;j++)
{
WriteData(DotByte);
}
DotByte=~DotByte;
}
}
//=========================
#ifndef __12864_H
#define __12864_H
#include &quot;sys.h&quot;
#define uchar unsigned char
#define uint unsigned int
#define CLEAR_SCREEN 0x01
#define AC_INIT 0x02
#define CURSE_ADD 0x06
#define FUN_MODE 0x30
#define DISPLAY_ON 0x0c
#define DISPLAY_OFF 0x08
#define CURSE_DIR 0x14
#define SET_CG_AC 0x40
#define SET_DD_AC 0x80
#define LCD_CS GPIOA, GPIO_Pin_0
#define LCD_DATA GPIOA, GPIO_Pin_1
#define LCD_CLK GPIOA, GPIO_Pin_2
#define LCD_PSB GPIOA, GPIO_Pin_3
#define LCD_RST GPIOA, GPIO_Pin_4
void SendByte(uchar Dbyte);
void WriteCommand(uchar Cbyte );
void WriteData(uchar Dbyte );
uchar ReceiveByte(void);
void CheckBusy(void);
void LCD_GPIO_Config(void);
void lcd_init(void);
void LcmClearTXT( void );
void LcmClearBMP( void );
void PutStr(uchar row,uchar col,uchar *puts);
void PutBMP(uchar *puts);
void DisplayDots(uchar DotByte);
#endif
//============================================
#include &quot;sys.h&quot;
#include &quot;delay.h&quot;
#include &quot;usart.h&quot;
#include &quot;led.h&quot;
#include &quot;12864.h&quot;
unsigned char str1[]=&quot;abcd&quot;;
int main(void)
{
delay_init(168);
LED_Init();
lcd_init();
PutStr(0,0,str1);
while(1)
{
}
}
//============================================================== |
|