Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC Timer0

Status
Not open for further replies.

Hesam Kamalan

New Member
hi'
I wrote program for 0-99 counter. but i don't know why interrupt not work. TMR0 never incremented and then interrupt not accure. tell me, where is my problem, and how can i run Timer0, please? my compiler is microC.

C code:

#define uchar unsigned char

//-----Function Prototype--------
void inith();
void first();
void second();

//----Variable Dicleration-------
uchar num[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
uchar j, hop; // j for second function and hop for switch

//----Interrupt routin-----------
void interrupt() {
TMR0=0;
INTCON=0xa0; // clear timer0 interrupt flag bit
if(j!=0) ~hop; // function hopping (first and second function)
}

//-------------------------------
//----Program Starts Here--------
void main(){

inith();

for( ;; ){
if(!hop)
first();
if(hop)
second();
}
}

void first(){
uchar i, d;

portb.F2=0;
portb.F6=1;
for(i=0;i<10;i++){
portd=num;
if(i==9){
j++;
if(j>9) j=0;
}
for(d=0;d<100;d++){ // 1's Delay
delay_ms(10);
if(hop) break;
}
if(hop) break;
}
}

void second(){
uchar i;

portb.F6=0;
portb.F2=1;
portd=num[j];
for(i=0;i<5;i++){ // 1's Delay
delay_ms(10);
if(hop) break;
}
}

void inith(){

portb=0x00;
trisb=0x01;

portd=0x00;
trisd=0x00;

hop=0;
TMR0=0;

OPTION_REG=0x01; // PSA = Timer , 1:4
INTCON=0xa0 ; // Global Interrupt enable
}
 
Last edited:
It has been a while since I did C and not yet on a PIC but it looks like your interrupt routine makes TMR0=0 on every interrupt so it will never increment.
 
thank you ClydeCrashKop,
but TMR0 is a register and i can write it. when i change contents of this (for example TMR0=96; ), i have this problem too.
 
yes you are right Nigel.
this program is correct when i use Timer1. timer1 has a TMR1ON for control of stop and start. but timer0 has not this control bit and i don't know how can i run timer0.
 
Thx blueroomelectronics.
I used PIC 16F877A.
becuse i can not run and work with this timer. and i didn't see anybody can work with this timer.
 
Timer0 works fine, it's a simple 8bit timer. Timer 1 & 2 are part of the 16F877A.
Timer0 is always running. Tough to turn off actually :), use the MPLAB simulator and set the T0CS to FOSC/4
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top