Slightom

SW-PS4-zad2-main

Mar 18th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.76 KB | None | 0 0
  1. #include "stm32f10x.h"
  2. #define SysTick_Frequency 9000000
  3.  
  4.  
  5. void GPIO_Config(void);
  6. void RCC_Config(void);
  7. uint16_t intTo7seg(uint8_t);                // konwersja na kod wyswietlacza 7-segmentowego
  8. void Delay (uint32_t ms);                   // powoduje wywolywanie przerwania z czestotliwoscia okreslona w parametrze
  9. void step();                                // wykonuje sie w kazdym przerwaniu
  10. void displayDigitOnNextDisplay(uint8_t x);  // wyswietla cyfre na jednym wyswietlaczu, pozostale gasi
  11.  
  12.  
  13. uint8_t  displayNumber = 0;           // numer wyswietlacza ktory ma byc aktualnie zapalony 0..3
  14. uint32_t interruptCounter = 0;        // zlicza przerwania 0..changeDigitTime/interruptFrequency
  15. uint8_t  digitNumber = 0;             // numer wyswietlanej cyfry 0..15
  16. uint32_t changeDigitTime = 1000;      // co ile ms ma zmienic sie wyswietlana cyfra
  17. uint32_t interruptFrequency = 5;      // co ile ms ma sie wywolac przerwanie
  18.  
  19.  
  20.  
  21. int main(void)
  22. {  
  23.     //konfiguracja systemu
  24.     RCC_Config();  
  25.     GPIO_Config();
  26.  
  27.     Delay(interruptFrequency);
  28.    
  29.     while (1)
  30.     {
  31.     };
  32.        
  33. }
  34.  
  35.  
  36.  
  37.    
  38.    
  39. void step()
  40. {
  41.     displayDigitOnNextDisplay(displayNumber++);
  42.    
  43.     if(displayNumber == 4) { displayNumber=0; };
  44.     if(interruptCounter++ == changeDigitTime/interruptFrequency)
  45.     {
  46.         interruptCounter = 0;
  47.         if(digitNumber++ == 15) { digitNumber=0; };
  48.     }
  49. }
  50.  
  51.  
  52.  
  53. void displayDigitOnNextDisplay(uint8_t x)
  54. {
  55.     uint16_t d1, d2, d3; // trzy wyswietlacze które nalezy zgasic
  56.     uint16_t d0;         // wyswietlacz, ktory nalezy zapalic
  57.    
  58.     GPIO_Write(GPIOB, intTo7seg(digitNumber)); // zapal segmenty wyswietlacza
  59.    
  60.     switch(x)
  61.     {
  62.         case 0: d0 = GPIO_Pin_11;
  63.                 d1 = GPIO_Pin_8;
  64.                 d2 = GPIO_Pin_9;
  65.                 d3 = GPIO_Pin_10;
  66.                 break;
  67.        
  68.         case 1: d0 = GPIO_Pin_10;
  69.                 d1 = GPIO_Pin_8;
  70.                 d2 = GPIO_Pin_9;
  71.                 d3 = GPIO_Pin_11;
  72.                 break;
  73.        
  74.         case 2: d0 = GPIO_Pin_9;
  75.                 d1 = GPIO_Pin_8;
  76.                 d2 = GPIO_Pin_10;
  77.                 d3 = GPIO_Pin_11;
  78.                 break;
  79.        
  80.         case 3: d0 = GPIO_Pin_8;
  81.                 d1 = GPIO_Pin_9;
  82.                 d2 = GPIO_Pin_10;
  83.                 d3 = GPIO_Pin_11;
  84.                 break;
  85.     }
  86.    
  87.     GPIO_ResetBits(GPIOB, d0); // wlacz wyswietlacz DS3
  88.     GPIO_SetBits(GPIOB,  d1 |  d2 | d3); // wylacz pozostale
  89. }
  90.  
  91.  
  92. // konwersja na kod wyswietlacza 7-segmentowego
  93. uint16_t intTo7seg(uint8_t cyfra)
  94. {
  95.     uint16_t result;
  96.     switch (cyfra)
  97.     {
  98.         case 0:  result=0xC0; break; // 11000000 0
  99.         case 1:  result=0xF9; break; // 11111001 1
  100.         case 2:  result=0xA4; break; // 10100100 2
  101.         case 3:  result=0xB0; break; // 10110000 3
  102.         case 4:  result=0x99; break; // 10011011 4
  103.         case 5:  result=0x92; break; // 10010010 5
  104.         case 6:  result=0x82; break; // 10000010 6
  105.         case 7:  result=0xF8; break; // 11111000 7
  106.         case 8:  result=0x80; break; // 10000000 8
  107.         case 9:  result=0x90; break; // 10010000 9
  108.         case 10: result=0x88; break; // 10001000 a
  109.         case 11: result=0x83; break; // 10000011 b
  110.         case 12: result=0xC6; break; // 11000110 c
  111.         case 13: result=0xA1; break; // 10100001 d
  112.         case 14: result=0x86; break; // 10000110 e
  113.         case 15: result=0x8E; break; // 10001110 f
  114.         default: result=0xFF; break; // 11111111 - nic do wyswietlenia
  115.     }
  116.    
  117.     return result;     
  118. }
  119.    
  120.  
  121.  
  122. void Delay (uint32_t ms) // nasza funkcja opóznienia wykorzystujaca timer SysTick
  123. {
  124.     if (SysTick_Config(SysTick_Frequency / 1000 * ms))
  125.     {
  126.         while(1);
  127.     }
  128.     SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);
  129. }
  130.  
  131.  
  132.    
  133. void RCC_Config(void)
  134. //konfigurowanie sygnalow taktujacych
  135. {
  136.   ErrorStatus HSEStartUpStatus;                          //zmienna opisujaca rezultat uruchomienia HSE
  137.  
  138.   RCC_DeInit();                                          //Reset ustawien RCC
  139.   RCC_HSEConfig(RCC_HSE_ON);                             //Wlaczenie HSE
  140.   HSEStartUpStatus = RCC_WaitForHSEStartUp();            //Odczekaj az HSE bedzie gotowy
  141.   if(HSEStartUpStatus == SUCCESS)
  142.   {
  143.     FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);//
  144.     FLASH_SetLatency(FLASH_Latency_2);                   //ustaw zwloke dla pamieci Flash; zaleznie od taktowania rdzenia
  145.                                                          //0:<24MHz; 1:24~48MHz; 2:>48MHz
  146.     RCC_HCLKConfig(RCC_SYSCLK_Div1);                     //ustaw HCLK=SYSCLK
  147.     RCC_PCLK2Config(RCC_HCLK_Div1);                      //ustaw PCLK2=HCLK
  148.     RCC_PCLK1Config(RCC_HCLK_Div2);                      //ustaw PCLK1=HCLK/2
  149.     RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); //ustaw PLLCLK = HSE*9 czyli 8MHz * 9 = 72 MHz
  150.     RCC_PLLCmd(ENABLE);                                  //wlacz PLL
  151.     while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);  //odczekaj na poprawne uruchomienie PLL
  152.     RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);           //ustaw PLL jako zrodlo sygnalu zegarowego
  153.     while(RCC_GetSYSCLKSource() != 0x08);                //odczekaj az PLL bedzie sygnalem zegarowym systemu
  154.  
  155.   /*Tu nalezy umiescic kod zwiazany z konfiguracja sygnalow zegarowych potrzebnych w programie peryferiow*/
  156.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);//wlacz taktowanie portu GPIO A
  157.    
  158.   } else {
  159.   }
  160. }
  161.  
  162.  
  163.  
  164. void GPIO_Config(void)
  165. {
  166.   //konfigurowanie portow GPIO
  167.   GPIO_InitTypeDef  GPIO_InitStructure;
  168.  
  169.     // disable JTAG
  170.     GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
  171.  
  172.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;;
  173.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  174.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;      //wyjscie push-pull
  175.   GPIO_Init(GPIOB, &GPIO_InitStructure);
  176.    
  177.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 ;
  178.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  179.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;      //wyjscie open drain
  180.   GPIO_Init(GPIOB, &GPIO_InitStructure);
  181.  
  182. }
Add Comment
Please, Sign In to add comment