Word To PDF Converter - Unregistered
Word To PDF Converter - Unregistered
Word To PDF Converter - Unregistered
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
//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.
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