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