📄 f_13_15a_tmr1sqwave_bad.c
字号:
#include "config.h"
#include "serial.c"
#include "serio.c"
// half period in timer tics
#define HPERIOD 0x0100
// 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) {
// clear timer 1 to reset match
// write TMR1L byte last!
// triggers 16-bit write
TMR1H = 0;
TMR1L = 0;
CCP1IF = 0;
}
}
void main(void){
// 19200 in HSPLL mode, crystal = 7.3728 MHz
serial_init(95,1);
// initialize timer 1
// prescale by 1
T1CKPS1 = 0; T1CKPS0 = 0;
T1OSCEN = 0; // disable the oscillator
TMR1CS = 0; //use internal clock
T1SYNC = 0; // sync extern clock
T1RD16 = 1; // 16 bit r/w to timer1
bitclr(TRISC,2);// set RC2/CCP1 as output
// initialize CCPR1 for compare
CCPR1H = (HPERIOD >> 8);
CCPR1L = (0xFF & HPERIOD);
CCP1CON = 0x02; // toggle mode
CCP1IF = 0; // clear CCP1IF interupt flag
CCP1IE = 1; // capture interrupt enable
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 + -