Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. //******************************************************************************
  2. // THE SOFTWARE INCLUDED IN THIS FILE IS FOR GUIDANCE ONLY.
  3. // AUTHOR SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT
  4. // OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  5. // FROM USE OF THIS SOFTWARE.
  6. //
  7. // PROGRAM ZAWARTY W TYM PLIKU PRZEZNACZONY JEST WYLACZNIE
  8. // DO CELOW SZKOLENIOWYCH. AUTOR NIE PONOSI ODPOWIEDZIALNOSCI
  9. // ZA ZADNE EWENTUALNE, BEZPOSREDNIE I POSREDNIE SZKODY
  10. // WYNIKLE Z JEGO WYKORZYSTANIA.
  11. //******************************************************************************
  12. #include "lcd.h"
  13. #include "stm32f10x_gpio.h"
  14.  
  15. GPIO_InitTypeDef GPIO_InitStructure;
  16. void wait(int us);
  17.  
  18. // wyslij pólbajt na linie danych wyswietlacza
  19. void LCD_WriteNibble(unsigned char nibble)
  20. {
  21. volatile unsigned int delayCnt = 0;
  22.  
  23. GPIO_WriteBit(LCD_GPIO, LCD_D4, (nibble & 0x01)); // ustaw bity na liniach
  24. GPIO_WriteBit(LCD_GPIO, LCD_D5, (nibble & 0x02)); // D4 – D7
  25. GPIO_WriteBit(LCD_GPIO, LCD_D6, (nibble & 0x04));
  26. GPIO_WriteBit(LCD_GPIO, LCD_D7, (nibble & 0x08));
  27.  
  28. GPIO_WriteBit(LCD_GPIO, LCD_E, Bit_SET); // ustaw wysoki poziom E
  29.  
  30. for(delayCnt = 0; delayCnt < 16; delayCnt++); // poczekaj troche…
  31.  
  32. GPIO_WriteBit(LCD_GPIO, LCD_E, Bit_RESET); // ustaw niski poziom E
  33. }
  34.  
  35. // wyslij komende do wyswietlacza
  36. void LCD_WriteCommand(unsigned char commandToWrite)
  37. {
  38. volatile unsigned int delayCnt = 0;
  39.  
  40. GPIO_WriteBit(LCD_GPIO, LCD_RS, Bit_RESET); // RS = 0 - komenda
  41. LCD_WriteNibble(commandToWrite >> 4); // wyslij starszy pólbajt
  42. LCD_WriteNibble(commandToWrite & 0x0F); // wyslij mlodszy pólbajt
  43.  
  44. if (commandToWrite > 3) // w zaleznosci od komendy dobierz opóznienie
  45. for(delayCnt = 0; delayCnt < 3000; delayCnt++);
  46. else
  47. for(delayCnt = 0; delayCnt < 150000; delayCnt++);
  48. }
  49.  
  50.  
  51.  
  52. // wyslij znak do wyswietlenia
  53. void LCD_WriteData(unsigned char dataToWrite)
  54. {
  55.  
  56. // uzupelnij samodzielnie
  57. GPIO_WriteBit(LCD_GPIO, LCD_RS, 1);
  58. GPIO_WriteBit(LCD_GPIO, LCD_D4, dataToWrite & 0x10);
  59. GPIO_WriteBit(LCD_GPIO, LCD_D5, dataToWrite & 0x20);
  60. GPIO_WriteBit(LCD_GPIO, LCD_D6, dataToWrite & 0x40);
  61. GPIO_WriteBit(LCD_GPIO, LCD_D7, dataToWrite & 0x80);
  62. GPIO_WriteBit(LCD_GPIO, LCD_E, Bit_SET); // ustaw wysoki poziom E
  63. wait(300);
  64. GPIO_WriteBit(LCD_GPIO, LCD_E, Bit_RESET); // ustaw niski poziom E
  65. GPIO_WriteBit(LCD_GPIO, LCD_D4, dataToWrite & 0x01);
  66. GPIO_WriteBit(LCD_GPIO, LCD_D5, dataToWrite & 0x02);
  67. GPIO_WriteBit(LCD_GPIO, LCD_D6, dataToWrite & 0x04);
  68. GPIO_WriteBit(LCD_GPIO, LCD_D7, dataToWrite & 0x08);
  69. GPIO_WriteBit(LCD_GPIO, LCD_E, Bit_SET); // ustaw wysoki poziom E
  70. wait(300);
  71. GPIO_WriteBit(LCD_GPIO, LCD_E, Bit_RESET); // ustaw niski poziom E
  72. GPIO_WriteBit(LCD_GPIO, LCD_RS, 0);
  73. wait(300);
  74. }
  75.  
  76.  
  77. // inicjalizacja wyswietlacza
  78. void LCD_Initialize(void)
  79. {
  80. GPIO_InitStructure.GPIO_Pin = LCD_D4|LCD_D5|LCD_D6|LCD_D7|LCD_RS|LCD_E;
  81. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  82. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  83. GPIO_Init(LCD_GPIO, &GPIO_InitStructure);
  84. // uzupelnij samodzielnie
  85. wait(150000);
  86. LCD_WriteNibble(0x03);
  87. wait(3000000);
  88. LCD_WriteNibble(0x03);
  89. wait(6000);
  90. LCD_WriteNibble(0x03);
  91. wait(6000);
  92. LCD_WriteNibble(0x02);
  93. wait(6000);
  94. LCD_WriteCommand(LCD_FUNCTION_SET | LCD_FONT8 | LCD_TWO_LINE);
  95. wait(6000);
  96. LCD_WriteCommand(LCD_DISPLAY_ONOFF | LCD_DISPLAY_OFF);
  97. wait(6000);
  98. LCD_WriteCommand(LCD_CLEAR);
  99. wait(600000);
  100. LCD_WriteCommand(LCD_ENTRY_MODE | LCD_EM_INCREMENT | LCD_EM_SHIFT_CURSOR);
  101. wait(6000);
  102. LCD_WriteCommand(LCD_DISPLAY_ONOFF | LCD_DISPLAY_ON);
  103. }
  104. void wait(int us) {
  105. unsigned int i = 0;
  106. for (; i< us; i++);
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement