CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug Reports on this forum. Send them to

Using the timers on the 18F26K22 /18F4xKxx 18F2xKxx + PLL

Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message

Joined: 20 Nov 2007
Posts: 2116
Location: albany ny

View user's profile Send private message AIM Address

Using the timers on the 18F26K22 /18F4xKxx 18F2xKxx + PLL
PostPosted: Sat Apr 25, 2015 12:31 pm     Reply with quote

This family of PICS has plenty of timers and it is easy to
create useful delay increments without using processor loops or
CCS defined DELAY_xx?() calls.

Do your best to understand the time domain performance of your code and adopt timer Polling whenever possible to minimize interrupt density and conflict.
To say nothing of improving foreground processor speed.


//  Timer non interrupt delay demo
//  8.388608 mhz master clock
//  18F4xKxx    18f2xKxx  chip family timer demo
//  using 4x PLL with local crystal oscillator
#include <18f26K22.h>

#fuses HSM
#use delay( clock=33554432) // some useful timings
#include <stdio.h>

#bit TMR0IF=0xFF2.2
#bit TMR1IF=0xF9E.0
#bit TMR2IF=0xF9E.1
#bit TMR3IF=0xFA1.1
#bit TMR4IF=0xF7E.0
#bit TMR5IF=0XF7E.1
#bit TMR6IF=0XF7E.2
// note: in std family 18F2620 #bit TMR4IF=0xFA1.3
//       no interrupts used in this demo
unsigned int32 seconds=0;

// !!!!!!!!!!!!!!!!!!!
// system call or interrupt conversion
// count elapsed seconds
// TMR0
void checksecs (void){   if (tmr0if){ tmr0if=0; seconds++; }}
// !!!!!!!!!!!!!!!!!!!
// DELAYS using timers and POLLING 
// TMR2
//  has range of 255 msecs max
//  call with 'c' as how many to wait
void count1ms(unsigned int8 c){
    set_timer4(0); tmr4if=0;
    while(c){      if (tmr4if){ tmr4if=0; --c;}}}
// range of 65535 msecs
void count1msL(unsigned int16 c){
    set_timer2(0);  tmr2if=0;
        while(c){  if (tmr2if){ tmr2if=0; --c;}}}
// !!!!!!!!!!!!!!!!!!!
// using timer 6 for increments of 50 usecs
void count50us(unsigned int8 c){
    while(c){      if (tmr6if){ tmr6if=0; --c;}}}
void count50usL(unsigned int16 c){
    set_timer6(0);     tmr6if=0;
    while(c){  if (tmr6if){ tmr6if=0; --c;}}}
// !!!!!!!!!!!!!!!!!!!
// NOTE: this call MUST call other stuff while busy
// TMR1   counts in increments of 31msecs
void count31ms(unsigned int16 c){
    set_timer1(0); tmr1if=0;
           if (tmr1if){ tmr1if=0; --c;}
           if (tmr0if){ tmr0if=0; seconds++; } // track seconds
           // << YOUR fast execution recurrent code here !!

// module
void timer_msetup(void){

     // rollover to 1 hz
     setup_timer_0(T0_DIV_128);  //

     // 31.25  millisecond/tick
     setup_timer_1(T1_INTERNAL|T1_DIV_BY_4); // was div 1

     // 62.5msecs cycle 16 cycles = 1 second <<<

     //  1 msec rollovers
     // 50 usec
     setup_timer_6(T6_DIV_BY_1,200,2);  //50 usecs per

void main(){
  unsigned int32 lclsecs=0;
      if (seconds !=lclsecs){  output_toggle(Pin_A4); }
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library All times are GMT - 6 Hours
Page 1 of 1

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Powered by phpBB © 2001, 2005 phpBB Group