ATXMega256 USART problem

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi
Hope someone can help me, because Atmel's support team don't want to, they log your support request, but no one attend to your problem. I am using an ATMega256A3 device in a vehicle tracking project. I am using 3 of the hardware USARTs in the device. According to the data sheet, all USARTs have a TXD pin, a RXD pin and a XCK pin (external clock). I use the USARTs in asynchronous mode and the internal baud rate generator is used to generate the requested baud rate from the peripheral clock, which is generated from a 14.7456Mhz external crystal. I have used the XCK pin on USART C0 to switch a transistor, to control power to a GSM modem. The problem I have is that the XCK pin on USART C0 seems to be in use by some peripheral function on that I/O pin (pin17). I think that it is USART C0. I need to have full control over the I/O pin (Port C bit 1) It might be one of the other peripheral functions on that pin. I use Timer/Counter C0 also, but only to generate a 32mSec interrupt. All the Capture/Compare pins are disabled. I am debugging the firmware with an JTag ICE MK2 in PDI mode and when I configure PORT C bit 1 as an output and try to switch the I/O high, the JTag ICE get stuck on that instruction and then eventually the execution reset to the reset vector. Can anyone give me any advice on what I do wrong? I have used a number of Mega chips in the past, but this is my first project with an XMega chip, so I am still learning how to use it.

Regards

Pieter Roos

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Just a bit more info, I don't want the XCK pin to function with the USART, I need to use it as a general purpose digital output pin.

Regards

Pieter Roos

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What are USARTC0 and TCC0 register values in your code?

Ozhan KD
Knowledge is POWER

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

For USARTC0:

void set_baud_xuartc0(unsigned char val)
{
	#ifdef XF_147456mhz
	switch (val)  //0=1200, 1=2400, 2=4800, 3=9600, 4=19200, 5=38400, 6=57600, 7=115200 8=230400 (clock = 14.7456Mhz)
	{
		case(0x00): USARTC0_BAUDCTRLA = 0xFF; USARTC0_BAUDCTRLB = 0x02; break;
		case(0x01): USARTC0_BAUDCTRLA = 0x7F; USARTC0_BAUDCTRLB = 0x01; break;
		case(0x02): USARTC0_BAUDCTRLA = 0xBF; USARTC0_BAUDCTRLB = 0x00; break;
		case(0x03): USARTC0_BAUDCTRLA = 0x5F; USARTC0_BAUDCTRLB = 0x00; break;
		case(0x04): USARTC0_BAUDCTRLA = 0x2F; USARTC0_BAUDCTRLB = 0x00; break;
		case(0x05): USARTC0_BAUDCTRLA = 0x17; USARTC0_BAUDCTRLB = 0x00; break;
		case(0x06): USARTC0_BAUDCTRLA = 0x0F; USARTC0_BAUDCTRLB = 0x00; break;
		case(0x07): USARTC0_BAUDCTRLA = 0x07; USARTC0_BAUDCTRLB = 0x00; break;
		case(0x08): USARTC0_BAUDCTRLA = 0x03; USARTC0_BAUDCTRLB = 0x00; break;
	}
	#endif
	#ifdef XF_73728mhz
	switch (val)  //0=1200, 1=2400, 2=4800, 3=9600, 4=19200, 5=38400, 6=57600, 7=115200 8=230400 (clock = 7.3728Mhz)
	{
		case(0x00): USARTC0_BAUDCTRLA = 0x7F; USARTC0_BAUDCTRLB = 0x01; break;
		case(0x01): USARTC0_BAUDCTRLA = 0xBF; USARTC0_BAUDCTRLB = 0x00; break;
		case(0x02): USARTC0_BAUDCTRLA = 0x5F; USARTC0_BAUDCTRLB = 0x00; break;
		case(0x03): USARTC0_BAUDCTRLA = 0x2F; USARTC0_BAUDCTRLB = 0x00; break;
		case(0x04): USARTC0_BAUDCTRLA = 0x17; USARTC0_BAUDCTRLB = 0x00; break;
		case(0x05): USARTC0_BAUDCTRLA = 0x0B; USARTC0_BAUDCTRLB = 0x00; break;
		case(0x06): USARTC0_BAUDCTRLA = 0x07; USARTC0_BAUDCTRLB = 0x00; break;
		case(0x07): USARTC0_BAUDCTRLA = 0x03; USARTC0_BAUDCTRLB = 0x00; break;
		case(0x08): USARTC0_BAUDCTRLA = 0x01; USARTC0_BAUDCTRLB = 0x00; break;
	}
	#endif
}

void setup_xuartc0(unsigned char baud, unsigned char size)  //buad = 0x00 - 0x08, size = 0x07 or 0x08
{
	cli();  //disable global interrupt
	bitset(PORTC_OUT, 0x03);  //make tx high
	bitset(PORTC_DIR, 0x03);  //make tx output
	set_baud_xuartc0(baud);  //set baud rate 
	if (size == 0x07)
	{
		USARTC0_CTRLC = 0x02;  //set char size to 7 bit
	}
	else
	{
		USARTC0_CTRLC = 0x03;  //set char size to 8 bit
	}
	USARTC0_CTRLA = 0x00;  //disable all interrupts
	USARTC0_CTRLB = 0x00; //rx disable, tx disable
	sei();  //enable global interrupt
}

void enable_tx_xuartc0(void)
{
	bitset(USARTC0_CTRLB, 0x03);  //enable uart tx
}

void enable_rx_xuartc0(void)
{
	bitclr(PORTC_DIR, 0x02);  //make io an input
	bitclr(PORTC_OUT, 0x02);  //disable internal pull up resistor
	bitset(USARTC0_CTRLB, 0x04);  //enable uart rx
}

And for TCC0:
void setup_TCC0(void)
{
	cli();
	TCC0_CTRLA = 0x04; //clock = clk/8 = 0.035sec @ 14.7456Mhz
	TCC0_CTRLB = 0x00;  //normal counter mode interrupt on top (overflow)
	TCC0_CTRLC = 0x00; 
	TCC0_CTRLD = 0x00;
	TCC0_CTRLE = 0x00;
	TCC0_INTCTRLA = 0x03;  //error int disabled, overflow int = high
	TCC0_INTCTRLB = 0x00;  //disable all capture compare interrupts
	TCC0_CTRLFCLR = 0xFF;  //set counter to count up
	TCC0_PER = timerval;  //set top value
	TCC0_PERBUF = timerval;
	bitset(TCC0_INTFLAGS, 0x00);  //clear overflow int flag
	bitset(TCC0_INTFLAGS, 0x07);  //clear Compare or Capture Channel x Interrupt Flag
	bitset(TCC0_INTFLAGS, 0x06);  //clear Compare or Capture Channel x Interrupt Flag
	bitset(TCC0_INTFLAGS, 0x05);  //clear Compare or Capture Channel x Interrupt Flag
	bitset(TCC0_INTFLAGS, 0x04);  //clear Compare or Capture Channel x Interrupt Flag
	TCC0_CNT = 0x0000;  //reset counter
	sei();
}

Sorry, I don't have the time now to learn how to use structures, so I am doing it the old fashion way.

I did some test on the other USARTS that I use and I don't have the same problem on their XCK pins. I was wandering if it might be a faulty I/O pin. I will try to get my second prototype PCB up and running and load the code in that XMega to see if the problem go away.

Pieter Roos