1:基础知识。 (官方都提供了 整理一下是为了方便以后开发的时候查看)
2:寄存器的了解
2.1 TOMD
2.2 对于 TOMD寄存器的配置
//;------------------------------------------------------------
//; P_TMR0_Ctrl (xxH) --------- T0MD Register
//;------------------------------------------------------------
//; C_TMR0_Clk C_TMR0_LowClk | Timer 0 Clock Source
//;------------------------------------------------------------
//; 0 x | From Instruction Clock
//; 1 0 | From External Pin
//; 1 1 | From Low Oscillator Frency (I_LRC or E_LXT)
//;------------------------------------------------------------
//;Bit[7,5:4] : Timer0 Clock Source Selection
#define C_TMR0_LowClk 0x80 //; Same as LCKTM0
#define C_TMR0_Clk 0x20 //; Same as T0CS
#define C_TMR0_ExtClk_Edge 0x10 //; Timer0 External Clock Edge Select --- 1:Falling Edge, 0:Rising Edge
//;Bit[6] : Reserved
//;Bit[3] : Watchdog Interrupt Source Selection / Timer0 Prescaler0 Selection
#define C_PS0_WDT 0x08 //; Prescaler0 is assigned to WDT
#define C_PS0_TMR0 0x00 //; Prescaler0 is assigned to TMR0
//;Bit[2:0] : Prescaler0 Dividing Rate Selection
#define C_PS0_Div 0x07 //; Prescaler0 Dividing Rate Selection
#define C_PS0_Div2 0x00
#define C_PS0_Div4 0x01
#define C_PS0_Div8 0x02
#define C_PS0_Div16 0x03
#define C_PS0_Div32 0x04
#define C_PS0_Div64 0x05
#define C_PS0_Div128 0x06
#define C_PS0_Div256 0x07
//
测试代码
=========================================================================*/
#include <ny8.h>
#include "ny8_constant.h"
#define UPDATE_REG(x) __asm__("MOVR _" #x ",F")
void main(void)
{
// Initialize System
DISI(); // Disable global interrupt
PCON = C_WDT_En | C_LVR_En; // Enable WDT, Enable LVR
// Initialize GPIO
IOSTB = 0xF8; // PB0.PB1.PB2: output mode, others: input mode
PORTB = 0x07; // PB0、PB1 & PB2 are output high
// Initialize Timer0
PCON1 = C_TMR0_En; // Enable Timer0
TMR0 = 0x00; // TMR0[7:0] = 0x00
T0MD = C_PS0_TMR0 | C_PS0_Div8; // Prescaler0 is assigned to Timer0, Prescaler0 dividing rate = 1:8, Timer0 clock source is instruction clock
// Initialize Interrupt
INTE = C_INT_TMR0; // Enable Timer0、Timer1、WDT overflow interrupt
ENI(); // Enable global interrupt
while(1);
}
// Interrupt Service Routine
void isr(void) __interrupt(0)
{
if(INTFbits.T0IF)
{
PORTBbits.PB0 ^=1; // PB0 Toggle
INTF= (unsigned char)~(C_INT_TMR0); // Clear T0IF
}
}