Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // PIO enable register
- #define PIOB_PER (volatile unsigned int *) 0xFFFFF400 // B - left LED
- #define PIOC_PER (volatile unsigned int *) 0xFFFFF600 // C - right LED
- // output disable register
- #define PIOC_ODR (volatile unsigned int *) 0xFFFFF614 // for both buttons
- // output data status register
- #define PIOB_ODSR (volatile unsigned int *) 0xFFFFF438 // for left LED
- // output enable register
- #define PIOB_OER (volatile unsigned int *) 0xFFFFF410 // B - left LED
- #define PIOC_OER (volatile unsigned int *) 0xFFFFF610 // C - right LED
- // set/clear for PB
- #define PIOB_SODR (volatile unsigned int *) 0xFFFFF430
- #define PIOB_CODR (volatile unsigned int *) 0xFFFFF434
- // set/clear for PC
- #define PIOC_SODR (volatile unsigned int *) 0xFFFFF630
- #define PIOC_CODR (volatile unsigned int *) 0xFFFFF634
- // pull-up enabled/disabled
- #define PIOC_PUER (volatile unsigned int *) 0xFFFFF664 // for buttons: BP1 and BP2
- #define PIOC_PUDR (volatile unsigned int *) 0xFFFFF660 // for buttons: BP1 and BP2
- // pull-up status
- #define PIOC_PDSR (volatile unsigned int *) 0xFFFFF63C
- // clock
- #define PMC_PCER (volatile unsigned int *) 0xFFFFF63C
- #define LEFT_BUTTON (1 << 5)
- #define RIGHT_BUTTON (1 << 4)
- #define LEFT_LED (1 << 8)
- #define RIGHT_LED (1 << 29)
- #define CLOCK (1 << 4)
- void dbgu_print_ascii(const char *a) {}
- void delay(int how_long) {
- volatile unsigned int counter = how_long;
- while(counter > 0)
- --counter;
- }
- void toggle() {
- if (!(*PIOB_ODSR & LEFT_LED)) {
- *PIOB_SODR = LEFT_LED;
- } else {
- *PIOB_CODR = LEFT_LED ;
- }
- }
- int isLeftButtonClicked() {
- if (!(*PIOC_PDSR & LEFT_BUTTON))
- return 1;
- return 0;
- }
- int isRightButtonClicked() {
- if (!(*PIOC_PDSR & RIGHT_BUTTON))
- return 1;
- return 0;
- }
- void setOutputLED1() {
- *PIOB_PER = LEFT_LED;
- *PIOB_OER = LEFT_LED;
- *PIOB_SODR = LEFT_LED; // turn off the LED1
- }
- void setOutputLED2() {
- *PIOC_PER = RIGHT_LED;
- *PIOC_OER = RIGHT_LED;
- *PIOB_SODR = RIGHT_LED; // turn off the LED2
- }
- void setInputBP1() {
- *PIOC_PER = LEFT_BUTTON;
- *PIOC_ODR = LEFT_BUTTON;
- *PIOC_PUER = LEFT_BUTTON;
- *PMC_PCER = CLOCK;
- }
- void setInputBP2() {
- *PIOC_PER = RIGHT_BUTTON;
- *PIOC_ODR = RIGHT_BUTTON;
- *PIOC_PUER = RIGHT_BUTTON;
- *PMC_PCER = CLOCK;
- }
- void configureIO() {
- setOutputLED1();
- setOutputLED2();
- setInputBP1();
- setInputBP2();
- }
- int main() {
- configureIO();
- int forCounter;
- while(1){
- toggle();
- for (forCounter = 0; forCounter < 100; ++forCounter) {
- if (isLeftButtonClicked()) {
- *PIOC_CODR = RIGHT_LED;
- } else if (isRightButtonClicked()) {
- *PIOC_SODR = RIGHT_LED;
- }
- delay(10000);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement