#ifndef __LCD_H__
#define __LCD_H__
#include "type.h"
//LCM122*32 PIN define
#define CLK PORTC.5 //pc5
#define SID PORTC.4 //pc4
#define CS PORTB.4 //pb4
#define SET_CLK() CLK = 1;
#define CLR_CLK() CLK = 0;
#define SET_SID() SID = 1;
#define CLR_SID() SID = 0;
#define LCD_Enable() CS = 1;
#define LCD_DISAble() CS = 0;
#define SID_PIN_IN() DDRC.4 = 0;
#define SID_PIN_OUT() DDRC.4 = 1;
#define CLK_PIN_OUT() DDRC.5 = 1;
#define CS_PIN_OUT() DDRB.4 = 1;
#define SID_PIN PINC.4 //read pinx, not port!
#define LCD_Hi_Z() DDRC &= 0xCF;
//lcd serial command
//#define LCD_WRITE_COMMAND 0b11111000
//#define LCD_WRITE_DATA 0b11111010
//#define LCD_READ_STATUS 0b11111100
//#define LCD_READ_DATA 0b11111110
#define LCD_WRITE_COMMAND 0xF8
#define LCD_WRITE_DATA 0xFA
#define LCD_READ_STATUS 0xFC
#define LCD_READ_DATA 0xFE
void serial_write_byte(byte data);
byte serial_read_byte(void);
void LCDWriteCmd(byte command);
void LCDWritEDAta(byte data);
void InitLCD(void);
void LCD_Write_CGRAM(byte nBytes);
void LCD_Write_CGRAM(byte nBytes);
void writechar(byte value);
void writestring(byte flash *strn);
void gotoxy(byte line, byte position);
void LCDclrscr(void);
void blink(byte on_off);
void writeNumber(word value);
#endif
/*------------------------------------------------------------
FileName : lcd.c
Created by : ZhengYanbo
Created date : 2006.3.17
Last modified: 2006.3.17
Comments: : serial drive for st7920
-------------------------------------------------------------*/
#include "lcd.h"
#include "type.h"
/*
flash byte CGRAM[128] =
{
//目录循环
0xE3,0x9E,0x91,0x11,0x89,0x11,0x89,0x1E,
0x89,0x14,0x91,0x12,0xE3,0x91,0x00,0x00,
0x00,0x00,0x3F,0xFC,0x40,0x02,0x40,0x42,
0x40,0x82,0x3D,0xFC,0x00,0x80,0x00,0x40,
//文件循环
0xEE,0x8F,0x84,0x88,0x84,0x88,0xE4,0x8E,
0x84,0x88,0x84,0x88,0x8E,0xEF,0x00,0x00,
0x00,0x00,0x3F,0xFC,0x40,0x02,0x40,0x22,
0x40,0x42,0x3E,0xFC,0x00,0x40,0x00,0x20,
//随机模式
0xE1,0x1B,0x92,0x95,0x94,0x55,0xE7,0xD5,
0xA4,0x51,0x94,0x51,0x00,0x00,0x00,0x00,
0x00,0x00,0x3F,0xFC,0x40,0x02,0x40,0x42,
0x40,0x82,0x3D,0xFC,0x00,0x80,0x00,0x40,
//磁盘循环
0xCE,0x74,0xA4,0x84,0x94,0x85,0x94,0x66,
0x94,0x14,0xA4,0x16,0xCE,0xE5,0x00,0x00,
0x00,0x00,0x3F,0xFC,0x40,0x02,0x40,0x42,
0x40,0x82,0x3D,0xFC,0x00,0x80,0x00,0x40
}; */
//**********************************************************
//用串行方式写一个字节数据到LCD
void serial_write_byte(byte data)
//**********************************************************
{
byte i;
//SID pin output mode
SID_PIN_OUT();
CLK_PIN_OUT();
CS_PIN_OUT();
for(i=0; i<8; i++)
{
if(data & 0x80)
SET_SID();
else
CLR_SID();
//send clk
CLR_CLK();
#asm("nop");
SET_CLK();
data<<=1;
}
}
//**********************************************************
//以串行方式从LCD中读一个字节,返回数据
byte serial_read_byte(void)
//**********************************************************
{
byte i, temp=0;
//SID pin input mode
SID_PIN_IN();
CLK_PIN_OUT();
CS_PIN_OUT();
for(i=0; i<8; i++)
{
temp<<=1;
//send clk
CLR_CLK();
#asm("nop");
SET_CLK();
//wait pin steady
#asm("nop");
temp = (byte)SID_PIN;
}
return(temp);
}
//**********************************************************
//读LCD的状态,读出BF信号
byte LCD_read_status(void)
//**********************************************************
{
byte temp;
LCD_Enable();
//send 0b11111100
serial_write_byte(LCD_READ_STATUS);
//read upper 4 bits
temp = serial_read_byte() & 0xF0;
//read lower 4 bits
temp = (serial_read_byte() & 0xF0)>>4 + temp;
LCD_Disable();
//return its value
return(temp);
}
//**********************************************************
//忙信号BF检查
//返回: 1->suCCess 0->error(time out)
byte check_busy(void)
//**********************************************************
{
byte time_out;
time_out=255;
while(LCD_read_status()&0x80)
{
time_out--;
//check time out
if(time_out==0) return 0;
};
return 1;
}
//**********************************************************
//写命令到LCM
void LCDWriteCmd(byte command)
//**********************************************************
{
//not check BF state
delay_us(100);
LCD_Enable();
//send 0b11111000
serial_write_byte(LCD_WRITE_COMMAND);
//write upper 4 bits
serial_write_byte(command & 0xF0);
//write lower 4 bits
serial_write_byte((command<<4) & 0xF0);
LCD_Disable();
}
//**********************************************************
//写数据到LCM
void LCDWriteData(byte data)
//**********************************************************
{
//not check BF state
delay_us(100);
LCD_Enable();
//send 0b11111010
serial_write_byte(LCD_WRITE_DATA);
//write upper 4 bits
serial_write_byte(data & 0xF0);
//write lower 4 bits
serial_write_byte((data<<4) & 0xF0);
LCD_Disable();
}
//***********************************************************
//Initialize LCD, then print logo
void InitLCD(void)
//***********************************************************
{
delay_ms(50); //Wait LCD ready
LCDWriteCmd(0x30); //function set
delay_us(100);
LCDWriteCmd(0x30); //function set
delay_us(100);
LCDWriteCmd(0x0C); //display on, cursor off
delay_us(100);
LCDWriteCmd(0x01); //clear lcd
delay_ms(10);
LCDWriteCmd(0x06); //Entry mode set
//Write LCD CGRAM
LCD_Write_CGRAM(128);
//Printing Logo
//LCDclrscr();
gotoxy(0,0);
writestring("ST7920串行驱动");
gotoxy(1,0);
writestring("版本:Test v1.0");
delay_ms(1000);
//second screen
LCDclrscr();
writestring("设计:Datazyb");
gotoxy(1,0);
writestring("日期:2006.3.17");
}
//***********************************************************
void LCD_Write_CGRAM(byte nBytes)
//***********************************************************
{
//Load user’s font characters
byte i;
//Set CGram addres;
LCDWriteCmd(0x40);
//load CGRAM characters
for(i=0;i
//***********************************************************
void writechar(byte value)
//***********************************************************
{
//Write 1 character
//#asm("cli");
LCDWriteData(value);
//Set High status
LCD_Hi_Z();
}
//***********************************************************
//Write a string from flash ROM
void writestring(byte flash *strn)
//***********************************************************
{
while (*strn!=0) writechar(*strn++);
}
//***********************************************************
//Gotoxy function. X=line number, Y=character position
void gotoxy(byte line, byte position)
//***********************************************************
{
byte address;
address=lcdLineStart[line]+position;
LCDWriteCmd(address);
}
//***********************************************************
//Clear LCD
void LCDclrscr(void)
//***********************************************************
{
LCDWriteCmd(0x01);
delay_ms(5); //Writing cycle time is 4.6ms for LCD128X64.
}
//***********************************************************
//Setup curson blink, based on player state
void blink(byte on_off)
//***********************************************************
{
//if on_off = 1 blink on, if on_off=0 blink off
LCDWriteCmd(0x0C + (on_off & 1));
}
//***********************************************************
//Write a decimal number on LCD
void writeNumber(word value)
//***********************************************************
{
byte temp[8],i=0;
do {
temp[i++]=value%10;
value=value/10;
}
while (value>0);
//start from back and print the number
for(;i>0;) writechar(temp[--i]+48);
}
//end of lcd.c
上一篇:控制74HC164数据转换程序
下一篇:KS0108 128X64 LCD C语言AVR程序
推荐阅读最新更新时间:2024-03-16 15:49