这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 综合技术 » 基础知识 » MEGA128,LCD 有玩MEGA128的吗?能否给我一个LCD的程序.

共4条 1/1 1 跳转至

MEGA128,LCD 有玩MEGA128的吗?能否给我一个LCD的程序.

院士
2006-09-17 18:14:16     打赏
MEGA128,LCD 有玩MEGA128的吗?能否给我一个LCD的程序.



关键词: MEGA128     有玩     的吗     能否     给我     一个     程序    

院士
2006-12-22 22:43:00     打赏
2楼
问 请大虾帮忙,我用总线方式连接.试验了晓奇的源代码.弄了半天都不行. 1: 这个有用吗?/*********************************************
This program was produced by the
CodeWizardAVR V1.24.1 Standard
Automatic Program Generator
?Copyright 1998-2003 HP InfoTech s.r.l.
http://www.hpinfotech.ro
e-mail:office@hpinfotech.ro

Project :
Version :
Date    : 2004-2-7
Author  : Shen Jianguo                    
Company :                                 
Comments:


Chip type           : ATmega128
Clock frequency     : 8.000000 MHz
Memory model        : Small
External SRAM size  : 0
Data Stack size     : 128
*********************************************/

#include <mega128.h>
#include <delay.h>

//********************************************
//SSC12J64中文LCD屏,带中文字库
//联接: LCD      MEGA128
//      RS ----- PORTC.0
//      RD ----- PORTC.1
//      EN ----- PORTC.2
//     DB4 ----- PORTC.4
//     DB5 ----- PORTC.5
//     DB6 ----- PORTC.6
//     DB7 ----- PORTC.7
//********************************************
#define LCD_RS PORTC.0
#define LCD_RD PORTC.1
#define LCD_EN PORTC.2
#define LCD_DB PORTC

// Declare your global variables here
char flash *str="Hello!";
char flash *str2="将数据输出到LCD接口,LCD数据口宽度为4bit";


//********************************************
//将数据输出到LCD接口,LCD数据口宽度为4bit
//rs: 1 操作指令寄存器,0 操作数据寄存器
//rd: 1 从LCD读出数据, 0 向LCD写数据
//db: 要写入的数据,仅底4位有效
//********************************************
void lcd_setport(char rs,char rd,char db)
{
db=db<<4;
LCD_RS=rs;
LCD_RD=rd;
delay_us(1);
LCD_EN=1;
delay_us(1);
LCD_DB=LCD_DB&0x0F;
LCD_DB=LCD_DB|db;
delay_us(2);
LCD_EN=0;
delay_us(10);
}

//********************************************
//LCD功能设定,LCD数据口宽度为4bit
//re: 1扩展指令, 0 基本指令
//********************************************
void lcd_function_set(char re)
{
char db;
db=db|(re<<2);
lcd_setport(0,0,0x02);
lcd_setport(0,0,db);
}

//********************************************
// LCD软件复位
//********************************************
void lcd_reset(void)
{
lcd_setport(0,0,0x03);
delay_ms(5);
lcd_setport(0,0,0x03);
delay_ms(1);
lcd_setport(0,0,0x03);
lcd_setport(0,0,0x03);
lcd_function_set(0);       //使用LCD基本指令
}

//**********************************************
//LCD开关设定
//display: 1 显示开, 0 显示关
//cursor:  1 光标开, 0 光标关
//blink:   1 光标处字符闪动开, 光标处字符闪动关
//**********************************************
void lcd_on_off(char display,char cursor,char blink)
{
char db;
db=0x08|(display<<2)|(cursor<<1)|blink;
lcd_setport(0,0,0x00);
lcd_setport(0,0,db);
}


//**********************************************
//LCD 清屏
//**********************************************
void lcd_clear(void)
{
lcd_setport(0,0,0x00);
lcd_setport(0,0,0x01);
delay_ms(5);
}

//**********************************************
//LCD显示回原点,设DDRAM地址为0,DDRAM内容不变
//**********************************************
void lcd_return_home(void)
{
lcd_setport(0,0,0x00);
lcd_setport(0,0,0x02);
}

//**********************************************
//输入方式设定,设定光标移动方向并指定整体是否移动
//id :1 增量方式, 0 减量方式
//s:  1 写入后整体移动,0 不移动
//**********************************************
void lcd_entry_mode(char id,char s)
{
char db;
db=0x04|(id<<1)|s;
lcd_setport(0,0,0x00);
lcd_setport(0,0,db);
}

//**********************************************
//移动光标或整体
//s_c: 1 整体显示移位,0 光标移动
//r_l: 1 左移, 0 右移
//**********************************************
void lcd_cour_disp_shift(char s_c,char r_l)
{
char db;
db=db|(s_c<<3)|(r_l<<2);
lcd_setport(0,0,0x01);
lcd_setport(0,0,db);
}

//**********************************************
//读取当前显示位置
//**********************************************
char lcd_read_address(void)
{
char address,address_l;

LCD_EN=0;
LCD_RS=0;
delay_us(1);
LCD_RD=1;
delay_us(1);
LCD_EN=1;
DDRC=0x07;
delay_us(1);
address=PINC&0x70;
LCD_EN=0;
delay_us(1);
LCD_EN=1;
delay_us(1);
address_l=PINC&0xF0;
address_l= address_l>>4;
address=address|address_l|0x80;
LCD_EN=0;
LCD_RD=0;
PORTC=0x00;
DDRC=0xFF;

return address;
}

//**********************************************
//设定CGRAM地址
//**********************************************
void lcd_set_cgram_address(char address)
{
char db;
db=0x04|(address>>4);
lcd_setport(0,0,db);
db=address;
lcd_setport(0,0,db);
}

//**********************************************
//设定DDRAM地址
//*********************************************
void lcd_set_ddram_address(char address)
{
char db;
db=(0x08|(address>>4))&0x0B;
lcd_setport(0,0,db);
db=address;
lcd_setport(0,0,db);
}

//**********************************************
//写数据到CGRAM 或DDRAM
//**********************************************
void lcd_write_ram(char data)
{
char db;
db=data>>4;
lcd_setport(1,0,db);
lcd_setport(1,0,data);
}


//**********************************************
//LCD初始化
//**********************************************
void lcd_inital(void)
{
lcd_reset();
lcd_setport(0,0,0x02);
lcd_on_off(1,1,0);
lcd_clear();
lcd_entry_mode(1,0);
delay_ms(5);
}

//**********************************************
//设定显示位置
// x 列位置,范围1-8
// y 行位置,范围1-4
//**********************************************
void lcd_gotoxy(char x,char y)
{
char address;

switch ( y )
    {
     case 1:
            address=0x80+x-1;
            break;
     case 2:
            address=0x90+x-1;
            break;  
     case 3:
            address=0x88+x-1;
            break;
     case 4:
            address=0x98+x-1;
            break;
     default: break;                
         
    }  
lcd_set_ddram_address(address);
}

//**********************************************
//显示一个字符
//**********************************************
void lcd_putc(char c)
{
lcd_write_ram(c);
}


//**********************************************
//显示一个字符串
//**********************************************
void  lcd_putsf(char  flash *str)
{
  char address;


  for(;;)
    {
     if(*str==0)
        break;
     else
        {
          address=lcd_read_address();
          
          if(address==0x87)
             {
              
              lcd_putc(*str);
              str++;
              if(*str==0)
                       break;
              lcd_putc(*str);
              str++;
              if(*str==0)
                       break;
              lcd_gotoxy(1,2);                  
             }
            if(address==0x97)
              {
              
              lcd_putc(*str);
              str++;
              if(*str==0)
                       break;
              lcd_putc(*str);
              str++;
              if(*str==0)
                       break;         
              lcd_gotoxy(1,3);
             }
           if(address==0x8F)
              {
              
              lcd_putc(*str);
              str++;
              if(*str==0)
                       break;
              lcd_putc(*str);
              str++;
              if(*str==0)
                       break;         
              lcd_gotoxy(1,4);
             }  
          lcd_putc(*str);
          str++;
      
        }   
    }
}     


//**********************************************
//在指定行显示一个字符串,
//字符串长度小于16个字符或8个汉字
//**********************************************
void lcd_putsf_line(char  flash *str,char line)
{  
   char i;
   lcd_gotoxy(1,line);
   for(i=0;i<=15;i++)
    {
     if(*str==0)
        break;
     else
        {
         lcd_putc(*str);
         str++;
      
        }   
    }
   lcd_on_off(1,0,0);
}  

//**********************************************
//整屏显示一个字符串,
//字符串长度小于64个字符或32个汉字
//**********************************************
void lcd_put_page(char  flash *str)
{
lcd_clear();
lcd_gotoxy(1,1);
lcd_putsf(str);
}  

unsigned int  hex2asc(unsigned char n)
{
      char i;
      unsigned int result;
      unsigned int *p_result;
      p_result=&result;
      for(i=0;i<2;i++)
        {
         
         *p_result=n%16+0x30;        
          p_result++;
          n=n/16;
        }
      return result;
}


void main(void)
{
// Declare your local variables here

// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTA=0x00;
DDRA=0x00;

// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0x00;

// Port C initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTC=0x00;
DDRC=0xFF;

// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;

// Port E initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTE=0x00;
DDRE=0x00;

// Port F initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTF=0x00;
DDRF=0x00;

// Port G initialization
// Func4=In Func3=In Func2=In Func1=In Func0=In
// State4=T State3=T State2=T State1=T State0=T
PORTG=0x00;
DDRG=0x00;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
ASSR=0x00;
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// OC1C output: Discon.
// Noise CANceler: Off
// Input Capture on Falling Edge
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
OCR1CH=0x00;
OCR1CL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// Timer/Counter 3 initialization
// Clock source: System Clock
// Clock value: Timer 3 Stopped
// Mode: Normal top=FFFFh
// OC3A output: Discon.
// OC3B output: Discon.
// OC3C output: Discon.
TCCR3A=0x00;
TCCR3B=0x00;
TCNT3H=0x00;
TCNT3L=0x00;
ICR3H=0x00;
ICR3L=0x00;
OCR3AH=0x00;
OCR3AL=0x00;
OCR3BH=0x00;
OCR3BL=0x00;
OCR3CH=0x00;
OCR3CL=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
// INT3: Off
// INT4: Off
// INT5: Off
// INT6: Off
// INT7: Off
EICRA=0x00;
EICRB=0x00;
EIMSK=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;
ETIMSK=0x00;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
// Analog Comparator Output: Off
ACSR=0x80;
SFIOR=0x00;

lcd_inital();

lcd_on_off(1,1,0);

lcd_gotoxy(5,1);      

lcd_put_page(str2);    

while (1)
      {
      // Place your code here
       delay_ms(1000);
       PORTB.2=0;
       delay_ms(1000);
       PORTB.2=1;
       };
}
2: 多谢这位大哥,我用的是T6963,不知道该怎么设置CPU不知道哪些CPU寄存器需要设置.该怎么设置
3: 我给你传一个,我放晓奇那里了http://www.xiao-qi.com/comm/project_43.zip 4: 基本搞定了.多谢2位大哥,还有晓奇大哥修正了硬件错误,在晓奇提供的程序基础上作了修改.
现在可以显示demo 画面了.


专家
2023-01-30 20:32:01     打赏
3楼

感谢分享


专家
2023-02-01 20:56:24     打赏
4楼

感谢分享


共4条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]