Guest User

Untitled

a guest
Oct 22nd, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.05 KB | None | 0 0
  1. #define F_CPU 1000000
  2. #include <avr/io.h>
  3. #include <avr/interrupt.h>
  4. #include <avr/pgmspace.h>
  5. #include <util/delay.h>
  6.  
  7. #define LCD_SCE_HI PORTA |= (1<<1)
  8. #define LCD_SCE_LO PORTA &= ~(1<<1)
  9. #define LCD_RST_HI PORTA |= (1<<0)
  10. #define LCD_RST_LO PORTA &= ~(1<<0)
  11. #define LCD_DC_HI PORTD |= (1<<2)
  12. #define LCD_DC_LO PORTD &= ~(1<<2)
  13. #define LCD_DN_HI PORTD |= (1<<3)
  14. #define LCD_DN_LO PORTD &= ~(1<<3)
  15. #define LCD_SCLK_HI PORTD |= (1<<4)
  16. #define LCD_SCLK_LO PORTD &= ~(1<<4)
  17. #define LCD_BACKLIGHT_HI PORTD |= (1<<5)
  18. #define LCD_BACKLIGHT_LO PORTD &= ~(1<<5)
  19.  
  20. #define LCD_C 0
  21. #define LCD_D 1
  22.  
  23. #define LCD_WIDTH 84
  24. #define LCD_HEIGHT 48
  25.  
  26. const uint8_t PROGMEM ascii_table[] =
  27. {
  28. 0x00, 0x00, 0x00, 0x00, 0x00, // 20
  29. 0x00, 0x00, 0x5f, 0x00, 0x00, // 21 !
  30. 0x00, 0x07, 0x00, 0x07, 0x00, // 22 "
  31. 0x14, 0x7f, 0x14, 0x7f, 0x14, // 23 #
  32. 0x24, 0x2a, 0x7f, 0x2a, 0x12, // 24 $
  33. 0x23, 0x13, 0x08, 0x64, 0x62, // 25 %
  34. 0x36, 0x49, 0x55, 0x22, 0x50, // 26 &
  35. 0x00, 0x05, 0x03, 0x00, 0x00, // 27 '
  36. 0x00, 0x1c, 0x22, 0x41, 0x00, // 28 (
  37. 0x00, 0x41, 0x22, 0x1c, 0x00, // 29 )
  38. 0x14, 0x08, 0x3e, 0x08, 0x14, // 2a *
  39. 0x08, 0x08, 0x3e, 0x08, 0x08, // 2b +
  40. 0x00, 0x50, 0x30, 0x00, 0x00, // 2c ,
  41. 0x08, 0x08, 0x08, 0x08, 0x08, // 2d -
  42. 0x00, 0x60, 0x60, 0x00, 0x00, // 2e .
  43. 0x20, 0x10, 0x08, 0x04, 0x02, // 2f /
  44. 0x3e, 0x51, 0x49, 0x45, 0x3e, // 30 0
  45. 0x00, 0x42, 0x7f, 0x40, 0x00, // 31 1
  46. 0x42, 0x61, 0x51, 0x49, 0x46, // 32 2
  47. 0x21, 0x41, 0x45, 0x4b, 0x31, // 33 3
  48. 0x18, 0x14, 0x12, 0x7f, 0x10, // 34 4
  49. 0x27, 0x45, 0x45, 0x45, 0x39, // 35 5
  50. 0x3c, 0x4a, 0x49, 0x49, 0x30, // 36 6
  51. 0x01, 0x71, 0x09, 0x05, 0x03, // 37 7
  52. 0x36, 0x49, 0x49, 0x49, 0x36, // 38 8
  53. 0x06, 0x49, 0x49, 0x29, 0x1e, // 39 9
  54. 0x00, 0x36, 0x36, 0x00, 0x00, // 3a :
  55. 0x00, 0x56, 0x36, 0x00, 0x00, // 3b ;
  56. 0x08, 0x14, 0x22, 0x41, 0x00, // 3c <
  57. 0x14, 0x14, 0x14, 0x14, 0x14, // 3d =
  58. 0x00, 0x41, 0x22, 0x14, 0x08, // 3e >
  59. 0x02, 0x01, 0x51, 0x09, 0x06, // 3f ?
  60. 0x32, 0x49, 0x79, 0x41, 0x3e, // 40 @
  61. 0x7e, 0x11, 0x11, 0x11, 0x7e, // 41 A
  62. 0x7f, 0x49, 0x49, 0x49, 0x36, // 42 B
  63. 0x3e, 0x41, 0x41, 0x41, 0x22, // 43 C
  64. 0x7f, 0x41, 0x41, 0x22, 0x1c, // 44 D
  65. 0x7f, 0x49, 0x49, 0x49, 0x41, // 45 E
  66. 0x7f, 0x09, 0x09, 0x09, 0x01, // 46 F
  67. 0x3e, 0x41, 0x49, 0x49, 0x7a, // 47 G
  68. 0x7f, 0x08, 0x08, 0x08, 0x7f, // 48 H
  69. 0x00, 0x41, 0x7f, 0x41, 0x00, // 49 I
  70. 0x20, 0x40, 0x41, 0x3f, 0x01, // 4a J
  71. 0x7f, 0x08, 0x14, 0x22, 0x41, // 4b K
  72. 0x7f, 0x40, 0x40, 0x40, 0x40, // 4c L
  73. 0x7f, 0x02, 0x0c, 0x02, 0x7f, // 4d M
  74. 0x7f, 0x04, 0x08, 0x10, 0x7f, // 4e N
  75. 0x3e, 0x41, 0x41, 0x41, 0x3e, // 4f O
  76. 0x7f, 0x09, 0x09, 0x09, 0x06, // 50 P
  77. 0x3e, 0x41, 0x51, 0x21, 0x5e, // 51 Q
  78. 0x7f, 0x09, 0x19, 0x29, 0x46, // 52 R
  79. 0x46, 0x49, 0x49, 0x49, 0x31, // 53 S
  80. 0x01, 0x01, 0x7f, 0x01, 0x01, // 54 T
  81. 0x3f, 0x40, 0x40, 0x40, 0x3f, // 55 U
  82. 0x1f, 0x20, 0x40, 0x20, 0x1f, // 56 V
  83. 0x3f, 0x40, 0x38, 0x40, 0x3f, // 57 W
  84. 0x63, 0x14, 0x08, 0x14, 0x63, // 58 X
  85. 0x07, 0x08, 0x70, 0x08, 0x07, // 59 Y
  86. 0x61, 0x51, 0x49, 0x45, 0x43, // 5a Z
  87. 0x00, 0x7f, 0x41, 0x41, 0x00, // 5b [
  88. 0x02, 0x04, 0x08, 0x10, 0x20, // 5c ¥
  89. 0x00, 0x41, 0x41, 0x7f, 0x00, // 5d ]
  90. 0x04, 0x02, 0x01, 0x02, 0x04, // 5e ^
  91. 0x40, 0x40, 0x40, 0x40, 0x40, // 5f _
  92. 0x00, 0x01, 0x02, 0x04, 0x00, // 60 `
  93. 0x20, 0x54, 0x54, 0x54, 0x78, // 61 a
  94. 0x7f, 0x48, 0x44, 0x44, 0x38, // 62 b
  95. 0x38, 0x44, 0x44, 0x44, 0x20, // 63 c
  96. 0x38, 0x44, 0x44, 0x48, 0x7f, // 64 d
  97. 0x38, 0x54, 0x54, 0x54, 0x18, // 65 e
  98. 0x08, 0x7e, 0x09, 0x01, 0x02, // 66 f
  99. 0x0c, 0x52, 0x52, 0x52, 0x3e, // 67 g
  100. 0x7f, 0x08, 0x04, 0x04, 0x78, // 68 h
  101. 0x00, 0x44, 0x7d, 0x40, 0x00, // 69 i
  102. 0x20, 0x40, 0x44, 0x3d, 0x00, // 6a j
  103. 0x7f, 0x10, 0x28, 0x44, 0x00, // 6b k
  104. 0x00, 0x41, 0x7f, 0x40, 0x00, // 6c l
  105. 0x7c, 0x04, 0x18, 0x04, 0x78, // 6d m
  106. 0x7c, 0x08, 0x04, 0x04, 0x78, // 6e n
  107. 0x38, 0x44, 0x44, 0x44, 0x38, // 6f o
  108. 0x7c, 0x14, 0x14, 0x14, 0x08, // 70 p
  109. 0x08, 0x14, 0x14, 0x18, 0x7c, // 71 q
  110. 0x7c, 0x08, 0x04, 0x04, 0x08, // 72 r
  111. 0x48, 0x54, 0x54, 0x54, 0x20, // 73 s
  112. 0x04, 0x3f, 0x44, 0x40, 0x20, // 74 t
  113. 0x3c, 0x40, 0x40, 0x20, 0x7c, // 75 u
  114. 0x1c, 0x20, 0x40, 0x20, 0x1c, // 76 v
  115. 0x3c, 0x40, 0x30, 0x40, 0x3c, // 77 w
  116. 0x44, 0x28, 0x10, 0x28, 0x44, // 78 x
  117. 0x0c, 0x50, 0x50, 0x50, 0x3c, // 79 y
  118. 0x44, 0x64, 0x54, 0x4c, 0x44, // 7a z
  119. 0x00, 0x08, 0x36, 0x41, 0x00, // 7b {
  120. 0x00, 0x00, 0x7f, 0x00, 0x00, // 7c |
  121. 0x00, 0x41, 0x36, 0x08, 0x00, // 7d }
  122. };
  123.  
  124. void lcd_write(uint8_t dc, uint8_t data)
  125. {
  126. if (dc)
  127. LCD_DC_HI;
  128. else
  129. LCD_DC_LO;
  130. LCD_SCE_LO;
  131. for (int i = 0; i < 8; i++)
  132. {
  133. if (data&(1<<7))
  134. LCD_DN_HI;
  135. else
  136. LCD_DN_LO;
  137. LCD_SCLK_LO;
  138. LCD_SCLK_HI;
  139. data <<= 1;
  140. }
  141. LCD_SCE_HI;
  142. LCD_DN_LO;
  143. }
  144.  
  145. void lcd_char(char c)
  146. {
  147. lcd_write(LCD_D, 0x00);
  148. for (int i = 0; i < 5; i++)
  149. lcd_write(LCD_D, pgm_read_byte(ascii_table+(c-0x20)*5+i));
  150. lcd_write(LCD_D, 0x00);
  151. }
  152.  
  153. void lcd_str(const char* str)
  154. {
  155. while (pgm_read_byte(str))
  156. {
  157. lcd_char(pgm_read_byte(str));
  158. str++;
  159. }
  160. }
  161.  
  162. void lcd_clear(void)
  163. {
  164. for (int i = 0; i < LCD_WIDTH*LCD_HEIGHT/8; i++)
  165. lcd_write(LCD_D, 0x00);
  166. }
  167.  
  168. void lcd_backlight(uint8_t enabled)
  169. {
  170. if (enabled)
  171. LCD_BACKLIGHT_HI;
  172. else
  173. LCD_BACKLIGHT_LO;
  174. }
  175.  
  176. void lcd_init(void)
  177. {
  178. // configure the LCD pins as outputs
  179. DDRA |= (1<<1);
  180. DDRA |= (1<<0);
  181. DDRD |= (1<<2);
  182. DDRD |= (1<<3);
  183. DDRD |= (1<<4);
  184. DDRD |= (1<<5);
  185. // initialize the LCD pins
  186. LCD_SCE_HI;
  187. LCD_BACKLIGHT_HI;
  188. LCD_RST_LO;
  189. LCD_RST_HI;
  190. // extended instruction set
  191. lcd_write(LCD_C, 0x21);
  192. // set Vop to 5V
  193. lcd_write(LCD_C, 0xA0);
  194. // set the temperature coefficient
  195. lcd_write(LCD_C, 0x04);
  196. // set the bias system
  197. lcd_write(LCD_C, 0x15);
  198. // active mode, horizontal addressing, basic instruction set
  199. lcd_write(LCD_C, 0x20);
  200. // normal mode
  201. lcd_write(LCD_C, 0x0C);
  202. // clear the LCD
  203. lcd_clear();
  204. }
  205.  
  206. int main(void)
  207. {
  208. lcd_init();
  209. int j = 0;
  210. while (1)
  211. {
  212. lcd_clear();
  213. for (int i = 0; i < 6; i++)
  214. {
  215. if ((i+j)%2 == 0)
  216. lcd_str(PSTR("Hello, World"));
  217. else
  218. lcd_str(PSTR("------------"));
  219. }
  220. j++;
  221. _delay_ms(1000);
  222. }
  223. return 0;
  224. }
Add Comment
Please, Sign In to add comment