Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

rfm12b initialization in codevisionavr

Status
Not open for further replies.
i've worked alot on this modules
now i recieve some garbage data on the reciver side
i've put 2 LEDs, one on the transmitter and one on the reciever side
when the data is sent, the LED on the transmitter is on and after the sending is finished, it is turned off
an LED on the reciever that's always ON,but is off when the data is being recieved
i think the code is fine
but garbage data is bothering,could it be from the modules
i mean could the rfm12b modules be broken?

- - - Updated - - -

i've got it working , but i recieve garbage data
what could be the possible reason?
 

the garbage data was a bad port initialization
now i got the data correctly
but sometimes i don't recieve anything while it is sent
here 's my code:
RECIEVER:
Code:
#include <delay.h>
#include <mega8.h>
#include <alcd.h>
#include <stdio.h> 
 #define SCK 2   // SPI clock
  #define SDO 1   // SPI Data output (RFM12B side)
  #define SDI 6   // SPI Data input (RFM12B side)
  #define CS  7   // SPI SS (chip select)
  #define NIRQ 5  // (PORTD)
  /* IO CONTROL */
  #define HI(x) PORTB |= (1<<(x))
  #define LO(x) PORTB &= ~(1<<(x))
  #define WAIT_NIRQ_LOW() while(PIND&(1<<NIRQ))
  /* LED */
  #define LED 0
  #define LED_OFF() PORTD &= ~(1<<LED)
  #define LED_ON() PORTD |= (1<<LED)

  void portInit() {
    HI(CS);
    HI(SDI);
    LO(SCK);
    DDRB = (1<<CS) | (1<<SDI) | (1<<SCK);
    DDRD = (1<<LED)|(1<<3);
    
  }


  unsigned int writeCmd(unsigned int cmd) {
    unsigned char i;
    unsigned int recv;
    recv = 0;
    LO(SCK);
    LO(CS);
    for(i=0; i<16; i++) {
      if(cmd&0x8000) HI(SDI); else LO(SDI);
      HI(SCK);
      recv<<=1;
      if( PINB&(1<<SDO) ) {
        recv|=0x0001;
      }
      LO(SCK);
      cmd<<=1;
    }
    HI(CS);
    return recv;
  }
  
void rfInit() {
    writeCmd(0x80D7); //EL,EF,868band,12.0pF
    writeCmd(0x8299); //er,!ebb,ET,ES,EX,!eb,!ew,DC (bug was here)
    writeCmd(0xA640); //freq select
    writeCmd(0xC647); //4.8kbps
    writeCmd(0x94A0); //VDI,FAST,134kHz,0dBm,-103dBm
    writeCmd(0xC2AC); //AL,!ml,DIG,DQD4
    writeCmd(0xCA81); //FIFO8,SYNC,!ff,DR (FIFO level = 8)
    writeCmd(0xCED4); //SYNC=2DD4;
    writeCmd(0xC483); //@PWR,NO RSTRIC,!st,!fi,OE,EN
    writeCmd(0x9850); //!mp,90kHz,MAX OUT
    writeCmd(0xCC17); //!OB1,!OB0, LPX,!ddy,DDIT,BW0
    writeCmd(0xE000); //NOT USED
    writeCmd(0xC800); //NOT USED
    writeCmd(0xC040); //1.66MHz,2.2V
  }

    
  unsigned char rfRecv() {
    unsigned int data;
    while(1) {
      data = writeCmd(0x0000);
      if ( (data&0x8000) ) {
        data = writeCmd(0xB000);
        return (data&0x00FF);
      }
    }  
  }

  void FIFOReset() {
    writeCmd(0xCA81);
    writeCmd(0xCA83);
  }


  void main(void) {
    unsigned char data=0;
    char s[16]; 
     lcd_init(20); 
    
    LED_ON();
    portInit();
    rfInit();
    FIFOReset();
    while(1) {
        LED_OFF();
        
          
          data = rfRecv();
     
      
        LED_ON();
        sprintf(s,"%d",data);
          data=0;
          lcd_clear();
          lcd_puts(s);
    }
    
  }
TRANSMITER:
Code:
#include <mega8.h>
#include <stdio.h>
#include <delay.h>
 #define SCK 2   // SPI clock
  #define SDO 1  // SPI Data output (RFM12B side)
  #define SDI 6   // SPI Data input (RFM12B side)
  #define CS  7   // SPI SS (chip select)
  #define NIRQ 5  // (PORTD)
  /* IO CONTROL */
  #define HI(x) PORTB |= (1<<(x))
  #define LO(x) PORTB &= ~(1<<(x))
  #define WAIT_NIRQ_LOW() while(PIND&(1<<NIRQ))
  /* LED */
  #define LED 2
  #define LED_OFF() PORTD &= ~(1<<LED)
  #define LED_ON() PORTD |= (1<<LED)
 int a;

  void portInit() {
    HI(CS);
    HI(SDI);
    LO(SCK);
    DDRB = (1<<CS) | (1<<SDI) | (1<<SCK);
    DDRD = (1<<LED)|0x01; 
  }


  unsigned int writeCmd(unsigned int cmd) {
    unsigned char i;
    unsigned int recv;
    recv = 0;
    LO(SCK);
    LO(CS);
    for(i=0; i<16; i++) {
      if(cmd&0x8000) HI(SDI); else LO(SDI);
      HI(SCK);
      recv<<=1;
      if( PINB&(1<<SDO) ) {
        recv|=0x0001;
      }
      LO(SCK);
      cmd<<=1;
    }
    HI(CS);
    return recv;
  }

  void rfInit() {
    writeCmd(0x80D7); //EL,EF,868band,12.0pF
    writeCmd(0x8239); //!er,!ebb,ET,ES,EX,!eb,!ew,DC
    writeCmd(0xA640); //frequency select
    writeCmd(0xC647); //4.8kbps
    writeCmd(0x94A0); //VDI,FAST,134kHz,0dBm,-103dBm
    writeCmd(0xC2AC); //AL,!ml,DIG,DQD4
    writeCmd(0xCA81); //FIFO8,SYNC,!ff,DR
    writeCmd(0xCED4); //SYNC=2DD4
    writeCmd(0xC483); //@PWR,NO RSTRIC,!st,!fi,OE,EN
    writeCmd(0x9850); //!mp,90kHz,MAX OUT
    writeCmd(0xCC17); //OB1, COB0, LPX, Iddy, CDDITپCBW0
    writeCmd(0xE000); //NOT USED
    writeCmd(0xC800); //NOT USED
    writeCmd(0xC040); //1.66MHz,2.2V
  }
  
  
  
  void rfSend(unsigned char data){
    WAIT_NIRQ_LOW();
    writeCmd(0xB800 + data);
  }
 
  void main() {
    volatile unsigned int i,j;
    #asm("cli");
    for(i=0;i<1000;i++)for(j=0;j<123;j++);
    UCSRA=0x00;
    UCSRB=0x18;
    UCSRC=0x86;
    UBRRH=0x00;
    UBRRL=0x19;
    portInit();
    rfInit();
    while(1){
      LED_ON();
       
       printf("Ready!");
       scanf("%d",&a);
      LED_OFF();
      writeCmd(0x0000);
      rfSend(0xAA); // PREAMBLE
      rfSend(0xAA);
      rfSend(0xAA);
      rfSend(0x2D); // SYNC
      rfSend(0xD4);
      
      for(i=0;i<10;i++)
      {
      rfSend(a);
      }
      
      rfSend(0xAA); // DUMMY BYTES
      rfSend(0xAA);
      rfSend(0xAA);
         // sophisticated delay
    }
  }

- - - Updated - - -

could it be from my codes?
or is it hardware problems??
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top