November 23, 2024, 07:38:25 AM

MOD-IO USART

Started by vikingaca, October 02, 2014, 03:11:26 PM

Previous topic - Next topic

vikingaca

Hello everyone,
After many tries without any satisfactory result, I decided to ask for help once more:-( Following the instructions from many sites and pdfs, I wrote this little code which should send one simple char over USART. Unfortunately, I am getting only null chars. Setting is 9600, 8-1-N, without any interrupt, and just sending from MOD-IO. I found that Rx and Tx pins can be found on UEXT terminal on pins 3 and 4, respectively. Code is here:

#include <avr/io.h>
#include <util/delay.h>
#include <inttypes.h>

/********************************************************/
/*   Const Definition            */
/********************************************************/
#define int ubrr 11
/*Fosc is 1843200, baud is 9600. According to formula
  ubrr= fosc/16/baud-1, result is 11. It is hardcoded lately
  in UBRRL*/

/********************************************************/
/*   Function declaration            */
/********************************************************/
void USART_init(void);
void USARTx(char data);

/********************************************************/
/*   Declaration of globalnih variables          */
/********************************************************/


/********************************************************/
/*   Main programm               */
/********************************************************/

void main()
{
   USART_init ();
   USARTx('P');
}

/********************************************************/
/*            Function definitions                  */
/********************************************************/
void USART_init (void)
{
   //UCSRC &= ~(1 << URSEL);
   /* Set baud rate prescaler*/
   UBRRH = 0x00;
   UBRRL = 0x0b;
   /* Enable receiver and transmitter */
   UCSRB = 0x18;
   /* Set frame format: 8data, 1stop bit, no parity */
   UCSRC =0x86;
}

void USARTx(char data)
{
   if ((UCSRA & 0x20)==0x20)
   UDR = data;
}

vikingaca

Come on guys...
Please help! In the meantime, I concluded that my fosc is 8MHz, so I already changed this in code. I'm now hitting the wall because I never get same chars as I'm trying to put on USART... e.g. 'T' somehow converts to 'e', and so on. I tried to change baudrate, number of stop bits, parity, nothing helps. Is there something special with this ATmega16 on MOD-IO about USART, is there some special fuse or register I didn't check.
Thanks  alot!

JohnS

You look to be asking about Atmel code but in UEXT forum.  There are Atmel places all over the net that must be more likely to help.

John

vikingaca

Hi JohnS,
Thanks for an answer. I taught that it is more likely specificity of MOD-IO than ATmega. Trust me, I spent days searching for something new at Atmel forums, but there is only variations on code I already presented earlier. I conclude finally that char that I get from UART is second complement of my char shifted right once... Isn't it strange? OK, if someone resolve this problem, please, post me something.
Thanks alot!

Lurch

Quotefosc/16/baud-1
I get 51, not 11. Strange.
Also, don't think this is clever. You don't even wait before sending the character. Could work, but ...
if it's not true it doesn't send anything.
  if ((UCSRA & 0x20)==0x20)
   UDR = data;

vikingaca

Hi Lurch, thanks for an answer!
I get 51 too, code is wrong in this part, as I said, I already change it. But I'm waiting before sending char... This part of code that you quote is equivalent to @while (!( UCSRA & (1<<UDRE)));@ because UDRE is 5th bit in UCSRA. I'll try this way, but, this is different syntax, semantic is the same...

vikingaca

Problem solved!
I forgot that there is no MAX3232 or MAX232 IC onboard. I used to work with Olimex LPC2148 which has one, so it was out of my scope... Two weeks are thrown, but some knowledge is gathered.
Thanks for help!