专注电子技术学习与研究
当前位置:单片机教程网 >> MCU设计实例 >> 浏览文章

ATmega128并行控制带字库的12864程序

作者:佚名   来源:本站原创   点击数:  更新时间:2012年04月04日   【字体:

  m128的特点是io口很多,所以我们可以用并行的方式来驱动此液晶屏,增加总线的速度,此程序是是网上整理收集而来,但已经通过本人验证可以使用,故在此发表.大家在应用的时候只需更改相应的io就行了.
 


软件:GCC V4.20
--------------------------------------------------------------- 
实验内容:写Lcd12864_ST7920。
---------------------------------------------------------------
硬件连接:
LCD12864_ST7920                  ATmega128
1.GND               --------        GND
2.VCC               --------        VCC
3.V0                --------        NC
4.RS(CS)            --------        PD7
5.R/W(SID)          --------        PG0
6.E(SCLK)           --------        PG1
7.D0                --------        PC0
8.D1                --------        PC1
9.D2                --------        PC2
10.D3               --------        PC3
11.D4               --------        PC4
12.D5               --------        PC5
13.D6               --------        PC6
14.D7               --------        PC7
15.PSB              --------        VCC
16.NC               --------        NC
17.RST              --------        VCC
18.NC               --------        NC
19.LED+             --------        VCC
20.LED-             --------        GND
以下是程序源代码:
---------------------------------------------------------------*/ 
#include <avr/io.h>
#include<avr/pgmspace.h>
#include <util/delay.h> 
#define  E_set        PORTG|=_BV(PG1)    //液晶使能
#define  E_clear      PORTG&=~_BV(PG1)
#define  RW_set       PORTG|=_BV(PG0)    //液晶读写
#define  RW_clear     PORTG&=~_BV(PG0)
#define  RS_set       PORTD|=_BV(PD7)    //液晶数据
#define  RS_clear     PORTD&=~_BV(PD7)   //液晶指令
unsigned char j=0;
unsigned char yb=0x80;
unsigned char name00[] PROGMEM={"飞射白鹿雪连天,"};
unsigned char name01[] PROGMEM={"笑书神侠倚碧鸳。"};
unsigned char name02[] PROGMEM={"自古英雄多奇逸,"};
unsigned char name03[] PROGMEM={"金老挥毫尽使然。"};
unsigned char name10[] PROGMEM={"李白乘舟将欲行,"};
unsigned char name11[] PROGMEM={"忽闻岸上踏歌声。"};
unsigned char name12[] PROGMEM={"桃花潭水深千尺,"};
unsigned char name13[] PROGMEM={"不及汪伦送我情。"};
unsigned char name20[] PROGMEM={"this is a 12864 "};
unsigned char name21[] PROGMEM={"display program,"};
unsigned char name22[] PROGMEM={"welcome to commu"};
unsigned char name23[] PROGMEM={"nicate with me! "};

void wr_com(unsigned char);
void wr_data(unsigned char);
void reset (void);
void outChinese(unsigned char,unsigned char,unsigned char *point);
void ydgb(void);
void outchar(unsigned char,unsigned char,unsigned char *point);

void wr_com(unsigned char value)//写指令,写指令时必须为RS=0;RW=0;
{
 E_clear;
 RS_clear;
 RW_clear;
 _delay_ms(1);
 PORTC=value; //并入字符数据
 E_set;
 _delay_ms(2); //如果没有延时就必须要加查忙指令
 E_clear;
}
void wr_data(unsigned char sj)//写数据,写数据时必须为 RS=1;RW=0;
{
 E_clear;
 RS_set;
 RW_clear;
 _delay_ms(1);
 PORTC=sj;
 E_set;
 _delay_ms(1);//如果没有延时就必须要加查忙指令
 E_clear;
}
void reset (void)
{
 wr_com(0x01);//清屏
 wr_com(0x08);//关显示
 wr_com(0x03);//归位
 wr_com(0x30);//功能设置
 wr_com(0x0f);//开显示
 wr_com(0x01);//清屏
}
//****************
//outChinese 为函数名
//place 为显示地址的首地址
//unit  字符长度
//charcode[]  要显示数据的内容
void outChinese(unsigned char place,unsigned char unit,unsigned char *point)
{
 unsigned char i,progdata;
 wr_com(place);
 for(i=0;i<unit*2;i++)//一个汉字为两个字符
 {
         progdata=pgm_read_byte(point+i);
         wr_data(progdata);
         }
}
void outchar(unsigned char place,unsigned char unit,unsigned char *point)
{
 unsigned char i,progdata;
 wr_com(place);
 for(i=0;i<unit;i++)
 {
         progdata=pgm_read_byte(point+i);
         wr_data(progdata);
         }
}
void ydgb(void)
{
  wr_com(yb);//显示光标位置
  yb=yb+0x01;
 if(yb==0x88){yb=0x90;}
 if(yb==0x97){yb=0x88;}
 if(yb==0x8f){yb=0x98;}
 if(yb==0xa0){yb=0x80;}
}
void display1(void)
{
 ydgb();
 outChinese(0x80,8,name00);//第一行:80-87H
 outChinese(0x90,8,name01);//第二行:90-97H
 outChinese(0x88,8,name02);//第三行:88-8FH
 outChinese(0x98,8,name03);//第四行:98-9FH
 //outchar(0x9f,2,name4);
}
void display2(void)
{
 ydgb();
 outChinese(0x80,8,name10);
 outChinese(0x90,8,name11);
 outChinese(0x88,8,name12);
 outChinese(0x98,8,name13);
}
void display3(void)
{
 ydgb();
 outchar(0x80,16,name20);
 outchar(0x90,16,name21);
 outchar(0x88,16,name22);
 outchar(0x98,16,name23);
}
int main(void)
{
 DDRC=0xff; //数据
 PORTC=0x00;
 DDRG=0xff; //命令
 PORTG=0xff;
 DDRD=0xff; //命令
 PORTD=0xff;
 reset();
 while(1)
 {
  ydgb();
  j++;
  ydgb();
  wr_com(0x01);//清屏
  if(j==3)j=0;
  switch(j)
  {
   case 0:display1();break;
   case 1:display2();break;
   case 2:display3();break;
  }
  _delay_ms(3000);
 }
} 
关闭窗口

相关文章