lcddisp.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include "derivative.h"
  2. #include "funct.h"
  3. int data = 0; //used to determine if a command or data is being sent, sets RS
  4. #define LCDDATA PTADHi
  5. #define LCDDATADDR DDRADHi
  6. #define LCDCONTROLDDR DDRM
  7. #define ECLK PTM_PTM4
  8. #define RW PTM_PTM5
  9. #define RS PTM_PTM6
  10. #define LINE1 0x80
  11. #define LINE2 0xc0
  12. void delay_ms(int delay) //Not a precise delay
  13. {
  14. int x;
  15. while (delay > 0)
  16. {
  17. for (x = 700; x > 0; x--);
  18. delay--;
  19. }
  20. }
  21. void init_LCD()
  22. {
  23. LCDDATADDR = 0xFF; //Set PORT AD 8-15 as outputs,
  24. LCDDATA = 0x00; // Clear Port AD 8-15 I/O register
  25. LCDCONTROLDDR = LCDCONTROLDDR | 0x70; //Set bits 4, 5, and 6 of Port M as outputs (ECLK, RW, RS respectively)
  26. delay_ms(5);
  27. ECLK = 1;
  28. delay_ms(50); //Small Start Delay
  29. //LCD Initialization
  30. data = 0;
  31. LCDDATA = 0x03;
  32. epulse();
  33. delay_ms(5);
  34. epulse();
  35. delay_ms(5);
  36. epulse();
  37. delay_ms(5);
  38. //Setup LCD Screen
  39. sendCommand(0x3C); //Set Interface Length, 8-data lines, 2 display lines
  40. sendCommand(0x10); //Turn off Display
  41. sendCommand(0x01); //Clear Display
  42. sendCommand(0x06); //Set Cursor Move direction, Auto-Increment cursor after each byte written, dont shift display
  43. sendCommand(0x0c); //Display on, cursor off
  44. }
  45. void display_string(char* SendStr)
  46. {
  47. unsigned char count=0;
  48. //return home
  49. sendCommand(LINE1); //Start from the beginning of the LCD (Line 1)
  50. while(*SendStr!=0)
  51. {
  52. if(count == 16)
  53. {
  54. sendCommand(LINE2); //Switch to Line 2
  55. }
  56. ++count;
  57. data = 1; //Send Character
  58. LCDDATA = *SendStr;
  59. epulse();
  60. ++SendStr;
  61. }
  62. }
  63. void Delay(unsigned int DelayCount)
  64. {
  65. unsigned int i, j;
  66. for(i = 0; i < DelayCount; i++)
  67. for(j = 0; j < 2000; j++)
  68. ;
  69. }
  70. void epulse(void) //Pulse E Clock
  71. {
  72. if (data) RS = 1;
  73. else RS = 0;
  74. delay_ms(1);
  75. ECLK = 0;
  76. delay_ms(1);
  77. ECLK = 1;
  78. }
  79. void sendCommand( int byte )//Send a byte command to LCD
  80. {
  81. data = 0; //Send Command
  82. LCDDATA = byte;
  83. epulse();
  84. }