Hey guys,
I've done a little clean up of my code and I think I'm really close to getting a link up and running. Since I'm mediocre at coding I may have made a mistake when it came to passing the address of a variable to a function that assigned that address to a pointer. Here is what I have so far:
//This part is just me making comments to remind myself of how I'm going to hook up the actual circuit
// THIS CODE IS INTENDED FOR USE WITH A NRF24L01 USED AS A PRIMARY RECEIVER
#include <pic16f627a.h>
#include <xc.h>//this header is needed for delay macro
#define _XTAL_FREQ 4000000//this value is needed for the delay macro
//RB0=7seg_output
//RB1=7seg_output
//RB2=7seg_output
//RB3=7seg_output
//RB4=7seg_output
//RB5=7seg_output
//RB6=7seg_output
//RB7=IRQ - - this pin is active low when triggered by interrupt.
//RA0=CE
//RA1=CSN
//RA2=SCK
//RA3=MOSI
//don't use RA4-pull down
//RA5=MISO*
//RA6=so far N/A-pull down
//RA7=so far N/A-pull down
//--------------------------------------------------------------------------------------------------------
//THIS IS WHERE I CONFIGURE THE PIC16F627A's INTERNAL REGISTERS
//Configure device
#pragma config FOSC = INTOSCIO // Oscillator Selection bits (INTOSC oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF // RA5/MCLR/VPP Pin Function Select bit (RA5/MCLR/VPP pin function is digital input, MCLR internally tied to VDD)
#pragma config BOREN = OFF // Brown-out Detect Enable bit (BOD disabled)
#pragma config LVP = OFF // Low-Voltage Programming Enable bit (RB4/PGM pin has PGM function, low-voltage programming enabled)
#pragma config CPD = OFF // Data EE Memory Code Protection bit (Data memory code protection off)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
//--------------------------------------------------------------------------------------------------------
//internal NRF registers
int NRF_Config[8]={0,1,0,0,1,1,1,1};//forces data that is received to trigger the IRQ pin
int NRF_EN_AA[8]={0,0,0,0,0,0,0,0};
int NRF_EN_RXADDR[8]={0,0,0,0,0,0,0,1};//enable datapipe zero only (for now)
int NRF_RF_CH[8]={0,0,0,0,0,0,1,0};//set up RF channel as 2.402GHz (2.4GHz+2MHz)
int NRF_RF_Setup[8]={0,0,0,0,1,1,1,1};
//------------------------------------------------------------------------------
int PICin[8]={0,0,0,0,0,0,0,0};//used as buffer for data leaving NRF
int NRFin[8]={0,0,0,0,0,0,0,0};//used as buffer for data entering NRF
//--------------------------------------------------------------------------------------------------------
//The function prototype below named "int shiftout(int *)" is the only thing I really don't know if I coded right. The program compiles but I don't know if it will actually work yet. //My idea is have arrays of 1's and 0's. I will then send the address of the first element in the array to this function. This function will then take this address and assign it to a //temporary pointer (at the bottom of my code you will find the function itself). Will it work like this?
//function prototypes
int init_PIC(void);//configures PIC for operation
int init_NRF(void);//configures NRF for operation
int shiftout(int *);//loop that shifts data into NRF
//int shiftin(void);//loop that shifts data into uController
//--------------------------------------------------------------------------------------------------------
//It's important to note that this program is not finished, inside the while statement I will be assigning values to PORTB which will operate a 7 segment LED. I also have to //code in the interrupt
main(void)//main program
{
init_PIC();
init_NRF();
while(1)
{
}
}
//--------------------------------------------------------------------------------------------------------
int init_PIC(void)
{
OPTION_REGbits.T0CS=0;//internal instruction clock
OPTION_REGbits.PSA=0; //prescaler assigned to WDT so timer0 increments at a 1:1 ratio
INTCON=0x00;//sets bit 7(global interrupts) and bit 3(RB port interrupt on change bit) high
CMCON=0x07;//turns comparators off
PORTA=0x00;//clear PORTA prior to operation
PORTB=0x00;//clear PORTB prior to operation
TRISA=0x20;//0010 0000
TRISB=0x80;//set RB7 as input(interrupt on change) and rest as outputs
PORTAbits.RA1=1;//set CSN high
}
int init_NRF(void)
{
PORTAbits.RA1=0;//set CSN low, this alerts NRF that it's about to receive SPI data
__delay_us(500);//delay to make sure enough time elapses between CSN going low and NRF ready for data packet
//--------------------------------------------------------
//update buffer,shift in w_register instruction,shift in NRF config settings
//NRF_Config
NRFin[2]=1;
shiftout(&NRFin[0]);
shiftout(&NRF_Config[0]);
//--------------------------------------------------------
//update buffer,shift in w_register instruction,shift in nrf_en_aa settings
//NRF_EN_AA
//the below for loop is only used to set all the elements of the NRFin array to 0
for(int a=0;a<8;a++)
NRFin[a]=0;
NRFin[2]=1;
NRFin[7]=1;
shiftout(&NRFin[0]);
shiftout(&NRF_EN_AA[0]);
//--------------------------------------------------------
//update buffer, shift in w_register instruction, shift in nrf_en_rxaddr settings
//EN_RXADDR
for(int a=0;a<8;a++)
NRFin[a]=0;
NRFin[2]=1;
NRFin[6]=1;
shiftout(&NRFin[0]);
shiftout(&NRF_EN_RXADDR[0]);
//update buffer, shift in w_register instruction, shift in nrf_rf_ch settings
//NRF_RF_CH
for(int a=0;a<8;a++)
NRFin[a]=0;
NRFin[7]=1;
NRFin[4]=1;
shiftout(&NRFin[0]);
shiftout(&NRF_RF_CH[0]);
//update buffer, shift in w_register instruction, shift in nrf_setup settings
//NRF_RF_Setup
for(int a=0;a<8;a++)
NRFin[a]=0;
NRFin[4]=1;
NRFin[6]=1;
shiftout(&NRF_RF_Setup[0]);
}
//*buff is a temporary pointer that will be assigned the address to the first element in whatever array is passed to it, currently the only thing I'm not 100% about
int shiftout (int *buff)
{
for (int b=0; b<8; b++)
{
PORTAbits.RA2=0;//bring CLK low
PORTAbits.RA3=*buff++;//put data on the MOSI pin
PORTAbits.RA2=1;//bring CLK high to shift in data to NRF
}
}