Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Delay and Timeout Routine for dsPIC in Hi-Tech dsPICC

Status
Not open for further replies.

saeed_pk

Full Member level 4
Joined
May 20, 2006
Messages
237
Helped
35
Reputation
68
Reaction score
28
Trophy points
1,308
Location
Islamabad, Pakistan
Activity points
2,655
Hi all;
I require Delay routine for Hi-Tech dsPICC, i can't use the file provided in samples folder because it is for 20 MHz i want to use 10 MHz with 16x PLL
please upload code if any one have
 

Most programs require some type of clock for timing. A common approach is to set up one of the timers to interrupt every 1mS and increment a global variable.
You can then have a timer routine. For example:

delay_1mS(50);

There are plenty of timers in a dsPic.
One thing to know about using unsigned ints as timers. You dont have to worry about overflow when subtracting one from the other, you always get the correct result.
Using Mplab C30 compiler but Im sure Hi-Tech is similer. You would need to adjust PR1 value for the correct timing. I use Mplab Sim stopwatch to check by placing a breakpoint in the interrupt routine.

Code:
/*--- Global sytem clock ---*/

volatile unsigned int sysclock;

/*--- Initialise Timer 1 (Used as system 1mS clock) ---*/

void start_timer_1(void)
  {
  T1CON = 0;                                                                                    
  T1CONbits.TCKPS = 2;  /* Prescaler 1:64 */ 
  IPC0bits.T1IP = 5;    /* High Priority */              
  IEC0bits.T1IE = 1; 
  IFS0bits.T1IF = 0;  
  PR1 = 576;            /* ~1mS */  
  TMR1 = 0;             
  T1CONbits.TON = 1;              
  }

/*--- Timer 1 Interrupt ~1mS system timer ---*/

void __attribute__((__interrupt__, no_auto_psv)) _T1Interrupt(void)
  {
  sysclock++;
  IFS0bits.T1IF = 0; 
  }

/*--- Millisecond delay ---*/

void delay_1mS(unsigned int milliseconds)
  {
  unsigned int timer;

  timer = sysclock;
 
  while((sysclock - timer) < milliseconds){
    ;
    }
  } 

/*--- End of File ---*/
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top