Forum Menu




 


Log in Problems?
New User? Sign Up!
AVR Freaks Forum Index

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
mutzel
PostPosted: Sep 06, 2011 - 04:27 PM
Rookie


Joined: Jun 02, 2009
Posts: 28


Hi,

I'm trying to use the UART interface of an XMEGA A4 to communicate with my PC. If I use the UART at a speed of 9600 BPS everything is working fine either at 2 MHz or 32 MHz processor speed. But if I set the UART speed to 19200 BPS than the data is only transfered correctly at a processor speed of 2 MHz but not at 32 MHz.

Here is the code I use for testing:
Code:

int main(void)
{
#ifdef RUN_AT_32MHZ
    CCP = CCP_IOREG_gc;   
    OSC.CTRL = OSC_RC32MEN_bm;
    while(!(OSC.STATUS & OSC_RC32MRDY_bm));
    CCP = CCP_IOREG_gc;
    CLK.CTRL = 0x01;

    /* those values were calculated using http://prototalk.net/forums/showthread.php?t=188 */
    uint16_t bsel = 3301;
    uint8_t bscale = -5;

#else

    uint16_t bsel = 353;
    uint8_t bscale = -6;

#endif

    PORTC.DIRSET = PIN3_bm;
    PORTC.DIRCLR = PIN2_bm;   
    USARTC0.CTRLC = USART_CHSIZE_8BIT_gc|USART_PMODE_DISABLED_gc;      
    USARTC0.BAUDCTRLA = (uint8_t)bsel;
    USARTC0.BAUDCTRLB =(bscale << USART_BSCALE0_bp) | (bsel  >> 8);   
    USARTC0.CTRLB |= USART_TXEN_bm;
    while(true)
    {
        while(!(USARTC0.STATUS & USART_DREIF_bm));
        USARTC0.DATA = 'x';       
    }
}


I don't think that it's a hardware fault, because it's working fine at 2 MHz.

What could cause the UART to not work anymore at 19200 BPS and 32 MHz?
 
 View user's profile Send private message  
Reply with quote Back to top
hzrnbgy
PostPosted: Sep 06, 2011 - 08:24 PM
Hangaround


Joined: Mar 26, 2011
Posts: 216
Location: Aloha state

somehow I doubt that the xmega cant run 19200 baud at 32MHz. I have mine running at 115200 with 32MHz

could be two things though
*baud prescaler
*actual clock speed
 
 View user's profile Send private message  
Reply with quote Back to top
DocJC
PostPosted: Sep 06, 2011 - 08:56 PM
Raving lunatic


Joined: Dec 11, 2007
Posts: 6980
Location: Cleveland, OH

In This Thread I post code for several baud rates straddling 19200. I, too, would expect it to work.

JC
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
DocJC
PostPosted: Sep 07, 2011 - 03:46 AM
Raving lunatic


Joined: Dec 11, 2007
Posts: 6980
Location: Cleveland, OH

Well, I haven't looked at this in detail in months, but I fired up my testbed and added 19200 Baud with 32 MHz clock and it worked fine. I tested 1200 and 115200, also, and they all worked.

I don't know C, so all I can say is a couple of recommendations.

Why are you using BScale?
For starters just use BScale = 0, (No scaling).

For a 32 MHz clock, BR 19200, BScale=0, Asych Mode, Normal Speed Clock, BSel = 103 dec. (103.1666 Dec)

Here are my two original Serial Port Test routines.
They configure the SP for polled operation, no interrupts.

I've omitted the R/W data to the USART subs.

Obviously it could all be calculated on the fly, but for initial testing it was easiest to just straight line code it.

Code:

Usartsetup:
   'Set Up XMega PortE USART 1 for 9600, N,8,1
   'System Clock is assumed 32 MHz. 
   'No Handshaking is used.
   'Micro's GLOBAL Interrupts should
   'be OFF while doing setup.
   '
   'Initialization:
   'First set the TxD pin value high 
   '(Not sure exactly why, but must make it an output)
   'Configure Portd.7 As Output
   Porte_pin7ctrl = &B00000000   'USART Output   PIN, PortD.7, Control, Totem Pole
   Porte_dir.7 = 1    'Output for USART
   Porte_out.7 = 1    'Set it high

   'Set up USART CTRLA, Control Register A, whcih controls USART Interrupt Triggering
   'Set for NO Interrupts for initial testing:
   Usarte1_ctrla = 0      '0000 0000

   'Set up USART CTRLB, Control Register B,
   'which Enables the USART Receiver and Transmitter.
   Usarte1_ctrlb = 24     '0001 1000

   'Set up USART CTRLC, Control Register C,
   'which determines USART MODE.
   'Asychronous, No Parity, 1 Stop,  8 Data Bits   0000 0011  3d
   Usarte1_ctrlc = 3

   Gosub Usartbrsetup 'Set up the Baud Rate Divisor(s)

   '++++++++++++++++++++
   'Turn ON Global interrupts here,
   'if turned them off for this setup ..........
   '+++++++++++++++++++

   Return

Usartbrsetup:
   'USART Baud Rate Setup
   'For XMega's Clock = 32 MHz.
   'Mode is N, 8, 1  No Parity, 8 Data Bits, 1 Stop Bit
   'Uses Bscale = 0
   'Supports:         (Could add others)
   '1200, 9600, 19200, 38400, 57600, 115200
   'On Entry have: Xbr, the desired Baud Rate.
   'e.g. Xbr = 57600
   'This sets the BaudCtrlA and BaudCtrlB
   'Registers with the correct divisor(s).
   'Set up USART Baud CtrlA, Baud Rate Control Register A
   '8 Least Sig bits of 12 bit divisor
   'Usarte1_baudctrla = 207    0000 1100 1111 as 12 bit
   'Set up USART Baud CtrlB, Baud Rate Control Register B
   '4 Most Sig bits of 12 bit divisor, and Bscale (0 for now).
   'Usarte1_baudctrlb = 0      0000 0000

   If Xbr = 1200 Then
      Usarte1_baudctrla = 130
      Usarte1_baudctrlb = 6
   Elseif Xbr = 9600 Then
      Usarte1_baudctrla = 207
      Usarte1_baudctrlb = 0
   Elseif Xbr = 19200 Then
      Usarte1_baudctrla = 103
      Usarte1_baudctrlb = 0
   Elseif Xbr = 38400 Then
      Usarte1_baudctrla = 51
      Usarte1_baudctrlb = 0
   Elseif Xbr = 57600 Then
      Usarte1_baudctrla = 34
      Usarte1_baudctrlb = 0
   Elseif Xbr = 115200 Then
      Usarte1_baudctrla = 16
      Usarte1_baudctrlb = 0
   Else
      'Error, unsupported Baudrate, set it for 9600
      Usarte1_baudctrla = 207
      Usarte1_baudctrlb = 0
   End If
   Return


JC
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
DocJC
PostPosted: Sep 07, 2011 - 04:02 AM
Raving lunatic


Joined: Dec 11, 2007
Posts: 6980
Location: Cleveland, OH

Note that as I re-read the data sheet, it states that writing to BAUDCTRLA will trigger an immediate update of the baud rate prescaler.

In the above routines I wrote to BaudCtrlA, and then to BaudCtrlB, and it appears that I should have done so in the opposite order, i.e. write to the B register, then the A register.

The above order worked, but the data sheet implies that one should write to the BaudCtrilA register LAST to initiate the change in the hardware.

JC
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
mutzel
PostPosted: Sep 07, 2011 - 08:19 AM
Rookie


Joined: Jun 02, 2009
Posts: 28


Thank you for all those replies!

I just figured out how to run my code on 32 MHz and 19200 BPS. It works if I set the XMEGAs VCC from 3.3 V to 2.9 V. I'm not that much an expert in electronics (the guy who does that for me is currently on vacation) and doesn't have any idea why this worked.

Can someone explain that behavior to me?
 
 View user's profile Send private message  
Reply with quote Back to top
indianajones11
PostPosted: Sep 07, 2011 - 08:27 AM
Raving lunatic


Joined: Nov 28, 2004
Posts: 3627
Location: San Diego, Ca

Lowering the Vcc is NOT the issue ! If anything you'd have to raise Vcc to get more speed for a CMOS chip. Do you have caps on all the power pins to gnd ? Xmega can run at 19,200 and the 2 BAUD numbers are correct.

_________________
1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1
 
 View user's profile Send private message  
Reply with quote Back to top
DocJC
PostPosted: Sep 07, 2011 - 12:41 PM
Raving lunatic


Joined: Dec 11, 2007
Posts: 6980
Location: Cleveland, OH

Agreed, something else was changed, also.

Changing the V+ isn't what made the system suddenly work.

Perhaps your terminal program was incorrectly configured the first time?

JC
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
mutzel
PostPosted: Sep 07, 2011 - 02:06 PM
Rookie


Joined: Jun 02, 2009
Posts: 28


No - nothing else changed, it's perfectly reproducible.

But I also found out that after a reset I will need to start at 3.3V and go down to 2.9V to make it work again.
 
 View user's profile Send private message  
Reply with quote Back to top
DocJC
PostPosted: Sep 07, 2011 - 03:00 PM
Raving lunatic


Joined: Dec 11, 2007
Posts: 6980
Location: Cleveland, OH

That is very odd.

Can you upload a photo of the board, and a schematic?

(Type a reply, then press the PREVIEW button to get to a box to Browse for the photo and schematic images, and the Add Addachment button.)

JC
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT + 1 Hour
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2006 The PNphpBB Group
Credits