访问手机版页面
你的位置:老古开发网 > Pic单片机 > PIC16系列单片机 > 正文  
PIC16F877A Capture Mode
内容导读:
/*Capture mode时,外部CCP1事件触发后,CCPR1H和CCPR1L将获取Timer1的TMR1H和TMR1L中的数值对于CCP1的触发事件,设置键CCP1Con中的相应位CCP1M3--CCP1M0CCP1IE使能中断,中断发生时,CCP1IF位设置为1,需要软件进行

/*Capture mode时,外部CCP1事件触发后,CCPR1H和CCPR1L将获取Timer1的TMR1H和TMR1L中的数值对于CCP1的触发事件,设置键CCP1Con中的相应位CCP1M3--CCP1M0CCP1IE使能中断,中断发生时,CCP1IF位设置为1,需要软件进行清除* capture mode的设置步骤见datasheet 的page 65 setup for capture operation*/实现功能:开始屏幕显示'system start '字样,然后若外部CCP1发生falling edge,则会点亮LED ,RA=1;同时将获取的timer1的16bit数发送到屏幕上,此时需要16进制显示数字(因为可能获取的数值不在ascii码范围内)#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) { //等待发送完毕 if (TRMT ==1 ) break; // 最好不要使用TXIF判断,这会在发送连续数据时发生数据丢失问题 }}/***********************************************/char uart_receive_byte(void) { char result = 0; if (RCIF == 1) //判是否收到了数据? { result = RCREG; //读取接收到的数据 } 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脚状态不稳定(可能是我的硬件电路设计不好)会不断捕捉到falling edge触发中断 CCP1IF = 0; while (1) { if (flag == 1) { flag = 0; temp = CCPR1H; uart_send_byte(temp); temp = CCPR1L; uart_send_byte(temp); // 将捕获的TMR1中内容显示在串口 //CCP1IE =1; } }}

标签: capture,mode,pic16f877a,
来源:互联网 作者:karen 时间:2018/12/18 11:00:01
相关阅读
推荐阅读
阅读排行
最近更新
商品推荐