Advertisement
Slightom

SW_PS2_zad1

Mar 4th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.41 KB | None | 0 0
  1. #include "stm32f10x.h"
  2. #include <math.h>
  3.  
  4. void GPIO_Config(void);
  5. void RCC_Config(void);
  6.  
  7. void setAllLigts(int tab[4]);
  8.  
  9. int main(void)
  10. {
  11.   volatile unsigned long int i;
  12.     int counter = 0;
  13.     int x;
  14.     int tab[4];
  15.  
  16.   //konfiguracja systemu
  17.   RCC_Config();  
  18.   GPIO_Config();
  19.   /*Tu nalezy umiescic ewentualne dalsze funkcje konfigurujace system*/
  20.   GPIO_ResetBits(GPIOB, GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7);      
  21.  
  22.  
  23.   while (1)
  24.     {        
  25.         for(i = 0; i < 0x40000ul; i++);  
  26.                
  27.         for(i=1; i<=4; i++)
  28.         {
  29.             x = (int)pow(2.0,(double)i);              // 2,  4,  8,  16
  30.             tab[i-1] = ((counter%x)<(x/2)) ? 0 : 1;   // <1, <2, <4, <8
  31.         }
  32.         setAllLigts(tab);
  33.        
  34.         if((++counter) == 16) { counter = 0; }
  35.   };
  36.    
  37.    
  38.   return 0;
  39. }
  40.  
  41. void setAllLigts(int tab[4])
  42. {
  43.     GPIO_WriteBit(GPIOB, GPIO_Pin_0, tab[0]==1 ? Bit_SET : Bit_RESET); 
  44.     GPIO_WriteBit(GPIOB, GPIO_Pin_1, tab[1]==1 ? Bit_SET : Bit_RESET); 
  45.     GPIO_WriteBit(GPIOB, GPIO_Pin_2, tab[2]==1 ? Bit_SET : Bit_RESET); 
  46.     GPIO_WriteBit(GPIOB, GPIO_Pin_3, tab[3]==1 ? Bit_SET : Bit_RESET); 
  47. }
  48.  
  49.  
  50. //konfigurowanie sygnalow taktujacych
  51. void RCC_Config(void)
  52. {
  53.   ErrorStatus HSEStartUpStatus;                          //zmienna opisujaca rezultat uruchomienia HSE
  54.    
  55.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
  56.    
  57.   RCC_DeInit();                                          //Reset ustawien RCC
  58.   RCC_HSEConfig(RCC_HSE_ON);                             //Wlaczenie HSE
  59.   HSEStartUpStatus = RCC_WaitForHSEStartUp();            //Odczekaj az HSE bedzie gotowy
  60.   if(HSEStartUpStatus == SUCCESS)
  61.   {
  62.     FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);//
  63.     FLASH_SetLatency(FLASH_Latency_2);                   //ustaw zwloke dla pamieci Flash; zaleznie od taktowania rdzenia
  64.                                                          //0:<24MHz; 1:24~48MHz; 2:>48MHz
  65.     RCC_HCLKConfig(RCC_SYSCLK_Div1);                     //ustaw HCLK=SYSCLK
  66.     RCC_PCLK2Config(RCC_HCLK_Div1);                      //ustaw PCLK2=HCLK
  67.     RCC_PCLK1Config(RCC_HCLK_Div2);                      //ustaw PCLK1=HCLK/2
  68.     RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); //ustaw PLLCLK = HSE*9 czyli 8MHz * 9 = 72 MHz
  69.     RCC_PLLCmd(ENABLE);                                  //wlacz PLL
  70.     while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);  //odczekaj na poprawne uruchomienie PLL
  71.     RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);           //ustaw PLL jako zrodlo sygnalu zegarowego
  72.     while(RCC_GetSYSCLKSource() != 0x08);                //odczekaj az PLL bedzie sygnalem zegarowym systemu
  73.  
  74.   /*Tu nalezy umiescic kod zwiazany z konfiguracja sygnalow zegarowych potrzebnych w programie peryferiow*/
  75.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);//wlacz taktowanie portu GPIO B
  76.    
  77.   } else
  78.     {
  79.   }
  80. }
  81.  
  82.  
  83. void GPIO_Config(void)
  84. {
  85.   //konfigurowanie portow GPIO
  86.   GPIO_InitTypeDef  GPIO_InitStructure;
  87.    
  88.     GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
  89.    
  90.   /*Tu nalezy umiescic kod zwiazany z konfiguracja poszczegolnych portow GPIO potrzebnych w programie*/
  91.   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;
  92.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  93.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;      //wyjscie push-pull
  94.   GPIO_Init(GPIOB, &GPIO_InitStructure);
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement