Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Delay10KTCYx in C18

Status
Not open for further replies.

Urahara

Member
Hi

I hv a 4MHz crystal on my board. Would like to do a 0.1sec delay using the Delay10KTCYx function found in delay.h of C18. My calculation as follows :

Instruction Cycle = 4,000,000/4 = 1,000,000 per sec
No of 10KTCY needed for 1 sec = 1,000,000/10,000 = 100
Therefore No of 10KTCY for 0.1 sec = 10

I hence pass the value of 10 when I call this function.

But it seems to be slower than the actual timing.

Did I do wrong somewhere? Comments welcome. Thks!
 
nope...wrote the codes under MPLAB IDE environment and programmed a 18F4550 uC. The pgm basically waits for 0.1sec, displays the elapsed waiting time on a LCD, then waits again, etc. I think displaying it on the LCD introduces some overheads, but the difference is too huge.

Am I correct in my calculations of 10x10KTCYx ? Thks!
 
Use one of the built in timers of the 18F4550 for accuracy. You can also use the ECCP module in "trigger special event" mode with the timer to get precise timing intervals.
 
Last edited:
Here's the code :

Code:
#include <p18f4550.h>
#include <delays.h>
#include <string.h>
#include <stdlib.h>
#include <xlcd.h>

#pragma config FOSC = XT_XT
#pragma config PWRT = OFF
#pragma config BOR = OFF 
#pragma config WDT = OFF
#pragma config MCLRE = ON
#pragma config PBADEN = OFF
#pragma config LVP = OFF
#pragma config USBDIV = 1

#define CLEAR_DISPLAY 0b00000001 /* Clear Display */

void main( void )
{
	int subsecond=0;
	int second=0;
	char string1[8];
	char string2[2];
	char dot[]=".";
	int i;

	init(); 
	LCD_init(); 

	while(1)
	{
		Delay10KTCYx(10);  // Delay of 0.1sec

		if (subsecond<9) {
			subsecond+=1;
		}
		else {
			second+=1;
			subsecond=0;
		}

		WriteCmdXLCD(CLEAR_DISPLAY);
		itoa(second,string1);
		strcat(string1,dot);
		itoa(subsecond,string2);
		strcat(string1,string2);
		putsXLCD(string1);
	}
}

Hv timed the code with a stopwatch. The former starts to visibly lag slightly behind the stopwatch around 18-20 seconds onwards. Certainly the LCD, string conversion codes as well as the delay function call add some overhead, but seems pretty high.

Or is my expectation wrong? ie,the overheads are not trivial. If so, is there a way to determine how much instruction cycle they would take so that I can factor these overheads into the delay?

Thks!
 
Hi kchriste

By internal timers, do you mean modules such as timer0, timer1? For timing to say 60secs at 0.1sec resolution, function Delay10KTCYx is not good enough?
 
Hi kchriste

By internal timers, do you mean modules such as timer0, timer1? For timing to say 60secs at 0.1sec resolution, function Delay10KTCYx is not good enough?

3v0 here. Yes I am sure that is what he means, TMR0 etc. The problem with the delay function is it create the delay by executing a fixed number of instructions. If interrupts are serviced the delay time is extended by the time used for the interrupts.

If you setup a TMR timer it counts independantly of the code executed. Hardware independant of the processor count clock cycles. It is very accurate.
 
Last edited:
Hv written another set of codes that use Timer1.

It is confirmed. The delay function is inaccurate, while that of Timer1 is, based on empirical observation between my stopwatch and the LCD output.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top