Thursday, 12 July 2012

Sending Integer & Float numbers through Serial Communication AVR ATmega- UART Header


FOR TRANSMITTING INTEGER & FLOAT VALUES THORUGH AVR ATMEGA8 SERIAL PORT. This is additional codecs for USART.H of the previous tutorial (Serial Comm Tutorial Part 1)
This header includes functions for transmitting integer & float type variables. Also it features getting input as integer numbers through serial port.

DOWNLOAD HEADER FILE
               UART2_H

CODE:

/*------------------------------------------------------------------------------------SOURCE OF ELECDUDE.COM---------------------------------------------------------------------------------------------------------FOR TRANSMITTING INTEGER & FLOAT VALUES----------------------------------------------------------------------------*/                           //this is additional codecs for USART.H#ifndef USART2_H#define USART2_H /*-----------------------------------------------------------------------------------HEADER FILES to be included-------------------------------------------------------------------------------------*/#include <avr/io.h>#include <avr/interrupt.h>#include <avr/pgmspace.h>#include "USART.h"/*---------------------------------------------------------------------------------------constants and macros----------------------------------------------------------------------------------------*/#define BIT(x)                  (1 << (x))                      //Set a particular bit mask#define CHECKBIT(x,b)   (x&b)                   //Checks bit status#define SETBIT(x,b)     x|=b;                   //Sets the particular bit#define CLEARBIT(x,b)   x&=~b;          //Sets the particular bit#define TOGGLEBIT(x,b)  x^=b;           //Toggles the particular bit /*************************************************************************Function: uart_putint()Purpose:  transmit integer(0-999 only) to UARTInput:    integer/number (0-999 only)Returns:  none**************************************************************************/extern void uart_putint(unsigned int i){        unsigned char h,t,o;        o= (i%10) | 0x30;//ones        i/=10;        t= (i%10) | 0x30;//tens        i=i%100;        h=(i/10) | 0x30;//hundreds        uart_putc(h);        uart_putc(t);        uart_putc(o);} /*************************************************************************Function: uart_putfloat()Purpose:  transmit float value(xxx.xx only) to UARTInput:    float value(xxx.xx only)Returns:  none**************************************************************************/extern void uart_putfloat(const float f){        unsigned int v,p;        long int num;        num=f*1000;        p=num%1000;        num=num/1000;        v=num;        uart_putint(v);        uart_putc('.');        uart_putint(p);} /*************************************************************************Function: uart_getint()Purpose:  receive integer(0-999 only) from UARTOutput:   integer/number (0-999 only)Returns:  none Function: void int2char(unsigned int i,unsigned char *s)Purpose:  to convert intger number to charOutput:   charactersReturns:  none **************************************************************************/ Note: use the header file downloaded, instead of copy pasting from this page. all the functions coding's are not included in this page

Viewers  comments are encouraged. 
This helps us to much more.
Thank you!!!
Reactions:

0 comments:

Post a Comment

Search Here...