Advertisement
Slightom

SW_PS2_zad3

Mar 4th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.15 KB | None | 0 0
  1. #include "stm32f10x.h"
  2.  
  3. typedef int bool;
  4. #define true 1
  5. #define false 0
  6.  
  7. void GPIO_Config(void);
  8. void RCC_Config(void);
  9.  
  10. void wait(unsigned long int* p, uint8_t* port_data, uint8_t* temp, uint8_t* button_state, int* direction, int tab[], int* lewyKoniec, int* dl, bool* permisison);
  11. void setAllLigts(int tab[8]);
  12. void setTable(int tab[8], int d, int dl, int* lewyKoniec);
  13.  
  14.  
  15. int main(void)
  16. {
  17.   volatile unsigned long int i;
  18.     int direction=1;       // 0-w lewo, 1-w prawo
  19.     volatile unsigned long int p = 0x40000ul;
  20.     uint8_t button_state=0xFF, temp=0, port_data ;
  21.     int lewyKoniec=3; // lewy poczatek pociagu
  22.     int dl=4;   // dlugosc pociagu
  23.     bool permission=true;
  24.    
  25.     int tab [8];
  26.     for(i=0; i<8; i++)
  27.     {
  28.         tab[i] = 0;
  29.     }
  30.     tab[3]=1;
  31.     tab[2]=1;
  32.     tab[1]=1;
  33.     tab[0]=1;
  34.    
  35.   //konfiguracja systemu
  36.   RCC_Config();  
  37.   GPIO_Config();
  38.   GPIO_ResetBits(GPIOA, GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7 );         
  39.   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);      
  40.  
  41.        
  42.     while (1)
  43.     {
  44.         port_data = GPIO_ReadInputData(GPIOA); //czytaj port GPIOA
  45.         temp = port_data ^ button_state; // czy stan przycisków sie zmienil?
  46.         temp &= button_state; // czy to byla zmiana z 1 na 0?
  47.         button_state = port_data; // zapamietaj nowy stan  
  48.                
  49.         permission=true;       
  50.         setTable(tab, direction, dl, &lewyKoniec); // ustal tablice
  51.         setAllLigts(tab); // zapal jedynki
  52.         wait(&p, &port_data, &temp, &button_state, &direction, tab, &lewyKoniec, &dl, &permission);
  53.   };
  54.    
  55.    
  56.    
  57.   return 0;
  58. }
  59.  
  60.  
  61. void wait(unsigned long int* p, uint8_t* port_data, uint8_t* temp, uint8_t* button_state, int* direction, int tab[], int* lewyKoniec, int* dl, bool* permisison)
  62. {
  63.     int i, j;
  64.     for(i=0; i<(*p); i++)
  65.     {
  66.         (*port_data) = GPIO_ReadInputData(GPIOA); //czytaj port GPIOA
  67.         (*temp) = (*port_data) ^ (*button_state); // czy stan przycisków sie zmienil?
  68.         (*temp) &= (*button_state); // czy to byla zmiana z 1 na 0?
  69.         (*button_state) = (*port_data); // zapamietaj nowy stan
  70.         if((*temp) != 0)
  71.         {
  72.             if((*temp) & 0x20) { (*p)/=1.2; }
  73.             else
  74.             if((*temp) & 0x40) { (*p)*=1.2; }
  75.             else
  76.             if((((*direction)==1) && ((*temp) & 0x10))) { (*direction)=0; }
  77.             else
  78.             if((((*direction)==0) && ((*temp) & 0x80))) { (*direction)=1; }    
  79.             else
  80.             if(((*temp) & 0x01) && (*permisison)==true) // SW0, zmniejszamy
  81.             {
  82.                 //for(j = 0; j < (*p); j++);     
  83.                 if((*dl)>1)
  84.                 {
  85.                     if((*direction) == 0)
  86.                     {
  87.                         tab[((*lewyKoniec)-(*dl)+1+8)%8] = 0;
  88.                         (*dl) -= 1;
  89.                     }
  90.                     else
  91.                     {
  92.                         tab[(*lewyKoniec)] = 0;
  93.                         (*lewyKoniec) = ((*lewyKoniec)-1+8)%8; // +8 bo w C nie liczy modulo z ujemnych
  94.                         (*dl) -= 1;
  95.                     }                  
  96.                     (*permisison)=false;
  97.                 }
  98.             }
  99.             else
  100.             if(((*temp) & 0x02) && (*permisison)==true) // SW1, zwiekszamy
  101.             {
  102.                 //+for(j = 0; j < (*p); j++);    
  103.                 if((*dl)<7)
  104.                 {
  105.                     if((*direction) == 0)
  106.                     {
  107.                         tab[((*lewyKoniec)-(*dl)+1+8)%8] = 1;
  108.                         (*dl) += 1;
  109.                     }
  110.                     else
  111.                     {
  112.                         (*lewyKoniec) = ((*lewyKoniec)+1)%8; // +8 bo w C nie liczy modulo z ujemnych
  113.                         tab[(*lewyKoniec)] = 1;
  114.                         (*dl) += 1;
  115.                     }
  116.                    
  117.                     (*permisison)=false;
  118.                 }
  119.             }
  120.         }
  121.     }
  122. }
  123.  
  124.  
  125. void setAllLigts(int tab[8])
  126. {
  127.     GPIO_WriteBit(GPIOB, GPIO_Pin_0, tab[0]==1 ? Bit_SET : Bit_RESET); 
  128.     GPIO_WriteBit(GPIOB, GPIO_Pin_1, tab[1]==1 ? Bit_SET : Bit_RESET); 
  129.     GPIO_WriteBit(GPIOB, GPIO_Pin_2, tab[2]==1 ? Bit_SET : Bit_RESET); 
  130.     GPIO_WriteBit(GPIOB, GPIO_Pin_3, tab[3]==1 ? Bit_SET : Bit_RESET); 
  131.     GPIO_WriteBit(GPIOB, GPIO_Pin_4, tab[4]==1 ? Bit_SET : Bit_RESET); 
  132.     GPIO_WriteBit(GPIOB, GPIO_Pin_5, tab[5]==1 ? Bit_SET : Bit_RESET); 
  133.     GPIO_WriteBit(GPIOB, GPIO_Pin_6, tab[6]==1 ? Bit_SET : Bit_RESET); 
  134.     GPIO_WriteBit(GPIOB, GPIO_Pin_7, tab[7]==1 ? Bit_SET : Bit_RESET); 
  135. }
  136.  
  137.  
  138. void setTable(int tab[8], int d, int dl, int* lewyKoniec)
  139. {
  140.    
  141.     if(d == 0) // jesli w lewo
  142.     {
  143.         tab[((*lewyKoniec)-(dl-1)+8)%8] = 0; // zerujemy prawy koniec ; +8 bo w C nie liczy modulo z ujemnych
  144.         (*lewyKoniec) = ((*lewyKoniec)+1)%8; // ustalamy nowy lewy koniec
  145.         tab[(*lewyKoniec)] = 1;              // wstawiamy 1 z lewej strony
  146.     }  
  147.     else // jesli w prawo
  148.     {
  149.         tab[(*lewyKoniec)] = 0;                // zerujemy lewy koniec
  150.         (*lewyKoniec) = ((*lewyKoniec)-1+8)%8; // ustalamy nowy lewy koniec ; +8 bo w C nie liczy modulo z ujemnych
  151.         tab[((*lewyKoniec)-(dl-1)+8)%8] = 1;   // wstawiamy 1 z lewej strony ; +8 bo w C nie liczy modulo z ujemnych
  152.     }
  153.    
  154. }
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161. //konfigurowanie sygnalow taktujacych ////////////////////////////////////////////////////////////////////////////
  162. void RCC_Config(void)
  163. {
  164.   ErrorStatus HSEStartUpStatus;                          //zmienna opisujaca rezultat uruchomienia HSE
  165.  
  166.   RCC_DeInit();                                          //Reset ustawien RCC
  167.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
  168.   RCC_HSEConfig(RCC_HSE_ON);                             //Wlaczenie HSE
  169.   HSEStartUpStatus = RCC_WaitForHSEStartUp();            //Odczekaj az HSE bedzie gotowy
  170.   if(HSEStartUpStatus == SUCCESS)
  171.   {
  172.     FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);//
  173.     FLASH_SetLatency(FLASH_Latency_2);                   //ustaw zwloke dla pamieci Flash; zaleznie od taktowania rdzenia
  174.                                                          //0:<24MHz; 1:24~48MHz; 2:>48MHz
  175.     RCC_HCLKConfig(RCC_SYSCLK_Div1);                     //ustaw HCLK=SYSCLK
  176.     RCC_PCLK2Config(RCC_HCLK_Div1);                      //ustaw PCLK2=HCLK
  177.     RCC_PCLK1Config(RCC_HCLK_Div2);                      //ustaw PCLK1=HCLK/2
  178.     RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); //ustaw PLLCLK = HSE*9 czyli 8MHz * 9 = 72 MHz
  179.     RCC_PLLCmd(ENABLE);                                  //wlacz PLL
  180.     while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);  //odczekaj na poprawne uruchomienie PLL
  181.     RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);           //ustaw PLL jako zrodlo sygnalu zegarowego
  182.     while(RCC_GetSYSCLKSource() != 0x08);                //odczekaj az PLL bedzie sygnalem zegarowym systemu
  183.  
  184.   /*Tu nalezy umiescic kod zwiazany z konfiguracja sygnalow zegarowych potrzebnych w programie peryferiow*/
  185.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);//wlacz taktowanie portu GPIO A
  186.    
  187.   } else {
  188.   }
  189. }
  190.  
  191.  
  192.  
  193. void GPIO_Config(void)
  194. {
  195.   //konfigurowanie portow GPIO
  196.   GPIO_InitTypeDef  GPIO_InitStructure;
  197. GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
  198.   /*Tu nalezy umiescic kod zwiazany z konfiguracja poszczegolnych portow GPIO potrzebnych w programie*/
  199.   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;
  200.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  201.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;      //wyjscie push-pull
  202.   GPIO_Init(GPIOB, &GPIO_InitStructure);
  203.  
  204.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
  205.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  206.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;      //wejscie bez podciagania
  207.   GPIO_Init(GPIOA, &GPIO_InitStructure);
  208.  
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement