Advertisement
PomozMi

ZAD 1 KOŁO PTM KAROL

Jan 25th, 2015
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.22 KB | None | 0 0
  1. #include "stm32f4xx_conf.h"
  2. #include "stm32f4xx_gpio.h"
  3. #include "stm32f4xx_rcc.h"
  4. #include "stm32f4xx_tim.h"
  5. #include "stm32f4xx_syscfg.h"
  6. #include "stm32f4xx_exti.h"
  7. #include "misc.h"
  8. #include "stm32f4xx_adc.h"
  9. #include "stm32f4xx_dac.h"
  10. #include "stm32f4xx_spi.h"
  11. #include "stm32f4xx_usart.h"
  12. #include "stm32f4xx_dma.h"
  13. #include "stm32f4_discovery_lis302dl.h"
  14.  
  15. //akcelerometr:
  16. int8_t acc_x;
  17. int8_t acc_y;
  18. int8_t acc_z;
  19.  
  20. unsigned int i;
  21.  
  22. void Diody_LED()
  23. {
  24.     // Diody LED
  25.     /* GPIOD Periph clock enable */
  26.     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
  27.     /* Configure PD12, PD13, PD14 and PD15 in output pushpull mode */ // DIODY LED
  28.     GPIO_InitTypeDef  GPIO_InitStructure;
  29.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
  30.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  31.     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  32.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;// maks prędkosc przelaczania wyprowadze
  33.     GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  34.     GPIO_Init(GPIOD, &GPIO_InitStructure);
  35.     //
  36. }
  37.  
  38. void Przycisk()
  39. {
  40.     // Przycisk
  41.     SystemCoreClockUpdate();
  42.  
  43.     GPIO_InitTypeDef  GPIO_InitStructure;
  44.     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  45.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  46.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  47.     //GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  48.     //GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; // maks prędkosc przelaczania wyprowadzen
  49.     GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  50.     GPIO_Init(GPIOA, &GPIO_InitStructure);
  51.     //
  52. }
  53.  
  54. void TIM3_confg()
  55. {
  56.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
  57.  
  58.     TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  59.  
  60.     /* Time base configuration */
  61.     //period 2000 prescaler 41999;  == 1s
  62.  
  63.     TIM_TimeBaseStructure.TIM_Period = 1000;
  64.     TIM_TimeBaseStructure.TIM_Prescaler = 41999;
  65.     TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  66.     TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  67.     TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
  68.  
  69.     TIM_Cmd(TIM3, ENABLE); // Timer uruchamiamy...
  70. /*
  71.  
  72. Możliwe jest odczytanie rejestru, w którym przechowywana jest aktualna wartość
  73. licznika:
  74.  
  75.     unsigned int counter = TIMx->CNT;
  76.  
  77. Oraz programowe odczytanie i reset flagi przepełnienia timera:
  78.  
  79.     if(TIM_GetFlagStatus(TIMx, TIM_FLAG_Update)) {// zmienić x
  80.         ...
  81.         ...
  82.         TIM_ClearFlag(TIMx, TIM_FLAG_Update);   // zmienić x
  83.     }
  84.  
  85. */
  86. }
  87. void TIM4_confg()
  88. {
  89.     SystemCoreClockUpdate();
  90.  
  91.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
  92.  
  93.     TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  94.  
  95.     /* Time base configuration */
  96.     //period 1 999  prescaler 41 999 ; timer == 1s
  97.  
  98.     TIM_TimeBaseStructure.TIM_Period = 999;
  99.     TIM_TimeBaseStructure.TIM_Prescaler = 41999;
  100.     TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  101.     TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  102.     TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
  103.  
  104.     TIM_Cmd(TIM4, ENABLE); // Timer uruchamiamy...
  105. /*
  106.  
  107. Możliwe jest odczytanie rejestru, w którym przechowywana jest aktualna wartość
  108. licznika:
  109.  
  110.     unsigned int counter = TIMx->CNT;
  111.  
  112. Oraz programowe odczytanie i reset flagi przepełnienia timera:
  113.  
  114.     if(TIM_GetFlagStatus(TIMx, TIM_FLAG_Update)) {// zmienić x
  115.         ...
  116.         ...
  117.         TIM_ClearFlag(TIMx, TIM_FLAG_Update);   // zmienić x
  118.     }
  119.  
  120. */
  121. }
  122.  
  123.  
  124. void TIM_COUNTER(){
  125.  
  126.  
  127.  
  128.     //##### Konfiguracja przerwań - Timer/Counter.
  129.  
  130.  
  131.     // ustawienie trybu pracy priorytetów przerwań
  132.     NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
  133.  
  134.  
  135.     NVIC_InitTypeDef NVIC_InitStructure;
  136.     // numer przerwania
  137.     NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
  138.     // priorytet główny
  139.     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;
  140.     // subpriorytet
  141.     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
  142.     // uruchom dany kanał
  143.     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  144.     // zapisz wypełnioną strukturę do rejestrów
  145.     NVIC_Init(&NVIC_InitStructure);
  146.     //####
  147.     // wyczyszczenie przerwania od timera 3 (wystąpiło przy konfiguracji timera)
  148.     TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
  149.     // zezwolenie na przerwania od przepełnienia dla timera 3
  150.     TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
  151.  
  152.     //###
  153.     //uruchomienie timera
  154.     TIM_Cmd(TIM3, ENABLE);
  155.  
  156. }
  157.  
  158. void TIM3_IRQHandler(void) //Obsługa przerwań - Timer / Counter.
  159. {
  160.     if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
  161.     {
  162.         // miejsce na kod wywoływany w momencie wystąpienia przerwania
  163.  
  164.         //#####
  165.  
  166.  
  167.         //#####
  168.  
  169.         // wyzerowanie flagi wyzwolonego przerwania
  170.         TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
  171.     }
  172. }
  173.  
  174. int main(void)
  175. {
  176.  
  177.     SystemInit();
  178.     SystemCoreClockUpdate(); // calculates the current core clock
  179.  
  180.     Diody_LED();
  181.     Przycisk();
  182.     //TIM3_confg();
  183.     TIM4_confg();
  184.     //PWM__(); // z diodami na pwm
  185.     //TIM_COUNTER();
  186.     //TIM3_IRQHandler();
  187.     //ZEWNETRZNE();
  188.     //EXTI1_IRQHandler();
  189.     //ADC__();
  190.     //DAC__();
  191.     //akcelerometr();
  192.  
  193.     /* Time base configuration */
  194.     //period 2000 prescaler 41999;  == 1s
  195.  
  196.  
  197.  
  198.  
  199.  
  200.             int stan=1, mode=0;
  201.             for(;;){
  202.                 if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0)!=0){
  203.                     if(mode==1) mode=0;
  204.                     else if(mode==0) mode=1;
  205.                    
  206.                     for (i=0;i<10000000;i++);
  207.                 }
  208.                 if(TIM_GetFlagStatus(TIM4, TIM_FLAG_Update)) {
  209.                     switch(stan) {
  210.                     case 1:
  211.                                         GPIO_SetBits(GPIOD, GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);
  212.                                         break;
  213.                                         case 2:
  214.                                         GPIO_ResetBits(GPIOD, GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);
  215.                                         break;
  216.                                         case 3:
  217.                                         GPIO_SetBits(GPIOD, GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);
  218.                                         break;
  219.                                         case 4:
  220.                                         GPIO_ResetBits(GPIOD, GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);
  221.                                         break;
  222.                                         case 5:
  223.                                         GPIO_SetBits(GPIOD, GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);
  224.                                         break;
  225.                                         case 6:
  226.                                         GPIO_ResetBits(GPIOD, GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);
  227.                                         break;
  228.                                         case 7:
  229.                                         GPIO_SetBits(GPIOD, GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);
  230.                                         break;
  231.                                         case 8:
  232.                                         GPIO_SetBits(GPIOD, GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);
  233.                                         break;
  234.                                         case 9:
  235.                                         GPIO_SetBits(GPIOD, GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);
  236.                                         break;
  237.                                         case 10:
  238.                                         GPIO_ResetBits(GPIOD, GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);
  239.                                         break;
  240.                                         case 11:
  241.                                         GPIO_SetBits(GPIOD, GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);
  242.                                         break;
  243.                                         case 14:
  244.                                         GPIO_ResetBits(GPIOD, GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);
  245.                                         break;
  246.                                         case 15:
  247.                                         GPIO_SetBits(GPIOD, GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);
  248.                                         break;
  249.                                         case 16:
  250.                                         GPIO_SetBits(GPIOD, GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);
  251.                                         break;
  252.                                         case 17:
  253.                                         GPIO_SetBits(GPIOD, GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);
  254.                                         break;
  255.                                         case 18:
  256.                                         GPIO_ResetBits(GPIOD, GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);
  257.                                         break;
  258.                                        
  259.                     }
  260.                     if(mode==0){
  261.                         if(stan+1!=19)stan++;
  262.                         else stan=1;
  263.                     }
  264.                     else if(mode==1){
  265.                         stan=4;
  266.                     }
  267.  
  268.                     TIM_ClearFlag(TIM4, TIM_FLAG_Update);
  269.                 }
  270.             }
  271.  
  272.  
  273. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement