This is a multi-part message in MIME format. ------=_NextPart_000_00BB_01C131A3.6911F7A0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Hi all, well, after my brief success with my timer I am kinda stuck again. I am running the attached code through the MPLAB simulator, and it keeps resetting half way through the DelayMs routine in servo.c. If I comment this out it fails in the DelayUs routine immediately below it. I know these delay routines are ok coz they work in different programs. Can anybody help me out with what might be causing this behavour? Thanks very much regards James Fitzsimons ------=_NextPart_000_00BB_01C131A3.6911F7A0 Content-Type: application/octet-stream; name="servo.c" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="servo.c" /********************************************************************** * * Filename: servo.c * * Description: servo-related functionality. * * Notes: The constants in this file are specific to PIC 16F84 * hardware. * * Written by James Fitzsimons for PIC hardware * 10/8/01 * * **********************************************************************/ #include #include "delay.h" /********************************************************************** * * Function: driveServo(int driveValue, unsigned char port) * * Description: Drives a hobby servo. * * Notes: This function is specific to PIC 16F84. * * Returns: None defined. * **********************************************************************/ void driveServo(int driveValue, unsigned char port) { int delay_remainder = 0; int delay_times = 0; int delay_amount = 0; delay_amount = driveValue - 1000; delay_times = delay_amount / 255; delay_remainder = delay_amount % delay_times; //set output port high port = 1; //delay for the pulse length DelayMs(1); do { DelayUs(driveValue); } while(--delay_times); DelayUs(delay_remainder); //set output port low port = 0; } /* driveServo() */ ------=_NextPart_000_00BB_01C131A3.6911F7A0 Content-Type: application/octet-stream; name="delay.h" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="delay.h" /*=0A= * Delay functions for HI-TECH C on the PIC=0A= *=0A= * Functions available:=0A= * DelayUs(x) Delay specified number of microseconds=0A= * DelayMs(x) Delay specified number of milliseconds=0A= *=0A= * Note that there are range limits: x must not exceed 255 - for xtal=0A= * frequencies > 12MHz the range for DelayUs is even smaller.=0A= * To use DelayUs it is only necessary to include this file; to use=0A= * DelayMs you must include delay.c in your project.=0A= *=0A= */=0A= =0A= /* Set the crystal frequency in the CPP predefined symbols list in=0A= HPDPIC, or on the PICC commmand line, e.g.=0A= picc -DXTAL_FREQ=3D4MHZ=0A= =0A= or=0A= picc -DXTAL_FREQ=3D100KHZ=0A= =0A= Note that this is the crystal frequency, the CPU clock is=0A= divided by 4.=0A= =0A= * MAKE SURE this code is compiled with full optimization!!!=0A= =0A= */=0A= =0A= #ifndef DELAY_ROUTINE //-- Check if already loaded!!!=0A= =0A= #define DELAY_ROUTINE //-- MAKE SURE CANNOT RELOAD =0A= #ifndef XTAL_FREQ=0A= //#error NO XTAL_FREQ DEFINED - Is Required for DelayUs!=0A= #define XTAL_FREQ 4MHZ /* Crystal frequency in MHz */=0A= #endif=0A= =0A= #define MHZ *1000 /* number of kHz in a MHz */=0A= #define KHZ *1 /* number of kHz in a kHz */=0A= =0A= #if XTAL_FREQ >=3D 12MHZ=0A= #define DelayUs(x) { unsigned char _dcnt; \=0A= _dcnt =3D (x)*((XTAL_FREQ)/(12MHZ)); \=0A= while(--_dcnt !=3D 0) \=0A= continue; }=0A= #else=0A= =0A= #define DelayUs(x) { unsigned char _dcnt; \=0A= _dcnt =3D (x)/(12MHZ/(XTAL_FREQ))|1; \=0A= while(--_dcnt !=3D 0) \=0A= continue; }=0A= #endif=0A= =0A= extern void DelayMs(unsigned char);=0A= #endif=0A= =0A= =0A= ------=_NextPart_000_00BB_01C131A3.6911F7A0 Content-Type: application/octet-stream; name="Delay.c" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="Delay.c" /*=0A= * Delay functions=0A= * See delay.h for details=0A= *=0A= * Make sure this code is compiled with full optimization!!!=0A= */=0A= =0A= #include "delay.h"=0A= =0A= void=0A= DelayMs(unsigned char cnt)=0A= {=0A= #if XTAL_FREQ <=3D 2MHZ=0A= do {=0A= DelayUs(996);=0A= } while(--cnt);=0A= #endif=0A= =0A= #if XTAL_FREQ > 2MHZ =0A= unsigned char i;=0A= do {=0A= i =3D 4;=0A= do {=0A= DelayUs(250);=0A= } while(--i);=0A= } while(--cnt);=0A= #endif=0A= }=0A= ------=_NextPart_000_00BB_01C131A3.6911F7A0-- -- http://www.piclist.com hint: PICList Posts must start with ONE topic: [PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads