How buffer UART?

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

Dear Friends

I'm searching "simply working code sample" for buffer UART on ATMEGA16.

Can you help me?

Thanks

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

Look in AVRlib for a general purpose buffer. Also there is one in the tutorial section by Dean.

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

Sure, I have a copy of it in Forth, or is it BASIC you are after? ASM? C? If C which compiler do you have, GCC,IAR,ICC or CVAVR? :?

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
//file rxintuart.c
//rx int on uart
//Oct 10 04 bob g initial edit
//Sept 9 07 Bob G recompile with v7.13

#include 
#include 
#include 
#include 

//------------------------
#define BUFSIZ 64
char rxbuf[BUFSIZ];
char rxindx,rxondx;

//-----------------------
void port_init(void){
  PORTA = 0xFF;
  DDRA  = 0xFF; 
 
  PORTB = 0xFF; 
  DDRB  = 0xFF; 
	 
  PORTC = 0xFF; 
  DDRC  = 0xFF; 
 
  PORTD = 0xFF; 
  DDRD  = 0xFF; 
}

//---------------------
//UART0 initialisation
// desired baud rate: 38400
// actual: baud rate:38462 (0.2%)
// char size: 8 bit
// parity: Disabled
void uart0_init(void){
  UCSRB = 0x00; //disable while setting baud rate
  UCSRA = 0x00;
  UCSRC = 0x06;
  UBRRL = 0x19; //set baud rate lo
  UBRRH = 0x00; //set baud rate hi
  UCSRB = 0x98;
}

//---------------------
void init_devices(void){
  //stop errant interrupts until set up
  CLI(); //disable all interrupts
  port_init();
  uart0_init();

  MCUCR = 0x00; //no ext mem
  SEI(); //re-enable interrupts
  //all peripherals are now initialised
}

//-------------------------
unsigned char kbhit(void){
//return nonzero if char waiting  intr version
unsigned char b;

  b=rxindx != rxondx;
  return b;
}

//-----------------------
int getchar(void){
//intr version... overrides version in library
char c;

  while(!kbhit()){}; //wait for char
  c=rxbuf[rxondx++]; //get char from rxbuf
  if(rxondx > BUFSIZ-1) rxondx=0;
  return c;  
}

//------------------------------
#pragma interrupt_handler uart_rx_isr:iv_USART_RX
void uart_rx_isr(void){
//uart has received a character in UDR  mega16version
char c;

  c=UDR;             //get char from usart
  rxbuf[rxindx++]=c; //put char in rxbuf
  if(rxindx > BUFSIZ-1) rxindx=0;  
}

//-----------------------
int putc(char c){
//put char to usart0... dont add lf after cr   mega16 version

  while((UCSRA & 0x20)==0){};	//0x20 is UDRE0. ==0 means data register full
  UDR=c; //send char
  return c;
}

//----------------------  
int putchar(char c)	{
//adds lf after cr

  if(c=='\n'){
    putc('\r');
  }  
  putc(c);
  return c;
}

//--------------
void echochars(void){
//echo chars out uart
char c;

  cprintf("echo chars  ctl-e to exit\n");
  while(1){
    if(kbhit()){
	    c=getchar();
	    if(c==0x05) return;
	    putc(c);
	  }
  }
}

//----------------
void menu(void){

  cprintf("cmds:\n");
  cprintf(" e echochars\n");
}

//----------------
void main(void){
//main program
char c;

  init_devices();
  cprintf("rxintuart Sept 9 07\n");
  menu();
  while(1){
	  c=getchar();
	  switch(c){
	    case 'e': echochars(); break;
	  }
  }  
}
//--------eof------------


Imagecraft compiler user

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

...of course that bit of code will not necessarily work unless the op uses ICC and he wants "simply working code sample"...but looks like he doesn't need it anymore anyway.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

Sorry friends

I have forget to say that I have AVR STUDIO 4 and WINAVR installed.
So the sample should work in AVR GCC project....

Thanks for have spend your time for me

Giuseppe

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

Think you could read the bit about interrupt handlers in gcc and change the one line of c that is different? (I'll let you find the line that needs to be changed... cant do everything)

Imagecraft compiler user