Вы находитесь на странице: 1из 3

( Word to PDF Converter - Unregistered ) http://www.Word-to-PDF-Converter.

net
Coding Part A Our first Hello World program is below. We are following almost the same series of steps as the 4 bit example in Part 1. 1. 2. #include <avr/io.h> 3. #include <stdlib.h> 4. #include <util/delay.h> 5. #include <stdio.h> 6. 7. //Define functions 8. //========================================================== 9. void io_init(void); //Initializes IO 10. void send_nibble(unsigned char __rs, unsigned char __data); 11. 12. //========================================================== 13. 14. int main (void) 15. { 16. io_init(); 17. 18. _delay_ms(15); 19. send_nibble(0,0b0010); //Set to 4 bit operation (note: 1 nibble operation) 20. _delay_ms(5); 21. 22. send_nibble(0,0b0010); //Function set, 4 bit 23. send_nibble(0,0b1000); 24. 25. send_nibble(0,0b0000); //Display ON, Cursor On, Cursor Blinking 26. send_nibble(0,0b1111); 27. 28. send_nibble(0,0b0000); //Clear Display 29. send_nibble(0,0b0001); 30. 31. send_nibble(0,0b0000); //Entry Mode, Increment cursor position, No display shift 32. send_nibble(0,0b0110); 33. 34. send_nibble(1,0b0100); //H 35. send_nibble(1,0b1000); 36. 37. send_nibble(1,0b0110); //e 38. send_nibble(1,0b0101); 39. 40. send_nibble(1,0b0110); //l 41. send_nibble(1,0b1100); 42. 43. send_nibble(1,0b0110); //l 44. send_nibble(1,0b1100); 45. 46. send_nibble(1,0b0110); //o 47. send_nibble(1,0b1111); 48. 49. send_nibble(1,0b0010); //Space 50. send_nibble(1,0b0000); 51. 52. send_nibble(1,0b0101); //w

( Word to PDF Converter - Unregistered ) http://www.Word-to-PDF-Converter.net


53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. send_nibble(1,0b0111); send_nibble(1,0b0110); send_nibble(1,0b1111); send_nibble(1,0b0111); send_nibble(1,0b0010); send_nibble(1,0b0110); send_nibble(1,0b1100); send_nibble(1,0b0110); send_nibble(1,0b0100); return(0); } void io_init (void) { /* PC 7: N/A PC 6: Reset PC 5: Enable PC 4: Register Select PC 3: Data 7 PC 2: Data 6 PC 1: Data 5 PC 0: Data 4 */ DDRC = 0b00111111; } void send_nibble(unsigned char __rs, unsigned char __data) { PORTC = (__rs<<4) | __data | 0b00100000; // Set RS &amp; Data. Set EN=High _delay_ms(1); PORTC = (__rs<<4) | __data; // Set RS &amp; Data. Set EN=Low _delay_ms(1); } //o

//r

//l

//d

Coding Part B
The previous example is fine, if all you want to do is understand the principles of how LCD displays are driven, but in practice coding in this manner is a bit painful. To make things easier we need to abstract away the finer implementation details and focus on the operations the developer needs to perform. The easiest way to do this is use a pre-existing library. A very popular HD44780 AVR library is Peter Fleurys LCD library. This library is very good but Im going to use alank2s slimmed down LCD library instead. My reasons are: Peters library requires that R/W be connected to one of the I/O ports alank2s library separates out settings into a separate .h file. I think this is a lot cleaner alank2s library compiles a bit more tightly alank2s library supports up to 4 LCD displays, each sharing the same data and RS lines, but having different EN lines.

( Word to PDF Converter - Unregistered ) http://www.Word-to-PDF-Converter.net


To use alank2s library Add hd44780.c and hd44780.h to your project Create hd44780_settings.h (an example is provided in alank2s zip file) and tailor it for you project Use the provided functions Using alanks library we create the next example program. This program does much more than our previous Hello World example and is only 30 lines of code. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. #include #include #include #include <avr/io.h> <util/delay.h> <stdlib.h> "hd44780.h"

int main (void) { lcd_init(); lcd_clrscr(); lcd_puts("Hello World"); lcd_goto(40); 15. char digit[1]; 16. for (int i=0;i<16;i++) 17. { 18. _delay_ms(250); 19. itoa(i,digit,16); 20. lcd_puts(digit); 21. } 22. 23. _delay_ms(1000); 24. lcd_clrscr(); 25. 26. lcd_puts("Goodbye"); 27. _delay_ms(2000); 28. lcd_clrscr(); 29. 30. return(0); 31. } //Position 40 is the start of line 2

Вам также может понравиться