Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stm32f30x_conf.h" // STM32 config
- #include "30021_io.h" // Input/output library for this course
- void initJoystick();
- char readJoystick();
- /*
- void initJoystick(void){
- RCC->AHBENR |= RCC_AHBPeriph_GPIOA; // Enable clock for GPIO Port
- RCC->AHBENR |= RCC_AHBPeriph_GPIOB;
- RCC->AHBENR |= RCC_AHBPeriph_GPIOC;
- GPIOA -> MODER &=~ (0x00000300 << 1); //CLEAR REGISTER A PA4
- GPIOB -> MODER &=~ (0x00000C03 << 1); //CLEAR REGISTER B PB0 and PB5
- GPIOC -> MODER &=~ (0x0000000F << 1); //CLEAR REGISTER C PC0 and PC1
- GPIOA -> PUPDR &=~ (0x00000300 << 1); //clear pull register
- GPIOB -> PUPDR &=~ (0x00000C03 << 1); //clear pull register
- GPIOC -> PUPDR &=~ (0x0000000F << 1); //clear pull register
- GPIOA -> PUPDR |= (0x00000200 << 1);
- GPIOB -> PUPDR |= (0x00000802 << 1);
- GPIOC -> PUPDR |= (0x00000000 << 1); //set PC0 with pull down (10 in the register)
- }
- */
- char readJoystick(void){
- char joystick=0x00;
- if((GPIOA->IDR & (0x0010 << 0))==1){joystick=0b00000001;}//up
- if((GPIOB->IDR & (0x0001 << 0))==1){joystick=0b00000010;}//down
- if((GPIOC->IDR & (0x0002 << 0))==1){joystick=0b00000100;}//left
- if((GPIOC->IDR & (0x0001 << 0))==1){joystick=0b00001000;}//right
- if((GPIOB->IDR & (0x0200 << 0))==1){joystick=0b00010000;}//center
- else{joystick=0x00;}
- return(joystick);
- }
- int main(void){
- init_usb_uart( 9600 );
- initJoystick();
- int8_t check=0x00;
- char joystick1=0x00;
- while(1){
- joystick1=readJoystick();
- if(check==0){
- if(joystick1==0b00000001){printf("up");}
- if(joystick1==0b00000010){printf("down");}
- if(joystick1==0b00000100){printf("left");}
- if(joystick1==0b00001000){printf("right");}
- if(joystick1==0b00010000){printf("Center");}
- printf("\n");
- check=1;
- }
- if(joystick1==0x00){
- check=0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement