asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
Using the timers on the 18F26K22 /18F4xKxx 18F2xKxx + PLL |
Posted: Sat Apr 25, 2015 12:31 pm |
|
|
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.
Code: |
// 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
#Fuses PLLEN,NOFCMEN,NOIESO,PUT,NOBROWNOUT
#Fuses CCP2C1,NOPBADEN,NOMCLR,MCLR,NOSTVREN,NOLVP,
#fuses NOXINST,NODEBUG,NOPROTECT,NOCPB,NOCPD,NOWRT,NOWRTC
#Fuses NOWRTB,NOWRTD,NOEBTR,NOEBTRB
#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){
if(c){
set_timer4(0); tmr4if=0;
while(c){ if (tmr4if){ tmr4if=0; --c;}}}
}
// range of 65535 msecs
void count1msL(unsigned int16 c){
if(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){
if(c){
set_timer6(0);tmr6if=0;
while(c){ if (tmr6if){ tmr6if=0; --c;}}}
}
void count50usL(unsigned int16 c){
if(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){
if(c){
set_timer1(0); tmr1if=0;
while(c){
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 <<<
setup_timer_3(T3_INTERNAL|T3_DIV_BY_8);
setup_timer_5(T5_INTERNAL|T5_DIV_BY_8);
// 1 msec rollovers
setup_timer_2(T2_DIV_BY_16,255,2);
setup_timer_4(T4_DIV_BY_16,255,2);
// 50 usec
setup_timer_6(T6_DIV_BY_1,200,2); //50 usecs per
}
void main(){
unsigned int32 lclsecs=0;
timer_msetup();
while(1){
checksecs();
if (seconds !=lclsecs){ output_toggle(Pin_A4); }
}
}
//
|
|
|