Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Looking for C code for ATmega8 USART

Status
Not open for further replies.

newman696

Newbie level 3
Joined
Mar 1, 2009
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,318
Hi, i make project PC thermometer, I use ATmega8, senzor LM35CZ, MAX 233..., I can`t find code in C for USART, can somebody help me? I know now if I must use internal oscilator or external krystal. PLease help...
 

atmega8 usart

search avrfreaks.net tutorials, There u can find how to use USART

Read it, try it out, comeup with questions if you have problem

Regards
Nandhu
 


usart atmega8

do you accept codevision codes ?

if yes tell me and i will upload a full example for u and dont forget to press helped me :D
 

    newman696

    Points: 2
    Helpful Answer Positive Rating
rs232 atmega8

yes i acept it ;) thank you very much
you can upload coplete code ??? :D wow
that could be fine

P.S. sorry i am Slovak, i cant so good speak English :|
 

uart atmega8

Hi, i need help. I use external crystal 7,3728 MHz for comunnication USART (atmega8) and for connection i need 2xcondenzator 22pF. Is it correct?

My calculation when i use crystal: 7.3728 MHz, 9600 baud rate gives UBRR = 47 and no error. its allright? If not, please help.
 

atmega8 uart example

Look out at atmega 8 datasheet you will find ubr setting for commonly used crystals with error percentage in detail.

MicroCon
 

atmega8 + rs232

newman696 said:
Hi, i need help. I use external crystal 7,3728 MHz for comunnication USART (atmega8) and for connection i need 2xcondenzator 22pF. Is it correct?

My calculation when i use crystal: 7.3728 MHz, 9600 baud rate gives UBRR = 47 and no error. its allright? If not, please help.

// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 9600
UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x2F;

corect Setting Value with 0% error
 

atmega8

I use AVR Studio and I need now set internal oscillator to 8MHz with baud rate 9600, value of UBRR=51. Pls help... I AVR STUDIO i set this on picture 1 and 2

here is my code: (only trying USART)
#include <avr/io.h>
#include <inttypes.h>

void USARTInit(uint16_t ubrr_value)
{
UBRRL = ubrr_value;
UBRRH = (ubrr_value>>8);
UCSRC=(1<<URSEL)|(1<<UCSZ0)|(1<<UCSZ1);
UCSRB=(1<<RXEN)|(1<<TXEN);
UCSRA=(1<<U2X);
}

char USARTReadChar()
{
while(!(UCSRA & (1<<RXC)))
{
}
return UDR;
}


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

int main(void)
{

int i = 0;
int a = 0;

USARTInit(51); //UBRR = 51

while(1)
{
USARTWriteChar('q');
for(i = 0; i < 300; i++)
{
a = 10;
}
}
}
 

atmega8 rs-232

Hai

Using internal 8MHz oscillator you will get error, try using 7.3728 external crystal.

the setting you show in your picture is for setting stk500 boards oscillator. It has nothing to do with your controller.

Selecting internal or external oscillator is done with fuse setting.

Selecting Baud rate has to be done in your code

MicroCon
www.crystalautomations.com
 

uart atmega8 code

dear all,

sorry for being late but here is the usart codevision project and the proteus simulation too
 

Re: ATmega8 RS232

Hey all.

I am trying to do something similar. I have an atMega168 and an STK500. I have a 7.3728 external crystal as well. I'm not sure if the STK500 has its own crystal (the instructions aren't clear).

I'm using sample code for an Interrupt controlled UART library.

In AVR studio 4 I assume i set the fuse for an external crystal between 3 and 8 but i'm not sure what other setting (there are several). Also, would i need to multiply down the frequency?

Very new to this. Very confused.

Basically, right now I am just trying to read a serial message that is 9600 8 bits, n, stop bit. Based on the byte read, i want to turn on the corresponding leds on the STK500.
 

ATmega8 RS232

Hey I need help in programing atmega serial interface...i don't know how...i get signal from icp1 pin and i have to transmit it to the serial interface...please help...it's urgent and I'm not good at programming.i'm using an 4 MHz crystal
 

Re: atmega8 rs-232

Dear microcon555,
I was working with the atmega8L micro-controller using the internal oscillator 1MHz and baud rate of 2500, but now I would like to use an external crystal 8MHz "or higher either up to 20MH" with baud rate 9600. I use the AVR Studio v4.18 to write my code and I have the Genius G840 programmer I use to burn the .hex file to the micro-controller using the G840 software. the only problem is that I don't know how to set the fuses with this prgrammer, I know the UBRRL = 0xFD and UBRRH = 0xD9 values from Engbedded AVR Fuse Calculator by selecting the Ext. Crystal/Resonator Medium Freq.; Start-up time 16K CK +64 ms; [CKSEL=1101 SUT=11] from the drop down menu. So can you help me set the fuses using the Genius G840 programmer.
/*--------Begining of Code-------------------------*/
#define FOSC 8000000// Clock Speed
#define BAUD 9600
#define MYUBRR ((FOSC/(16*BAUD))-1)

/* We must always include *.h in our protothreads code. */
#include <avr/io.h>
//#include <util/delay.h>
#include <avr/eeprom.h>

/*----------------------------------------------------------------
-----------------FUNCTIONS DECLARATION----------------------------
-----------------------------------------------------------------*/
void InitUART( unsigned char baudrate );
void TransmitByte( unsigned char data );
//void Init_Ports(void);
char isCharAvailable(void);
char receiveChar(void);
//void send_sds1(struct button B);
void PowerLed(void);

/*----------------------------------------------------------------
-----------------MAIN FUNCTION------------------------------------
-----------------------------------------------------------------*/
int count=0;
char string[225];
int loop;


int main()
{

InitUART(MYUBRR); //MYUBRR = 51 /* Set the baudrate to 9600 bps using a 8MHz internal oscillator */

TransmitByte('A');
TransmitByte('T');
PowerLed();

for(;;) /* Forever */
{
while(isCharAvailable() == 1)
{
// If a new character is received, get it
string[count] = receiveChar();
count++;
TransmitByte(string);
}
}
}

/*----------------------------------------------------------------
------------FUNCTIONS TO Initialize UART--------------------------
-----------------------------------------------------------------*/

void InitUART( unsigned char ubrr )//unsigned char baudrate )
{

/* Set baud rate */
UBRRH = (unsigned char)(ubrr>>8);
UBRRL = (unsigned char)ubrr;

UCSRB = (UCSRB | _BV(RXEN) | _BV(TXEN) ); /* Enable UART receiver and transmitter */
}

/*----------------------------------------------------------------
------------FUNCTIONS TO READ UART-------------------------------
-----------------------------------------------------------------*/
char receiveChar()
{
return UDR;/* Return the data */
}

char isCharAvailable()
{
// indicate a char has been received?
if ( (UCSRA & (0x80)) ) return 1;
else return 0;
}

/*----------------------------------------------------------------
------------FUNCTIONS TO WRITE UART-------------------------------
-----------------------------------------------------------------*/

void TransmitByte( unsigned char data )
{
while ( !(UCSRA & (_BV(UDRE))) ); /* Wait for empty transmit buffer */
UDR = data; /* Start transmittion */
}

/*----------------------------------------------------------------
------------FUNCTIONS TO Power On LED-----------------------------
-----------------------------------------------------------------*/
void PowerLed(void)
{
// enable PC1 as power LED
DDRC |= (1<<PC1);

// PIN1 PORTC set -> LED on
PORTC |= (1 << PC1);
}
/*--------End of code--------------------------*/
Another issue "for another project" is that I have another micro-controller atmega8L that I want to use it's internal oscillator of 8MHz with baud rate 9600 in the UART but when I program it and test it nothing is coming out of the TX and nothing is going into the RX even though I enabled the TX and RX in the code and I doubled checked the hyperterminal setting with baud rate 9600, databit 8, parity none, stop bit 1, and flow control none. The power LED works fine though, any idea what could be the problem:

/*--------Begining of Code-------------------------*/
#define FOSC 8000000// Clock Speed
#define BAUD 9600
#define MYUBRR ((FOSC/(16*BAUD))-1)

/* We must always include *.h in our protothreads code. */
#include <avr/io.h>
//#include <util/delay.h>
#include <avr/eeprom.h>

/*----------------------------------------------------------------
-----------------FUNCTIONS DECLARATION----------------------------
-----------------------------------------------------------------*/
void InitUART( unsigned char baudrate );
void TransmitByte( unsigned char data );
//void Init_Ports(void);
char isCharAvailable(void);
char receiveChar(void);
//void send_sds1(struct button B);
void PowerLed(void);

/*----------------------------------------------------------------
-----------------MAIN FUNCTION------------------------------------
-----------------------------------------------------------------*/
int count=0;
char string[225];
int loop;


int main()
{

InitUART(MYUBRR); //MYUBRR = 51 /* Set the baudrate to 9600 bps using a 8MHz internal oscillator */

TransmitByte('A');
TransmitByte('T');
PowerLed();

for(;;) /* Forever */
{
while(isCharAvailable() == 1)
{
// If a new character is received, get it
string[count] = receiveChar();
count++;
TransmitByte(string);
}
}
}

/*----------------------------------------------------------------
------------FUNCTIONS TO Initialize UART--------------------------
-----------------------------------------------------------------*/

void InitUART( unsigned char ubrr )//unsigned char baudrate )
{

/* Set baud rate */
UBRRH = (unsigned char)(ubrr>>8);
UBRRL = (unsigned char)ubrr;

UCSRB = (UCSRB | _BV(RXEN) | _BV(TXEN) ); /* Enable UART receiver and transmitter */
}

/*----------------------------------------------------------------
------------FUNCTIONS TO READ UART-------------------------------
-----------------------------------------------------------------*/
char receiveChar()
{
return UDR;/* Return the data */
}

char isCharAvailable()
{
// indicate a char has been received?
if ( (UCSRA & (0x80)) ) return 1;
else return 0;
}

/*----------------------------------------------------------------
------------FUNCTIONS TO WRITE UART-------------------------------
-----------------------------------------------------------------*/

void TransmitByte( unsigned char data )
{
while ( !(UCSRA & (_BV(UDRE))) ); /* Wait for empty transmit buffer */
UDR = data; /* Start transmittion */
}

/*----------------------------------------------------------------
------------FUNCTIONS TO Power On LED-----------------------------
-----------------------------------------------------------------*/
void PowerLed(void)
{
// enable PC1 as power LED
DDRC |= (1<<PC1);

// PIN1 PORTC set -> LED on
PORTC |= (1 << PC1);
}

/*--------End of code--------------------------*/


Thanks in advance for your help, looking forward for your reply :)
 

This is a segament of codes with Rectifier of USART --
/*
程序从UART 等待接收一字节,接收到数据后立即将数据又从UART 发送回去。
中断方式?
*/
/*
中断方式UART 测试程序
main.c
硬件:CA-M8
时钟:外部4MHz
芯艺 2004-12-01
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/signal.h>
/*
#define uchar unsigned char
#define uint unsigned int
*/
#define CK 4000000; //时钟4.0MHz
typedef unsigned char uchar, *puchar;
typedef unsigned int uint, *puint;
uchar g_bTxdPos=0; //发送定位计数器
uchar g_bTxdLen=0; //等待发送字节数
uchar g_bRxdPos=0; //接收定位计数器
uchar g_bRxdLen=0; //等待接收字节数
uchar g_aSendBuf[16]; //发送数据绶冲区
/*
uchar g_aRecvBuf[16];
*/
uchar g_aRecvBuf[16] =
{
0xa5, 0xa5, 0xa5, 0xa5,
0xa5, 0xa5, 0xa5, 0xa5,
0xa5, 0xa5, 0xa5, 0xa5,
0xa5, 0xa5, 0xa5, 0xa5,
}; //接收数据缓冲区
//接收中断
SIGNAL(SIG_UART_RECV)
{
/*
uchar c=UDR;
*/
if(g_bRxdLen>0)
{
/*
g_aRecvBuf[g_bRxdPos++]=c;
*/
g_bRxdLen--;
}
}
//发送中断
SIGNAL (SIG_UART_TRANS)
{
if(--g_bTxdLen>0)
UDR=g_aSendBuf[++g_bTxdPos];
}
//是否接收完成
uchar IsRecvComplete(void)
{
return g_bRxdLen==0;
}
//从发送缓冲区发送指定长度数据
/*
void SendToUart(uchar size)
{
g_bTxdPos=0;
g_bTxdLen=size;
UDR=g_aSendBuf[0];
while(g_bTxdLen>0);
}
*/
void Usart_Init(uint baud)
{
uint baudstatus;
//uart 初始化
//接收使能、发送使能、接收中断允许、发送中断允许
UCSRB=(1<<RXCIE)|(1<<TXCIE)|(1<<RXEN)|(1<<TXEN);
baudstatus=CK/(baud*16) -1;
UBRRH=(baudstatus>>8)&0xff;
UBRRL=baudstatus&0xff;
/*
UBRRL=25; // baud=9600 UBRR=CK/(baud*16) -1
*/
}
void SendToUart(uchar size)
{
unsigned char ctmp;
unsigned int j;
g_bTxdPos=0;
g_bTxdLen=size;
for(j=0; j<size; j++)
{
if((j+1)<size)
ctmp = g_aSendBuf[j+1];
else
{
ctmp = g_aSendBuf[j];
g_aSendBuf[j] += ctmp;
break;
}
ctmp += g_aSendBuf[j];
g_aSendBuf[j] = ctmp;
}
UDR=g_aSendBuf[0];
while(g_bTxdLen>0);
}
//接收指定长度数据到接收缓冲区
void RecvFromUart(uchar size,uchar bwait)
{
g_bRxdPos=0;
g_bRxdLen=size;
if(bwait)
while(g_bRxdLen>0);
}
int main( void )
{
uchar i;
Usart_Init(9600);
/*
//uart 初始化
//接收使能、发送使能、接收中断允许、发送中断允许
UCSRB=(1<<RXCIE)|(1<<TXCIE)|(1<<RXEN)|(1<<TXEN);
UBRRL=25; // baud=9600 UBRR=CK/(baud*16) -1
*/
sei();//总中断允许
while(1)
{
//异步接收16 字节数据
RecvFromUart(16,0);
//等待接收完成
while(!IsRecvComplete());
//将接收到的数据复制到发送缓冲区
for(i=0;i<16;i++)
g_aSendBuf=g_aRecvBuf;
//发送回接收到的数据
SendToUart(16);
}
}
//Hardware System: Use 2.048MHz Crystal Oscillator for Embedded System and 4.0MHz for upper system, the PC System!!
 

Check this code for atmega 8

int main()
{

init_UART(MYUBRR);
while(1)
{
USART_tx_string("shree4u G M\n\r");

rxdata = USART_Receive();

USART_Transmit(rxdata);

}
}

/* INIT USART (RS-232) */

void init_UART( int ubrr )
{
UBRRH = (unsigned char)(ubrr>>8);
UBRRL = (unsigned char)ubrr;
//UCSRB = (1 << TXEN); // Enable RX, TX & RX interrupt
UCSRB = (1<<RXEN)|(1<<TXEN);


UCSRC = (3 << UCSZ0); //asynchronous 8 N 1
}

/* SEND A STRING TO THE RS-232 */

void USART_tx_string( char *data )
{
while ((*data != '\0'))
{
//while (!(UCSRA & (1 <<UDRE)));
//UDR = *data;
//data++;
USART_Transmit(*data++);
}
}

unsigned char USART_Receive(void)
{
char data;
/* Wait for data to be received */
while ( !(UCSRA & (1<<RXC)) )
;
/* Get and return received data from buffer */
data = UDR;
return data;
}

void USART_Transmit( char data )
{
/* Wait for empty transmit buffer */
while ( !( UCSRA & (1<<UDRE)) )
;
/* Put data into buffer, sends the data */
UDR = data;
}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top