Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

PIC18f46J50 timer issue

Status
Not open for further replies.

Jinzpaul4u

Full Member level 4
Joined
Feb 1, 2012
Messages
231
Helped
59
Reputation
118
Reaction score
60
Trophy points
1,328
Location
India
Activity points
2,822
Hi there,

Greetings!

Do any of you know why my code ain't working?




#include <p18F46J50.h> // Always include the header file

//Defaut pragma code for 18F46J50
//#pragma config WDTEN = OFF //WDT disabled (enabled by SWDTEN bit)
#pragma config PLLDIV = 3 //Divide by 3 (12 MHz oscillator input)
#pragma config STVREN = ON //stack overflow/underflow reset enabled
//#pragma config XINST = OFF //Extended instruction set disabled
#pragma config CPUDIV = OSC1 //No CPU system clock divide
#pragma config CP0 = OFF //Program memory is not code-protected
//#pragma config OSC = HSPLL //HS oscillator, PLL enabled, HSPLL used by USB
#pragma config FCMEN = OFF //Fail-Safe Clock Monitor disabled
#pragma config IESO = OFF //Two-Speed Start-up disabled
#pragma config WDTPS = 32768 //1:32768
#pragma config DSWDTOSC = INTOSCREF //DSWDT uses INTOSC/INTRC as clock
//#pragma config RTCOSC = T1OSCREF //RTCC uses T1OSC/T1CKI as clock
//#pragma config DSBOREN = OFF //Zero-Power BOR disabled in Deep Sleep
//#pragma config DSWDTEN = OFF //Disabled
#pragma config DSWDTPS = 8192 //1:8,192 (8.5 seconds)
//#pragma config IOL1WAY = OFF //IOLOCK bit can be set and cleared
#pragma config MSSP7B_EN = MSK7 //7 Bit address masking
#pragma config WPFP = PAGE_1 //Write Protect Program Flash Page 0
#pragma config WPEND = PAGE_0 //Start protection at page 0
#pragma config WPCFG = OFF //Write/Erase last page protect Disabled
#pragma config WPDIS = OFF //WPFP[5:0], WPEND, and WPCFG bits ignored
#pragma config T1DIG = ON //Sec Osc clock source may be selected
#pragma config LPT1OSC = OFF //high power Timer1 mode


void ISR(void) ;



void main(void)
{

unsigned int timerValue = 1;
// Timer0 configuration
T0CONbits.TMR0ON = 0; // Stop the timer
T0CONbits.T08BIT = 0; // Run in 16-bit mode
T0CONbits.T0CS = 0; // Use system clock to increment timer
T0CONbits.PSA = 0; // A prescaler is assigned for Timer0
T0CONbits.T0PS2 = 0; // Use a 1:256 prescaler
T0CONbits.T0PS1 = 0;
T0CONbits.T0PS0 = 0;

TMR0L=timerValue ;
TMR0H=(timerValue>>8);

INTCONbits.GIEH = 1; // enable global interrupt
INTCONbits.TMR0IE = 1;// enable TIM0 interrupt
INTCONbits.TMR0IF = 0;// clear TIM0 interrupt flag

// LED configuration
TRISDbits.TRISD4 = 0; // Configure RD4, pin 2, as an output
LATDbits.LATD4=0; // pin is low

T0CONbits.TMR0ON = 1; // Start the timer

while(1){}; // Program loop

} // END MAIN


#pragma interruptlow LowISR
void LowISR(void)
{
}
////////////////////////////////////////
#pragma interruptlow HighISR
void HighISR(void)
{
ISR();
}
////////////////////////////////////////
#pragma code lowVector=0x18
void LowVector(void){_asm goto LowISR _endasm}
#pragma code highVector=0x08
void HighVector(void){_asm goto HighISR _endasm}
#pragma code

///////////////////////////////////////////////////////////////
void ISR(void)
{
T0CONbits.TMR0ON = 1; //Disable Timer0
INTCONbits.TMR0IF = 0; //Clear Timer 0 Interrupt Flag
LATDbits.LATD4 =~LATDbits.LATD4 ;// toggle the led
//write some code section here.
}




Any kind of feedback would be apperciable !

Thanks.
 

Change LATDbits.LATD4 = 1 ;// toggle the led

to

LATDbits.LATD4 = 0 ;// toggle the led

some delay

LATDbits.LATD4 =~LATDbits.LATD4 ;// toggle the led

some delay

and see if that works.
 
What error you are getting: Did you try debug the code?

Suggestions:

1. disable all interrupts before setting & enabling required interrupts and clearing flags
2. Enable Global Interrupts GIE/GIEH/GIEL only after all the required interrupts set and enabled (best putting just before while(1))
3. See the example sequence (for clarity purpose only).
INTCON = 0;
setting TRIS, PORT, ANSEL(if reqd) registers
clear Interrupts Flags and setting corresponding Interrupts (example, Timer0)
Enable GIE/GIEH/GIEL
while(1){
}
T0CONbits.TMR0ON = 1; //Disable Timer0
You enabled again TMR0 not disabled.

Of course, you don't need to disable here(as per your current requirement). Also, clear Interrupt flags only after you completed your required action, for example, lit the LED. Otherwise, if any further timer0 interrupt occurred before completing your work, it may reenter in to the ISR again and your pending work becomes void. Consider the following sequence.

Code:
INTCONbits.TMR0IF = 0; //Clear Timer 0 Interrupt Flag
 LATDbits.LATD4 =~LATDbits.LATD4 ;// toggle the led

this will be better:

Code:
LATDbits.LATD4 =~LATDbits.LATD4 ;// toggle the led
INTCONbits.TMR0IF = 0; //Clear Timer 0 Interrupt Flag
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top