SHARE
TWEET

Untitled

a guest Sep 17th, 2019 94 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "stm32f30x_conf.h" // STM32 config
  2. #include "30021_io.h" // Input/output library for this course
  3.  
  4.  
  5. void initJoystick();
  6. char readJoystick();
  7. /*
  8. void initJoystick(void){
  9.  
  10. RCC->AHBENR |= RCC_AHBPeriph_GPIOA;         // Enable clock for GPIO Port
  11. RCC->AHBENR |= RCC_AHBPeriph_GPIOB;
  12. RCC->AHBENR |= RCC_AHBPeriph_GPIOC;
  13.  
  14. GPIOA -> MODER &=~  (0x00000300 << 1); //CLEAR REGISTER A PA4
  15. GPIOB -> MODER &=~  (0x00000C03 << 1); //CLEAR REGISTER B PB0 and PB5
  16. GPIOC -> MODER &=~  (0x0000000F << 1); //CLEAR REGISTER C PC0 and PC1
  17.  
  18. GPIOA -> PUPDR &=~   (0x00000300 << 1); //clear pull register
  19. GPIOB -> PUPDR &=~   (0x00000C03 << 1); //clear pull register
  20. GPIOC -> PUPDR &=~   (0x0000000F << 1); //clear pull register
  21.  
  22.  
  23. GPIOA -> PUPDR |=   (0x00000200 << 1);
  24. GPIOB -> PUPDR |=   (0x00000802 << 1);
  25. GPIOC -> PUPDR |=   (0x00000000 << 1);    //set PC0 with pull down (10 in the register)
  26.  
  27. }
  28. */
  29. char readJoystick(void){
  30.  
  31.     char joystick=0x00;
  32.  
  33.     if((GPIOA->IDR & (0x0010 << 0))==1){joystick=0b00000001;}//up
  34.     if((GPIOB->IDR & (0x0001 << 0))==1){joystick=0b00000010;}//down
  35.     if((GPIOC->IDR & (0x0002 << 0))==1){joystick=0b00000100;}//left
  36.     if((GPIOC->IDR & (0x0001 << 0))==1){joystick=0b00001000;}//right
  37.     if((GPIOB->IDR & (0x0200 << 0))==1){joystick=0b00010000;}//center
  38.  
  39.     else{joystick=0x00;}
  40.  
  41.     return(joystick);
  42. }
  43.  
  44. int main(void){
  45.  
  46.     init_usb_uart( 9600 );
  47.     initJoystick();
  48.     int8_t check=0x00;
  49.     char joystick1=0x00;
  50.  
  51.     while(1){
  52.  
  53.         joystick1=readJoystick();
  54.  
  55.         if(check==0){
  56.             if(joystick1==0b00000001){printf("up");}
  57.             if(joystick1==0b00000010){printf("down");}
  58.             if(joystick1==0b00000100){printf("left");}
  59.             if(joystick1==0b00001000){printf("right");}
  60.             if(joystick1==0b00010000){printf("Center");}
  61.             printf("\n");
  62.             check=1;
  63.         }
  64.  
  65.         if(joystick1==0x00){
  66.             check=0;
  67.         }
  68.  
  69.  
  70.     }
  71.  
  72. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top