Advertisement
Guest User

Untitled

a guest
Feb 17th, 2016
524
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.20 KB | None | 0 0
  1. #include "stm32f4xx.h"
  2. #include "stm32f4xx_rcc.h"
  3. #include "stm32f4xx_gpio.h"
  4. #include "misc.h"
  5. #include "stm32f4xx_exti.h"
  6. #include "stm32f4xx_syscfg.h"
  7.  
  8. #define PS2_CLOCK GPIO_Pin_5
  9. #define PS2_DATA GPIO_Pin_4
  10. #define PS2_PORT GPIOC
  11.  
  12. static uint8_t znak = 0x00;
  13. volatile static uint8_t liczba_bitow = 11;
  14.  
  15. void GPIO_config();
  16. void InterruptConfig();
  17.  
  18. void GPIO_config(){
  19.     GPIO_InitTypeDef gpio_sett; //struktura do ustawien
  20.  
  21.     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); //wlaczenie taktowania zegara dla wszystkich pinow PA
  22.     GPIO_StructInit(&gpio_sett);
  23.     gpio_sett.GPIO_Pin = PS2_DATA | PS2_CLOCK;
  24.     gpio_sett.GPIO_Mode = GPIO_Mode_IN; //jako wejscie
  25.     gpio_sett.GPIO_PuPd = GPIO_PuPd_NOPULL;
  26.     gpio_sett.GPIO_Mode = GPIO_Speed_100MHz; //speed na 100
  27.     GPIO_Init(PS2_PORT,&gpio_sett);
  28.  
  29. }
  30.  
  31. void InterruptConfig(){
  32.     EXTI_InitTypeDef exti_sett;
  33.     NVIC_InitTypeDef nvic_sett;
  34.  
  35.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
  36.  
  37.     SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOC, EXTI_PinSource5);
  38.  
  39.     EXTI_StructInit(&exti_sett);
  40.     exti_sett.EXTI_Mode = EXTI_Mode_Interrupt; //jako przerwanie
  41.     exti_sett.EXTI_Trigger = EXTI_Trigger_Falling; //na zbocze opadajace
  42.     exti_sett.EXTI_Line = EXTI_Line5; //linia 5 bo pin PC5
  43.     exti_sett.EXTI_LineCmd = ENABLE;
  44.     EXTI_Init(&exti_sett);
  45.  
  46.     EXTI_ClearITPendingBit(EXTI9_5_IRQn);
  47.  
  48.     nvic_sett.NVIC_IRQChannel = EXTI9_5_IRQn; //wywolywane przez EXTI
  49.     nvic_sett.NVIC_IRQChannelPreemptionPriority = 0;
  50.     nvic_sett.NVIC_IRQChannelSubPriority = 0;
  51.     nvic_sett.NVIC_IRQChannelCmd = ENABLE;
  52.     NVIC_Init(&nvic_sett);
  53.  
  54.     NVIC_ClearPendingIRQ(EXTI9_5_IRQn);
  55. }
  56.  
  57. void EXTI9_5_IRQHandler(){
  58.     uint8_t pomiar;
  59.     if(EXTI_GetITStatus(EXTI_Line5) != RESET)
  60.         {
  61.         pomiar = GPIO_ReadInputDataBit(PS2_PORT, PS2_DATA);
  62.         if (liczba_bitow==11)
  63.         {
  64.             znak = 0x00;
  65.             if (0 == pomiar)
  66.             {
  67.                 --liczba_bitow;
  68.             }
  69.         } else
  70.         if (liczba_bitow<11 && liczba_bitow>2)
  71.         {
  72.             znak = (znak >> 1);
  73.             if (0 != pomiar)
  74.                 znak = znak + 0x80;
  75.             --liczba_bitow;
  76.         } else
  77.         {
  78.             if (--liczba_bitow == 0)
  79.             {
  80.  
  81.                 liczba_bitow = 11;
  82.             }
  83.         }
  84.         EXTI_ClearITPendingBit(EXTI_Line5);
  85.     }
  86. }
  87.  
  88. int main(void)
  89. {
  90.     GPIO_config();
  91.     InterruptConfig();
  92.     while(1){}
  93.  
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement