OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 29705|回复: 0

我的AVR资料[程序篇] 1602液晶字符串滚动(源码7)

[复制链接]

530

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
165352
金钱
165352
注册时间
2010-12-1
在线时间
2108 小时
发表于 2010-12-21 01:06:13 | 显示全部楼层 |阅读模式

    本实验实现了在1602上显示一个字符串,并滚动这个字符串.源码如下: 
#include <mega16.h>
#include <delay.h>
 
#define LCD_DATA_PORT  PORTA   //the next three port should be the same
#define LCD_DATA_DDR   DDRA    //must use the high 4bit
#define LCD_DATA_PIN   PINA 

#define LCD_RS         PORTA.0 //RS lcd
#define LCD_WR         PORTA.1//WR lcd
#define LCD_EN         PORTA.2 //EN lcd

#define LCD_DRS        DDRA.0  //WR direction define
#define LCD_DWR        DDRA.1  //RS direction define
#define LCD_DEN        DDRA.2  //EN direction define
#define LCD_DATA       0xf0    //DATA PORT
#define uchar unsigned char
#define uint unsigned int

/*
LCD_Write(1,command)
1602 command:0x0f show cursor and flash
             0x0c do now show cursor
   
*/
void LCD_en_write(void)  //enable LCD
{
  LCD_DEN=1;//SET LCD_EN OUTPUT
  LCD_EN=1;//EN=1
  delay_us(10);
  LCD_EN=0;//EN=0
}
//cord:when 1,write command when 0,write data
//data:command or data you want to write to 1602
void LCD_Write(char cord,unsigned char data) //write data
{
    delay_us(25);
    LCD_DRS=1;//SET RS OUTPUT
    if(cord==0)LCD_RS=1; //RS=1,write data
    else LCD_RS=0;//RS=0,write command
    LCD_DATA_PORT&=0X0f;       //clr high 4bit
    LCD_DATA_PORT|=data&0xf0;  //wirte high 4bit
    LCD_en_write();
    data=data<<4;               //turn the low 4bit to high 4bit
    LCD_DATA_PORT&=0X0f;        //clr high 4bit
    LCD_DATA_PORT|=data&0xf0;   //write low 4bit
    LCD_en_write();
}

void LCD_init(void)       //lcd init
{
    LCD_DWR=1;//set en output
    LCD_WR=0;//write enable
    LCD_DATA_DDR|=LCD_DATA;   //set data port out
    LCD_EN=1;// set EN out
    LCD_RS=1;// set RS out
    delay_us(40);
    LCD_Write(1,0x28);  //4bit show
    LCD_Write(1,0x10);  //show cursor
    LCD_Write(1,0x06);  //cursor move right
    LCD_Write(1,0x01);  //clr
    delay_ms(2);
}

//orientation a dress
//x:0-15
//y:0 or 1
void LCD_set_xy( unsigned char x, unsigned char y )  //write address funcation
{
    unsigned char address;
    if (y==0) address=0x80+x;
    else address=0xc0+x;
    LCD_Write(1,address);
}
//from a appointed address write string to 1602
//col x=0~15,row y=0,1 
void LCD_write_string(unsigned char X,unsigned char Y,unsigned char flash *s)
{
    LCD_set_xy(X,Y); //write address
    while (*s)  // write the char to show
    {
        LCD_Write(0,*s);
        s++;
    }  
}
//write a char to appointed address
void LCD_write_char(unsigned char X,unsigned char Y,unsigned char data) //col x=0~15,row y=0,1
{
    LCD_set_xy(X,Y); //write address
    LCD_Write(0,data);//write data; 
}
void main(void)
{              
   unsigned char temp[]="Fuck you bitch!!";
   unsigned char t;
  
   delay_ms(200);//delay 4s
   LCD_init();
   LCD_Write(1,0x0f);  //show cursor            
   LCD_set_xy(0,0);
   for(;;)
   {    
        for(t=0;t<16;t++)
        {
            LCD_Write(0,temp[t]);
            delay_ms(300);   
        }   
        LCD_Write(1,0x01);  //clear
        delay_ms(10);//can't remove
        LCD_set_xy(0,0);
   }
}

我是开源电子网www.openedv.com站长,有关站务问题请与我联系。
正点原子STM32开发板购买店铺http://openedv.taobao.com
正点原子官方微信公众平台,点击这里关注“正点原子”
正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2024-11-24 13:33

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表