Introduction
AT32UC3A0 series MCU has 109 GPIO pins. Each GPIO pin can be programmed for digital input, digital output or it can be assigned to one of 3 peripheral functions, which is specific to that pin.
Example
GPIO pin 21(PA21) can be programmed to be used as general purpose digital I/O, or it can be configured for Analog input
Each GPIO line has a unique number. PA, PB, PC and PX ports do not directly correspond to the GPIO ports. To find the corresponding port and pin, the following formulas can be used:
GPIO port = floor((GPIO number) / 32), example: floor((36)/32) = 1
GPIO pin = GPIO number mod 32, example: 36 mod 32 = 4
Example: Say if LED0 is on PB27. By referring AT32UC3A0512 datasheet pg.47, we know that GPIO number for PB27 is 59, therefore
GPIO port = floor(59/32) = 1
GPIO pin = 59 mod 32 = 27
Now lets see how the GPIO can be configured for digital out on LED1(PB28, GPIO number 60) without using ASF.
1: #include <asf.h> 2: 3: int main (void) 4: { 5: 6: AVR32_GPIO.port[1].gpers |= 1 <<28; //enable GPIO control 7: AVR32_GPIO.port[1].oders |= 1 <<28; //enable output driver 8: 9: AVR32_GPIO.port[1].ovrc |= 1 << 28; //clear the gpio pin 10: 11: while(1); 12: 13: }
If PB28(GPIO number 60, AT32UC3A0512 datasheet pg.47 ) i.e LED1 is to be set for digital out, we have to find GPIO port and pin for it first.So using the formula,GPIO port = floor(60/32) = 1GPIO pin = 60 mod 32 = 28We have to configure now the GPIO port 1, pin 28 for be digital out and not for peripheral function.
It is done by setting bit in gpers register. If the bit is clear, then GPIO pin will be used for peripheral function.
AVR32_GPIO.port[1].gpers |= 1 <<28;
The above line sets 28th pin on GPIO port 1. Now the pin will act as GPIO I/O
Now to set the GPIO pin 28 of port 1 as output, we have to set the 28th bit on oders(Output Driver Enable Register)
AVR32_GPIO.port[1].oders |= 1 <<28; //enable output driver
And to set output value low, we have to set bit Output Value Register Clear(ovrc) register
AVR32_GPIO.port[1].ovrc |= 1 << 28;
Since LED is connected between vcc and the pb28, so by setting the pin low , led will turn on.
Refer pg175 of AT32UC3A datasheet for complete description of GPIO registers.
thank you very much for your tutorial..
ReplyDeletecurrently i'm using the EVK1105 too.
it's been hard finding tutorial as simple and as thorough as what you did here.. :)
I have to say thank you very much too....
ReplyDeletei got no experince with uc, i got only basic c knowledge :S
... i use AC32UC3C-AK (UC3C_EK)