Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Tools
Old 29th October 2007, 10:53 AM   #1
Default PIC Timer0

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,0x6 F};
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[i];
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 by Hesam Kamalan; 29th October 2007 at 12:09 PM.
Hesam Kamalan is offline  
Old 29th October 2007, 11:58 AM   #2
Default

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.
__________________
C:\WHUT ?
Beware the asterisk *
ClydeCrashKop is offline  
Old 29th October 2007, 12:08 PM   #3
Default

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.
Hesam Kamalan is offline  
Old 29th October 2007, 12:21 PM   #4
Default

I don't do C either, but TMR0 is the poorest of the available timers, and has a LOT of restrictions.
__________________
PIC programmer software, and PIC Tutorials at:
http://www.winpicprog.co.uk
Nigel Goodwin is online now  
Old 29th October 2007, 12:41 PM   #5
Default

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.
Hesam Kamalan is offline  
Old 29th October 2007, 01:15 PM   #6
Default

Which PIC are you using? Why not use Timer1 if it works?
__________________
Bill
Smart Kits build Smart People

http://www.blueroomelectronics.com/
blueroomelectronics is offline  
Old 29th October 2007, 03:07 PM   #7
Default

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.
Hesam Kamalan is offline  
Old 29th October 2007, 03:32 PM   #8
Default

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
__________________
Bill
Smart Kits build Smart People

http://www.blueroomelectronics.com/

Last edited by blueroomelectronics; 29th October 2007 at 03:34 PM.
blueroomelectronics is offline  
Reply

Tags
pic, timer0

Thread Tools
Display Modes


Similar
Title Starter Forum Replies Latest
Timer0 Prescale lord loh. Micro Controllers 10 8th October 2008 06:14 PM
High ADC sampling rate PIC, 18F needed? bananasiong Micro Controllers 24 28th October 2007 01:13 PM
Four PIC with One LCD.. meera83 Micro Controllers 13 20th September 2007 07:40 AM
16F877 Timer0 Interrupt Affecting ADC sebana Micro Controllers 13 12th September 2007 10:43 AM
Using the Timer0 module wonbinbk Micro Controllers 2 14th September 2004 07:06 AM



All times are GMT. The time now is 11:44 AM.


Electronic Circuits  |  Learning Electronics
eXTReMe Tracker