本帖最后由 wanghe334 于 2015-3-24 15:15 编辑
配置 timer1 的操作:
1. 将TON 位置1 (= 1)。
2. 使用TCKPS<1:0> 位选择定时器预分频比。
3. 使用TCS 和TGATE 位设置时钟和门控模式。
4. 将TSYNC位置1或清零来配置同步或异步操作。
5. 将定时器的周期值装入PR1 寄存器。
6. 如果需要中断,将中断允许位T1IE 置1。使用中
断优先级位T1IP<2:0> 来设置中断优先级。
timer1的代码:
- /*
- * File: ??mainXC16.c
- * Author: Administrator
- *
- * Created on 2015?3?23?, ??10:20
- */
- #include "xc.h"
- _FBS
- (
- BWRP_OFF & // Boot Segment Write Protect (Disabled)
- BSS_OFF // Boot segment Protect (No boot flash segment)
- )
- //_FGS
- //(
- // GWRP_OFF & // General Segment Flash Write Protect (General segment may be written)
- // GSS0_OFF // General Segment Code Protect (No Protection)
- //)
- _FOSCSEL
- (
- FNOSC_FRC & // Oscillator Select (8MHz FRC with Postscaler (FRCDIV))
- // SOSCSRC_ANA & // SOSC Source Type (Analog Mode for use with crystal)
- // LPRCSEL_HP & // LPRC Power and Accuracy (High Power/High Accuracy)
- IESO_ON // Internal External Switch Over bit (Internal External Switchover mode enabled (Two-speed Start-up enabled))
- )
- _FOSC
- (
- // POSCMD_HS & // Primary Oscillator Mode (Primary oscillator disabled)
- OSCIOFNC_ON & // CLKO Enable Configuration bit (CLKO output signal enabled)
- POSCFREQ_MS & // Primary Oscillator Frequency Range Configuration bits (Primary oscillator/external clock frequency between 100kHz to 8MHz)
- SOSCSEL_SOSCHP &// SOSC Power Selection Configuration bits (Secondary Oscillator configured for high-power operation)
- FCKSM_CSECME // Clock Switching and Monitor Selection (Clock Switching and Fail-safe Clock Monitor Enabled)
- )
- _FWDT
- (
- WDTPS_PS32768 & // Watchdog Timer Postscale Select bits (1:32768)
- FWPSA_PR128 & // WDT Prescaler bit (WDT prescaler ratio of 1:128)
- FWDTEN_OFF & // Watchdog Timer Enable bits (WDT disabled in hardware; SWDTEN bit disabled)
- WINDIS_OFF // Windowed Watchdog Timer Disable bit (Standard WDT selected (windowed WDT disabled))
- )
- // Warning:
- // Always enable MCLRE_ON config bit setting so that the MCLR pin function will
- // work for low-voltage In-Circuit Serial Programming (ICSP). The Microstick
- // programming circuitry only supports low-voltage ICSP. If you disable MCLR pin
- // functionality, a high-voltage ICSP tool will be required to re-program the
- // part in the future.
- _FPOR
- (
- BOREN_BOR3 & // Brown-out Reset Enable bits (Enabled in hardware; SBOREN bit disabled)
- PWRTEN_ON & // Power-up Timer Enable (PWRT enabled)
- I2C1SEL_PRI & // Alternate I2C1 Pin Mapping bit (Default SCL1/SDA1 Pins for I2C1)
- BORV_V18 & // Brown-out Reset Voltage bits (Brown-out Reset at 1.8V)
- MCLRE_ON // MCLR Pin Enable bit (RA5 input disabled; MCLR enabled)
- )
- _FICD
- (
- ICS_PGx3 // ICD Pin Placement Select (EMUC/EMUD share PGC3/PGD3)
- )
- int main(void) {
- TRISAbits.TRISA0 = 0;
- LATAbits.LATA0 = 1;
-
- //Init timer1.
- T1CON = 0x00; //Stops the Timer1 and reset control reg.
- T1CONbits.TCKPS0 =1;
- T1CONbits.TCKPS1 =0;
-
- TMR1 = 0x00; //Clear contents of the timer register
- PR1 = 0x000f; //Load the Period register with the value 0xFFFF
- IPC0bits.T1IP = 0x01; //Setup Timer1 interrupt for desired priority level.
-
- IFS0bits.T1IF = 0;
- IEC0bits.T1IE = 1;
- T1CONbits.TON = 1;
-
-
- while(1)
- {
-
- ;
- }
-
- return 0;
- }
- void __attribute__((interrupt, shadow, no_auto_psv)) _T1Interrupt(void)
- {
- LATAbits.LATA0 ^= 1;
- /* Interrupt Service Routine code goes here. */
- IFS0bits.T1IF = 0; //Reset Timer1 interrupt flag and Return from ISR
-
- }
复制代码
0
|
|
|
|
兄弟,你知道吗?这个是Proteus软件啊!呵呵
来吧,发Proteus板块呗。需要的帖子,而且我现在也在研究pic,我们可以一起的!
|
|
|
|
|