Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.61 KB | None | 0 0
  1. #include <avr/io.h>
  2. #include <util/delay.h>
  3. #include <util/atomic.h>
  4.  
  5. #include "lcd.h"
  6.  
  7. static inline void LCDInitPorts(void)
  8. {
  9. #ifdef LCD4
  10.     //<4-bit interface port initialization>
  11.     DDRB|=_BV(1);
  12.     DDRB|=_BV(0);
  13.     DDRD|=_BV(7);
  14.     DDRD|=_BV(6);
  15. #else
  16.     //<8-bit interface port initialization>
  17. #endif
  18.  
  19.     RS_DDR|=_BV(RS_PIN);
  20.     RW_DDR|=_BV(RW_PIN);
  21.     EN_DDR|=_BV(EN_PIN);
  22. }
  23.  
  24. static inline void LCDBus(unsigned char data)
  25. {
  26. #ifdef LCD4
  27.     //<4-bit interface bus output>
  28.     ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
  29.     {
  30.         unsigned char a=PORTD;
  31.         a&=0x3F;
  32.         a|=(data<<6)&0xC0;
  33.  
  34.         unsigned char b=PORTB;
  35.         b&=0xFC;
  36.         b|=(data>>2)&0x03;
  37.  
  38.         PORTD=a;
  39.         PORTB=b;
  40.     }
  41. #else
  42.     //<8-bit interface bus output>
  43. #endif
  44. }
  45.  
  46. static inline void RS(unsigned char value)
  47. {
  48.     if(value)RS_PORT|=_BV(RS_PIN);
  49.     else RS_PORT&=~_BV(RS_PIN);
  50. }
  51.  
  52. static inline void RW(unsigned char value)
  53. {
  54.     if(value)RW_PORT|=_BV(RW_PIN);
  55.     else RW_PORT&=~_BV(RW_PIN);
  56. }
  57.  
  58. static inline void EN(unsigned char value)
  59. {
  60.     if(value)EN_PORT|=_BV(EN_PIN);
  61.     else EN_PORT&=~_BV(EN_PIN);
  62. }
  63.  
  64. #ifdef LCD4
  65. static void LCDWriteHalfByte(unsigned char data)
  66. {
  67.     RS(0);
  68.     RW(0);
  69.     EN(1);
  70.     LCDBus(data&0x0F);
  71.     _delay_us(1);
  72.     EN(0);
  73. }
  74. #endif
  75.  
  76. static void LCDWriteByte(unsigned char data)
  77. {
  78.     RW(0);
  79.  
  80. #ifdef LCD4
  81.     EN(1);
  82.     LCDBus((data&0xF0)>>4); //high nibble
  83.     _delay_us(1);
  84.     EN(0);
  85.  
  86.     _delay_us(1);
  87.  
  88.     EN(1);
  89.     LCDBus(data&0x0F); //low nibble
  90.     _delay_us(1);
  91.     EN(0);
  92.  
  93. #else
  94.     EN(1);
  95.     LCDBus(data);
  96.     _delay_us(1);
  97.     EN(0);
  98. #endif
  99. }
  100.  
  101. void LCDCommand(unsigned char cmd)
  102. {
  103.     RS(0);
  104.     LCDWriteByte(cmd);
  105. }
  106.  
  107. void LCDData(unsigned char data)
  108. {
  109.     RS(1);
  110.     LCDWriteByte(data);
  111. }
  112.  
  113. void LCDBusy(void)
  114. {
  115.     _delay_us(70);
  116. }
  117.  
  118. void LCDInit(void)
  119. {
  120.     LCDInitPorts();
  121.  
  122. #ifdef LCD4
  123.     _delay_ms(20);
  124.     LCDWriteHalfByte(0x03);
  125.     _delay_ms(5);
  126.     LCDWriteHalfByte(0x03);
  127.     _delay_us(120);
  128.     LCDWriteHalfByte(0x03);
  129.     _delay_ms(5);
  130.     LCDWriteHalfByte(0x02);
  131.     _delay_ms(3);
  132.     LCDCommand(0x28);
  133.     _delay_ms(3);
  134.     LCDCommand(0x08);
  135.     _delay_ms(3);
  136.     LCDCommand(0x01);
  137.     _delay_ms(3);
  138.     LCDCommand(0x06);
  139.     _delay_ms(3);
  140.     LCDCommand(0x0c);
  141. #else
  142.     _delay_ms(20);
  143.     LCDCommand(0x38);
  144.     _delay_ms(5);
  145.     LCDCommand(0x38);
  146.     _delay_us(120);
  147.     LCDCommand(0x38);
  148.     _delay_ms(3);
  149.     LCDCommand(0x38);
  150.     _delay_ms(3);
  151.     LCDCommand(0x08);
  152.     _delay_ms(3);
  153.     LCDCommand(0x01);
  154.     _delay_ms(3);
  155.     LCDCommand(0x06);
  156.     _delay_ms(3);
  157.     LCDCommand(0x0c);
  158. #endif
  159. }
  160.  
  161. void LCDWriteString(char* str, unsigned char strLen)
  162. {
  163.     for(unsigned char i=strLen;i;i--)
  164.     {
  165.         LCDBusy();
  166.         LCDData(*str++);
  167.     }
  168. }
  169.  
  170. void LCDWriteStringZ(char* str)
  171. {
  172.     while(*str)
  173.     {
  174.         LCDBusy();
  175.         LCDData(*str++);
  176.     }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement