DPRG
DPRG List  



[DPRG] Sending RS232 to Atmel

Subject: [DPRG] Sending RS232 to Atmel
From: Aishah Anwar sycochatterbox at yahoo.com
Date: Tue Oct 11 07:24:22 CDT 2005

Hi  Kip,

Below is the usart coding i did.
Do i need an interrupt signal for my uart? Since
currently its just polling for data. Thank you.

Regards
Aishah

void init_usart(void) {

	UCSRA = 1<<TXC | 0<<U2X | 0<<MPCM;
	UCSRB = 0<<RXCIE | 0<<TXCIE | 0<<UDRIE | 1<<RXEN |
1<<TXEN | 0<<UCSZ2 
| 0<<TXB8;
	UCSRC = 1<<URSEL | 0<<UMSEL | 0<<UPM1 | 0<<UPM0 |
0<<USBS | 1<<UCSZ1 | 
1<<UCSZ0 
| 0<<UCPOL;
	
	UBRRH = (unsigned char) BAUD_RATE_DIVISOR >> 8;
	UBRRL = (unsigned char) BAUD_RATE_DIVISOR;
}

//Here is a simple function to send a single
character:

int usart_putc(char c) {

	if(c == '\n') usart_putc('\r');
	while(bit_is_clear(UCSRA, UDRE));
	UDR = c;

	return 0; // always works
}

//Here is a simple function to send a nul-terminated
string:

void usart_puts(char *s) {
	
	while(*s) {
		usart_putc(*s);
		s++;
	}
}

//Another (blocking) function to wait for a character
to arrive from the USART:

int usart_getc(void) {

	while(bit_is_clear(UCSRA, RXC)); // wait for it

	return UDR; // read character from USART data
register
}

// Check if data has been sent from the VB program.
// Returns 0 if data has been sent
int checkForSerialData(void)
{
	return bit_is_clear(UCSRA, RXC);	// Check if
something has been sent to the AVR serial port.
}



void USART_Init(unsigned int baud)
{
UBRRH = (unsigned char)(baud>>8);
UBRRL = (unsigned char)baud;
UCSRB = (1<<RXEN) | (1<<TXEN);
UCSRC = (1<<URSEL) | (1<<USBS) | (3<<UCSZ0);
}

void USART_Transmit(unsigned char data)
{
		while (!(UCSRA & (1<<UDRE)));
		UDR = data;
}

unsigned char USART_RECEIVE(void)
{
		while (!(UCSRA & (1<<RXC)));
		return UDR;
}

--- Kipton Moravec <kip at kdream.com> wrote:

> Show us the code where you initialize the UART. 
> 
> Kip
> 
> On Mon, 2005-10-10 at 20:19 -0700, Aishah Anwar
> wrote:
> > Hi,
> > I am using the Max233 to convert the rs232
> signals. 
> > 
> > Regards
> > Aishah
> > 
> > --- Kipton Moravec <kip at kdream.com> wrote:
> > 
> > > I know this may seem like a stupid question, but
> > > what is the chip you
> > > are using for translation from Atmel to RS-232
> > > levels? 
> > > 
> > > Sometimes people forget that the Atmel has a
> UART,
> > > but a RS-232 chip is
> > > needed to convert the signals to RS-232 levels. 
> > > 
> > > Kip
> > > 
> > > On Mon, 2005-10-10 at 17:47 -0700, Aishah Anwar
> > > wrote:
> > > > Hi,
> > > > I have a query. I have programmed my Atmel
> ATMEGA8
> > > > chip to be able to receive data from my GUI
> > > (Visual
> > > > Basic 6) using the UARTS. I am sending a
> character
> > > '1'
> > > > from the VB to my Atmel chip.
> > > > 
> > > > 
> > > > Right now any characters i send to the atmel
> chip
> > > from
> > > > the VB, it keeps going to default. Is it
> because
> > > the
> > > > Atmel chip is unable to know that a character
> is
> > > sent
> > > > from the VB? Below is just a small part of the
> > > program
> > > > of my ATMEL.
> > > > 
> > > > I would really appreciate some assistance.
> Kindly
> > > > assist? Thank you very much.
> > > > 
> > > > Regards
> > > > Aishah
> > > > while(1)
> > > > 	{
> > > > 
> > > > d = USART_RECEIVE();	// Wait for a character
> from
> > > the
> > > > VB program
> > > > switch (d) {
> > > > 
> > > > case '3':	// Check if VB sent the character 1
> > > > ~(_BV(0)|_BV(2)); // Turn Lamp off
> > > > 			
> > > > while (checkForSerialData() != 0) { 
> > > > delay_ms(300);
> > > > PORTC = (_BV(0)|_BV(2)); // Turn Lamp on
> > > > delay_ms(300);
> > > > PORTC = ~(_BV(0)|_BV(2));// Turn Lamp off
> > > > }
> > > > break;
> > > > 
> > > > case '2':	// Check if VB sent the character 2
> > > > (_BV(0)|_BV(2));// Turn Lamp off
> > > > pwmIsEnabled = 0;
> > > > break;
> > > > 
> > > > case '1':	// Check if VB sent the character 1
> > > > PORTC = ~(_BV(0)|_BV(2));// Turn Lamp off
> > > > pwmIsEnabled = 0;
> > > > break;
> > > > 
> > > > default:
> > > > PORTC = ~PORTC;
> > > > }
> > > > usart_putc('a');	// Tell the VB program that
> we
> > > have
> > > > recieved something
> > > > 
> > > > 
> > > > 	
> > > > 		
> > > > __________________________________ 
> > > > Yahoo! Mail - PC Magazine Editors' Choice 2005
> 
> > > > http://mail.yahoo.com
> > > >
> _______________________________________________
> > > > DPRGlist mailing list
> > > > DPRGlist at dprg.org
> > > > http://list.dprg.org/mailman/listinfo/dprglist
> > > > 
> > > -- 
> > > Kipton Moravec <kip at kdream.com>
> > > 
> > > 
> > 
> > 
> > 
> > 		
> > __________________________________ 
> > Yahoo! Music Unlimited 
> > Access over 1 million songs. Try it free.
> > http://music.yahoo.com/unlimited/
> > 
> -- 
> Kipton Moravec <kip at kdream.com>
> 
> 



	
		
__________________________________ 
Yahoo! Mail - PC Magazine Editors' Choice 2005 
http://mail.yahoo.com

More information about the DPRG mailing list