View previous topic :: View next topic |
Author |
Message |
michael_s
Joined: 10 Nov 2010 Posts: 4 Location: UK
|
PIC16F716 4MhZ Clock delay_us wrong timing |
Posted: Wed Nov 10, 2010 9:21 am |
|
|
Hi all i am new on the forum,
My name is Michael.
I have a problem with my PIC16F716 i am using a 4Mhz oscillator i defined that on the beginning of the software.
When i am trying to make 1us delay by delay_us(1), it is making aprox. 5us delay.. Can i do anything about that ?
Thank you for your help.
Regards,
Michael. |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Wed Nov 10, 2010 9:33 am |
|
|
Show us a complete compilable program that demonstrates the problem. Try to keep it under a dozen lines or so. Also tell us the version of your compiler (4.xxx). _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
michael_s
Joined: 10 Nov 2010 Posts: 4 Location: UK
|
|
Posted: Wed Nov 10, 2010 10:00 am |
|
|
Hi thank you for the answer, there is a code I am using, it is just a sketch because I just start playing with PIC so I am fresh .
Regards.
Code: |
/******************************************************************************/
// Filename: main.c
// Compiler: CCS PIC compiler
// Micro: PIC 16F716
// Authors:
// Revision:
/******************************************************************************/
/******************************************************************************/
#include <16F716.h>
#use delay(clock=4000000,crystal) // Osc freq = 4MHz
#use fast_io(A) // I/O direction not set for each I/O instruction
#use fast_io(B)
#fuses XT,NOWDT,NOBROWNOUT,PUT,PROTECT //Fuse bits settings
#include "definitions.h" //Include Header File with all definitions and declarations
#include "initialization.h" //Initialize PIC - system all hardware counters ETC...
/*****************************************************************************
PROTOTYPE DEFINITION
******************************************************************************/
void Initialize (void); // Initialize all system I/O etc...
void default_states (void); // Initialize all variables and default port states on powewr up
///////////////////////
// HALF SECOND DELAY///
///////////////////////
void WaitHalfSec (void)
{
delay_ms(500);
}
/////////////////////////////
// ONE SECOND DELAY//////////
/////////////////////////////
void WaitSec (void)
{
delay_ms(1000);
}
// /// ///////// // // //
//// //// // // // //// //
///// // // // // // // // //
// // // // ///////// // // // //
// // // // // // // // //
// // // // // // // //
// // // // // // ////
/************************************************************************/
/*********** Main program starts here. ***********/
/************************************************************************/
void main()
{
Initialize(); // Initialize the PIC (all hardware Timers etc...)
default_states(); // Initialize all variables on the system
disable_interrupts (GLOBAL);
while (true) // this is main loop
{
delay_us(1);
output_toggle(LED);
}
}
/************************************************************************/
/*********** Main program finish here. ***********/
/************************************************************************/
// Include additional code.
#include "interupts_handlers.h" // All procedures regarding Interrupts External or Internal
#include "default_states.h" //Set all defaults states of ports. |
PS. I just run the program without any delay
Just wrote inside the main loob While(TRUE)
Code: | output_bit(LED,1);
output_bit(LED,0); |
And the status of the LED line is changing for high for 3 us and for low for 5us. The time should be much shorter isn't it ? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19549
|
|
Posted: Wed Nov 10, 2010 10:30 am |
|
|
That is not the delay taking 5uSec, but the loop, and I/O.
Your processor is basically running at 1MIPS.
When you perform an input or output operation using 'standard_io' (the default), the code will update the TRIS register, and then the actual I/O register. So the 'output_toggle' will be two instructions.
The delay, will be coded as a single 'nop' instruction.
The loop, will be a simple 'jump', but these take two instruction times.
So, 5 instruction times, = 5uSec.
The delay is only taking 1uSec, as programmed, but the other code takes 4uSec to execute...
You can prove this by the simple expedient of removing the delay. Loop takes 4uSec without the delay present.
All instructions take time. In this case most are just one or two processor cycles. Try something like dividing two float numbers, and the time can leap into the mSecs...
Best Wishes |
|
|
michael_s
Joined: 10 Nov 2010 Posts: 4 Location: UK
|
|
Posted: Wed Nov 10, 2010 10:41 am |
|
|
Wow
Thank You for answer, can I find anywhere the table with time each instruction taking ?
Regards.
Michael |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19549
|
|
Posted: Wed Nov 10, 2010 10:50 am |
|
|
No.....
Do a search here. The easiest way is to use the MPLAB simulator and stopwatch function to time how long the code takes. The timings of the assembler instructions are in the data sheet for the chip. Problem is that a 'C' instruction, can take different times, according to where the data is stored, relative to the last value used, and will take different times for each different data type. There might easily be something over fifty 'possible' times for just one instruction....
Best Wishes |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
michael_s
Joined: 10 Nov 2010 Posts: 4 Location: UK
|
|
Posted: Wed Nov 10, 2010 2:03 pm |
|
|
Ok thank for all of you for a help...
If there is anything like a CCS PIC tutorial for dummies ??
I would start doing things properly
Regards,
Michael. |
|
|
|