/*Capture mode時,外部CCP1事件觸發(fā)后,CCPR1H和CCPR1L將獲取Timer1的TMR1H和TMR1L中的數(shù)值對于CCP1的觸發(fā)事件,設置鍵CCP1Con中的相應位CCP1M3--CCP1M0CCP1IE使能中斷,中斷發(fā)生時,CCP1IF位設置為1,需要軟件進行清除* capture mode的設置步驟見datasheet 的page 65 setup for capture operation*/實現(xiàn)功能:開始屏幕顯示'system start '字樣,然后若外部CCP1發(fā)生falling edge,則會點亮LED ,RA=1;同時將獲取的timer1的16bit數(shù)發(fā)送到屏幕上,此時需要16進制顯示數(shù)字(因為可能獲取的數(shù)值不在ascii碼范圍內(nèi))#include #define uchar unsigned char#define uint unsigned int// CONFIG#pragma config FOSC = HS // 12MHZ ????#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT enabled)#pragma config PWRTE = ON // Power-up Timer Enable bit (PWRT disabled)#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)#define DELAY 200bit flag = 0;uchar str[]="system start!";void delay1ms(uint DelayTime) { uint temp; for (; DelayTime > 0; DelayTime--) { for (temp = 0; temp < 270; temp++) { ; } }}/** 設置步驟見datasheet 的page 65 setup for capture operation*/void Init_Capture() { CCP1M3 = 0; CCP1M2 = 1; CCP1M1 = 0; CCP1M0 = 0; // EVERY FALLING EDGE CAPTURE MODE CCPR1H = 0X00; CCPR1L = 0XFF; CCP1IE = 1; TRISC2 = 1; // INPUT MODE // TIMER1 SETTINGS T1CKPS1 = 0; T1CKPS0 = 0; T1OSCEN = 1; TMR1CS = 0; //FOSC /4 TMR1H = 0X00; TMR1L = 0X00; // INITIAL VALUE TMR1ON = 1;}void interrupt ISR() { if (CCP1IF == 1 && CCP1IE == 1) { RA0 ^= 1; CCP1IF = 0; CCP1IE = 0; flag = 1; } if (T0IE && T0IF) { T0IF = 0; //在此加入 TMR0 中斷服務 } if (TMR1IE && TMR1IF) //判 TMR1 中斷 { TMR1IF = 0; //在此加入 TMR1 中斷服務 }}void uart_init(void) { SPBRG = 0x4d; //設置通訊波特率為9600/秒 SPEN = 1; //1 = Serial port enabled (configures RC7/RX/DT and // RC6/TX/CK pins as serial port pins) RX9 = 0; //0 = Selects 8-bit reception SYNC = 0; //Asynchronous mode BRGH = 1; //1 = High speed TX9 = 0; //0 = Selects 8-bit transmission TXEN = 1; //1 = Transmit enabled CREN = 1; TRISC = 0x80; // bit7 rx input bit6 tx output 初始化端口C的方向控制寄存器( //對應的比特為0是輸出口,比特為1輸入口)}/***********************************************/void uart_send_byte(uchar uart_data) { TXREG = uart_data; while (1) { //等待發(fā)送完畢 if (TRMT ==1 ) break; // 最好不要使用TXIF判斷,這會在發(fā)送連續(xù)數(shù)據(jù)時發(fā)生數(shù)據(jù)丟失問題 }}/***********************************************/char uart_receive_byte(void) { char result = 0; if (RCIF == 1) //判是否收到了數(shù)據(jù)? { result = RCREG; //讀取接收到的數(shù)據(jù) } return (result);}void main(void){ uchar i, temp; uchar *str1; TRISA0 = 0; RA0 = 0; Init_Capture(); uart_init(); str1 = &str[0]; while(*str1) { uart_send_byte(*str1); str1++; } PEIE = 1; GIE = 1; // 在此處開中斷使能的原因是:開機后,CCP1 pin腳狀態(tài)不穩(wěn)定(可能是我的硬件電路設計不好)會不斷捕捉到falling edge觸發(fā)中斷 CCP1IF = 0; while (1) { if (flag == 1) { flag = 0; temp = CCPR1H; uart_send_byte(temp); temp = CCPR1L; uart_send_byte(temp); // 將捕獲的TMR1中內(nèi)容顯示在串口 //CCP1IE =1; } }}