完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
我正在设计一个仪表,在LCD显示器上显示2个频率,我能够实现1的频率(通过MCCP),但是我不能理解如何使用CCP2模块进行第二次捕获。
以上来自于百度翻译 以下为原文 I am designing an Meter to display 2 Frequency on a LCD Display,I am Able to achieve 1 frequency (Via MCCP) but I cant understand how to engage 2nd Capture using CCP2 Module.MCU Used -pic18F4520 /* * File: main.c * Author: Golden Hash * * Created on December 10, 2017, 4:13 PM */ //#define _XTAL_FREQ 20000000 #include #include #define USE_OR_MASKS #include #include #include "HD44780.h" //#define f_timer 2000000 #pragma config OSC = HS // Oscillator Selection bits (Internal oscillator block, port function on RA6 and RA7) #pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled) #pragma config IESO = OFF // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled) // CONFIG2L #pragma config PWRT = OFF // Power-up Timer Enable bit (PWRT disabled) #pragma config BOREN = SBORDIS // Brown-out Reset Enable bits (Brown-out Reset enabled in hardware only (SBOREN is disabled)) #pragma config BORV = 3 // Brown Out Reset Voltage bits (Minimum setting) // CONFIG2H #pragma config WDT = OFF // Watchdog Timer Enable bit (WDT enabled) #pragma config WDTPS = 32768 // Watchdog Timer Postscale Select bits (1:32768) // CONFIG3H #pragma config CCP2MX = PORTC // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1) #pragma config PBADEN = ON // PORTB A/D Enable bit (PORTB<4:0> pins are configured as analog input channels on Reset) #pragma config LPT1OSC = OFF // Low-Power Timer1 Oscillator Enable bit (Timer1 configured for higher power operation) #pragma config MCLRE = ON // MCLR Pin Enable bit (MCLR pin enabled; RE3 input pin disabled) // CONFIG4L #pragma config STVREN = ON // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset) #pragma config LVP = OFF // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled) #pragma config XINST = OFF // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode)) // CONFIG5L #pragma config CP0 = OFF // Code Protection bit (Block 0 (000800-001FFFh) not code-protected) #pragma config CP1 = OFF // Code Protection bit (Block 1 (002000-003FFFh) not code-protected) #pragma config CP2 = OFF // Code Protection bit (Block 2 (004000-005FFFh) not code-protected) #pragma config CP3 = OFF // Code Protection bit (Block 3 (006000-007FFFh) not code-protected) // CONFIG5H #pragma config CPB = OFF // Boot Block Code Protection bit (Boot block (000000-0007FFh) not code-protected) #pragma config CPD = OFF // Data EEPROM Code Protection bit (Data EEPROM not code-protected) // CONFIG6L #pragma config WRT0 = OFF // Write Protection bit (Block 0 (000800-001FFFh) not write-protected) #pragma config WRT1 = OFF // Write Protection bit (Block 1 (002000-003FFFh) not write-protected) #pragma config WRT2 = OFF // Write Protection bit (Block 2 (004000-005FFFh) not write-protected) #pragma config WRT3 = OFF // Write Protection bit (Block 3 (006000-007FFFh) not write-protected) // CONFIG6H #pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write-protected) #pragma config WRTB = OFF // Boot Block Write Protection bit (Boot block (000000-0007FFh) not write-protected) #pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM not write-protected) // CONFIG7L #pragma config EBTR0 = OFF // Table Read Protection bit (Block 0 (000800-001FFFh) not protected from table reads executed in other blocks) #pragma config EBTR1 = OFF // Table Read Protection bit (Block 1 (002000-003FFFh) not protected from table reads executed in other blocks) #pragma config EBTR2 = OFF // Table Read Protection bit (Block 2 (004000-005FFFh) not protected from table reads executed in other blocks) #pragma config EBTR3 = OFF // Table Read Protection bit (Block 3 (006000-007FFFh) not protected from table reads executed in other blocks) // CONFIG7H #pragma config EBTRB = OFF // Boot Block Table Read Protection bit (Boot block (000000-0007FFh) not protected from table reads executed in other blocks) #include #include #include #include "HD44780.h" //############################################################################# #define ONE_SECOND_X_100 100000/16 //prescalar used 1:8 #define ONE_SECOND_X_1000 1000000/16 #define TRUE 1 #define FALSE 0 unsigned char readyToCalculate = FALSE; unsigned char captureEvent = FALSE; unsigned char timer2flag = FALSE; unsigned char zeroFrequency = TRUE; unsigned char mode = 1; float timerPast; volatile unsigned char Roll_over_cnt=0; union join { unsigned char parts[2]; unsigned int whole; }; union join timerCurrent; unsigned long remaining_T=0; float calculation(float time, float * pastTime, int mode) { float returnData; float timeBetween = time - *pastTime; *pastTime = time; if (mode == 1) // FREQENCY_HIGH_RESOLUTION { returnData = ONE_SECOND_X_1000 / timeBetween; } else if (mode == 2) // FREQENCY_LOW_RESOLUTION { returnData = ONE_SECOND_X_100 / timeBetween; } return returnData; } void timers(void) { //timer 2 500mS timer TMR2ON = 0; //timer off T2CKPS1=1; T2CKPS0 = 1; // pre scalar 64 TOUTPS3 =TOUTPS2 =TOUTPS1=TOUTPS0 = 0b1111; //post scalar 16 PR2 = 255; //255 tmr preiod counter TMR2IE = 1; // enable timer interrupts TMR2IF = 0; // clear timer interrupt flag TMR2ON = 1; //timer on //timer 1 for input capture TMR1ON = 0; //timer off TMR1CS = 0; //forc / 4 T1CKPS0=1;// pre scalar 8 T1CKPS1=1; TMR1IE = 1; // enable timer interrupts TMR1IF = 0; // clear timer interrupt flag TMR1ON = 1; //timer on } void input_capure(void) { TRISC2=1; //CAPTURE INPUT //RC2 input CCP1IF = 0; // cleanr flag CCP1CON = 0x05; //ccp capures on every rising edge CCP1IE = 1; //ccp module interrupt enable } void initialize(void) { timers(); input_capure(); PEIE = 1; //enable perifiral interrupts GIE = 1; //enable global interrupts } void interrupt isr(void) { // timer 2 interrupt if (TMR2IF) { static int timeCount = 0; if (timeCount >= 2) { timeCount = 0; timer2flag = TRUE; } else { timeCount++; } TMR2IF = 0; //clear interrupt flag } // timer 1 interrupt if (TMR1IF) { Roll_over_cnt++; TMR1IF = 0; //clear interrupt flag } if (CCP1IF) { timerCurrent.parts[0] = CCPR1L; timerCurrent.parts[1] = CCPR1H; if (zeroFrequency == TRUE) { zeroFrequency = FALSE; captureEvent = TRUE; timerPast = timerCurrent.whole; } else { readyToCalculate = TRUE; captureEvent = TRUE; } Roll_over_cnt=0; CCP1IF = 0; // cleanr flag } } unsigned char buff1[10],buff2[4]; void main() { unsigned int i=0; ADCON0bits.ADON=0;//DISABLE ANALOG PORT AND ENABLE DIGITAL PORT ON 18F4520 TRISCbits.RC7=1; TRISCbits.RC6=1; ADCON1=0x0F;// ANALOG MODULE DISABLED ADCON0bits.ADON=0;// ANALOG MODULE DISABLED TRISCbits.RC1=1; //PORTC Set To Inputs (CCP1 is on PortC) TRISCbits.RC2 =1; LCDInit(LS_NONE); //Clear the display LCDClear(); float dataHolder=0; initialize(); while(1) { if (readyToCalculate == TRUE) { dataHolder = calculation(timerCurrent.whole, &timerPast,mode); readyToCalculate = FALSE; } if (timer2flag == TRUE) { if (captureEvent == FALSE) { LCDClear(); LCDGotoXY(0,0); LCDWriteString("00000"); } else { LCDClear(); sprintf( buff2,"%.2f",dataHolder); i=dataHolder*100; LCDGotoXY(0,0); LCDWriteString(buff2); captureEvent = FALSE; } timer2flag = FALSE; } } } |
|
相关推荐
1个回答
|
|
有什么原因不能复制CCP1到CCP2的CCP代码吗?显然,需要一些简单的更改来适应并使用正确的输入PIN,但它似乎并不太困难。我有一个旧的数据表,好像在将CCP2寄存器(ECCP外设)标记为CCP1时出错。我想这可能会让人困惑。也许一个更新的数据表已经确定了。XC8 1.44中的PIC.18F420.H文件也不包含CCP2CON寄存器中的上2位。然而,在你的申请中似乎不需要这些。我错过了什么,就是挂断你的电话?
以上来自于百度翻译 以下为原文 Is there some reason you can't copy the CCP code you have changing CCP1 to CCP2? Obviously a number of simple changes are needed to fit this in and use the correct input pin, but it doesn't seem too difficult. I have an old datasheet that seems to have an error in labeling CCP2 registers (the ECCP peripheral) as CCP1. I guess that could be confusing. Perhaps a newer datasheet has fixed that. The pic.18f4520.h file in xc8 1.44 also doesn't include the upper 2 bits in the CCP2CON register. These don't seem needed in your application, however. What am I missing that is hanging you up? |
|
|
|
只有小组成员才能发言,加入小组>>
5184 浏览 9 评论
2005 浏览 8 评论
1932 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
3179 浏览 3 评论
请问电源和晶体值之间有什么关系吗?PIC在正常条件下运行4MHz需要多少电压?
2229 浏览 5 评论
739浏览 1评论
626浏览 1评论
有偿咨询,关于MPLAB X IPE烧录PIC32MX所遇到的问题
511浏览 1评论
PIC Kit3出现目标设备ID(00000000)与预期的设备ID(02c20000)不匹配。是什么原因
637浏览 0评论
535浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-28 07:51 , Processed in 1.182614 second(s), Total 77, Slave 61 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号