Serial Communications with the USART:Again whilst the AVR-32 framework offers examples of using the USART they are not so transparent and easy to configure for your own use. This section will demonstrate how to set up the USART to send characters. These can be picked up on a computer using a “com-port” viewer such as “RealTerm” using a simple “USB TTL RS-232” interface which can be purchased very cheaply from china. A schematic for wiring up the microcontroller is shown opposite. Ports PC16 (and PC15) will be used for the USART. This choice is made with reference to section 3.2.1 table 3-1 (page 11) of the data sheet shown opposite. We see these chosen pins can be used as USART0-TXD & USART0-RXD if we select GPIO function D. The first thing that must be done with the microcontroller is to change the system clock, as by default this is set to RCSYS which is 115kHz, (see page 60 of the data sheet), and is a little slow for useful baud rates of serial communication. For convince we will switch to the built in 8MHz system clock. Before we can switch the main system clock to the internal 8MHz clock it first has to be enabled. This is done with the RCR8register, which is part of the System Control InterFace module (page 114). The 8MHz clock is calibrated at start-up, in order to preserve this calibration value we will first copy it to a variable before rewriting it, with the enable bit, to the register. This register is protected by a lock however, and in order to unlock the register, to make changes to it, we have to write both a key (0xAA) and the address offset of the RCR8 register (0x0048) to theSCIF UNLOCK register (page 100). Again the auto-complete function of AVR studio is a great help, and has values for these already implemented. (The actual value of the auto complete AVR32_SCIF_UNLOCK_KEY_VALUE variable is shown as it is selected. Placing the cursor on this text brings up its defined value at the top of the code window.) Having unlocked the RCR8 register we can now write to it and enable the clock:Once activated and running, we can now switch the main system clock over, using the Main Clock ConTRoLregisters in thePower Manager module (page 60). Again this register is lock protected:Having unlocked the MCCTRLregister we can now write to switch over the clock source:Next comes the job of setting up the TX output pin. Firstly we switch the PC16 port pin to peripheral mode by clearing the corresponding GPIO bit (page 462). This allows the pin to be used by one of the other peripheral functions of the port:From the GPIO function table (show at the start of this section) we can see that function Dis for using this pin as USART0 TX. This is selected by programming the corresponding Peripheral Mux Registers in the GPIOmodule for that pin. For Peripheral function D we need to set PMR2, PMR1, PMR0 to 011 (see page 267). We can now initialise USART0. In this case we will set it up in simple asynchronous mode, 8-bits, no parity, 38,400 Baud. The first of these parameters are set using the Mode Register of the USART module. All bits in this register are reset to zero at start-up as shown in table 25-16 page 625 so only the following need to be changed: Next the correct Baud rate for an 8MHz clock is set using the Baud Rate Generation Register and changing the Clock Divider bits to 13 (see page 569):We can now activate the transmit function using the TX ENable bits of the USART Control Register (page 626):The USART is now set up and ready to transmit data by writing a character to the TX CHARacter bits of the Transmitter Holding Register. An additional while loop can be used for when transmitting many characters to delay until the Transmitter Holding Register is again empty (page 635). The completed program now looks like this: [Comment – 04/18 it seems the added while loop does not actually cause any delay once complied!]
unsignedcharCalibrationValue;CalibrationValue=AVR32_SCIF.RCCR8.calib; // Store 8MHz Calibration valueAVR32_SCIF.unlock=((AVR32_SCIF_UNLOCK_KEY_VALUE<<AVR32_SCIF_UNLOCK_KEY_OFFSET)|AVR32_SCIF_RCCR8); // Unlock the register to allow a change to be made
AVR32_SCIF.rccr8=((1<<AVR32_SCIF_RCCR8_RCOSC8_EN_OFFSET)|CalibrationValue);// Enable 8MHZ clock with original calibration
AVR32_PM.unlock=((AVR32_PM_UNLOCK_KEY_VALUE<<AVR32_PM_UNLOCK_KEY_OFFSET)|AVR32_PM_MCCTRL_MCSEL_OFFSET);// Unlock the PM register to allow a change of clock source
AVR32_PM.mcctrl=AVR32_PM_MCCTRL_MCSEL_RCOSC8;// Select internal 8MHz main clock
AVR32_GPIO.port[2].gperc=1<<16;// Set to peripheral mode (clear bit)
AVR32_USART0.THR.txchr=’!’;// Send ! characterwhile( !(AVR32_USART0.CSR.usart_mode.txrdy&1) ){;// wait here until buffer empties}
#include<avr32/io.h>voidInitialise_USART(void);unsignedinti;intmain(void){Initialise_USART();while(1){AVR32_USART0.THR.txchr=’!’;// Send ! characterwhile(!(AVR32_USART0.CSR.usart_mode.txrdy&0b1)){;// wait here until buffer empties}i=0;while(i<90000){i=i+1;}}}//=========================================================================voidInitialise_USART(void){// Initialise the USART for serial comms// For 384OO baud CD is 13 @ 8MHz 20 @12 MHz// TX is on USART0 PC16 – GPIO 80 = port 2 pin 16unsignedcharCalibrationValue;// Initialise 8MHZ system clockCalibrationValue=AVR32_SCIF.RCCR8.calib;// Store 8MHz Calibration valueAVR32_SCIF.unlock=((AVR32_SCIF_UNLOCK_KEY_VALUE<<AVR32_SCIF_UNLOCK_KEY_OFFSET)|AVR32_SCIF_RCCR8);// Unlock the register to allow a change to be madeAVR32_SCIF.rccr8=((1<<AVR32_SCIF_RCCR8_RCOSC8_EN_OFFSET)|CalibrationValue);// Enable 8MHZ clock with original calibrationAVR32_PM.unlock=((AVR32_PM_UNLOCK_KEY_VALUE<<AVR32_PM_UNLOCK_KEY_OFFSET)|AVR32_PM_MCCTRL_MCSEL_OFFSET);// Unlock the PM register to allow a change of clock sourceAVR32_PM.mcctrl=AVR32_PM_MCCTRL_MCSEL_RCOSC8;// Select internal 8MHz main clock (see p 60)// Set up the GPIO registers – Peripheral interface D withAVR32_GPIO.port[2].gperc=1<<16;// Set to peripheral mode (clear bit)// Set PMR2,PMR1,PMR0 to 011 -D for the mux GPIO register see page 468AVR32_GPIO.port[2].pmr0s=1<<16;// SetAVR32_GPIO.port[2].pmr1s=1<<16;// SetAVR32_GPIO.port[2].pmr2c=1<<16;// Clear// Program USART0 (all bits set to 0 on reset)AVR32_USART0.MR.usart_mode.par=0b100;// No ParityAVR32_USART0.MR.usart_mode.chrl=0b11;// 8 Data BitsAVR32_USART0.BRGR.cd=13;// Set Baud Rate to 38400 @ 8MHzAVR32_USART0.CR.usart_mode.txen=1;// Activate transmitter}//=========================================================================