• 1

    ..

  • 2

    ...

  • 3

    ...

Showing posts with label SERIAL COMMUNICATION USING AVR ATMEGA8. Show all posts
Showing posts with label SERIAL COMMUNICATION USING AVR ATMEGA8. Show all posts

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!!!

Thursday, 24 May 2012

AVR SERIAL COMMUNICATION - EXAMPLE ATMEGA 8 TUTORIAL - USART HEADER FILE


Here is the code for serial communication using AVR ATmega 8 RISC controller.
The functions used in this tutorial are

  • uart_puts("zzz")       :-> sends a string of character from data memory (RAM)
  • uart_puts_P("xxxx"):-> sends a string of character from program memory (FLASH memory)
  • uart_putc('x')          :->  sends a single character

           This has functions to transmit data from program memory also.



  • Click to download project files & Proteus demo.Click to download project files & Proteus demo.
  •  

/*******************************************************************************
                        SERIAL COMMUNICATION ATM8
   
    This source code introduces you to the working of the USART Module
    available in ATMEGA 8 @8MHz system clock.
   
    This code does the task of preparing a menu driven user interface on
    hyperterminal using the USART Module.

    Here the USART Module is configured to operate in the Asychronous
    mode. With a Frame format of 8-bit Data with 1 stop bit and no parity
    bit the baud rate is fixed for 9600 bps.

***********************************ELECDUDE.COM*********************************/


#define F_CPU 8000000UL            //Define the MPU operating frequency

#include <avr/io.h>                //Header file for AVR device specific I/O Definitions.
#include <avr/interrupt.h>            //Header file for incorportaing interrupt handling faclity (not used here).
#include <util/delay.h>                //Header file for incorporating delay routines.
#include <avr/pgmspace.h>            //Header file for incorporating Program space string utilities.
#include <stdlib.h>                //ISO Standard library of C functions and macros.
#include "USART.h"                //Defines C functions for accessing USART Module.


#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

void WaitMs(unsigned int ms);
void welcome_screen(void);

int main()
{
    unsigned int ch;

    uart_init(9600);        //Initialise the USART Module with the given Baudrate and
                                                            //Frame format.   
    uart_puts_P("\r\n\t************************************************ ");
    uart_puts_P("\r\n\t             ATMEGA 8 DEVELOPMENT BOARD ");
    uart_puts_P("\r\n\t************************************************ ");
    uart_puts("\r\n\t\t  ");
    uart_puts_P("\rWelcome to ElecDude.  Press any to display.....");
    uart_puts("\r\r\r\r");   
   
    while(1)   //Enter into a uncoditional while loop..
        {
            ch=uart_getc();
            uart_puts("\n\n\t\t");
            uart_putc('<');
            uart_putc(ch);
            uart_putc('>');
            WaitMs(100);
        }

    return 0;
}


void WaitMs(unsigned int ms)    //Generate a delay of ms milli second.
{
    unsigned int i;

    for(i=0;i<=ms/10;i++)
    {
        _delay_ms(10);
    }
}








Search Here...