# M8 and M32 USART

**URL:** <https://community.sparkfun.com/t/m8-and-m32-usart/2252>\
**Category:** MicroMod\
**Tags:** avr-atmel\
**Created:** [July 18, 2006, 9:55pm UTC](https://community.sparkfun.com/t/m8-and-m32-usart/2252 "2006-07-18T21:55:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nathaniel\_Lewis](https://avatars.discourse-cdn.com/v4/letter/n/ec9cab/32.png) [@Nathaniel\_Lewis](https://community.sparkfun.com/u/Nathaniel_Lewis)\
**Post date:** [July 18, 2006, 9:55pm UTC](https://community.sparkfun.com/t/m8-and-m32-usart/2252/1 "2006-07-18T21:55:55Z")

</div>

:? Can some one give me some code to run the M8 and M32 USART in Win-AVR(ie. AVR-GCC c code for the avr)? I’ve looked at the datasheet but it won’t work.

Nathaniel

---

<div class="post-metadata">

**Author:** ![soda](https://avatars.discourse-cdn.com/v4/letter/s/cc9497/32.png) [@soda](https://community.sparkfun.com/u/soda)\
**Post date:** [March 21, 2007, 3:55pm UTC](https://community.sparkfun.com/t/m8-and-m32-usart/2252/2 "2007-03-21T15:55:42Z")

</div>

> Nathaniel Lewis:  
> :? Can some one give me some code to run the M8 and M32 USART in Win-AVR(ie. AVR-GCC c code for the avr)? I’ve looked at the datasheet but it won’t work.
> 
> Nathaniel

try to look to datasheet, there is a two registers sharing one address location (i dont remember their names). register you write to depends on state of one bit (msb or lsb). but as I wrote, you have to read datasheet 🙂 (furtunately there is this Ctrl+F thing 🙂 )

---

<div class="post-metadata">

**Author:** ![bibinjohn](https://avatars.discourse-cdn.com/v4/letter/b/f6c823/32.png) [@bibinjohn](https://community.sparkfun.com/u/bibinjohn)\
**Post date:** [December 8, 2007, 4:51pm UTC](https://community.sparkfun.com/t/m8-and-m32-usart/2252/3 "2007-12-08T16:51:56Z")

</div>

try this code

/\*----------------------------------------------------------------

-----------------MAIN FUNCTION------------------------------------

-----------------------------------------------------------------\*/

void main()

{

unsigned char a;

//lcd initializationsa

Init\_Ports();

InitUART( 25 ); /\* Set the baudrate to 2400 bps using a 1MHz crystal \*/

for(;😉 /\* Forever \*/

{

TransmitByte(0x99); /\* Echo the received character \*/

Receive\_PORT=ReceiveByte();

}

}

/\*----------------------------------------------------------------

------------FUNCTIONS TO Initialize UART--------------------------

-----------------------------------------------------------------\*/

void InitUART( unsigned char baudrate )

{

UBRRL = baudrate; /\* Set the baud rate \*/

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

}

/\*----------------------------------------------------------------

------------FUNCTIONS TO READ UART-------------------------------

-----------------------------------------------------------------\*/

unsigned char ReceiveByte( void )

{

while ( !(UCSRA & (\_BV(RXC))) ); /\* Wait for incomming data \*/

return UDR;/\* Return the data \*/

}

/\*----------------------------------------------------------------

------------FUNCTIONS TO WRITE UART-------------------------------

-----------------------------------------------------------------\*/

void TransmitByte( unsigned char data )

{

while ( !(UCSRA & (\_BV(UDRE))) ); /\* Wait for empty transmit buffer \*/

UDR = data; /\* Start transmittion \*/

}

void Init\_Ports(void)

{

Receive\_DDR=0xFF; //setting that port for output

Receive\_PORT=0XFF; //setting all bits high for starting

}

See the frequency you are using and set baud rates properly, otherwise you will get errors. See the troubleshooting tips

[http://avrmicrocontroller.googlepages.c … lport.html](http://avrmicrocontroller.googlepages.com/troubleshootingtipsforserialport.html)

Bibin John

[www.bibinjohn.tk](http://www.bibinjohn.tk)
