0
\$\begingroup\$

i wrote a code in mikroc for pic16f877A in which i have used a timer0 and timer1. i debug my code and found that timer0 is not working but i am not able to understand the reason behind it. I even checked datasheet for the OPTION_REG but couldn't figure out the problem plz help. i am new to pic programming. I just uploaded the function in which timer0 and timer1 is setup. UART is used to check whether each line is getting executed and i found after printing the value of TMR1H it stops.

 int feedback()
 {
 sh = 0;                                 //variable to come out of the while loop after interrupt occured.
 UART1_WRITE_TEXT("inside feedback");
 UART1_Write(13); // newline

 TMR0=193;
 Uart1_Intout_ReturnInt(TMR0);
 UART1_Write(13); // newline

 INTCON=0xA0;
 Uart1_Intout_ReturnInt(INTCON);
 UART1_Write(13); // newline

 TMR1L=0;
 Uart1_Intout_ReturnInt(TMR1L);
 UART1_Write(13); // newline

 TMR1H=0;
 Uart1_Intout_ReturnInt(TMR1H);
 UART1_Write(13); // newline
                                               //here the problem starts
 OPTION_REG.PSA=0;
 OPTION_REG.PS2=0;
 OPTION_REG.PS1=1;
 OPTION_REG.PS0=0;
 OPTION_REG.T0SE=0;
 OPTION_REG.T0CS=0;
 UART1_WRITE_TEXT("timer0 start");     //this msg and following all messages does not show up 
 UART1_Write(13); // newline

 T1CON=0x07;
 UART1_WRITE_TEXT("timer1 start");
 UART1_Write(13); // newline

 sh=1;
 Uart1_Intout_ReturnInt(sh);
 UART1_Write(13); // newline

while(sh==1){
Uart1_Intout_ReturnInt(sh);
UART1_Write(13); // newline

UART1_WRITE_TEXT("inside while loop");
}

UART1_WRITE_TEXT("outside while loop");

TMR1H=TMR1H*1000;
sh=TMR1H+TMR1L;
actualOut=(60*2*sh)/8;
Uart1_Intout_ReturnInt(actualOut);
UART1_Write(13); // newline

return actualOut;
}
void interrupt(){

 T1CON=0x00;
 sh=0;

}

///////////main function/////////////

 void main() {

 UART1_Init(9600);

 pro=0.5,i=0,der=0;
 setPoint=80,sel=1;
 actualOut=20;
 pOut=0,iOut=0,dOut=0;
 out=16;
 error=0,lastError=0;

 ADCON0=0;
 ADCON1=0x0f;
 CMCON = 0x07;
 PORTC=0;
 TRISC = 0b00000001;

  while(1){

 out = pid(actualOut);

 duty=(short)out;
 pwm(duty);

 actualOut = feedback();

  }
  }
\$\endgroup\$
  • \$\begingroup\$ Post your config bit settings too. Have you remembered to disable the Watchdog Timer? \$\endgroup\$ – brhans Feb 16 '15 at 19:43
  • 2
    \$\begingroup\$ You can't just slap together a few calls to some canned libraries and expect everything to work without you having to actually understand things. For example, does UART1_WRITE just write a byte, or does it first wait for the UART to be ready? \$\endgroup\$ – Olin Lathrop Feb 16 '15 at 19:53
3
\$\begingroup\$

I'm not going to spend much time looking at higher level code with some compiler getting between me and the system. However, you're not ever clearing the timer 1 interrupt condition.

\$\endgroup\$
  • \$\begingroup\$ i cleared interrupt and it worked thanku u @Olin Lathrop \$\endgroup\$ – dcmotor Feb 16 '15 at 20:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.