.

INCLUDES RFID interfacing with avr GPS interfacing with avr RF Module interfacing with avr Stepper Motor With AVR

Saturday, December 17, 2011

INTERFACE RF MODULE WITH ATMEGA8

Transmitter Side:-
#include <avr/io.h>
#include <util/delay.h>
#ifndef F_CPU
//define cpu clock speed if not defined
#define F_CPU 1000000
#endif //set desired baud rate
#define BAUDRATE 2400
//calculate UBRR value

#define UBRRVALUE
((F_CPU/(BAUDRATE*16UL))-1) //define receive parameters
#define SYNC 0XAA
// synchro signal
#define RADDR 0x44  
// address bits


void USART_Init(void)

{
 //Set baud rate
 UBRRL=(unsigned char)UBRRVALUE;
//low byte
 UBRRH=(unsigned char)(UBRRVALUE>>8);
//high byte
 //Set data frame format: asynchronous mode,no parity, 1 stop bit, 8 bit size
 UCSRC=(1<<URSEL)|(0<<UMSEL)|(0<<UPM1)|(0<<UPM0)|(0<<USBS)|(0<<UCSZ2)|(1<<UCSZ1)|(1<<UCSZ0);
 
//Enable Transmitter and Receiver and Interrupt on receive complete
 UCSRB=(1<<TXEN);
}

void USART_vSendByte(unsigned char Data)
{
 
// Wait if a byte is being transmitted
  while((UCSRA&(1<<UDRE)) == 0);
  // Transmit data
  UDR = Data;
}

void Send_Packet(unsigned char addr, unsigned char cmd)
{
 USART_vSendByte(SYNC);
//send synchro byte 
 USART_vSendByte(addr)
;//send receiver address
 USART_vSendByte(cmd);
//send increment command
 USART_vSendByte((addr+cmd));
//send checksum
}

void delayms(unsigned char t)
//delay in ms
{
unsigned char i;
for(i=0;i<t;i++)
_delay_ms(1);
}

int main(void)
{
unsigned char a;
USART_Init();
DDRB=0xff;
DDRC=0x00;
PORTB=0xff;

while(1)

 {
//endless transmission
 //send command to switch led ON

  a=PINC;

a=a&0x0f;
 Send_Packet(RADDR, a);
 }

 return 0;

}

Receiver's Side:-
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#ifndef F_CPU
//define cpu clock speed if not defined
#define F_CPU 1000000
#endif
//set desired baud rate
#define BAUDRATE 2400
//calculate UBRR value
#define UBRRVAL
((F_CPU/(BAUDRATE*16UL))-1)
//define receive parameters
#define SYNC 0XAA
// synchro signal
#define RADDR 0x44

void USART_Init(void)
{
 //Set baud rate
 UBRRL=(unsigned char)UBRRVAL;
//low byte
 UBRRH=(unsigned char)(UBRRVAL>>8);
//high byte
 //Set data frame format: asynchronous mode,no parity, 1 stop bit, 8 bit size
 UCSRC=(1<<URSEL)|(0<<UMSEL)|(0<<UPM1)|(0<<UPM0)|(0<<USBS)|(0<<UCSZ2)|(1<<UCSZ1)|(1<<UCSZ0); 
 //Enable Transmitter and Receiver and Interrupt on receive complete
 UCSRB=(1<<RXEN)|(1<<RXCIE);//|(1<<TXEN);
 //enable global interrupts
}

unsigned char USART_vReceiveByte(void)
{
 
// Wait until a byte has been received
  while((UCSRA&(1<<RXC)) == 0);
  // Return received data
  return UDR;
}

ISR(USART_RXC_vect)
{
 //define variables
 unsigned char raddress, data, chk;
//transmitter address
 
//receive destination address
 raddress=USART_vReceiveByte();
 //receive data
 data=USART_vReceiveByte();
 //receive checksum
 chk=USART_vReceiveByte();
 
//compare received checksum with calculated
 if(chk==(raddress+data))
//if match perform operations
 {
  //if transmitter address match
  if(raddress==RADDR)
  {
  if(data==0x0e)
  {
  PORTC=0x01;
  _delay_ms(500);
  PORTC=0x02;
  _delay_ms(500);
  PORTC=0x04;
  _delay_ms(500);
  PORTC=0x08;
  _delay_ms(500);
  PORTC=0x00;
  _delay_ms(500);
  PORTD=0xff;
  _delay_ms(1000);
  }
  else if(data==0x0d)
  {
  PORTC=0x0a;
  _delay_ms(500);
  PORTC=0x05;
  _delay_ms(500);
  PORTC=0x00;
  _delay_ms(500);
  PORTD=0xff;
  _delay_ms(1000);
  }
  else if(data==0x0b)
  {
  PORTC=0x08;
  _delay_ms(500);
  PORTC=0x04;
  _delay_ms(500);
  PORTC=0x02;
  _delay_ms(500);
  PORTC=0x01;
  _delay_ms(500);
  PORTC=0x00;
  _delay_ms(500);
  PORTD=0xff;
  _delay_ms(1000);
  }
  else if(data==0x07)
  {
  PORTC=0x05;
  _delay_ms(500);
  PORTC=0x0a;
  _delay_ms(500);
  PORTC=0x05;
  _delay_ms(500);
  PORTC=0x0a;
  _delay_ms(500);
  PORTC=0x00;
  _delay_ms(500);
  PORTD=0xff;
  _delay_ms(1000);
  }
  else if(data==0x05)
  {
  PORTC=0x05;
  }
  else if(data==0x06)
  {
  PORTC=0x06;
  }
  else if(data==0x04)
  {
  PORTC=0x07;
  }
  else if(data==0x08)
  {
  PORTC=0x08;
  }
  else if(data==0x09)
  {
  PORTC=0x09;
  }
  else if(data==0x0a)
  {
  PORTC=0x0a;
  }
  else if(data==0x03)
  {
  PORTC=0x0b;
  }
  else if(data==0x0c)
  {
  PORTC=0x0c;
  }
  else if(data==0x02)
  {
  PORTC=0x0d;
  }
  else if(data==0x01)
  {
  PORTC=0x0e;
  }
  else if(data==0x0f)
  {
  PORTC=0x0f;
  }
  else
  {
  //blink led as error
  PORTC=0x00;
//LEDs OFF
  _delay_ms(10);
  PORTC=0xff;
//LEDs ON 

  }

  }

 }

}

void Main_Init(void)
{
 PORTC=0x0f;//LED OFF
 DDRC=0X0f;
//define port C pin 0 as output;
 DDRD=0xff;
 //enable global interrupts
 sei();
}

int main(void)
{
 Main_Init();
 USART_Init();
 while(1)
 {
 }
 //nothing here interrupts are working
 return 0;
}

 CIRCUIT

No comments:

Post a Comment