ICC AVR LCD1602 display program

Publisher:世界因你而精彩Latest update time:2017-12-15 Source: eefocusKeywords:icc  avr  LCD1602  display Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Entry parameter description: 

    // control port 
    //#define SET_RS sbi(PORTB,5) 
    //#define CLR_RS cbi(PORTB,5) 

    //#define OUT_RS  sbi(DDRB,5) 
     
    //#define SET_RW  sbi(PORTB,6) 
    //#define CLR_RW  cbi(PORTB,6) 
    //#define OUT_RW  sbi(DDRB,6) 
     
    //#define SET_E   sbi(PORTB,7) 
    //#define CLR_E   cbi(PORTB,7) 
    //#define OUT_E   sbi(DDRB,7) 
     
    // data port 
    //#define SET_D4  sbi(PORTD,4) 
    //#define CLR_D4  cbi(PORTD,4) 
    //#define OUT_D4  sbi(DDRD,4) 
     
    //#define SET_D5  sbi(PORTD,5) 
    //#define CLR_D5  cbi(PORTD,5) 
    //#define OUT_D5  sbi(DDRD,5) 
     
    //#define SET_D6  sbi(PORTD,6) 
    //#define CLR_D6  cbi(PORTD,6) 
    //#define OUT_D6  sbi(DDRD,6) 
     
    //#define SET_D7  sbi(PORTD,7) 
    //#define CLR_D7  cbi(PORTD,7) 
    //#define OUT_D7  sbi(DDRD,7) 
     
    // busy port 
    //#define GET_BF  gbi(PIND,7) 
    //#define OUT_BF  sbi(DDRD,7) 
    //#define IN_BF      cbi(DDRD,7) 
     
---------------------------------------------------------------------- 
接口定义: 
LCD1602                ATmega16 
1.GND        --------    GND 
2.VCC        --------    VCC 
3.V0        --------    V0 
4.RS        --------    由外部程序定义 
5.R/W        --------    由外部程序定义 
6.E        --------    由外部程序定义 
7.D0        --------    NC 
8.D1        --------    NC 
9.D2        --------    NC 
10.D3        --------    NC 
11.D4        --------    由外部程序定义 
12.D5        --------    由外部程序定义 
13.D6        --------    由外部程序定义 
14.D7        --------    由外部程序定义 
15.LED+        --------    VCC 
16.LED-        --------    GND 

说明: 
(1)使用ATmega16的7根IO口操作LCD1602 
(2)该程序的优点是:7根IO可任意定义,不需分布在固定的一组PORT口上 
(3)该程序的缺点是:IO定义的写法较为繁琐 
---------------------------------------------------------------------- 
待定参数说明: 
    //#define DELAY()        {_nop_();_nop_();_nop_();} 

----------------------------------------------------------------------     
对外变量说明: 

---------------------------------------------------------------------- 
对外函数说明: 

---------------------------------------------------------------------- 
10101010101010101010101010101010101010101010101010101010101010101010*/ 

#ifndef LCD1602_H 
#define LCD1602_H 

#include "D:\ICC_H\CmmICC.H" 

/* 待定参数 */ 
#define DELAY()        {NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();} 

/* Writing without considering portability */ 
//uint8 bdata bdat; 
//sbit bdat0=bdat^0; 
//sbit bdat1=bdat^1; 
//sbit bdat2=bdat^2; 
//sbit bdat3=bdat^ 3; 
//sbit bdat4=bdat^4; 
//sbit bdat5=bdat^5; 
//sbit bdat6=bdat^6; 
//sbit bdat7=bdat^7; 
/* Writing method considering portability */ 
uint8 bdat; 
#define bdat0 (bdat&0x01) 
#define bdat1 (bdat&0x02) 
#define bdat2 (bdat&0x04) 
#define bdat3 (bdat&0x08) 
#define bdat4 (bdat&0x10) 
#define bdat5 (bdat&0x20) 
#define bdat6 (bdat&0x40) 
#define bdat7 (bdat&0x80) 

#define CGRAM0 0x00 
#define CGRAM1 0x01 
#define CGRAM2 0x02 
#define CGRAM3 0x03 
#define CGRAM4 0x04 
#define CGRAM5 0x05 
#define CGRAM6 0x06 
#define CGRAM7 0x07 

#define TRUE 1 
#define FALSE 0 
bool LCD1602Err = FALSE; 

/*-------------------------------------- ------------------------------ 
Function name: LCD1602 read read read read busy ~ 
Function function: It is said to be read read read Reading is busy~ 
Note: For high-speed CPU, delay should be added, it seems to be nonsense~ 
Prompt description: No 
input: 
Return: None 
-------------------- ------------------------------------------------*/ 
void busy(void) 

    uint16 busyCounter=0; 
    bool busySta; //used to detect LCD busy status 
    IN_BF; 
    SET_D4; 
    SET_D5; 
    SET_D6; 
    SET_D7; 
    DELAY(); 
    CLR_RS; 
    DELAY(); 
    SET_RW; 
    DELAY(); 

    do 
    { 
        SET_E;    
        DELAY() ; 
/* Read the values ​​of AC4-AC6 and BF here. The program does not need to record the values ​​of AC4-AC6, so they are not stored. */ 
        busySta=(bool)GET_BF;   
        CLR_E; 
        DELAY();  
/* Read "BUSY" When the "D4-D7" state may have changed, it must be set to output "1" again */ 
        SET_D4; 
        SET_D5; 
        SET_D6; 
        SET_D7; 
        DELAY(); 
        SET_E;    
        DELAY(); 
/* Here, the AC0-AC3 bits are read Value, the program does not need to record the value of AC0-AC3, so it is not stored*/ 
        CLR_E;    
        DELAY(); 
        if(busyCounter==1000) 
        { 
            LCD1602Err=TRUE; //Mark LCD1602 error, convenient for reporting error to system 
            return; //Avoid program blocking due to LCD1602 error 
        } 
        busyCounter++; 
    } 
    while(busySta); 

    LCD1602Err =FALSE; 
    CLR_E; 
    OUT_BF; 

/*---------------------------------------- ---------------------------- 
Function name: LCD1602 write operation 
function function: 
Note: For high-speed CPU, delay should be added, which seems to be nonsense~ 
Hint: No 
input: 
Return: None 
--------------------------------------------------------------------*/ 
void write(bool flag,uint8 dat) //flag=0:command,flag=1:data 
{   
    bdat=dat; 
    busy(); 
    if(flag) 
        SET_RS; 
    else 
        CLR_RS; 
    DELAY(); 
    CLR_RW; 
    DELAY(); 
    if(bdat4) 
        SET_D4; 
    else 
        CLR_D4; 
    if(bdat5) 
        SET_D5; 
    else 
        CLR_D5; 
    if(bdat6  )
        SET_D6; 
    else 
        CLR_D6; 
    if(bdat7) 
        SET_D7; 
    else 
        CLR_D7; 
    DELAY(); 
    SET_E;     
    DELAY(); 
    CLR_E; 
    DELAY();  

    if(bdat0) 
        SET_D4; 
    else 
        CLR_D4; 
    if(bdat1) 
        SET_D5; 
    else 
        CLR_D5; 
    if(bdat2) 
        SET_D6; 
    else 
        CLR_D6; 
    if(bdat3) 
        SET_D7; 
    else 
        CLR_D7; 
    DELAY(); 
    SET_E;         
    DELAY(); 
    CLR_E; 
    DELAY(); 

/*------------------------------------------------------------------------------------ 
Function name: LCD1602 read operation 
Function function: 
Note: For high-speed CPU, a delay should be added. It seems to be nonsense. 
Tip description: No 
input: 
Return: None 
--------------------------------------------------------------------*/ 
//void read(uint8 adr) 
//{     
//} 
/*-------------------------------------------------------------------- 
Function name: LCD1602 sets CGRAM content 
Function function: 
Note: For high-speed CPU, a delay should be added. It seems to be nonsense. 
Tip description: Call LCD1602_setCG(0,userCh) to write the user-defined character "userCh" 
input Input: "adr" data range: 0-8, "buf" is the character "userCh" that the user needs to write 
Return: None 
-------------------------------------------------------------------------------------*/ 
void LCD1602_setCGRAM(uint8 adr,const uint8 buf[8]) 
{     
    uint8 i; 
    write(0,0x40+adr*8); 
    for(i=0;i<8;i++) 
        write(1,buf[i]); 
/* The following writing method must not be sampled, because the input is an array and the last one is not '\0' */ 
    //while(*buf) 
        //write(1,*buf++); 

/*-------------------------------------------------------------------- 
Function name: LCD1602 command setting 
function Function: 
Note: For high-speed CPU, a delay should be added. It seems to be nonsense~ 
Tips: 
输    入:"CLR_SCR"/"GO_HOME"/"AC_INC"/"AC_DEC"... 
返    回:无 
--------------------------------------------------------------------*/ 
//----    function  ------  1  --------  0  ----LcdWordPos-- 
//        dispEn        |   Enable    |  DISAble    |    bit2 
//        cursorEn    |   Enable    |  Disable    |    bit1 
//        blinkEn        |   Enable    |  Disable    |    bit0 
//------------------------------------------------------ 
//        isACinc        |    INC_AC  |  DEC_AC    |    bit1 
//        shiftEn        |   Enable    |  Disable    |    bit0 
//------------------------------------------------------ 
void LCD1602_setCmd(uint8  *str) 

    static bool dispEn  =0; 
    static bool cursorEn=0; 
    static bool blinkEn =0; 
    static bool shiftEn =0; 
    static bool isACinc =0; 
     
    if(!strcmp(str,"CLR_SCR"))            //clear screen 
        write(0,0x01); 

    else if(!strcmp(str,"GO_HOME"))        //set AC go home 
        write(0,0x02); 
/*-------------------------------------------------- 
isACinc & shiftEn 共用一个命令设置 
--------------------------------------------------*/ 
    else if(!strcmp(str,"INC_AC"))        //set AC as inc mode 
    { 
        isACinc=1; 
        if(shiftEn) 
            write(0,0x07); 
        else  
            write(0,0x06); 
    }         
    else if(!strcmp(str,"DEC_AC"))        //set AC as dec mode 
    {     
        isACinc=0; 
        if(shiftEn) 
            write(0,0x05); 
        else 
            write(0,0x04); 
    }         
    else if(!strcmp(str,"EN_SHIFT"))    //enable shift 
    { 
        shiftEn=1; 
        if(isACinc) 
            write(0,0x07); 
        else 
            write(0,0x06); 
    } 
    else if(!strcmp(str,"DIS_SHIFT"))    //disable shift 
    { 
        shiftEn=0; 
        if(isACinc) 
            write(0,0x05); 
        else 
            write(0,0x04); 
    }     
/*-------------------------------------------------- 
dispEn & cursorEn & blinkEn共用一个命令设置 
--------------------------------------------------*/ 
    else if(!strcmp(str,"OPEN_LCD"))    //opern lcd 
    { 
        dispEn=1; 
        if(cursorEn) 
            if(blinkEn) 
                write(0,0x0F); 
            else 
                write(0,0x0E); 
        else 
            if(blinkEn) 
                write(0,0x0D); 
            else 
                write(0,0x0C); 
    }         
    else if(!strcmp(str,"CLOSE_LCD"))    //close lcd 
    { 
        dispEn=0; 
        if(cursorEn) 
            if(blinkEn) 
                write(0,0x0B); 
            else 
                write(0,0x0A); 
        else 
            if(blinkEn) 
                write(0,0x09); 
            else 
                write(0,0x08); 
    }     
    else if(!strcmp(str,"OPEN_CURS"))    //open cursor     
    { 
        cursorEn=1; 
        if(dispEn) 
            if(blinkEn) 
                write(0,0x0F); 
            else 
                write(0,0x0E); 
        else 
            if(blinkEn) 
                write(0,0x0B); 
            else 
                write(0,0x0A); 
    }     
    else if(!strcmp(str,"CLOSE_CURS"))    //close cursor 
    { 
        cursorEn=0; 
        if(dispEn) 
            if(blinkEn) 
                write(0,0x0D); 
            else 
                write(0,0x0C); 
        else 
            if(blinkEn) 
                write(0,0x09); 
            else 
                write(0,0x08); 
    } 
    else if(!strcmp(str,"EN_BLINK"))    //enable blink cursor 
    { 
        blinkEn=1; 
        if(dispEn) 
            if(cursorEn) 
                write(0,0x0F); 
            else 
                write(0,0x0D); 
        else 
            if(cursorEn) 
                write(0,0x0B); 
            else 
                write(0,0x09); 
    } 
    else if(!strcmp(str,"DIS_BLINK"))    //disable blink cursor 
    { 
        blinkEn=0; 
        if(dispEn) 
            if(cursorEn) 
                write(0,0x0E); 
            else 
                write(0,0x0C); 
        else 
            if(cursorEn) 
                write(0,0x0A); 
            else 
                write(0,0x08); 
    } 
/*-------------------------------------------------- 
dispEn & cursorEn & blinkEn共用一个命令设置 
--------------------------------------------------*/ 
    else if(!strcmp(str,"RIGHT_SCR")) //right shift screen 
        write(0,0x1c); 
    else if(!strcmp(str,"LEFT_SCR")) //left shift screen 
        write(0,0x18); 
    else if(!strcmp(str,"RIGHT_CURS")) //right shift cursor 
        write(0,0x14); 
    else if(!strcmp(str,"LEFT_CURS")) //left shift cursor 
        write(0,0x10); 

/*-------------------------------------------------------------------- 
Function name: LCD1602 initialization 
function function: 
Note: 
Tip description: No 
input: 
Return: None 
--------------------------------------------------------------------*/ 
void LCD1602_init(void) 
{    
    OUT_RS; 
    OUT_RW; 
    OUT_E; 
    OUT_D4; 
    OUT_D5; 
    OUT_D6; 
    OUT_D7; 
    delay50ms(1); 

    CLR_D7; 
    CLR_D6; 
    SET_D5; 
    SET_D4; 
    DELAY(); 
    CLR_RS; 
    DELAY(); 
    CLR_RW;                          

    SET_E; 
    DELAY();      CLR_E 
    ; 
    delay50us(200);         
    SET_E;  DELAY     (  );     CLR_E;      delay50us      (200);          SET_E          ;      D5      ;      CLR_D4      ;      DELAY();      SET_E;      DELAY();      CLR_E                             ;     DELAY();      LCD1602_setCmd("OPEN_LCD");      LCD1602_setCmd("CLR_SCR");      LCD1602_setCmd("INC_AC");      //LCD1602_setCmd("OPEN_CURS");      //LCD1602_set Cmd("GO_HOME");  }  /*--------------------------------------------------------------------    Macro name: Set AC value    Macro function: Set AC value  Notes:  Tip Description:  Input:  Return: None  --------------------------------------------------------------------*/  #define LCD1602_setAC(adr) write(0,adr)  /*--------------------------------------------------------------------  Function name: Output a character  Function function:  Notes: For high-speed CPU, delay should be added, which seems to be nonsense~  Tip Description: Call LCD1602_putc(0x80,'A'), then output  'A' at the first character of the first line Input  : Return: None  --------------------------------------------------------------------*/  void LCD1602_putc(uint8 adr,uint8 ch)  {      write(0,adr);      write(1,ch);  } 







     





































/*-------------------------------------------------------------------- 
Function name: Output a string 
Function: 
Note: No 
prompt Description: Call LCD1602_puts(0x80,"waveShare"), then output "waveShare" from the first position of the first line Input 

Return: None 
--------------------------------------------------------------------*/ 
void LCD1602_puts(uint8 startAdr,uint8 *str) 

    /* 
    while(*str) 
    { 
        LCD1602_putc(addr++,*str++); 
    } 
    */ 
    //LCD1602_setCmd("AC++"); 
    write(0,startAdr); 
    while(*str) 
        write(1,*str++); 

/*-------------------------------------------------------------------- 
Function name: Output a value (with 0) 
Function function: Sometimes you may not need "123", but "00123" 
Note: No 
prompt Description: Call LCD1602_putd0(0x8F,123,5), then output "00123" from 0x8B to 0X8F 
Input: 
Return: None 
--------------------------------------------------------------------*/ 
//for example:dat=123,length=6,output 000123  
void LCD1602_putd0(uint8 endAdr,uint32 dat,uint8 length) 

    sint8 i; 
    speaData(dat,length); 
    //LCD1602_setCmd("AC++"); 
    write(0,endAdr-length+1); 
    for(i=length-1;i>=0;i--) 
        write(1,dataElem[i]+0x30); 

/*-------------------------------------------------------------------- 
Function name: Output a value (without 0) 
Function function: 
Note: No 
prompt Description: Call LCD1602_putd(0x8F,123,5), then output "123" from 0x8B to 0X8F Input 

Return: None 
--------------------------------------------------------------------*/ 
void LCD1602_putd(uint8 endAdr,uint32 dat,uint8 length) 

    sint8 i; 
    sint8 effectLen; 
    if(dat>999999) 
         effectLen=7; 
    else if(dat>99999) 
         effectLen=6; 
    else if(dat>9999) 
         effectLen=5; 
    else if(dat>999) 
         effectLen=4; 
    else if(dat>99) 
         effectLen=3; 
    else if(dat>9) 
         effectLen=2; 
    else 
         effectLen=1; 
    speaData(dat,effectLen); 
    //LCD1602_setCmd("AC++"); 
    if(length>effectLen) 
    { 
        write(0,endAdr-length+1); 
        for(i=length-effectLen-1;i>=0;i--) 
            write(1,' '); 
    } 
    for(i=effectLen-1;i>=0;i--) 
    { 
        if(i==0||dataElem[i]) 
        { 
            write(0, endAdr-i); 
            for(;i>=0;i--) 
                write(1,dataElem[i]+0x30); 
        } 
    } 

/*-------------------------------------------------------------------- 
Function name: Output a mixed string 
Function function: 
Note: It is best not to load this function, because it will take up nearly 1K space 
Tips: Call LCD1602_sprintf(0x8F,12AB,4), then output "12ABok" from 0x8B to 0X8F 
Input: 
Return: None 
--------------------------------------------------------------------*/ 
//void LCD1602_sprintf(uint8 startAdr,uint32 dat,uint8 length) 
//{ 
// /* clear the display area first here! */ 
// //LCD1602_puts(addr," "); 
// sprintf(t,"%luok",dat); 
// //LCD1602_setCmd("AC++"); 
// LCD1602_puts(addr,t); 
//} 

#endif 


Keywords:icc  avr  LCD1602  display Reference address:ICC AVR LCD1602 display program

Previous article:AVR MCU serial port multi-machine communication program
Next article:AVR multi-machine communication

Recommended ReadingLatest update time:2024-11-15 23:42

AVR MCU fuse settings and detailed rescue methods
Fuse bits are a unique feature of ATMEL's AVR microcontrollers. Each model of AVR microcontroller has some fuse bits with specific meanings, and its characteristics are shown as multiple erasable E²PROM. By configuring (programming) these fuse bits, users can set some features, parameters, and I/O configurations of AV
[Microcontroller]
AVR MCU fuse settings and detailed rescue methods
AVR microcontroller tutorial - serial port sending
So far, our development board can only process a small amount of data: read a few pin levels, output a few LEDs, and at most use a digital tube to display a two-digit number. As for inputting a command, outputting a debugging message, or even using scanf and printf for input and output, it is unimaginable on the devic
[Microcontroller]
AVR microcontroller tutorial - serial port sending
Design of AVR reset circuit
1. Anti-interference design of reset pin: Compared with the traditional 51 single-chip microcomputer, the AVR single-chip microcomputer has a built-in reset circuit, and the reset time can be controlled in the fuse position. Therefore, the AVR single-chip microcomputer can be reset normally without an external power-o
[Microcontroller]
Learning experience of AVR and PIC microcontrollers
      The key to successfully compiling two types of MCU project files is that the project file name and storage directory must be in English letters and must not be Chinese character directory! Otherwise, there will be compilation errors. I used the Chinese character directory "My Documents" to compile the project fi
[Microcontroller]
How to sample 8-channel AD in AVR microcontroller
//*********ATmega128,8channel ADC sampling + LED display + USART******* #include iom128v.h #include macros.h #define uchar unsigned char #define uint unsigned int #define ulong unsigned long #define FOSC   1843200 #define baud   9600 #define MyUBBR   (uint)((ulong)FOSC/(16*(ulong)baud)-1) const uchar table ={0x3f,0x0
[Microcontroller]
AVR128 Program - 12864 LCD Display
#include   #define uint unsigned int #define uchar unsigned char   #define psbset   DDRB |= (1 4) #define psbset   PORTB |= (1 4) #define psbclr   PORTB &=~(1 4)   #define rsout   DDRB |= (1 5) #define rsset   PORTB |=(1 5) #define rsclr   PORTB &=~(1 5)   #define route   DDRB |= (1 6) #defi
[Microcontroller]
[AVR] High Voltage Parallel Programming --- Basic Knowledge
I locked several Mega16 chips with a shake of my hand, which cost dozens of dollars... It is necessary to build a high-voltage parallel programmer 1. Wiring method and port definition RDY/BSY busy flag. (0: device is busy 1: waiting for new command) OE output enable bit low level effective WR write pulse low level i
[Microcontroller]
ATMEL chip decryption: AVR and ARM selection analysis
Atmel chip decryption is to use special equipment or self-made equipment, exploit the loopholes or software defects in the chip design of the microcontroller, and use a variety of technical means to extract key information from the chip and obtain the chip decryption of the program in the microcontroller. In the resea
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号