TODOPIC

Otros Microcontroladores / Dispositivos programables => Microcontroladores 8 bits => Mensaje iniciado por: genereisen en 26 de Enero de 2013, 14:39:52

Título: ayuda con atmel y modulo rf
Publicado por: genereisen en 26 de Enero de 2013, 14:39:52
Hola,
estoy intentando programar un modulo mrf24j40mb con un atmega8l para obtener el valor RSSI de la señal enviada, pero no lo consigo. A continuación dejo el código, creo que el problema esta en la asignación de canal libre o en el coordinador PAN. Muchas gracias por vuestro tiempo.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include <mega8.h>
#include <stdlib.h>

#include "WifiHeader.h"

int measurement_number=0;
char message_number=0;//count messages in one measurement
//unsigned int mask;
unsigned char byte;

Message *pointer,*current;//pointer to struct "Message" (equal to one measurement)

bit process_started=0;
bit wait=0;//for module B waiting
bit sleep_enabled=0;
bit start_comm=0;
bit no_response=0;
bit mem_full=0;
bit message_send=0;
bit retransmit=0;
bit end=0;
bit process_initiated=0;

unsigned char clock_timeout=0; //for time measurement
unsigned char clock_counter=CLOCK_FREQUENCY;
unsigned char frame_lenght;
unsigned int rx_adress;

// External Interrupt 0 service routine
interrupt [EXT_INT0] void ext_int0_isr(void)
{
    MCUCR=0x00;//disable sleep
    GICR&=0x3F;   //disable INT1 and INT2
    wake_wifi();
    GIFR=0xC0;
    MEM_FULL_LED=1;     //start pressed indication
    FINISH_LED=1;       //start pressed indication
    NO_RESPONSE_LED=1;   //start pressed indication, now 3 diodes, not all 4
    process_initiated=1; //start pressed indication
    start_comm=1;
    sleep_enabled=0;
    GIFR|=0x40;
}

// External Interrupt 1 service routine
interrupt [EXT_INT1] void ext_int1_isr(void)
{
    GICR&=0x3F;   //disable INT1 and INT2
    writeByte(0x39,0x04);//BBREG1,RXDECINV set to prevent recieving while reading
    byte=readByte(0x31);//INTSTAT, clear interrupt

    byte=readLongByte(0x300);
    frame_lenght=byte-HEADER_LENGHT-2;
   
    if(frame_lenght==1)
    {
       
            rx_adress=0x300+byte;
        byte=readLongByte(0x308);
        if(byte==0x66)
        {
            wait=0;//communication succesfull
            retransmit=0;
            TCCR1B=0x00;
            TCNT1H=0x00;
            TCNT1L=0x00;
            if(process_initiated)
            {
                MEM_FULL_LED=0;
                FINISH_LED=0;
                NO_RESPONSE_LED=0;
                process_initiated=0;
            }
            PROCESS_STARTED_LED=1;
            process_started=1;
        }
    }else if(!wait)  //if there was measurement start byte
    {
        pointer->lqi_B[message_number]=readLongByte(0x308);
        pointer->rssi_B[message_number]=readLongByte(0x309);
        message_send=0;
        TCCR1B=0x00;
        TCNT1H=0x00;
        TCNT1L=0x00;
    }
   
    GIFR=0x80;
    GICR|=0x80; //enable only INT1
   
    writeByte(0x39,0x00);//BBREG1,RXDECINV not set to allow recieving
       
    //Delay_ms(3);
}

// Timer 1 output compare A interrupt service routine
interrupt [TIM1_COMPA] void timer1_compa_isr(void)
{
    TCCR1B=0x00;
    TCNT1H=0x00;
    TCNT1L=0x00;
    clock_counter--;
    if(!clock_counter)
    { 
        if(process_initiated)
            {
                MEM_FULL_LED=0;
                FINISH_LED=0;
                NO_RESPONSE_LED=0;
                process_initiated=0;
            }
           
        clock_counter=CLOCK_FREQUENCY;
        if(clock_timeout)
        {
            clock_timeout--;
            if(!clock_timeout)
            {
                if(process_started)
                {
                    PROCESS_STARTED_LED=0;
                    process_started=0;
                }
                if(no_response)
                {
                    no_response=0;
                    NO_RESPONSE_LED=0;
                    wait=0;//clock_timeout is already 0
                    sleep_enabled=1;
                }
                if(mem_full)
                {
                    MEM_FULL_LED=0;
                    mem_full=0;
                    sleep_enabled=1;
                }
                if(end)
                {
                     FINISH_LED=0;
                     end=0;
                     sleep_enabled=1;
                }
                if(wait)
                {
                    NO_RESPONSE_LED=1;
                    no_response=1;
                    clock_timeout=6;//3sec
                    TCCR1B=0x05;
                }
                if(message_send)
                {
                  message_send=0;
                  NO_RESPONSE_LED=1;
                  no_response=1;
                  clock_timeout=6;//3sec
                  TCCR1B=0x05; 
                }
               
            }
            else 
            {
                TCCR1B=0x05;//nothing to report
            }
             
        }
        if(retransmit)
             {
                retransmit=0;
                TCCR1B=0x05;
             }
    }else TCCR1B=0x05;   
}

void destruct(Message *deleted)
{
    if(measurement_number>1)
    {
      while(deleted->previous)
      {
        current=deleted->previous;
        deleted=current;
        free(current->next);
      }
      free(deleted);
    }else if(measurement_number==1)
    {
        free(deleted);
    }
    measurement_number=0;
   
}

void main(void)
{
// Declare your local variables here

// Input/Output Ports initialization
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=Out
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=0
PORTB=0x00;
DDRB=0x01;

// Port C initialization
// Func6=In Func5=In Func4=In Func3=In Func2=Out Func1=In Func0=In
// State6=T State5=T State4=T State3=T State2=0 State1=T State0=T
PORTC=0x10;
DDRC=0x0E;

// Port D initialization
// Func7=In Func6=In Func5=Out Func4=Out Func3=Out Func2=In Func1=Out Func0=In
// State7=T State6=P State5=0 State4=0 State3=0 State2=P State1=0 State0=T
PORTD=0x4C;
DDRD=0x72;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
TCCR0=0x00;
TCNT0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 0.977 kHz
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: On
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x01;
OCR1AL=0xE8;//pola s
OCR1BH=0x00;
OCR1BL=0x00;

// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Low level
// INT1: Low level
//GIMSK|=0x40;
GICR&=0x3F;   //disable interrupt
MCUCR=0x00;
GIFR=0xC0;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x10;
// Global enable interrupts
#asm("sei")
 
   CSN=1;
   delaySPI();
   SCLK=0; //iddle when low
   
    MEM_FULL_LED=0;
    FINISH_LED=0;
    NO_RESPONSE_LED=0;
   
   
   wake_wifi();
   initialize();
    writeByte(0x39,0x04);//BBREG1,RXDECINV set to prevent recieving
    byte=readByte(0x31);//intstat read, clear interrupts
    //sleep_wifi();
   
    sleep_MCU();
   
   
   
   while(1)
   {
      int i;
       
      if(start_comm)
      {
        //start timer1
        TCCR1B=0x05;
        clock_timeout=20;
        while(clock_timeout && (!no_response) && (!process_started))
        {
            writeByte(0x39,0x04);//BBREG1,RXDECINV set to prevent recieving while transmitting
            SREG&=0x7F;//disable interrupts
            byte=readByte(0x31);//intstat read, clear interrupts
            writeByte(0x0D,0x25);//RXFLUSH //WAKE low polarity ,only data packet recieved
            SREG|=0x80;//enable interrupts
            GICR|=0x80;//enable interrupt 1, message recieved
            writeByte(0x39,0x00);//BBREG1,RXDECINV not set to allow recieving
            retransmit=1;
            wait=1;
            transmit(1,pointer);
                       
            while(retransmit);
           
        }
        start_comm=0;
     }
     
      if(sleep_enabled)
      {
        sleep_enabled=0;
        writeByte(0x39,0x04);//BBREG1,RXDECINV set to prevent recieving
        byte=readByte(0x31);//intstat read, clear interrupts
        //sleep_wifi();
        sleep_MCU();
      }
     
      if(process_started)
      {
        clock_timeout=6;//3sec
        TCCR1B=0x05;
        while(process_started);//3 sec delay
        pointer=(Message*)malloc(sizeof(Message));
       
        if(pointer)
        {
            measurement_number++;
            if(measurement_number==1)
            {
                pointer->previous=0;
            }
            else
            {
                pointer->previous=current;
                current->next=pointer;
            }
            message_number=0;
            for(i=0;i<10;i++)
            {
               pointer->n_message=measurement_number;
               pointer->lqi_A[message_number]=readLongByte(0x310);
               pointer->rssi_A[message_number]=readLongByte(0x311);
               SREG&=0x7F;//disable interrupts
                transmit(2,pointer);
               SREG|=0x80;//enable interrupts
               message_send=1;
               clock_timeout=6;
               TCCR1B=0x00;
                TCNT1H=0x00;
                TCNT1L=0x00;
                TCCR1B=0x05;
               message_number++;
               while(message_send);
               TCCR1B=0x00;
            }
            pointer->next=0;
            current=pointer;
            clock_timeout=6;
            TCCR1B=0x00;
            TCNT1H=0x00;
            TCNT1L=0x00;
            TCCR1B=0x05;
            FINISH_LED=1;
        }
        else
        {
           MEM_FULL_LED=1;
           mem_full=1;
           clock_counter=6;
           TCCR1B=0x00;
            TCNT1H=0x00;
            TCNT1L=0x00;
            TCCR1B=0x05;
            while(mem_full)
            {
                if(!DESTRUCT)
                {
                    mem_full=0;
                     
                    TCCR1B=0x00;
                    TCNT1H=0x00;
                    TCNT1L=0x00;
                    destruct(pointer); //clear memory
                    MEM_FULL_LED=0;
                    sleep_enabled=1;
                }
            }
        }
      }
   }
     
   
};
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++