SparkFun Forums 

Where electronics enthusiasts find answers.

All things pertaining to wireless and RF links
By xmtmega
#43001
Hi guys,

Can you please help me with a few problems below:

1. I am using mplab to program the microcontroller 18LF242 with a 3V button battery. Is it alright as I found out that sometimes the PIC doesnt run and the Vdd is actually between 2.6 - 2.9V ?

2. I dont receive any signals from the transmitter... ? I am not sure if is because of the programming codes? Below are the receiver and transmitter codes:

#include <p18f242.h>
#include <delays.h>
#define DATA PORTCbits.RC2
#define CLK1 PORTCbits.RC1
#define CS PORTBbits.RB1
#define CE PORTBbits.RB5
#define act_addr 0b10101010 //Addr
#define data_w 16
#define addr_w 8
#define crc 0b01 // CRC enable
#define mode 0b01001111 // Rx2En = 0, Shockburst Mode, 250kbps,16 MHz module crystal, 0db Power
#define rf_ch 0x00 // 2400 MHz frequency channel

#pragma config OSC = HS
#pragma config WDT = OFF
#pragma config LVP = OFF

void main()
{
TRISCbits.TRISC3 = 0; // ON PWR_UP
PORTCbits.RC3 = 1;

Init_RF();
while(1){
initInterrupt();
Rx_En();
}
}

void Init_RF (void)
{
char addr_w_crc = ( addr_w << 2 )| crc;
char config[15] = {0,data_w, 0, 0, 0, 0, 0, 0, 0, 0, 0, act_addr, addr_w_crc, mode, 1};

int i, j;

TRISCbits.TRISC2 = 0;
TRISCbits.TRISC1 = 0;
TRISBbits.TRISB1 = 0;
TRISBbits.TRISB5 = 0;
Delay10KTCYx(2); // 5 msec (tpd2sby)

CE = 0; // Set configuration mode
CS = 1; // Select configuration register
Delay10TCYx(4); // 10 usec (tcs2data)
for ( i = 0; i < 15; i++)
{
for ( j = 7; j >= 0; j--) // send the configuration
{
DATA = config >> j;
CLK(); // Clock in the data
}
}
CS = 0;
}

void Rx_En(void)
{
int i;
char Rx_2400MHz = (rf_ch << 1) | 1; // configuration word
TRISCbits.TRISC2 = 0; // Set Outputs
TRISCbits.TRISC1 = 0;
TRISBbits.TRISB1 = 0;
TRISBbits.TRISB5 = 0;

CE = 0; // Set configuration mode
CS = 1; // Select configuration register
Delay10TCYx(4); // 10 usec (tcs2data)

for ( i = 7; i >= 0; i--)
{
DATA = Rx_2400MHz >> i; // Shift out Rx address
CLK(); // Clock in the data
}

CS = 0; // shift the configuration word
TRISCbits.TRISC2 = 1; // Set Data as Input
Delay100TCYx(10); // 250 usec (tsettling)
initInterrupt(); // Allow DR1 to interrupt MCU
CE = 1; // Turn on ACTIVE RX Mode
Delay10TCYx(4); // 10 usec (tce2data)
}

void Receive (void)
{
int i;
payload = 0;
//CE = 0;
for ( i = 15; i >= 0; i--)
{
CLK1 = 1; // Clock in the data
Delay10TCYx(10); // 500 nsec (tsetup)
Delay10TCYx(10); // 500 nsec (tsetup)
payload |= DATA << i; // Shift in payload
CLK1 = 0;
Delay10TCYx(10); // 500 nsec (tsetup)
Delay10TCYx(10); // 500 nsec (tsetup)
}
}

//Initialisation of Interrupt subroutines
void initInterrupt (void) {
INTCONbits.INT0IE = 1; //enable INT1
RCONbits.IPEN = 1; //enable priority interrupts
INTCONbits.GIEH = 1; //enable all high priority interrupts
}

void HI_isr_handler (void);
#pragma code high_vector_section = 0x08
void high_vector (void){
_asm
GOTO HI_isr_handler
_endasm
}

#pragma code
#pragma interrupt HI_isr_handler
void HI_isr_handler (void){
if (INTCONbits.INT0IF ==1) {
Receive();
INTCONbits.INT0IF = 0;
}
}

void Off_interrupt (void){
INTCONbits.INT0IE = 0; //enable INT1
RCONbits.IPEN = 0; //enable priority interrupts
INTCONbits.GIEH = 0; //enable all high priority interrupts return;
INTCONbits.INT0IF = 0;
}

void CLK(void)
{
Delay1TCY(); // 500 nsec (tsetup)
Delay1TCY(); // 500 nsec (tsetup)
CLK1 = 1; // clock in the value
Delay1TCY(); // 500 nsec (tsetup)
Delay1TCY(); // 500 nsec (tsetup)
CLK1 = 0;
}
///////////Transmiter part//////////////////////
void main()
{
Init_RF();

while(1){
TRISCbits.TRISC3 = 0; // ON PWR_UP
PORTCbits.RC3 = 1;

Delay1KTCYx(12);
Transmit(170);
Delay10KTCYx(100); // 250 msec
}
} // End void main()

void Transmit (int tx_payload)
{
int i;
Tx_En();
for ( i = 7; i >= 0; i--)
{
DATA = act_addr >> i; // Shift out Rx address
CLK(); // Clock in the data
}
for ( i = 15; i >= 0; i--)
{
DATA = tx_payload >> i; // Shift out payload CLK(); // Clock in the data
}
CE = 0;
}

void Tx_En(void)
{
int i;
char Tx_2400MHz = (rf_ch << 1);
TRISCbits.TRISC2 = 0; //Set as outputs
TRISCbits.TRISC1 = 0;
TRISBbits.TRISB1 = 0;
TRISBbits.TRISB5 = 0;

CE = 0;
CS = 1;
Delay10TCYx(4); // 10 usec (tcs2data)
for ( i = 7; i >= 0; i--)
{
DATA = Tx_2400MHz >> i; // Shift out Rx address
CLK(); // Clock in the data
}
CS = 0; // shift the configuration word

Off_interrupt();// Disallow RD1 to interrupt MCU

Delay100TCYx(10); // 250 usec (tsettling)
CE = 1;
Delay10TCYx(4); // 10 usec (tce2data)
}
By jasonharper
#43002
At 2.6V, a 18LF242 is only rated to run at a maximum of 13.8 MHz (see figure 22-2 in the datasheet). Based on comments in your code, you seem to be trying to run it at 16 MHz. If that 2.6V was measured with a multimeter, then I imagine that the lowest voltage seen by the PIC (at moments of maximum current consumption) is actually somewhat lower than that, making your hardware even further out of spec.
By xmtmega
#43111
Thanks ! It works fine now, it was such a careless mistake.

But, I still having trouble to get any signal from the receiver.. Since I disable the CRC, there is no random data which is expected to be there.

Is that because of any mistakes from the code ?

Appreciate for your help.
By brennen
#43129
If you try to send a data packet do you get the TX_DS interrupt on the transmitter? If not, you're not going to be receiving anything at the receiver. Check out my site (listed in my signature below), as I have working code for the 18F252, and you could easily use it as a framework to get your chip running by changing a few of the #defines (the code was created for Microchip's C18 compiler). Otherwise you could just peruse the files to see how it's done and find what's going on in your code.