#ifndef __LCD_H
#define __LCD_H
#include "sys.h"
#define SCLK PDout(7)
#define SID PDout(5)
#define CS PDout(11)
#define LCD_PSB PCout(11)
#define LCD_RST PCout(12)
void Delayus(u32 di);
void lcd_config(void);
void lcd_sendbyte(unsigned char bbyte);
void lcd_write_com(unsigned char command);
void lcd_write_dat(unsigned char data);
void lcd_set_XY(unsigned char X,unsigned char Y);
void lcd_print_string(unsigned char X,unsigned char Y,unsigned char *s);
void lcd_init(void);
#endif
#include "lcd.h"
#include "sys.h"
void Delayus(u32 di)
{
u32 da,db;
for(da=0;da<di;da++)
{
for(db=0;db<12;db++);
}
}
void lcd_config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_7|GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11|GPIO_Pin_12;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_SetBits(GPIOC,GPIO_Pin_11);
GPIO_SetBits(GPIOC,GPIO_Pin_12);
}
void lcd_sendbyte(unsigned char bbyte)
{
unsigned char i;
for(i=0;i<8;i++)
{
if((bbyte)&0x80)
{
SID=1;
}
else
{
SID=0;
}
Delayus(1);
SCLK=1;
Delayus(1);
SCLK=0;
bbyte<<=1;
}
Delayus(1);
}
void lcd_write_com(unsigned char command)
{
CS=1;
lcd_sendbyte(0xf8);
Delayus(10);
lcd_sendbyte(command&0xf0);
Delayus(5);
lcd_sendbyte((command<<4)&0xf0);
Delayus(5);
CS=0;
}
void lcd_write_dat(unsigned char data)
{
CS=1;
lcd_sendbyte(0xfa);
Delayus(10);
lcd_sendbyte(data&0xf0);
Delayus(5);
lcd_sendbyte((data<<4)&0xf0);
Delayus(5);
CS=0;
}
void lcd_set_XY(unsigned char X,unsigned char Y)
{
unsigned char address;
switch(X)
{
case 0: address = 0x80+Y; break;
case 1: address = 0x80+Y; break;
case 2: address = 0x90+Y; break;
case 3: address = 0x88+Y; break;
case 4: address = 0x98+Y; break;
default: address = 0x80 +Y; break;
}
lcd_write_com(address);
Delayus(72);
}
void lcd_print_string(unsigned char X,unsigned char Y,unsigned char *s)
{
lcd_set_XY(X,Y);
while(*s)
{
lcd_write_dat(*s);
s++;
Delayus(72);
}
}
void lcd_init(void)
{
LCD_PSB=0;
Delayus(5);
lcd_write_com(0x30);
Delayus(5);
lcd_write_com(0x0c);
Delayus(5);
lcd_write_com(0x01);
Delayus(5);
}
#include "sys.h"
#include "lcd.h"
int main(void)
{
lcd_config();
lcd_init();
while(1)
{
lcd_print_string(1,1,"123456");
}
}
|