Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdint.h>
- #include <stdbool.h>
- #include "include/AT91SAM9263.h"
- #define DELAY_TIME 100
- #define LED1 (1 << 8) //PB8
- #define LED2 (1 << 29) //PC29
- #define RIGHTCLIC (1 << 4) //PC4
- #define LEFTCLIC (1 << 5) //PC5
- #define ENABLECLOCK (1 << 4) //change name!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- #define DELAY_IN_SEC (0.001)
- #define PIV ( (DELAY_IN_SEC / (160*0.000000001) ) - 1)
- bool flag = false;
- void TIMER_INT_handler (void){
- if (AT91C_BASE_PITC->PITC_PIMR & AT91C_PITC_PITIEN ){
- flag = true;
- volatile unsigned int clear = AT91C_BASE_PITC->PITC_PIVR;
- }
- }
- void ConfigureLEDs (void)
- {
- *AT91C_PIOB_PER = LED1;
- *AT91C_PIOB_OER = LED1;
- *AT91C_PIOC_PER = LED2;
- *AT91C_PIOC_OER = LED2;
- *AT91C_PIOB_SODR = LED1;
- *AT91C_PIOC_SODR = LED2;
- }
- void ConfigureButtons (void) {
- //enable peripherial control of both pins
- *AT91C_PIOC_PER = RIGHTCLIC;
- *AT91C_PIOC_PER = LEFTCLIC;
- //disable output, which means that the port works as input
- *AT91C_PIOC_ODR = RIGHTCLIC;
- *AT91C_PIOC_ODR = LEFTCLIC;
- *AT91C_PIOB_PPUER = RIGHTCLIC;
- *AT91C_PIOB_PPUER = LEFTCLIC;
- //enable clock
- *AT91C_PMC_PCER = ENABLECLOCK;
- }
- void PIT_Init (void) {
- AT91C_BASE_PITC->PITC_PIMR = PIV; // in mode register er set periodic interval value
- AT91C_BASE_AIC->AIC_IDCR = 1 << AT91C_ID_SYS; //disable PIT Timer interrupts
- AT91C_BASE_AIC->AIC_SMR[AT91C_ID_SYS] = (AT91C_AIC_SRCTYPE_INT_LEVEL_SENSITIVE | AT91C_AIC_PRIOR_LOWEST); //configure method of interrupt triggering: level, edge
- AT91C_BASE_AIC->AIC_SVR[AT91C_ID_SYS] = (unsigned int)TIMER_INT_handler;// name for interrupt handler
- AT91C_BASE_AIC->AIC_ICCR = 1 << AT91C_ID_SYS;
- AT91C_BASE_AIC->AIC_IECR = 1 << AT91C_ID_SYS;
- AT91C_BASE_PITC->PITC_PIMR |= AT91C_PITC_PITIEN; // enable PIT interrupt
- volatile unsigned int clear = AT91C_BASE_PITC->PITC_PIVR; //reading the register resets it
- }
- void PIT_delay_ms (int delay_in_ms ) {
- int counter = 0;
- for(counter; counter < delay_in_ms; counter++) {
- AT91C_BASE_PITC->PITC_PIMR |= AT91C_PITC_PITEN;
- while(!flag){}
- flag = false;
- }
- }
- void CheckButton (void) {
- if ( *AT91C_PIOC_PDSR == (1 << 4 ) ) {
- *AT91C_PIOC_CODR = LED2;
- }
- if ( *AT91C_PIOC_PDSR == (1 << 5 ) ) {
- *AT91C_PIOC_SODR = LED2;
- }
- }
- int main(void) {
- ConfigureLEDs();
- ConfigureButtons();
- PIT_Init();
- while(1){
- int counter;
- *AT91C_PIOB_CODR = LED1; //turn on LED1
- for(counter = 0; counter < 100; counter++){
- PIT_delay_ms(10);
- CheckButton();
- }
- *AT91C_PIOB_SODR = LED1; //turn off LED2
- for(counter = 0; counter < 100; counter++){
- PIT_delay_ms(10);
- CheckButton();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement