📄 f_13_15b_tmr1sqwave_good.c
字号:
#include "config.h"
#include "serial.c"
#include "serio.c"
#define HPERIOD 0x0100
unsigned int match;
// uses Timer1, compare & toggle mode to generate sq wave
#if defined(HI_TECH_C)
void interrupt timer_isr(void)
#endif
#if defined(__18CXX)
#pragma interrupt timer_isr
void timer_isr(void)
#endif
{
if (CCP1IF) {
// don't clear timer1, change compare register
match = match + HPERIOD;
CCPR1H = match >> 8;
CCPR1L = match & 0xFF;
CCP1IF = 0;
}
}
void main(void){
// 19200 in HSPLL mode, crystal = 7.3728 MHz
serial_init(95,1);
// initialize timer 1
T1CKPS1 = 0; T1CKPS0 = 0;// prescale by 1
// use internal clock
T1OSCEN = 0; TMR1CS = 0; T1SYNC = 0;
T1RD16 = 1; // 16 bit r/w to timer1
bitclr(TRISC,2);// set RC2/CCP1 as output
// initialize CCPR1 for compare
match = HPERIOD;
CCPR1H = (HPERIOD >> 8);
CCPR1L = (0xFF & HPERIOD);
CCP1CON = 0x02; // toggle mode
// interrupt enable
CCP1IF = 0; CCP1IE = 1;
TMR1ON = 1; // enable timer 1
IPEN = 0; PEIE = 1; GIE = 1;
printf("Configured!");pcrlf();
while(1); // interrupt does all work
// of generating sqwave
}
//for MCC18, place the interrupt vector goto
#if defined(__18CXX)
#if defined(HIGH_INTERRUPT)
#pragma code HighVector=HIGH_INTERRUPT
#else
#pragma code HighVector=0x0008
#endif
void HighVector (void)
{
_asm goto timer_isr _endasm
}
#pragma code
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -