|
![](static/image/common/ico_lz.png)
楼主 |
发表于 2005-9-3 16:41:08
|
显示全部楼层
才下了OCMJ 2X8C看了一下,估计驱动方式是ST7920的。
我的驱动:(前提,那个PSB已经接了地,真正要接的数据线只有2根!这也是我为什么喜欢用ST7920的原因。)
#include <mega16.h>
//#include <macros.h>
#define LCD_DATA_PORT PORTB.0
#define LCD_EN PORTB.1
#define LCD_CS PORTB.2
unsigned char busy=0;
/*--------------------------------------------------------------------------------------------------
Public function prototypes
--------------------------------------------------------------------------------------------------*/
void LCD_init (void);
void LCD_en_write (void);
void LCD_write_char (unsigned command,unsigned data);
void LCD_write_hanzi (unsigned data);
void LCD_set_xy (unsigned char x, unsigned char y);
//void LCD_write_string (unsigned char X,unsigned char Y,unsigned char *s);
void delay_nus (unsigned int n);
void delay_nms (unsigned int n);
/* while(busy)
{
check_busy();
}
*/
void LCD_init(void) //液晶初始化
{
unsigned char i=0;
delay_nms(150);
for(i=0;i<3;i++)
{
LCD_write_char(0,0x30); //8位显示
delay_nus(150);
}
LCD_write_char(0,0x0c);
delay_nus(150);
LCD_write_char(0,0x0f); //显示开,光标开,闪烁开
delay_nus(150);
LCD_write_char(0,0x01); //清屏
delay_nms(10);
LCD_write_char(0,0x06);
delay_nus(150);;
//delay_nms(100);
}
void LCD_set_xy( unsigned char x, unsigned char y ) //写地址函数
{
unsigned char address;
if (y == 0) address = 0x80 + x;
else if (y == 1) address = 0x90 + x;
else if (y == 2) address = 0x88 + x;
else if (y == 3) address = 0x98 + x;
LCD_write_char(0,address);
}
void LCD_en_write(void) //液晶使能
{
LCD_EN=0;
delay_nus(50);
LCD_EN=1;
delay_nus(50);
}
void LCD_write_char(unsigned command,unsigned data) // 写数据
{
unsigned char i,j;
for(i=0;i<5;i++)
{
LCD_DATA_PORT=1; //1 1 1 1 1
LCD_en_write();
}
LCD_DATA_PORT=0; //RW
LCD_en_write();
LCD_DATA_PORT=command; //RS
LCD_en_write();
LCD_DATA_PORT=0; // 0
LCD_en_write(); //写串口输入方式的头
for(j=0;j<2;j++)
{
for(i=0;i<4;i++)
{
if((0x80&data)>0)
LCD_DATA_PORT=1;
else LCD_DATA_PORT=0;
LCD_en_write();
data=data<<1;
}
for(i=0;i<4;i++)
{
LCD_DATA_PORT=0; //0 0 0 0
LCD_en_write();
}
}
}
void LCD_write_hanzi(unsigned data) // 写数据
{
LCD_write_char(1,((0Xff00&data)>>8));
delay_nus(50);
LCD_write_char(1,((0xff&data)));
delay_nus(50);
}
void main(void)
{
DDRA=0xff; // 数据为输出
DDRB=0xff; //置位RS.EN
LCD_CS=1;
LCD_EN=0;
delay_nms(10);
LCD_init();
for(;;) //for循环
{
LCD_set_xy(0,0);
LCD_write_char(1,'W');
LCD_write_char(1,'e');
LCD_write_char(1,'n');
LCD_write_char(1,'d');
LCD_write_char(1,'u');
LCD_write_char(1,':');
//delay_nms(500);
//LCD_set_xy(1,0);
LCD_set_xy(3,0);
LCD_write_char(1,'2');
LCD_write_char(1,'8');
LCD_write_char(1,'C');
// delay_nms(500);
delay_nms(1000);
LCD_set_xy(0,1);
LCD_write_char(1,'F');
LCD_write_char(1,'e');
LCD_write_char(1,'n');
LCD_write_char(1,'g');
LCD_write_char(1,'s');
LCD_write_char(1,'u');
LCD_write_char(1,':');
LCD_set_xy(7,1);
LCD_write_char(1,'3');
//delay_nms(500);
//LCD_set_xy(1,0);
delay_nms(1000);
LCD_set_xy(0,2);
LCD_write_hanzi('早');
LCD_set_xy(1,2);
LCD_write_hanzi('上');
LCD_set_xy(2,2);
LCD_write_hanzi('好');
LCD_write_hanzi('早');
//LCD_set_xy(1,2);
LCD_write_hanzi('上');
//LCD_set_xy(2,2);
LCD_write_hanzi('我');
LCD_write_hanzi('爱');
LCD_write_hanzi('你');
delay_nms(1000);
LCD_set_xy(0,3);
LCD_write_hanzi('电');
LCD_write_hanzi('子');
LCD_write_hanzi('设');
LCD_write_hanzi('计');
LCD_write_hanzi('大');
LCD_write_hanzi('赛');
LCD_write_hanzi('参');
LCD_write_hanzi('与');
LCD_set_xy(8,3);
delay_nms(1000);
// delay_nms(2);
// LCD_write_string(0,0,"Wellcome");
// LCD_write_string(0,1,"www.OURAVR.com!");
// delay_nms(5000);
//LCD_write_string(0,0,"happy new year!");
// LCD_write_string(0,1,"amork:everfriend");
// delay_nms(5000);
}
}
/*-----------------------------------------------------------------------
延时函数
系统时钟:8M
-----------------------------------------------------------------------*/
void delay_1us(void) //1us延时函数
{
#asm("nop");
}
void delay_nus(unsigned int n) //N us延时函数
{
unsigned int i=0;
for (i=0;i<n;i++)
delay_1us();
}
void delay_1ms(void) //1ms延时函数
{
unsigned int i;
for (i=0;i<1140;i++);
}
void delay_nms(unsigned int n) //N ms延时函数
{
unsigned int i=0;
for (i=0;i<n;i++)
delay_1ms();
} |
|