|
发表于 2011-4-10 04:06:13
|
显示全部楼层
here is an example.
the following code was written for pic24. it runs four timers and each at different intervals and calls user-defined routines to perform some tasks.
========code================
#include <p24fxxxx.h> //we use pic24f
#include "gpio.h"
//#include "delay.h" //we use delay routines
#include "rtc2.h" //we use tmr2
#include "rtc3.h" //we use tmr3
#include "rtc4.h" //we use tmr4
#include "rtc5.h" //we use tmr5
//config fues settings
_CONFIG1(JTAGEN_OFF & GCP_OFF & FWDTEN_OFF); //JTAG OFF, CODE PROTECTION OFF, WATCH DOG TIMER OFF
_CONFIG2(IESO_OFF & FNOSC_PRI & FCKSM_CSDCMD & POSCMOD_XT); //TWO-STAGE START OFF, PRIMARY OSCILLATOR (HS/XT/EC), CLOCK FAILURE SWITCH DISABLED, PRIMARY OSCILLATOR IS XT
//hardware configuration
#define LED_PORT LATB
#define LED_PORT_IN PORTB
#define LED_DDR TRISB
#define LED2 (1<<0) //led0 on pb0
#define LED3 (1<<1) //led0 on pb1
#define LED4 (1<<2) //led0 on pb2
#define LED5 (1<<3) //led0 on pb3
#define LED2_DLY RTC_100ms //dly for led2
#define LED3_DLY LED2_DLY << 1 //dly for led3
#define LED4_DLY LED3_DLY << 1 //dly for led4
#define LED5_DLY LED4_DLY << 1 //dly for led5
//end hardware configuration
void my_tmr2_isr(void) {
IO_FLP(LED_PORT, LED2); //flip led0
}
void my_tmr3_isr(void) {
IO_FLP(LED_PORT, LED3); //flip led0
}
void my_tmr4_isr(void) {
IO_FLP(LED_PORT, LED4); //flip led0
}
void my_tmr5_isr(void) {
IO_FLP(LED_PORT, LED5); //flip led0
}
void mcu_init(void) {
IO_CLR(LED_PORT, LED2 | LED3 | LED4 | LED5); //clear led0
IO_OUT(LED_DDR, LED2 | LED3 | LED4 | LED5); //led0 as output
//CLKDIV = 0x2000; //not supported by proteus
//DOZE2=0, DOZE1=1, DOZE0=0; //clkdivider = 0b010 -> 1:4
}
int main(void)
{
mcu_init(); //reset the mcu
rtc2_init(TMR_PS_1x, LED2_DLY); //initialize rtc2's period to led2_dly
rtc2_act(my_tmr2_isr); //implement my_tmr2_isr for tmr2 isr
rtc3_init(TMR_PS_1x, LED3_DLY); //initialize rtc3's period to led3_dly
rtc3_act(my_tmr3_isr); //implement my_tmr3_isr for tmr3 isr
rtc4_init(TMR_PS_1x, LED4_DLY); //initialize rtc4's period to led4_dly
rtc4_act(my_tmr4_isr); //implement my_tmr4_isr for tmr4 isr
rtc5_init(TMR_PS_1x, LED5_DLY); //initialize rtc5's period to led5_dly
rtc5_act(my_tmr5_isr); //implement my_tmr5_isr for tmr5 isr
ei(); //enable global isr
while(1) {
//delay_ms(LED_DLY); //waste some time
}
}
===============end==========
the "api" are the macros used to manipulate the port (IO_CLR, IO_SET, IO_FLP, IO_OUT, and IO_IN), and the timer routines are rtcx_init() and rtcx_act().
I would have similar routines doing the same thing on my target chip. and once I move to my code to that target environment, all I need to do is to link in the right api, recompile and I am ready to go. |
|