Motorola 68HC11 Help with C language. Making an accurate hardware timer.

Discussion in 'Embedded Systems and Microcontrollers' started by brightjoey, Nov 26, 2011.

  1. brightjoey

    Thread Starter New Member

    Nov 12, 2009
    14
    0
    Hello. I have a problem with understanding how to implement accurately a real time timer using 68HC11 with C. Now I don't have any previous knowledge in assembly so I can't use most of the reference book out there.

    The problem is to make a real time clock that counts accurately.
    My lecturer provides me with 2 codes that is to be put together. 1 to count the offset period of 1 cycle of the timer and 2 to is a real time timer.


    I need a direction to try making it work so help is appreciated.

    Code ( (Unknown Language)):
    1.  
    2. // Some further examples of setting timer unit
    3. // No. 2 - Measure Waveform period using TIC1
    4. // I.T. - 25/10/04
    5. // Sets up TOC2 to toggle mode and then reads TCNT and adds required
    6. offset period
    7.  
    8.  
    9. #include <iof1.h>
    10. #include <stdio.h>
    11. void main(void)
    12.  
    13. {
    14. unsigned int data1,result;
    15. float freq;
    16. unsigned int *tcnt,*toc2,*tic1;
    17. unsigned char *tflg1,*tctl2;
    18. tctl2=(unsigned char*)0x21;
    19. tflg1=(unsigned char*)0x23;
    20. toc2=(unsigned char*)0x18;
    21. tcnt=(unsigned char*)0x0e;
    22. tic1=(unsigned char*)0x0e;
    23. *tctl2=0x10; //set TIC1 to RISING EDGE CAPTURE mode
    24. for(;;)
    25. {
    26. *tflg1=0x04; //Clear TIC1 Flag
    27. while(((*tflg1)&0x04)==0); //Wait for TIC1 FLAG
    28. data1=*tic1; //Read TIC1 register
    29. *tflg1=0x04; //Clear TIC1 Flag
    30. while(((&tflg1)&0x04)==0); //Wait for TIC1 FLAG
    31. result=*tic1-data1; //subtract first time from second
    32. freq = 2.0e6/(float)result;//calculate frequency assume 2Mhz CLk
    33. printf("Interval was %u Frequency is %5.0f\n\r",result,freq);
    34. }
    35. }
    36.  
    37.  
    Code to count the offset period between the previous period and the next one.

    Code ( (Unknown Language)):
    1.  
    2. // Some further examples of setting timer unit
    3. // No. 3 - Polling of Real time Clock
    4. // I.T. - 25/10/04
    5. #include <stdio.h>
    6. /*Real time clock program - POLLING of timer*/
    7. void main()
    8. {
    9. unsigned char *padr,*paddr,*tflg2,*pactl,*tmsk2;
    10. int hours,mins,secs,ticks;
    11. tflg2=(unsigned char*)0x25;
    12. pactl=(unsigned char*)0x26;
    13. *pactl = 0x3;
    14. for(;;)
    15. {
    16. *tflg2 = 0x40; /*reset timer*/
    17. while((*tflg2 & 0x40) == 0); /*wait for timeout*/
    18. ticks++;
    19. if (ticks>=30)
    20. {
    21. ticks=0;
    22. secs++;
    23. printf("%i:%i:%i\r",hours,mins,secs);
    24. }
    25. if (secs>=60)
    26. {
    27.  
    28. secs=0;
    29. mins++;
    30. }
    31. if (mins>=60)
    32. {
    33. mins=0;
    34. hours++;
    35. }
    36. if (hours>=24)
    37. {
    38. hours=0;
    39. }
    40. }
    41. }
    42.  
    A real-time timer. But if it runs for a long time it will not be accurate anymore.
     
  2. MrChips

    Moderator

    Oct 2, 2009
    12,414
    3,353
    The timer will only be as accurate as the XTAL. If you want to keep perfect time, you have to put trimmer caps on the XTAL and adjust the frequency using a calibrated frequency standard. The accuracy has nothing to do with the MCU code.
     
  3. Yako

    New Member

    Nov 24, 2011
    245
    2
    You're in for a clock cycle counting exercise.

    All of your instructions in the code: logic conditions, calls and what not -- the amount of time required to execute them needs to be taken into account and some how formulated and subtracted from the delay routine to arrive at an accurate 1 second tick.

    Have fun with that one.
     
    Last edited: Nov 26, 2011
  4. Yako

    New Member

    Nov 24, 2011
    245
    2
    And that too.
     
  5. Yako

    New Member

    Nov 24, 2011
    245
    2
    What this guy is doing is he is making a clock using a software timer, whereas mine uses a hardware timer. This is not an easy task.
     
  6. brightjoey

    Thread Starter New Member

    Nov 12, 2009
    14
    0
    I don't know if the XTAL has trimmer caps or not but I can't mess with the hardware as it is fixed by the board. My lecturer main purpose for us is to make it accurate in C program.

    In code 1, If I use the difference of both tic1 and data1 that is "result" and add it back to data1. I could get back tic1 value. make it read into another address. and use that new value as the new clock cycle period. So the clock tick will forever use the constant clock cycle period.

    Will that work?
     
  7. MrChips

    Moderator

    Oct 2, 2009
    12,414
    3,353
    You do not have to do any clock cycle counting. Your objective will be to use the XTAL oscillator and hence the CPU clock to drive the hardware timer (which is built in as part of the MCU).
    You will set the hardware timer to give you periodic interrupts at a convenient period, for example, 100us. You just have to count interrupts. If this is done right, there is no loss of counts and your time will be as accurate as the XTAL.
     
  8. MrChips

    Moderator

    Oct 2, 2009
    12,414
    3,353
    Yako, I do not know what are your qualifications. I am not into flaming anybody but you are beginning to irritate me. Half the time you do not know what you are talking about. The purpose of this forum is to give sound advice to people who can use it, not to increase your post counts.
     
  9. brightjoey

    Thread Starter New Member

    Nov 12, 2009
    14
    0
    Okay I'm almost feeling you but I still don't really understand. I activate a Real time-Periodic Interrupt Flag, such as


    *tmsk2=0x40 //Enable RTI Interrupt

    @interrupt void timer (void)
    { Count how many interrupts here

    *tflg2=0x40 // Reset RTI Flag

    }
     
  10. brightjoey

    Thread Starter New Member

    Nov 12, 2009
    14
    0
    Sorry double post
     
  11. MrChips

    Moderator

    Oct 2, 2009
    12,414
    3,353
    OK, let's begin. Assuming your XTAL is 8MHz, then E-CLOCK is 2MHz.
    One clock period is 0.5us.

    You are going to use a Timer Output Compare function.
    In your Timer Output Compare interrupt handler, you will add 20,000 to the Output Compare register. This will give you an interrupt every 10ms.

    If you wish, you can add 2000 to the Output Compare register. This will give you a 1ms time base instead of 10ms. Up to you.

    As easy as pie.
     
Loading...