Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1.  
  2.  
  3.  
  4. #include "stm32f30x_conf.h" // STM32 config
  5. #include "30021_io.h" // Input/output library for this course
  6.  
  7.  
  8. void init_board();
  9. char readJoystick();
  10.  
  11. void init_board(void){
  12. RCC->AHBENR |= RCC_AHBPeriph_GPIOA; // Enable clock for GPIO Port
  13. RCC->AHBENR |= RCC_AHBPeriph_GPIOB;
  14. RCC->AHBENR |= RCC_AHBPeriph_GPIOC;
  15.  
  16. GPIOA -> MODER &=~ (0x00000003 << (4 * 2)); //CLEAR REGISTER A PA4
  17. GPIOB -> MODER &=~ (0x00000003 << (0 * 2)); //CLEAR REGISTER B PB0
  18. GPIOB -> MODER &=~ (0x00000003 << (5 * 2)); //CLEAR REGISTER B PB5
  19. GPIOC -> MODER &=~ (0x00000003 << (0 * 2)); //CLEAR REGISTER C PC0
  20. GPIOC -> MODER &=~ (0x00000003 << (1 * 2)); //CLEAR REGISTER C PC1
  21.  
  22. GPIOA -> PUPDR &=~ (0x00000003 << (4 * 2)); //clear pull register
  23. GPIOB -> PUPDR &=~ (0x00000003 << (0 * 2)); //clear pull register
  24. GPIOB -> PUPDR &=~ (0x00000003 << (5 * 2)); //clear pull register
  25. GPIOC -> PUPDR &=~ (0x00000003 << (0 * 2)); //clear pull register
  26. GPIOC -> PUPDR &=~ (0x00000003 << (1 * 2)); //clear pull register
  27.  
  28. GPIOA -> PUPDR |= (0x00000002 << (4 * 2)); //Set Pulldown A PA4
  29. GPIOB -> PUPDR |= (0x00000002 << (0 * 2)); //Set Pulldown B PB0
  30. GPIOB -> PUPDR |= (0x00000002 << (5 * 2)); //Set Pulldown B PB5
  31. GPIOC -> PUPDR |= (0x00000002 << (0 * 2)); //Set Pulldown C PC0
  32. GPIOC -> PUPDR |= (0x00000002 << (1 * 2)); //Set Pulldown C PC1
  33. }
  34.  
  35. char readJoystick(void){
  36. char joystick = 0x00;
  37.  
  38. if(GPIOA->IDR & (0x0001 << 4)){joystick=0b00000001;}//up
  39. if(GPIOB->IDR & (0x0001 << 0)){joystick=0b00000010;}//down
  40. if(GPIOC->IDR & (0x0001 << 1)){joystick=0b00000100;}//left
  41. if(GPIOC->IDR & (0x0001 << 0)){joystick=0b00001000;}//right
  42. if(GPIOB->IDR & (0x0001 << 5)){joystick=0b00010000;}//center
  43.  
  44. return(joystick);
  45. }
  46.  
  47. int main(void){
  48.  
  49. init_board();
  50.  
  51. init_usb_uart( 9600 );
  52.  
  53. int8_t check = 0;
  54. char position = 0;
  55.  
  56. while(1){
  57.  
  58. position = readJoystick();
  59.  
  60. if(check==0){
  61. if(position == 0b00000001){printf("up");}
  62. if(position == 0b00000010){printf("down");}
  63. if(position == 0b00000100){printf("left");}
  64. if(position == 0b00001000){printf("right");}
  65. if(position == 0b00010000){printf("Center");}
  66. printf("\n");
  67. check = 1;
  68. }
  69.  
  70. if(position==0x00){
  71. check=0;
  72. }
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement