Advertisement
Guest User

Untitled

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