I am a newbie in programming pic. Now, I am trying to do a project which can use RF module to control high and low interrupt, but it doesn't work on low prority which using RB interrupt. Here is my code
- CODE: SELECT_ALL_CODE
/ Program for Seven segment multiplexing using PIC18F4550 Microcontroller
#include <p18f4550.h>
#define _XTAL_FREQ 12000000
#pragma config PLLDIV = 5
#pragma config CPUDIV = OSC1_PLL2
#pragma config USBDIV = 2
#pragma config FOSC = HS
#pragma config FCMEN = OFF
#pragma config IESO = OFF
#pragma config PWRT = OFF
#pragma config BOR = OFF
#pragma config BORV = 3
#pragma config VREGEN = OFF
#pragma config WDT = OFF
#pragma config WDTPS = 32768
#pragma config CCP2MX = ON
#pragma config PBADEN = OFF
#pragma config LPT1OSC = OFF
#pragma config MCLRE = ON
#pragma config STVREN = ON
#pragma config LVP = OFF
#pragma config ICPRT = OFF
#pragma config XINST = OFF
#pragma config DEBUG = OFF
#pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF
#pragma config CPB = OFF
#pragma config CPD = OFF
#pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF
#pragma config WRTC = OFF
#pragma config WRTB = OFF
#pragma config WRTD = OFF
#pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF
#pragma config EBTRB = OFF
//============================================================================
// define
//============================================================================
//#define display PORTD
#define HEADER 0xaa
unsigned char num,no,portB_val;
//=============================================================================
// function prototype
//=============================================================================
void high_ISR(void);
void yourHighPriorityInterruptCode(void);
void low_ISR(void);
void yourLowPriorityInterruptCode(void);
void Interrupt_Init(void);
void PortBLoc (void);
unsigned char uart_rec(void);
unsigned char read_packet(void);
void Delay_ms ( int delay ); //delay function for 1 ms
void check();
/////////////////////////////////////////////////////////////////////////////////////////////////
/*Configure interrupt using asm*/
#pragma code HIGH_INTERRUPT_VECTOR = 0x000008
void high_ISR(void)
{
_asm
goto yourHighPriorityInterruptCode
_endasm
}
#pragma code
///////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////
/*Configure interrupt using asm*/
#pragma code LOW_INTERRUPT_VECTOR = 0x000018
void low_ISR(void)
{
_asm
goto yourLowPriorityInterruptCode
_endasm
}
#pragma code
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
/**/
#pragma interrupt yourHighPriorityInterruptCode
void yourHighPriorityInterruptCode(void)
{
if (PIR1bits.RCIF == 1)
{
RCSTAbits.CREN=1; //enable continuos receive
if(RCSTAbits.OERR==0)
{
no=read_packet(); //receive data if overrun error free
PortBLoc();
}
else
{
RCSTAbits.CREN=0; //if overrun error, disable continuos receive
PORTAbits.RA1 = 1;
LATAbits.LATA1 = 1;
}
}
}
#pragma code
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma interrupt yourLowPriorityInterruptCode
void yourLowPriorityInterruptCode(void)
{
if (INTCONbits.RBIF == 1)
{
portB_val = PORTB; //Read PORTD from PORTB
PORTD = portB_val;
Delay_ms(5000);
INTCONbits.RBIF = 0; //Clear the PORTB RBIF flag
}
}
#pragma code
/////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////
/*function on initiate intterrupt*/
void Interrupt_Init(void)
{
//Set up intterrupt
RCON = 0b10000000;
INTCONbits.GIE = 1; //Enable all high interrupt
INTCONbits.PEIE = 1; //enable all low interrupt
INTCONbits.RBIE = 1; //enable rb interrupt
INTCON2bits.RBIP = 0; //port b interrupt in low priority
INTCON2bits.RBPU = 1; //enable portb pulls up
INTCONbits.RBIF = 0; //Make sure that the PORTB interrupt is not already set
IPR1bits.RCIP = 1; //set EUSART Receive Interrupt Priority bit to high priority
PIE1bits.RCIE = 1; //enable EUSART Receive Interrupt Enable bit
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////
/*PORT B Bits Changes LOCATION*/
void PortBLoc(void)
{
if (no == 0)
{
PORTBbits.RB4 = 1;
PORTBbits.RB5 = 0;
PORTBbits.RB6 = 0;
PORTBbits.RB7 = 0;
}
else if (no == 1)
{
PORTBbits.RB4 = 0;
PORTBbits.RB5 = 1;
PORTBbits.RB6 = 0;
PORTBbits.RB7 = 0;
}
else if( no == 2)
{
PORTBbits.RB4 = 0;
PORTBbits.RB5 = 0;
PORTBbits.RB6 = 1;
PORTBbits.RB7 = 0;
}
else if (no == 3)
{
PORTBbits.RB4 = 0;
PORTBbits.RB5 = 0;
PORTBbits.RB6 = 0;
PORTBbits.RB7 = 1;
}
else if (no == 4)
{
PORTBbits.RB4 = 1;
PORTBbits.RB5 = 0;
PORTBbits.RB6 = 1;
PORTBbits.RB7 = 0;
}
else if (no == 5)
{
PORTBbits.RB4 = 1;
PORTBbits.RB5 = 0;
PORTBbits.RB6 = 0;
PORTBbits.RB7 = 1;
}
else if (no == 6)
{
PORTBbits.RB4 = 1;
PORTBbits.RB5 = 1;
PORTBbits.RB6 = 1;
PORTBbits.RB7 = 0;
}
else if (no == 7)
{
PORTBbits.RB4 = 1;
PORTBbits.RB5 = 1;
PORTBbits.RB6 = 0;
PORTBbits.RB7 = 1;
}
else if (no == 8)
{
PORTBbits.RB4 = 1;
PORTBbits.RB5 = 0;
PORTBbits.RB6 = 1;
PORTBbits.RB7 = 1;
}
else if (no == 9)
{
PORTBbits.RB4 = 1;
PORTBbits.RB5 = 1;
PORTBbits.RB6 = 1;
PORTBbits.RB7 = 1;
}
}
void Delay_ms ( int delay )
{
int ms, i;
for ( ms = 0; ms < delay; ms ++ )
for ( i = 0; i < 235; i ++ );
}
//============================================================================
// main function
//============================================================================
void main(void)
{
//assign variable
//set I/O input output
TRISB = 0; //configure PORTB <4:7> as input
PORTB = 0;
TRISA = 0; //configure PORTA as output
TRISD = 0; //configure PORTD as output
PORTD = 0;
//setup USART
SPBRG = 153; //set boud rate to 1200bps for 12Mhz crystal
TXSTAbits.BRGH = 0; //baud rate low speed option
RCSTAbits.SPEN = 1; //enable serial port
RCSTAbits.RX9 = 0; //8-bit reception
RCSTAbits.CREN = 1; //enable continuous reception
Interrupt_Init();
while(1) //infinity loop //main loop
{
PORTA = 1;
LATAbits.LATA1 = 1;
}
}
//==================================================================================
// functions
//===================================================================================
unsigned char uart_rec(void) //receive uart value
{
unsigned char rec_data;
while(PIR1bits.RCIF==0); //wait for data
rec_data = RCREG;
return rec_data; //return the received data
}
unsigned char read_packet(void)
{
// Buffer for received byte.
unsigned char received_byte;
// Counter to indicate the current position of the received data packet.
static unsigned char counter = 0;
// Buffers for the data and checksum.
unsigned char data;
unsigned char checksum;
// We loop until the checksum is correct.
do {
// We will ignore the sync data and assume the header byte is the start of packet.
// Keep reading until the header byte is received.
while (uart_rec() != HEADER);
// The following byte shoulde be the data byte.
data = uart_rec();
// Then the last byte is the checksum.
checksum = uart_rec();
} while (checksum != (unsigned char)(HEADER + data));
// If the checksum is correct, return the data.
return data;
}
I really dun know wad to do, the port D just won't be light up.
Could anyone help me to solve this problem?