We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hello all, I am using GPT3 for timing purposes in my RTOS (I am not running it on hardware yet - just in the IDE). I tried to use the example supplied in the "Getting started and creating applications" document (page 163) which is supposed to induce a 1000Hz frequency, but it counts too fast! the following code snippet displays "41" when actually 30 seconds have elapsed. Can it be related to the platform (the fact that I am not running it on the target yet)? here is the code: #include <reg167.h> #include <intrins.h> #include <stdio.h> static unsigned long volatile timer_tick = 0UL; static int sec = 0 ; static void timer3_irq (void) interrupt T3INT = 35 { timer_tick++; if (timer_tick % 1000 == 0) { printf("time sec=%d\n", sec) ; sec++ ; timer_tick = 0 ; } } void timer3_setup (unsigned int ticks_per_sec) { unsigned int reload; unsigned long frequency = 2500000; unsigned int prescale; for (prescale = 0; prescale < 8; prescale++) { if ((frequency / ticks_per_sec) <= 65535) break; frequency /= 2; } reload = frequency / ticks_per_sec; T3CON = 0x0080; /* 2.5MHz, Timer Mode, Count Down */ T3CON |= prescale; T2CON = 0x0027; /* Setup T2 for reload operation on any T3OTL */ T2 = reload; /* Reload for T3 */ T3 = reload; /* Start T3 with proper reload */ T3IC = 0x0044; /* Timer 3 interrupt enabled, Level 1 */ T3R = 1; /* Start Timer */ IEN = 1; /* Enable Interrupts */ } void timer3_delay (unsigned long ticks) { unsigned long start_tick; unsigned long timer_tick_copy; _atomic_ (0); /* start un-interruptable code */ start_tick = timer_tick_copy; _endatomic_ (); /* end un-interruptable code */ while (1) { _atomic_ (0); /* start un-interruptable code */ timer_tick_copy = timer_tick; _endatomic_ (); /* end un-interruptable code */ if ((timer_tick_copy - start_tick) > ticks) break; } } void main (void) { /**** Setup serial port function for Uart usage ****/ P3 |= 0x0400; /* SET PORT 3.10 OUTPUT LATCH (TXD) */ DP3 |= 0x0400; /* SET PORT 3.10 DIRECTION CONTROL (TXD OUTPUT) */ DP3 &= 0xF7FF; /* RESET PORT 3.11 DIRECTION CONTROL (RXD INPUT) */ S0TIC = 0x80; /* SET TRANSMIT INTERRUPT FLAG */ S0RIC = 0x00; /* DELETE RECEIVE INTERRUPT FLAG */ S0BG = 0x20; /* SET BAUDRATE TO 19200 BAUD */ S0CON = 0x8011; /* SET SERIAL MODE */ DP7 = 0x01; /* Setup P7.0 for output */ ODP7 = 0x01; /* Setup P7.0 for open-drain */ timer3_setup (1000); /* Setup timer for 1000Hz operation */ while (1) { timer3_delay (100); /* Wait 0.10 seconds */ P7 |= 0x01; /* Turn on P7.0 */ timer3_delay (100); /* Wait 0.10 seconds */ P7 &= ~0x01; /* Turn off P7.0 */ } } Thanks in advance, Tamir Michael
Ok you didn't say if you are using a C166 or XC166? I believe the example assumes a 20MHz CPU Clock. So for the XC16x device assuming an 8MHz external crystal you should have something like PLLCON = 0x6E05 (will get you a 20MHz CPU frequency). For C166 look at the configuration settings under the debug tab along with the crystal frequency you have chosen.