|
楼主 |
发表于 2010-6-16 08:14:09
|
显示全部楼层
here is the code, on a 12F675.
============code===============
//signal generation. based on an approach proposed by roman black
//utilizes tmr0
#include <htc.h>
#include "gpio.h"
__CONFIG(MCLRDIS & BORDIS & WDTDIS & PWRTEN & INTIO);
#define IO_PORT GPIO
#define IO_DDR TRISIO
#define IO_PIN (1<<0)
#define TMR0_PS 0b001 //prescaler = 1<<(TMR0_PS+1)
#define Mhz 000000ul
#define Fxtal 4Mhz //crystal frequency
#define TICKS (Fxtal/4) //ticks per second
#define TIME_INTERVAL 100 //time interval, in ms
#define TMR_MAX (TIME_INTERVAL*TICKS/1000ul) //tmr trigger
unsigned char tmr_flag=0;
void interrupt tmr0_isr(void) {
static unsigned long tmr_count=0;
T0IF=0; //clear the tmr flag
tmr_count += 256<<(1+TMR0_PS); //advance the tmr. tmr0 is 8bit
if (tmr_count >= TMR_MAX) {
tmr_count -= TMR_MAX; //retaining the error
tmr_flag=1; //set the tmr flag
}
}
void tmr0_init(void) { //initialize the tmr0
T0CS=0; //tmr source is internal clock
PSA=0; //prescaler assigned to tmr0
PS2=(TMR0_PS & 0b100) >>2; //establish the prescaler
PS1=(TMR0_PS & 0b010) >>1;
PS0=(TMR0_PS & 0b001) >>0;
T0IE=1; //turn on tmr0 interrupt
GIE=1; //turn on global interrupt
}
void mcu_init(void) {
ANSEL=0x00; //all pins gpio
CMCON=0x07; //analog comparators off
IO_OUT(IO_DDR, IO_PIN); //io_pin as output
IO_CLR(IO_PORT, IO_PIN); //clear io_pin
}
void
main(void)
{
mcu_init(); //initialize the mcu
tmr0_init(); //initialize the timer
while (1){
//TODO Auto-generated main function
if (tmr_flag) { //flag has been set
tmr_flag=0; //reset the flag
IO_FLP(IO_PORT, IO_PIN); //flip io_pin
}
}
}
=================================================
you can easily change it to another mcu. all it needed is to have a timer and interrupt capabilities. In this case, we used an 8-bit timer, but it can be easily a 16-bit timer as well.
Roman Black is actually multi-talented. a great guy to talk to as well. |
|