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 init_board();
- char readJoystick();
- void init_board(void){
- RCC->AHBENR |= RCC_AHBPeriph_GPIOA; // Enable clock for GPIO Port
- RCC->AHBENR |= RCC_AHBPeriph_GPIOB;
- RCC->AHBENR |= RCC_AHBPeriph_GPIOC;
- GPIOA -> MODER &=~ (0x00000003 << (4 * 2)); //CLEAR REGISTER A PA4
- GPIOB -> MODER &=~ (0x00000003 << (0 * 2)); //CLEAR REGISTER B PB0
- GPIOB -> MODER &=~ (0x00000003 << (5 * 2)); //CLEAR REGISTER B PB5
- GPIOC -> MODER &=~ (0x00000003 << (0 * 2)); //CLEAR REGISTER C PC0
- GPIOC -> MODER &=~ (0x00000003 << (1 * 2)); //CLEAR REGISTER C PC1
- GPIOA -> PUPDR &=~ (0x00000003 << (4 * 2)); //clear pull register
- GPIOB -> PUPDR &=~ (0x00000003 << (0 * 2)); //clear pull register
- GPIOB -> PUPDR &=~ (0x00000003 << (5 * 2)); //clear pull register
- GPIOC -> PUPDR &=~ (0x00000003 << (0 * 2)); //clear pull register
- GPIOC -> PUPDR &=~ (0x00000003 << (1 * 2)); //clear pull register
- GPIOA -> PUPDR |= (0x00000002 << (4 * 2)); //Set Pulldown A PA4
- GPIOB -> PUPDR |= (0x00000002 << (0 * 2)); //Set Pulldown B PB0
- GPIOB -> PUPDR |= (0x00000002 << (5 * 2)); //Set Pulldown B PB5
- GPIOC -> PUPDR |= (0x00000002 << (0 * 2)); //Set Pulldown C PC0
- GPIOC -> PUPDR |= (0x00000002 << (1 * 2)); //Set Pulldown C PC1
- }
- char readJoystick(void){
- char joystick = 0x00;
- if(GPIOA->IDR & (0x0001 << 4)){joystick=0b00000001;}//up
- if(GPIOB->IDR & (0x0001 << 0)){joystick=0b00000010;}//down
- if(GPIOC->IDR & (0x0001 << 1)){joystick=0b00000100;}//left
- if(GPIOC->IDR & (0x0001 << 0)){joystick=0b00001000;}//right
- if(GPIOB->IDR & (0x0001 << 5)){joystick=0b00010000;}//center
- return(joystick);
- }
- int main(void){
- init_board();
- init_usb_uart( 9600 );
- int8_t check = 0;
- char position = 0;
- while(1){
- position = readJoystick();
- if(check==0){
- if(position == 0b00000001){printf("up");}
- if(position == 0b00000010){printf("down");}
- if(position == 0b00000100){printf("left");}
- if(position == 0b00001000){printf("right");}
- if(position == 0b00010000){printf("Center");}
- printf("\n");
- check = 1;
- }
- if(position==0x00){
- check=0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement