ceterumcenseo icon

LCD1602.c

ceterumcenseo | PRO | 03/27/19 05:54:34 PM UTC | 0 ⭐ | 588 👁️ | Never ⏰ | []
C |

3.36 KB

|

None

|

0 👍

/

0 👎

#include <bcm2835.h>
#include <stdio.h>
 
#define D0 6
#define D1 5
#define D2 13
#define D3 19
#define D4 26
#define D5 21
#define D6 20
#define D7 16
 
#define RS 17
#define RW 27
#define E  22
int data_pin[]={D0,D1,D2,D3,D4,D5,D6,D7};
 
char lcdbuf[]={"hello Raspberry!"};
#define LEAVE_CHAR (16-(sizeof(lcdbuf)-1))
void display_string(int x,int y,char *s);
void display_xy(int x,int y);
void lcd_dataWrite(char data);
void WriteDatalcd(char Wdata);
void lcd_init(void);
void WriteCommandlcd(char Wdata);
void gpio_init(void);
void display_char(int x,int y,char s);
void move_display(void);
 
int main(int argc, char **argv)
{
    
 
    if (!bcm2835_init())return 1;
        
    gpio_init();
    
    lcd_init();
    
    while(1)
    {
    
            WriteCommandlcd(0x01);//clear screen
            
        
        move_display();
                    
        bcm2835_delay(500);
 
  }
  
  
   bcm2835_close();
 
  return 0;
    
    
}
 
void lcd_init(void)
{
    bcm2835_delay(10);
    
    WriteCommandlcd(0x38);
    bcm2835_delay(10);
    WriteCommandlcd(0x38);
        bcm2835_delay(10);
        WriteCommandlcd(0x38);
        bcm2835_delay(10);
 
    WriteCommandlcd(0x06);//
        bcm2835_delay(10);
    WriteCommandlcd(0x0c);
        bcm2835_delay(10);
    WriteCommandlcd(0x01);
    
    
    
    
}
 
void WriteCommandlcd(char Wdata)//write command to LCD
{
        
    
        bcm2835_gpio_write(RS, LOW);
        bcm2835_gpio_write(RW, LOW);
        bcm2835_gpio_write(E, HIGH);
        
        lcd_dataWrite(Wdata);
        bcm2835_delay(10);
        bcm2835_gpio_write(E,LOW);
    
    
    
}
 
void WriteDatalcd(char Wdata)//write date to LCD
{
    
    
        bcm2835_gpio_write(RS, HIGH);
        bcm2835_gpio_write(RW, LOW);
        bcm2835_gpio_write(E, HIGH);
        
        lcd_dataWrite(Wdata);
        bcm2835_delay(10);
        bcm2835_gpio_write(E, LOW);
    
    
    
}
 
void lcd_dataWrite(char data)//write date to D0~D7
{
    int i,move=0x80;
    
    for(i = 7; i >=0; i--)
            {
                if(data&move)   
                    {
                        bcm2835_gpio_write(data_pin[i], HIGH);
                        //printf("1");
                    }
                else
                    {
                        bcm2835_gpio_write(data_pin[i], LOW);
                        //printf("0");
                    }
                move=move>>1;
                
                
            }
      // printf("\r\n");
    
}
 
void display_xy(int x,int y)//set start location
{
    if(y==1)
        x+=0x40;
    
    x+=0x80;
            
    WriteCommandlcd(x);
    
    
    
}
 
 
void display_string(int x,int y,char *s)//display string
{
    display_xy(x,y);
    while(*s)
    {
        WriteDatalcd(*s);
        s++;
        
    }   
    
}
 
void display_char(int x,int y,char s)//display single char
{
    display_xy(x,y);
 
        WriteDatalcd(s);
        
    
}
 
void gpio_init(void)
{
    int i=8;
    
      while(i--)
      {
         bcm2835_gpio_fsel(data_pin[i], BCM2835_GPIO_FSEL_OUTP);
         
      }
        
    bcm2835_gpio_fsel(RS, BCM2835_GPIO_FSEL_OUTP);
    bcm2835_gpio_fsel(E, BCM2835_GPIO_FSEL_OUTP);
    bcm2835_gpio_fsel(RW, BCM2835_GPIO_FSEL_OUTP);
    
    
}
 
 
void move_display(void)//word move display
{
    
    static int x=0,y=0,i;
    
        if(x>=16&&y==0)
            {
                y=1;
                x=0;
            }
        else if(x>=16&&y==1)
            {
                    y=0;
                    x=0;
            }
                
                
            if(x>=LEAVE_CHAR&&y==0)
                {
                    i=0;
                    while(i<(x-LEAVE_CHAR))
                    {
                    display_char(i,1,lcdbuf[(sizeof(lcdbuf)-1)-(x-LEAVE_CHAR)+i]);
                    i++;
                }
                }
                
                
                if(x>=LEAVE_CHAR&&y==1)
                {
                    i=0;
                    while(i<(x-LEAVE_CHAR))
                    {
                    display_char(i,0,lcdbuf[(sizeof(lcdbuf)-1)-(x-LEAVE_CHAR)+i]);
                    i++;
                }
                }
                display_string(x,y,lcdbuf);
                x++;
                
    
    
}

Comments