need help code

Thread Starter

ect_09

Joined May 6, 2012
180
Hello,

i write this code for 2sec delay using MIKROE calculator.
its woking
Rich (BB code):
#define _XTAL_FREQ 8000000



#include<htc.h>


unsigned a=0;
#pragma config WDT=OFF
#pragma config LVP=OFF
#pragma config BOR=OFF

void InitTimer0(){
  T0CON	 = 0x85;
  TMR0H	 = 0x0B;
  TMR0L	 = 0xDB;
  INTCONbits.GIE= 1;
  INTCONbits.TMR0IE= 1;
}

void Interrupt(){
  if (INTCONbits.TMR0IF){
    INTCONbits.TMR0IF = 0;
    TMR0H	 = 0x0B;
    TMR0L	 = 0xDB;
    a++;
    //Enter your code here
  }
}

void main() {

    TRISB=0;
  PORTB=0;
  InitTimer0();
  while(1)
  {
      if(a == 1)
      {
          a=0;

          PORTB=~PORTB;
      }

  }


  
}
but when i write this code like this as given
Rich (BB code):
#define _XTAL_FREQ 8000000



#include<htc.h>


unsigned a=0;
#pragma config WDT=OFF
#pragma config LVP=OFF
#pragma config BOR=OFF

void InitTimer0(){
  T0CON	 = 0x84;
  TMR0H	 = 0x0B;
  TMR0L	 = 0xDB;
  INTCONbits.GIE= 1;
  INTCONbits.TMR0IE= 1;
}

void Interrupt(){
  if (INTCONbits.TMR0IF){
    INTCONbits.TMR0IF = 0;
    TMR0H	 = 0x0B;
    TMR0L	 = 0xDB;
    a++;
    //Enter your code here
  }
}

void main() {

    TRISB=0;
  PORTB=0;
  //InitTimer0();
  while(1)
  {
      if(a == 1)
      {
          a=0;

          PORTB=~PORTB;
      InitTimer0();
}

  }


  
}
this code doesn't work.
can anyone explain the reason of it.why its does not work in this case..???

Thanks.....
 

tshuck

Joined Oct 18, 2012
3,534
I'm not following...

Why do you think re-initializing the timer and resetting the count should behave the same way as your delay?

...also, your interrupt is never configured in the second case, so what do you expect to increment a?
 

Thread Starter

ect_09

Joined May 6, 2012
180
i worked for toggle portb after 1sec.

but when i put

InitTimer0()
in while loop it doesnot work..why???
 

ErnieM

Joined Apr 24, 2011
8,377
i worked for toggle portb after 1sec.

but when i put

InitTimer0()
in while loop it doesnot work..why???

The while loop is a very tight loop meaning it executes very fast over and over.

Each time it does you call InitTimer0() so you set the time back to zero before the time passes.

It is doing what you told it to do: never fire the timer.

Put the init back where you had it.
 

John P

Joined Oct 14, 2008
2,025
No Ernie, I think you've got it wrong there. InitTimer is only called when a==1, and that only happens after the timer has triggered. If the timer has never triggered at all, then the timer never gets initialized(!!) and so the test condition can never be met. Alternatively, start a off with the value 1, so a call to InitTimer gets made the first time the test is done.

However, I don't like that a==1 test at all. What if for some reason the interrupt occurs twice before the main() routine checks the value of a? Then it has to wait until a wraps all the way around to 0xFFFF and restarts, and even then the chance of catching it on the next pass is as bad as the first time. Better to use a single-bit flag or make the test condition "if (a != 0)". If I were writing the code, I'd make it "if (a == 0) continue" and then make the rest of the loop un-bracketed and un-indented.

Also, I would make a an "unsigned char" rather than "unsigned". At least, I assume HiTech C defines "unsigned" as 2 bytes? It certainly only needs to be a single byte, in fact it only needs to be a single bit.

Rich (BB code):
void main() {

    TRISB=0;
    PORTB=0;
    InitTimer0();              // Must do this to start the timer!
    while(1)
    {
        if(a == 0)
            continue;
      
       a=0;
       PORTB=~PORTB;
       InitTimer0();
    }
 }
 

ErnieM

Joined Apr 24, 2011
8,377
No Ernie, I think you've got it wrong there.
Why yes I am. I saw or didn't see the brackets.


So what my revered second opinion tells me is the revised code doesn't work because InitTimer0() needs to be called to begin the interrupt stream, and it is only called AFTER the interrupt fires.

Logically, InitTimer0() never gets called.

I don't see any problems with the original code except the delay time is fixed. That's a simple fix by adding a parameter for the desired delay. I'd pass in how many cycles I want and let the routine compute (0xFFFF - cycle count).
 

Thread Starter

ect_09

Joined May 6, 2012
180
like continue statements ,how much keyword are used with PIC ??

can you tell me...or anyother helping material??
 
Top