PIC16F877A coding

Status
Not open for further replies.

jameschia

New Member
I am now doing a project in which I using Timer 0 Interrupt and UART transmission at the same time.
At first, it works perfectly before I add in UART transmission configuration. Once I add in UART transmission configuration, the program will never run the Interrupt function. Do any one can tell me what is the problem?

//Configuration
//==================================================================
__CONFIG ( 0x3E31 );

#define BAUD 9600
#define _XTAL_FREQ 4000000
//#define DELAY 28
#define ALLOW_RECIEVE RA5
//#define TRAN_SONIC RB2
#define LED1 RB5
#define SONIC_IN RC2
#define RX_PIN RC7
#define TX_PIN RC6


void pic_init(void);

int counter;

static void interrupt isr(void)
{
if(TMR0IE && TMR0IF)
{
//TMR0 Overflow ISR
//Clear Flag
TMR0IF=0;


if (counter > 500)
{
LED1 = 1;
}
else
LED1 = 0;

counter++;

if (counter > 1000)
{
counter = 0;
}
}
}


void main()
{

pic_init(); //initialize PIC
TMR0IE = 1;

while(1);

}

void pic_init(void)
{
ADCON0=0b00000001; //Turn on A/D module
ADCON1=0b10000000; //Right Justify

TRISA=0b00001111;
TRISB=0b01001111;
TRISC=0b00000000;
TRISD=0b00000000;
TRISE=0b00000000;

PORTA=0b00000000;
PORTB=0b00000000;
PORTC=0b00000000;
PORTD=0b00000000;
PORTE=0b00000000;

OPTION_REG=0b00000000;
INTCON=0b11010000;
PIE1=0b00100000; //Enable RX interrupt

TMR0 = 0b11110000;

//initialize UART
SPBRG=(int)(_XTAL_FREQ/(64.0*BAUD)-1);
BRGH = 1; //baud rate high speed option
TXEN = 1; //enable transmission
TX9 = 0;
CREN = 1; //enable reception
SPEN = 1; //enable serial port
RX9 = 0;

TX_PIN = 1;
RCIE = 0;
GIE = 0;
PEIE = 0;
TMR0IE = 0;
}
 
you made a mistake here:
TMR0IE = 0;
Timer 0 interrupt enable bit is set as 0 (disable) instead of 1(enable)
this should be "TMR0IE = 1"
try that out
 
No. I test it on a breadboard. When I delete those UART configuration, I my LED blinking.
But, once I add UART configuration, my LED doesn't blink. Means it never run the Interrupt function.
 
Last edited:
i think ive found it
delete these guys:
GIE = 0;
PEIE = 0;
you have disabled global and peripheral interrupt
make them:
GIE = 1;
PEIE = 1;
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…