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.

PIC18F2321 TIMR0 issue

Status
Not open for further replies.

chimera786

Member level 2
Member level 2
Joined
Jun 16, 2011
Messages
42
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Visit site
Activity points
1,710
Hey there!

This is my first time messing with the 18F series. This uC is definitely powerful.. in fact an overkill for the applications that I have been/am developing.

So, I decided to baby step this uC just because the datasheet is long and had way too many jam packed features.

I have been trying to get the TMRO to work with a simple PORTB toggle. But it doesn't seem to be working. I have looked high and low thru the datasheet but it doesn't look like im missing anything

I am probing the output of PORTB with a o-scope but the thing is that I am getting a constant square wave of 490Hz.

Basically, the timer0 is expiring at the wrong interval, rather than expiring at the freq i want it to

Please take a look at it guys-- its so simple that i cant find it..maybe Ive overlooked something.. maybe im not configuring some registers correctly..



Code:
void main() {
     
     OSCTUNE=0x00;
     OSCCON=0xCE;// oscillator at 1Mhz
     ADCON1=0xFF;// Make sure pins are IOs

     INTCON=0xA0;// Global + TMR0 interrupt ON

     T0CON=0xC8;// Configure TMR0 as 8bit no prescale
     TMR0L=100;

     TRISB=0x00;// PORTB as OUTPUT
     PORTB=0;
     
}

void interrupt (void) {

    if (INTCON.B2){
                   INTCON.B2=0;
                   PORTB=~PORTB;
    }

}
 
Last edited:

Since you say that the timing is off, my guess is that your timing output sets an output frequency of ~750Hz.

Make these two changes (I've shown them in the code):
  • Add an infinite loop in the code.
  • Reload TMR0 each interrupt.

Code:
void main() {
     
     OSCTUNE=0x00;
     OSCCON=0xCE;// oscillator at 1Mhz
     ADCON1=0xFF;// Make sure pins are IOs

     INTCON=0xA0;// Global + TMR0 interrupt ON

     T0CON=0xC8;// Configure TMR0 as 8bit no prescale
     TMR0L=100;

     TRISB=0x00;// PORTB as OUTPUT
     PORTB=0;
     [B][COLOR="#FF0000"]while(1);[/COLOR][/B]
}

void interrupt (void) {

    if (INTCON.B2){
                   INTCON.B2=0;
                   PORTB=~PORTB;
                   [B][COLOR="#FF0000"]TMR0L = 100;[/COLOR][/B]
    }

}

Hope this helps.
Tahmid.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top