Advertisement
Guest User

Untitled

a guest
Mar 13th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | None | 0 0
  1. //4-bit LCD init code Pieter Thomas & Patrick Van Torre 2018
  2.  
  3. #include <c8051f340.h>
  4.  
  5. #define LCD P4 //P4 and higher ports cannot be bit addressed!
  6. #define RS 2 //Px^1
  7. #define RW 1 //Px^0
  8. #define EN 8 //Px^3
  9.  
  10.  
  11. //-----------------------------------------------------------------------------
  12. // Function PROTOTYPES
  13. //-----------------------------------------------------------------------------
  14. void Init_Device(void);
  15. void LCD_Init(void);
  16. void LCD_Delay(int time);
  17. void LCD_Clock(char lcddata);
  18. void LCD_Busy(void);
  19. void LCD_Send(char teken);
  20.  
  21. char buffer[17];
  22.  
  23. void main(void)
  24. {
  25. unsigned char n=1;
  26. unsigned long teller=0;
  27.  
  28.  
  29. Init_Device();
  30.  
  31. LCD_Init();
  32.  
  33. LCD_Send('U');
  34. LCD_Send('G');
  35. LCD_Send('e');
  36. LCD_Send('n');
  37. LCD_Send('t');
  38.  
  39. //Opdracht 1
  40. /*
  41. LCD_printString("De Dies Natalis stond dit jaar volledig in het teken van de 200e verjaardag van de UGent en was het ankerevent in het jubileumjaar, waarin maatschappelijk engagement centraal staat.");
  42.  
  43. LCD_Clear();
  44. LCD_Delay(255);
  45.  
  46. LCD_printString("Na de zitting vond een feestelijk concert plaats in de Opera Gent, met onder meer de universitaire hymne.");
  47. */
  48.  
  49.  
  50. while(1);
  51.  
  52. }
  53.  
  54.  
  55.  
  56. //-----------------------------------------------------------------------------
  57. // LCD_Init Robust version written by P. Van Torre DEC 2014
  58. //-----------------------------------------------------------------------------
  59. //
  60. void LCD_Init (void)
  61. {
  62. //wait a while after power on
  63. LCD_Delay(2550);
  64.  
  65. //go to 8-bit mode no matter what (8 -> 8 / 4 -> 8)
  66. LCD_Clock(0x30);
  67. LCD_Delay(2550);
  68. LCD_Clock(0x30);
  69. LCD_Delay(250);
  70. LCD_Clock(0x30);
  71. LCD_Delay(250);
  72.  
  73.  
  74. //now go to 4-bit mode
  75. LCD_Clock(0x20);
  76. LCD_Delay(250);
  77.  
  78. //interface definitely in 4-bit mode now, independent of history
  79.  
  80. //4-bit 2-line
  81. LCD_Clock(0x20);
  82. LCD_Clock(0x80);
  83. LCD_Delay(250);
  84.  
  85. //no shift ; cursor right
  86. LCD_Clock(0x10);
  87. LCD_Clock(0x40);
  88. LCD_Delay(250);
  89.  
  90. //display on ; cursor off ; no blinking
  91. LCD_Clock(0x00);
  92. LCD_Clock(0xC0);
  93. LCD_Delay(250);
  94.  
  95. //cursor moves right ; no display shift
  96. LCD_Clock(0x00);
  97. LCD_Clock(0x60);
  98. LCD_Delay(250);
  99.  
  100. LCD_uitschrijvenGetal(8653);
  101. }
  102.  
  103. //-----------------------------------------------------------------------------
  104. // LCD_Delay
  105. //-----------------------------------------------------------------------------
  106. //
  107. void LCD_Delay (int time)
  108. {
  109. int n,m;
  110. for (n=0;n<time;n++)
  111. {
  112. for (m=0;m<255;m++);
  113. }
  114. }
  115.  
  116. //-----------------------------------------------------------------------------
  117. // LCD_Clock
  118. //-----------------------------------------------------------------------------
  119. //
  120. void LCD_Clock (char lcddata)
  121. {
  122. LCD = lcddata;
  123. LCD &= ~EN;
  124. LCD_Delay(10);
  125. LCD |= EN;
  126. LCD_Delay(10);
  127. LCD &= ~EN;
  128. LCD_Delay(10);
  129. }
  130.  
  131.  
  132.  
  133. //-----------------------------------------------------------------------------
  134. // LCD_Busy PVT2014
  135. //-----------------------------------------------------------------------------
  136. //
  137. void LCD_Busy (void)
  138. {
  139. bit busy=1;
  140. unsigned char timeout=0;
  141.  
  142. while(busy&++timeout)
  143. {
  144. LCD=0xF1; //check busy flag command
  145. LCD_Delay(1);
  146. LCD |= EN;
  147. LCD_Delay(1);
  148. busy=((LCD&0x80)==0x80);
  149. LCD &= ~EN;
  150. LCD_Delay(1);
  151.  
  152. // 2 cycles in 4-bit mode
  153. LCD |= EN;
  154. LCD_Delay(1);
  155. LCD &= ~EN;
  156. LCD_Delay(1);
  157. }
  158. }
  159.  
  160.  
  161. //-----------------------------------------------------------------------------
  162. // LCD_Send
  163. //-----------------------------------------------------------------------------
  164. //
  165. void LCD_Send (char teken)
  166. {
  167. LCD_Clock(teken & 0xf0 | RS);
  168. LCD_Clock(((teken & 0x0f)<<4) | RS);
  169. LCD_Busy(); //wait until LCD ready
  170. }
  171. /*
  172. void binairNaarDecimaal(unsigned long input){
  173. int decimaalGetal = 0, i = 0, rest;
  174. while(input != 0) {
  175. rest = input%10;
  176. input /= 10;
  177. decimaalGetal += rest * pow(2,i);
  178. ++i;
  179. }
  180. return decimaalGetal;
  181. }
  182. */
  183. void LCD_uitschrijvenGetal(unsigned long input){
  184. int teller = 0;
  185. int getalInArray[16]; //Getal kan maximum 16 tekens bevatten
  186. while(input > 0){
  187. getalInArray[teller] = (input%10);
  188. input = input / 10;
  189. teller++;
  190. }
  191. int i = 0;
  192. for(i = teller-1; i >= 0 ; i--){
  193. LCD_Send(getalInArray[teller]);
  194. }
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement