Atmel website | ARM Community | AVR freaks | Technical Support
Banner
 FAQ •  Search •  Register •  Login 

All times are UTC + 1 hour [ DST ]




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: AT91SAM7S256 reading inputs :s
PostPosted: Tue Aug 04, 2009 7:57 pm 
Offline

Joined: Tue Aug 04, 2009 7:47 pm
Posts: 1
Hi,
First time here :)
I started arm programming, but i already have difficulties reading input pins (don't laugh :D)

Here is the code i use :

Code:
//  *****************************************************************************
//                     main.c
//
//     Demonstration program for Olimex SAM7-H256 Evaluation Board
//
//     blinks LED0 (pin PA8) with an endless loop
//
//  Authors:  James P Lynch  September 23, 2006; Olimex, Mar 2007
//  *****************************************************************************

//  *******************************************************
//                Header Files
//  *******************************************************
#include "AT91SAM7S256.h"
#include "board.h"

//  *******************************************************
//                Function Prototypes
//  *******************************************************
void Timer0IrqHandler(void);
void FiqHandler(void);

//  *******************************************************
//                External References
//  *******************************************************
extern   void LowLevelInit(void);
extern   void TimerSetup(void);
extern   unsigned enableIRQ(void);
extern   unsigned enableFIQ(void);

//  *******************************************************
//               Global Variables
//  *******************************************************
unsigned int   FiqCount = 0;      // global uninitialized variable      


//  *******************************************************
//                     MAIN
//  ******************************************************/
int   main (void) {
   unsigned long   j=0;                        // loop counter (stack variable)
   unsigned long   IdleCount = 0;               // idle loop blink counter (2x)
   
   // Initialize the Atmel AT91SAM7S256 (watchdog, PLL clock, default interrupts, etc.)
   // ---------------------------------------------------------------------------------
   LowLevelInit();
   
   
   // Turn on the peripheral clock for Timer0
   // ---------------------------------------
   
   // pointer to PMC data structure
   volatile AT91PS_PMC   pPMC = AT91C_BASE_PMC;
   
   // enable Timer0 peripheral clock      
   pPMC->PMC_PCER = (1<<AT91C_ID_TC0);   
   
   
   // Set up the PIO ports
   // --------------------         

   // pointer to PIO data structure
   volatile AT91PS_PIO   pPIO = AT91C_BASE_PIOA;
   
   // PIO Output Enable Register - sets pins P0 - P3 to outputs         
   pPIO->PIO_OER = LED_MASK;
   pPIO->PIO_ODR = PA6; // input
   pPIO->PIO_OER = PA20; // output
   pPIO->PIO_OER = PA22;
   pPIO->PIO_OER = PA24;
   pPIO->PIO_OER = PA26;
   pPIO->PIO_OER = PA28;
   
   // PIO Set Output Data Register - turns off the four LEDs                  
   pPIO->PIO_SODR = LED_MASK;                  
   
   
   // Set up the Advanced Interrupt Controller AIC for Timer 0
   // --------------------------------------------------------
   
   // pointer to AIC data structure 
   volatile AT91PS_AIC   pAIC = AT91C_BASE_AIC;
                              
   // Disable timer 0 interrupt in AIC Interrupt Disable Command Register      
   pAIC->AIC_IDCR = (1<<AT91C_ID_TC0);                                    
   
   // Set the TC0 IRQ handler address in AIC Source Vector Register[12]
   pAIC->AIC_SVR[AT91C_ID_TC0] = (unsigned int)Timer0IrqHandler;            
   
   // Set the interrupt source type and priority in AIC Source Mode Register[12]
   pAIC->AIC_SMR[AT91C_ID_TC0] = (AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL | 0x4 );   
   
   // Clear the TC0 interrupt in AIC Interrupt Clear Command Register
   pAIC->AIC_ICCR = (1<<AT91C_ID_TC0);                               
   
   // Remove disable timer 0 interrupt in AIC Interrupt Disable Command Register         
   pAIC->AIC_IDCR = (0<<AT91C_ID_TC0);                                 
   
   // Enable the TC0 interrupt in AIC Interrupt Enable Command Register
   pAIC->AIC_IECR = (1<<AT91C_ID_TC0);                               
   
   
   
   // Set up the Advanced Interrupt Controller AIC for FIQ (pushbutton SW1)
   // ---------------------------------------------------------------------
   
    // Disable FIQ interrupt in AIC Interrupt Disable Command Register   
   pAIC->AIC_IDCR = (1<<AT91C_ID_FIQ);                                       
   
   // Set the interrupt source type in AIC Source Mode Register[0]
   pAIC->AIC_SMR[AT91C_ID_FIQ] = (AT91C_AIC_SRCTYPE_INT_POSITIVE_EDGE);      
   
   // Clear the FIQ interrupt in AIC Interrupt Clear Command Register
   pAIC->AIC_ICCR = (1<<AT91C_ID_FIQ);                               
   
   // Remove disable FIQ interrupt in AIC Interrupt Disable Command Register      
   pAIC->AIC_IDCR = (0<<AT91C_ID_FIQ);                                    
   
   // Enable the FIQ interrupt in AIC Interrupt Enable Command Register
   pAIC->AIC_IECR = (1<<AT91C_ID_FIQ);                               
   
   
   // Setup timer0 to generate a 50 msec periodic interrupt
   // -----------------------------------------------------
   
   TimerSetup();


   // enable interrupts
   // -----------------
   
   enableIRQ();
   enableFIQ();


   // endless background blink loop
   // -----------------------------
   
   while(1)
   {
      pPIO->PIO_CODR = PA26;
      pPIO->PIO_CODR = PA28;

      if  ((pPIO->PIO_ODSR & LED1) == LED1)      // read previous state of LED1
      {
         pPIO->PIO_CODR = LED1;               // turn LED1 (DS1) on
         pPIO->PIO_CODR = PA20;
         pPIO->PIO_SODR = PA24;
      }
      else
      {
         pPIO->PIO_SODR = LED1;               // turn LED1 (DS1) off
         pPIO->PIO_SODR = PA20;
         pPIO->PIO_CODR = PA24;
      }
      
      if  ((pPIO->PIO_PDSR & PA6) == 0)
      {
         pPIO->PIO_CODR = PA22;
      }
      else
      {
         pPIO->PIO_SODR = PA22;
      }

      for (j = 1000000; j != 0; j-- );         // wait 1 second  2000000

      IdleCount++;                        // count # of times through the idle loop

   }
}


Basicaly i added the following in order to test the IO pins.
I have two LEDs for PA20 and PA24, which i can blink.
I also have a switch ON/OFF on pin PA6.
I would like to light a LED on PA22 if PA6==1 or unlight it if PA6==0.

Code:
if  ((pPIO->PIO_ODSR & LED1) == LED1)      // read previous state of LED1
      {
         pPIO->PIO_CODR = LED1;               // turn LED1 (DS1) on
         pPIO->PIO_CODR = PA20;
         pPIO->PIO_SODR = PA24;
      }
      else
      {
         pPIO->PIO_SODR = LED1;               // turn LED1 (DS1) off
         pPIO->PIO_SODR = PA20;
         pPIO->PIO_CODR = PA24;
      }
      
      if  ((pPIO->PIO_PDSR & PA6) == 0)
      {
         pPIO->PIO_CODR = PA22;
      }
      else
      {
         pPIO->PIO_SODR = PA22;
      }


But i am not sure of the syntax, i could not find some easy tutorial to begin arm programming.

Could someone tell me if this is correct ?

Thank you.


Top
 Profile  
 
 Post subject: Re: AT91SAM7S256 reading inputs :s
PostPosted: Wed Aug 05, 2009 9:02 am 
Offline

Joined: Thu Dec 02, 2004 2:28 pm
Posts: 454
hello,
i would recommend to use one of the examples of the software packages as a starting point.
you can find the appropriate package under the following link:
http://www.atmel.com/dyn/products/tools ... ol_id=4343

regards
gerhard


Top
 Profile  
 
 Post subject: Re: AT91SAM7S256 reading inputs :s
PostPosted: Thu Aug 06, 2009 10:03 pm 
Offline

Joined: Fri Jul 10, 2009 8:16 pm
Posts: 15
In order to read from PIO you must enable it's clock.

I see that you enabled a clock for TC0:

Code:
pPMC->PMC_PCER = (1<<AT91C_ID_TC0);


but not for the PIOA or PIOB embedded peripheral.

BTW, instead of writing:

pPIO->PIO_OER = LED_MASK;

normally you would write:
Code:
AT91C_BASE_PIOA->PIO_OER;


That's what I did...


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC + 1 hour [ DST ]


Who is online

Users browsing this forum: Google [Bot] and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: