Guest User

Untitled

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