Advertisement
Slightom

SW_PS2_zad2

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