Advertisement
Domy131097

[LV4] Mikroračunalni sustavi - vanjski prekidi

Apr 21st, 2018
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.03 KB | None | 0 0
  1. /*================================= VANJSKI PREKIDI =================================*/
  2.  
  3. /* Includes */
  4. #include <stddef.h>
  5. #include "stm32f10x.h"
  6. #include "stm32f10x_rcc.h"
  7. #include "stm32f10x_gpio.h"
  8. #include "stm32f10x_exti.h"
  9.  
  10.  
  11. int main(void) {
  12.     GPIO_InitTypeDef GPIO_InitStruct;
  13.     EXTI_InitTypeDef EXTI_InitStruct;
  14.     NVIC_InitTypeDef NVIC_InitStruct;
  15.  
  16.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); //jer se portovi GPIOA i GPIOB nalaze na busu APB2
  17.  
  18.     //Konfiguriranje PB0 kao izlazni (crvena ledica)
  19.     GPIO_StructInit(&GPIO_InitStruct);
  20.     GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
  21.     GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
  22.     GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
  23.     GPIO_WriteBit(GPIOB, GPIO_Pin_0, Bit_SET);
  24.     GPIO_Init(GPIOB, &GPIO_InitStruct);
  25.  
  26.     //Konfiguriranje PA0 kao ulazni (tipkalo)
  27.     GPIO_StructInit(&GPIO_InitStruct);
  28.     GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
  29.     GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  30.     GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
  31.     GPIO_Init(GPIOA, &GPIO_InitStruct);
  32.  
  33.     //Konfiguriranje PA0 za koristenje vanjskog prekida
  34.     GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0);
  35.     EXTI_StructInit(&EXTI_InitStruct);
  36.     EXTI_InitStruct.EXTI_Line = EXTI_Line0;
  37.     EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt;
  38.     EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Falling;
  39.     EXTI_InitStruct.EXTI_LineCmd = ENABLE;
  40.     EXTI_Init(&EXTI_InitStruct);
  41.  
  42.     //Konfiguriranje NVIC
  43.     NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); //odabir grupe
  44.     NVIC_InitStruct.NVIC_IRQChannel = EXTI0_IRQn;
  45.     NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
  46.     NVIC_InitStruct.NVIC_IRQChannelSubPriority = 5;
  47.     NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
  48.     NVIC_Init(&NVIC_InitStruct);
  49.  
  50.     while(1) {
  51.  
  52.     }
  53. }
  54.  
  55. //ISR za PB0
  56. void EXTI0_IRQHandler(void) {
  57.     static uint32_t brojac = 0;
  58.  
  59.     if(brojac%2 == 0) {
  60.         GPIO_WriteBit(GPIOB, GPIO_Pin_0, Bit_RESET);
  61.     }
  62.     else {
  63.         GPIO_WriteBit(GPIOB, GPIO_Pin_0, Bit_SET);
  64.     }
  65.     brojac++;
  66.     EXTI_ClearITPendingBit(EXTI_Line0);
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement