need help code

Discussion in 'Embedded Systems and Microcontrollers' started by ect_09, May 16, 2013.

  1. ect_09

    Thread Starter Member

    May 6, 2012
    180
    1
    Hello,

    i write this code for 2sec delay using MIKROE calculator.
    its woking
    Code ( (Unknown Language)):
    1.  
    2. #define _XTAL_FREQ 8000000
    3.  
    4.  
    5.  
    6. #include<htc.h>
    7.  
    8.  
    9. unsigned a=0;
    10. #pragma config WDT=OFF
    11. #pragma config LVP=OFF
    12. #pragma config BOR=OFF
    13.  
    14. void InitTimer0(){
    15.   T0CON  = 0x85;
    16.   TMR0H  = 0x0B;
    17.   TMR0L  = 0xDB;
    18.   INTCONbits.GIE= 1;
    19.   INTCONbits.TMR0IE= 1;
    20. }
    21.  
    22. void Interrupt(){
    23.   if (INTCONbits.TMR0IF){
    24.     INTCONbits.TMR0IF = 0;
    25.     TMR0H    = 0x0B;
    26.     TMR0L    = 0xDB;
    27.     a++;
    28.     //Enter your code here
    29.   }
    30. }
    31.  
    32. void main() {
    33.  
    34.     TRISB=0;
    35.   PORTB=0;
    36.   InitTimer0();
    37.   while(1)
    38.   {
    39.       if(a == 1)
    40.       {
    41.           a=0;
    42.  
    43.           PORTB=~PORTB;
    44.       }
    45.  
    46.   }
    47.  
    48.  
    49.  
    50. }
    51.  
    52.  
    53.  
    54.  
    but when i write this code like this as given
    Code ( (Unknown Language)):
    1.  
    2. #define _XTAL_FREQ 8000000
    3.  
    4.  
    5.  
    6. #include<htc.h>
    7.  
    8.  
    9. unsigned a=0;
    10. #pragma config WDT=OFF
    11. #pragma config LVP=OFF
    12. #pragma config BOR=OFF
    13.  
    14. void InitTimer0(){
    15.   T0CON  = 0x84;
    16.   TMR0H  = 0x0B;
    17.   TMR0L  = 0xDB;
    18.   INTCONbits.GIE= 1;
    19.   INTCONbits.TMR0IE= 1;
    20. }
    21.  
    22. void Interrupt(){
    23.   if (INTCONbits.TMR0IF){
    24.     INTCONbits.TMR0IF = 0;
    25.     TMR0H    = 0x0B;
    26.     TMR0L    = 0xDB;
    27.     a++;
    28.     //Enter your code here
    29.   }
    30. }
    31.  
    32. void main() {
    33.  
    34.     TRISB=0;
    35.   PORTB=0;
    36.   //InitTimer0();
    37.   while(1)
    38.   {
    39.       if(a == 1)
    40.       {
    41.           a=0;
    42.  
    43.           PORTB=~PORTB;
    44.       InitTimer0();
    45. }
    46.  
    47.   }
    48.  
    49.  
    50.  
    51. }
    52.  
    53.  
    54.  
    55.  
    this code doesn't work.
    can anyone explain the reason of it.why its does not work in this case..???

    Thanks.....
     
  2. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    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?
     
  3. ect_09

    Thread Starter Member

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

    but when i put

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

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605

    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.
     
  5. John P

    AAC Fanatic!

    Oct 14, 2008
    1,632
    224
    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.

    Code ( (Unknown Language)):
    1.  
    2. void main() {
    3.  
    4.     TRISB=0;
    5.     PORTB=0;
    6.     InitTimer0();              // Must do this to start the timer!
    7.     while(1)
    8.     {
    9.         if(a == 0)
    10.             continue;
    11.      
    12.        a=0;
    13.        PORTB=~PORTB;
    14.        InitTimer0();
    15.     }
    16.  }
    17.  
     
    ErnieM likes this.
  6. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    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).
     
  7. ect_09

    Thread Starter Member

    May 6, 2012
    180
    1
    what its mean.????

    Code ( (Unknown Language)):
    1.  continue;
    that you write in above..
     
  8. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    The continue statement skips back to the beginning of the loop inside for( ; ; ) and while() loops.
     
  9. ect_09

    Thread Starter Member

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

    can you tell me...or anyother helping material??
     
  10. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    It's not a PIC thing it is a standard (though obscure) C statement. You can find it in any C/C++ reference.
     
Loading...