Advertisement
ceterumcenseo

LCD1602.c

Mar 27th, 2019
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.36 KB | None | 0 0
  1. #include <bcm2835.h>
  2. #include <stdio.h>
  3.  
  4. #define D0 6
  5. #define D1 5
  6. #define D2 13
  7. #define D3 19
  8. #define D4 26
  9. #define D5 21
  10. #define D6 20
  11. #define D7 16
  12.  
  13. #define RS 17
  14. #define RW 27
  15. #define E  22
  16. int data_pin[]={D0,D1,D2,D3,D4,D5,D6,D7};
  17.  
  18. char lcdbuf[]={"hello Raspberry!"};
  19. #define LEAVE_CHAR (16-(sizeof(lcdbuf)-1))
  20. void display_string(int x,int y,char *s);
  21. void display_xy(int x,int y);
  22. void lcd_dataWrite(char data);
  23. void WriteDatalcd(char Wdata);
  24. void lcd_init(void);
  25. void WriteCommandlcd(char Wdata);
  26. void gpio_init(void);
  27. void display_char(int x,int y,char s);
  28. void move_display(void);
  29.  
  30. int main(int argc, char **argv)
  31. {
  32.    
  33.  
  34.     if (!bcm2835_init())return 1;
  35.        
  36.     gpio_init();
  37.    
  38.     lcd_init();
  39.    
  40.     while(1)
  41.     {
  42.    
  43.             WriteCommandlcd(0x01);//clear screen
  44.            
  45.        
  46.         move_display();
  47.                    
  48.         bcm2835_delay(500);
  49.  
  50.   }
  51.  
  52.  
  53.    bcm2835_close();
  54.  
  55.   return 0;
  56.    
  57.    
  58. }
  59.  
  60. void lcd_init(void)
  61. {
  62.     bcm2835_delay(10);
  63.    
  64.     WriteCommandlcd(0x38);
  65.     bcm2835_delay(10);
  66.     WriteCommandlcd(0x38);
  67.         bcm2835_delay(10);
  68.         WriteCommandlcd(0x38);
  69.         bcm2835_delay(10);
  70.  
  71.     WriteCommandlcd(0x06);//
  72.         bcm2835_delay(10);
  73.     WriteCommandlcd(0x0c);
  74.         bcm2835_delay(10);
  75.     WriteCommandlcd(0x01);
  76.    
  77.    
  78.    
  79.    
  80. }
  81.  
  82. void WriteCommandlcd(char Wdata)//write command to LCD
  83. {
  84.        
  85.    
  86.         bcm2835_gpio_write(RS, LOW);
  87.         bcm2835_gpio_write(RW, LOW);
  88.         bcm2835_gpio_write(E, HIGH);
  89.        
  90.         lcd_dataWrite(Wdata);
  91.         bcm2835_delay(10);
  92.         bcm2835_gpio_write(E,LOW);
  93.    
  94.    
  95.    
  96. }
  97.  
  98. void WriteDatalcd(char Wdata)//write date to LCD
  99. {
  100.    
  101.    
  102.         bcm2835_gpio_write(RS, HIGH);
  103.         bcm2835_gpio_write(RW, LOW);
  104.         bcm2835_gpio_write(E, HIGH);
  105.        
  106.         lcd_dataWrite(Wdata);
  107.         bcm2835_delay(10);
  108.         bcm2835_gpio_write(E, LOW);
  109.    
  110.    
  111.    
  112. }
  113.  
  114. void lcd_dataWrite(char data)//write date to D0~D7
  115. {
  116.     int i,move=0x80;
  117.    
  118.     for(i = 7; i >=0; i--)
  119.             {
  120.                 if(data&move)  
  121.                     {
  122.                         bcm2835_gpio_write(data_pin[i], HIGH);
  123.                         //printf("1");
  124.                     }
  125.                 else
  126.                     {
  127.                         bcm2835_gpio_write(data_pin[i], LOW);
  128.                         //printf("0");
  129.                     }
  130.                 move=move>>1;
  131.                
  132.                
  133.             }
  134.       // printf("\r\n");
  135.    
  136. }
  137.  
  138. void display_xy(int x,int y)//set start location
  139. {
  140.     if(y==1)
  141.         x+=0x40;
  142.    
  143.     x+=0x80;
  144.            
  145.     WriteCommandlcd(x);
  146.    
  147.    
  148.    
  149. }
  150.  
  151.  
  152. void display_string(int x,int y,char *s)//display string
  153. {
  154.     display_xy(x,y);
  155.     while(*s)
  156.     {
  157.         WriteDatalcd(*s);
  158.         s++;
  159.        
  160.     }  
  161.    
  162. }
  163.  
  164. void display_char(int x,int y,char s)//display single char
  165. {
  166.     display_xy(x,y);
  167.  
  168.         WriteDatalcd(s);
  169.        
  170.    
  171. }
  172.  
  173. void gpio_init(void)
  174. {
  175.     int i=8;
  176.    
  177.       while(i--)
  178.       {
  179.          bcm2835_gpio_fsel(data_pin[i], BCM2835_GPIO_FSEL_OUTP);
  180.          
  181.       }
  182.        
  183.     bcm2835_gpio_fsel(RS, BCM2835_GPIO_FSEL_OUTP);
  184.     bcm2835_gpio_fsel(E, BCM2835_GPIO_FSEL_OUTP);
  185.     bcm2835_gpio_fsel(RW, BCM2835_GPIO_FSEL_OUTP);
  186.    
  187.    
  188. }
  189.  
  190.  
  191. void move_display(void)//word move display
  192. {
  193.    
  194.     static int x=0,y=0,i;
  195.    
  196.         if(x>=16&&y==0)
  197.             {
  198.                 y=1;
  199.                 x=0;
  200.             }
  201.         else if(x>=16&&y==1)
  202.             {
  203.                     y=0;
  204.                     x=0;
  205.             }
  206.                
  207.                
  208.             if(x>=LEAVE_CHAR&&y==0)
  209.                 {
  210.                     i=0;
  211.                     while(i<(x-LEAVE_CHAR))
  212.                     {
  213.                     display_char(i,1,lcdbuf[(sizeof(lcdbuf)-1)-(x-LEAVE_CHAR)+i]);
  214.                     i++;
  215.                 }
  216.                 }
  217.                
  218.                
  219.                 if(x>=LEAVE_CHAR&&y==1)
  220.                 {
  221.                     i=0;
  222.                     while(i<(x-LEAVE_CHAR))
  223.                     {
  224.                     display_char(i,0,lcdbuf[(sizeof(lcdbuf)-1)-(x-LEAVE_CHAR)+i]);
  225.                     i++;
  226.                 }
  227.                 }
  228.                 display_string(x,y,lcdbuf);
  229.                 x++;
  230.                
  231.    
  232.    
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement