Advertisement
dargenn

Untitled

Mar 22nd, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.78 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.  
  13.  
  14. #include "stm32f10x.h"
  15. #include "lcd.h"
  16.  
  17. void GPIO_Config(void);
  18. void RCC_Config(void);
  19. void LCD_Initialize(int);
  20. void LCD_WriteData(unsigned char);
  21. GPIO_InitTypeDef GPIO_InitStructure;
  22. void LCD_WriteNibble(unsigned char);
  23.  
  24. int main(void)
  25. //konfiguracja systemu
  26. {
  27.  
  28. unsigned char tekst[18] = {"Monika Pietyszuk!"};
  29. unsigned char tekst2[6] = {"Monika"};
  30. int i = 0;
  31. int j = 0;
  32. int k = 0;
  33. int liczba = 0x08;
  34.  
  35. RCC_Config();
  36. GPIO_Config();
  37.  
  38. for(j=0;j<8;j++) {
  39. LCD_Initialize(liczba); // Inicjalizuj LCD
  40. LCD_WriteCommand(LCD_CLEAR); // Wyczysc LCD
  41.  
  42. for(k=0;k<320000 * 6;k++);
  43. liczba += 0x01;
  44. }
  45. while (1);
  46. return 0;
  47. }
  48.  
  49.  
  50. void RCC_Config(void)
  51. //konfigurowanie sygnalow taktujacych
  52. {
  53. ErrorStatus HSEStartUpStatus; //zmienna opisujaca rezultat uruchomienia HSE
  54.  
  55. RCC_DeInit(); //Reset ustawien RCC
  56. RCC_HSEConfig(RCC_HSE_ON); //Wlaczenie HSE
  57. HSEStartUpStatus = RCC_WaitForHSEStartUp(); //Odczekaj az HSE bedzie gotowy
  58. if(HSEStartUpStatus == SUCCESS)
  59. {
  60. FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);//
  61. FLASH_SetLatency(FLASH_Latency_2); //ustaw zwloke dla pamieci Flash; zaleznie od taktowania rdzenia
  62. //0:<24MHz; 1:24~48MHz; 2:>48MHz
  63. RCC_HCLKConfig(RCC_SYSCLK_Div1); //ustaw HCLK=SYSCLK
  64. RCC_PCLK2Config(RCC_HCLK_Div1); //ustaw PCLK2=HCLK
  65. RCC_PCLK1Config(RCC_HCLK_Div2); //ustaw PCLK1=HCLK/2
  66. RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); //ustaw PLLCLK = HSE*9 czyli 8MHz * 9 = 72 MHz
  67. //inne przykladowe konfiguracje:
  68. //RCC_PLLConfig(RCC_PLLSource_HSI_Div2, RCC_PLLMul_9); //ustaw PLLCLK = HSI/2*9 czyli 8MHz / 2 * 9 = 36 MHz
  69. //RCC_PLLConfig(RCC_PLLSource_HSE_Div2, RCC_PLLMul_9); //ustaw PLLCLK = HSE/2*9 czyli 8MHz / 2 * 9 = 36 MHz
  70. RCC_PLLCmd(ENABLE); //wlacz PLL
  71. while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); //odczekaj na poprawne uruchomienie PLL
  72. RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //ustaw PLL jako zrodlo sygnalu zegarowego
  73. while(RCC_GetSYSCLKSource() != 0x08); //odczekaj az PLL bedzie sygnalem zegarowym systemu
  74.  
  75. /*Tu nalezy umiescic kod zwiazany z konfiguracja sygnalow zegarowych potrzebnych w programie peryferiow*/
  76. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);//wlacz taktowanie portu GPIO B
  77.  
  78. } else {
  79. }
  80. }
  81.  
  82.  
  83.  
  84. void GPIO_Config(void)
  85. {
  86. //konfigurowanie portow GPIO
  87. GPIO_InitTypeDef GPIO_InitStructure;
  88.  
  89. // disable JTAG
  90. GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
  91.  
  92. /*Tu nalezy umiescic kod zwiazany z konfiguracja poszczegolnych portow GPIO potrzebnych w programie*/
  93. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 ;
  94. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  95. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  96. GPIO_Init(GPIOA, &GPIO_InitStructure);
  97. }
  98.  
  99. void LCD_Initialize(int liczba) {
  100. unsigned int i = 0;
  101. GPIO_InitStructure.GPIO_Pin = LCD_D0|LCD_D1|LCD_D2|LCD_D3|LCD_D4|LCD_D5|LCD_D6|LCD_D7|LCD_RS|LCD_E;
  102. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  103. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  104. GPIO_Init(LCD_GPIO, &GPIO_InitStructure);
  105. for(; i < 150000; i++);
  106. LCD_WriteNibble(0x03);
  107. for(i = 0; i < 3000000; i++);
  108. LCD_WriteNibble(0x03);
  109. for(i = 0; i < 6000; i++);
  110. LCD_WriteNibble(0x03);
  111. for(i = 0; i < 6000; i++);
  112. LCD_WriteNibble(0x02);
  113. for(i = 0; i < 6000; i++);
  114. LCD_WriteCommand(LCD_FUNCTION_SET | LCD_FONT8 | LCD_TWO_LINE);
  115. for(i = 0; i < 6000; i++);
  116. LCD_WriteCommand(liczba | LCD_DISPLAY_OFF);
  117. for(i = 0; i < 6000; i++);
  118. LCD_WriteCommand(LCD_CLEAR);
  119. for(i = 0; i < 600000; i++);
  120. LCD_WriteCommand(LCD_ENTRY_MODE
  121. | LCD_EM_INCREMENT | LCD_EM_SHIFT_CURSOR);
  122. for(i = 0; i < 6000; i++);
  123. LCD_WriteCommand(liczba | LCD_DISPLAY_ON);
  124. }
  125.  
  126. void LCD_WriteData(unsigned char dataToWrite)
  127. {
  128. unsigned int delayCnt = 0;
  129. GPIO_WriteBit(LCD_GPIO, LCD_RS, 1);
  130. GPIO_WriteBit(LCD_GPIO, LCD_D4, dataToWrite & 0x10);
  131. GPIO_WriteBit(LCD_GPIO, LCD_D5, dataToWrite & 0x20);
  132. GPIO_WriteBit(LCD_GPIO, LCD_D6, dataToWrite & 0x40);
  133. GPIO_WriteBit(LCD_GPIO, LCD_D7, dataToWrite & 0x80);
  134. GPIO_WriteBit(LCD_GPIO, LCD_E, Bit_SET);
  135. // ustaw wysoki poziom E
  136. for(delayCnt = 0; delayCnt < 300; delayCnt++);
  137. // poczekaj troche?
  138. GPIO_WriteBit(LCD_GPIO, LCD_E, Bit_RESET);
  139. // ustaw niski poziom E
  140. GPIO_WriteBit(LCD_GPIO, LCD_D4, dataToWrite & 0x01);
  141. GPIO_WriteBit(LCD_GPIO, LCD_D5, dataToWrite & 0x02);
  142. GPIO_WriteBit(LCD_GPIO, LCD_D6, dataToWrite & 0x04);
  143. GPIO_WriteBit(LCD_GPIO, LCD_D7, dataToWrite & 0x08);
  144. GPIO_WriteBit(LCD_GPIO, LCD_E, Bit_SET);
  145. // ustaw wysoki poziom E
  146. for(delayCnt = 0; delayCnt < 300; delayCnt++);
  147. // poczekaj troche?
  148. GPIO_WriteBit(LCD_GPIO, LCD_E, Bit_RESET);
  149. // ustaw niski poziom E
  150. GPIO_WriteBit(LCD_GPIO, LCD_RS, 0);
  151. }
  152.  
  153. void LCD_WriteCommand(unsigned char commandToWrite)
  154. {
  155. volatile unsigned int delayCnt = 0;
  156. GPIO_WriteBit(LCD_GPIO, LCD_RS, Bit_RESET); // RS = 0 - komenda
  157. LCD_WriteNibble(commandToWrite >> 4); // wyslij starszy p?lbajt
  158. LCD_WriteNibble(commandToWrite & 0x0F); // wyslij mlodszy p?lbajt
  159. if (commandToWrite > 3)
  160. // w zaleznosci od komendy dobierz opóznienie
  161. for(delayCnt = 0; delayCnt < 3000; delayCnt++);
  162. else
  163. for(delayCnt = 0; delayCnt < 150000; delayCnt++);
  164. }
  165.  
  166. void LCD_WriteNibble(unsigned char nibble)
  167. {
  168. volatile unsigned int delayCnt = 0;
  169. GPIO_WriteBit(LCD_GPIO, LCD_D4, (nibble & 0x01)); // ustaw bity na liniach
  170. GPIO_WriteBit(LCD_GPIO, LCD_D5, (nibble & 0x02)); // D4 – D7
  171. GPIO_WriteBit(LCD_GPIO, LCD_D6, (nibble & 0x04));
  172. GPIO_WriteBit(LCD_GPIO, LCD_D7, (nibble & 0x08));
  173. GPIO_WriteBit(LCD_GPIO, LCD_E, Bit_SET); // ustaw wysoki poziom E
  174. for(delayCnt = 0; delayCnt < 16; delayCnt++); // poczekaj troche…
  175.  
  176. GPIO_WriteBit(LCD_GPIO, LCD_E, Bit_RESET); // ustaw niski poziom E
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement