gets for second UART port

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

Hello!
I try to realize a gets-and puts-function for the USART1 on the ATMega162
with Codevision AVR.
I couldn't find the definitions for gets and puts for USART0.
Are there any header-files or other applications which uses these functions?
I haven't got much experience in C.
Thanxxx for your help.

Greetz
Woohoo

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

In CodeVision, all the standard I/O functions invoke putchar() & getchar() to put/get a byte.

In a dual-UART AVR, there are putchar0()/getchar0(), and putchar1()/getchar1().

If you want to switch the output of gets() to the UART1 primitives (it uses the UART0 primitives by default), then you will need to specify your own primitives to use.

I haven't used that mechanism. Search the Help for "alternate", and search http://groups.yahoo.com/group/co... for "putchar1".

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

You could use something like

int putchar(char c){
if(usinguart1){
 putchar1(c);
}else{
 putchar0(c);
}

Imagecraft compiler user

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

you could use the following functions for putchar and getchar:

void putchar(char c)
{
switch (stream_id)
{
case 0: // the output will be directed to USART0
while ((UCSR0A & 0b00100000)==0);
UDR0=c;
break;

case 1: // the output will be directed to USART1
while ((UCSR1A & 0b00100000)==0);
UDR1=c;
break;
};
}

char getchar(void)
{

switch (stream_id)
{
case 0: // the output will be directed to USART0
while ((UCSR0A & 0b10000000)==0);
return(UDR0);
break;

case 1: // the output will be directed to USART1
while ((UCSR1A & 0b10000000)==0);
return(UDR1);
break;
};
}

be sure to use the preprocessor directives in the beginning of your program so that the compiler knows to use your new putchar functions instead of the usual ones:

#define _ALTERNATE_PUTCHAR_
#define _ALTERNATE_GETCHAR_

then all you have to do is define a global variable stream_id and use it like this:
// define this globally
bit stream_id;

// do this inside your main or another function
stream_id = 0;
printf("test uart 0");
stream_id = 1;
printf("test uart 1");

That should work (no promises).

- Tal

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

Hi!
There are already functions putchar1 and getchar1 for the USART1 in Codevision.
But I need a gets1 and puts1 function so that it is possible to receive
and transmit a string (char[16]) with UART1.
I found something like this:

void puts1(char *str)
{
while (*str)
{ /* until *str != '\0' */
putchar1(*str);
str++;
}
}
Is it also possible for gets1???
I tried, but it didn't work...

Thanxxx

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

What did you try when you rolled your own gets1()?

Remember that gets() should be looking for a EOF or newline sequence to indicate the end of the string, as opposed to the NUL character used by puts().

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

Download the imagecraft compiler. They include the c source for the runtime library. gets is a 2 or 3 line function... should be in any c book....

Imagecraft compiler user

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

we checked your code for puts1.what is the code for gets1()?