完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
你好,我正在使用PIC18F46K22微控制器来控制LED的去焊,然后在开关按钮被按下时在三个LED上应用PWM。现在AM股票如何使LED ECP引脚响应这一点。现在我的唯一问题是,大约3分钟后,LED关闭。这是一个不寻常的中断调用在我的代码,但我不能追踪它。任何提示,我会欣赏。
以上来自于百度翻译 以下为原文 Hello, I am working with pic18f46k22 microcontroller to control debouching of led and then apply pwm on the three leds when the switch button is pressed. For now am stock with how to make the leds eccp pins to respond to that. The debouching is working bit fine for now the only problem i have with it, is that after about 3 minutes the leds turn off. Which is an unusual interrupt calling in my code but i can't trace it out yet. Any hints I will appreciate. interrupt void interrupt high_priority__isr_handler(void){ if(INTCONbits.TMR0IF){ INTCONbits.TMR0IF =0; if(WPUBbits.WPUB2 && INTCON2bits.TMR0IP && buttonPressed != PORTBbits.RB2){ WPUBbits.WPUB2 =0; //pull up disable TMR0H =0; TMR0L =0; } } if(PIR1bits.TMR2IF){ PIR1bits.TMR2IF =0; T2CONbits.TMR2ON = 1; CCPR2L =0x07; //187 CCPR1L =0x07; //TMR2 =0; if(buttonPressed !=PORTBbits.RB2){ if(PORTBbits.RB2 ==0){ //button pressed LED0_OUT = !LED0_OUT; LED1_OUT = !LED1_OUT; LED2_OUT = !LED2_OUT; //flag =1; __delay_ms(50); if(PORTBbits.RB2 ==1){ //button release LED0_OUT =0; LED2_OUT =0; LED2_OUT =0; //flag =2; __delay_ms(50); } if(PORTBbits.RB2 ==1){ //button release and off leds LED0_OUT =1; LED2_OUT =1; LED2_OUT =1; // flag =3; __delay_ms(50); } } buttonPressed = PORTBbits.RB2; //update } } } main void main(void){ /*CCP2 P2A*/ TRISBbits.RB5 =0; /*CCP2 P2A*/ TRISCbits.RC1 =0; /*CCP1 P1A*/ TRISCbits.RC2 =0; /*LED OUTPUTS*/ //TRISBbits.RB0 = 0; //set RB0 to output //TRISBbits.RB1 = 0; //set RB1 to output //TRISBbits.RB2 = 0; //set RB2 to output /////////////////////////////////////////////////////////////////////////////// // PORTB<4:0> pins are configured as digital I/O on Reset //ANSELB = 0; //PBADEN: PORTB A/D Enable bit ANSELBbits.ANSB2 =0; ANSELBbits.ANSB5 =0; ANSELBbits.ANSB3 =0; ANSELCbits.ANSC2 =0; ////////////////////////////////////////////////////////////////////////////////////// TRISBbits.RB7 = 1; //set data pin input //PORTB Data Direction Register ///// INTERNAL OSCILLATOR MUX REGISTERS 16MHz//////////////////////////////////////////// //HFINTOSC 16MHz OSCCONbits.IRCF0 =1; OSCCONbits.IRCF1 =1; OSCCONbits.IRCF2 =1; //OSCILLATOR TUNING REGISTER OSCTUNEbits.PLLEN = 1; // Frequency Multiplier 4xPLL for HFINTOSC Enable bit //sources between4 MHz and 16 MHz ///////////////////////////////////////////////////////////////////////////////////////////// ///BUTTON//////////////////////////////////////////// TRISBbits.RB2 = 1; //INPUT PORTBbits.RB2 = 1; /////////////////////////////////////////////////////// ////Interrupt Priority Enable //////////////////////////////////////////////////// RCONbits.IPEN = 1; //Enable priority levels on interrupts TMR0H = 0xC2; // preset for Timer0 MSB register TMR0L = 0xF6; // preset for Timer0 LSB register init(); /*WPUB WEAK PULL-UP*/ WPUBbits.WPUB2 =1; /////////////////////////////////////////////////////////////////////////////////////// /*PR2 TIMER2 PERIOD REGISTER*/ PR2 =0x0F; // load with pwm period value 15 /*TIMER2 REGISTER*/ TMR2 =0; //INITIAL /*CAPTURE/COMPARE/PWM REGISTER 2, LOW BYTE*/ CCPR1L =0x07; // load this register and DCxB<1:0> bit //of CCPxcon register with PWM duty cycle CCPR1H =0x00; CCPR2L =0x07; CCPR2H =0; CCPR3L =0x07; CCPR3H =0x00; uint16_t dutyValue = 31; eccp1Set(); eccp2Set(); eccp3Set(); eccp1Set_dutyValue(dutyValue); eccp2Set_dutyValue(dutyValue); eccp3Set_dutyValue(dutyValue); buttonPressed = PORTBbits.RB2; LED0_OUT =0; LED1_OUT = 0; LED2_OUT = 0; counter++; if (counter > 32768){ //49910 |32768 LED0_OUT = ! LED0_OUT; LED2_OUT = ! LED2_OUT; LED1_OUT = ! LED1_OUT; counter = 0; __delay_ms(1000); } while(1){ T2CONbits.TMR2ON = 1; TMR2 =0; } //return (0); } Enhanced PWM void eccp2Set(void){ //RC1 /*ECCP2AS CCPX AUTO-SHUTDOWN CONTROL REGISTER*/ ECCP2ASbits.CCP2ASE =0;//must be cleared in software to restart PWM ECCP2ASbits.CCP2AS =0; //Auto-shutdown is disabled ECCP2ASbits.P2SSAC =0; //00 = Drive pins PxA and PxC to ?0? ECCP2ASbits.P2SSBD =0; /*PWM2CON ENHANCED PWM CONTROL REGISTER*/ PWM2CONbits.P2RSEN =1;//CCP2ASE bit clear software // PWM2CONbits.P2DC = 0xE8D4A510000;//Number of FOSC/4 (4 * TOSC) // PWM Delay Count bits /*CC1CON*/ CCP2CON = 0x1C; //CCP2CONbits.P2M = 0b00; //CCP2CONbits.DC2B = 0b01; // PWM Duty Cycle Least Significant bits //CCP2CONbits.CCP2M = 0b1100; //11xx =: PWM mode /*CCPTMRS0 PWM TIMER SELECT CONTROL REGSTER*/ //C2TSEL<1:0>: CCP2 Timer Selection bits CCPTMRS0bits.C2TSEL = 0b00; /*PSTR2CON PWMSTEERING CONTROL REGISTER*/ PSTR2CONbits.STR2SYNC =0; PSTR2CONbits.STR2D =0; //PxD assign to port pin PSTR2CONbits.STR2C =0; PSTR2CONbits.STR2B =0; PSTR2CONbits.STR2A =1; }//end eccp2Set void eccp2Set_dutyValue(uint16_t dutyValue){ // Writing to 8 MSBs of pwm duty cycle in CCPRL register CCPR2L = (dutyValue << 2); // Writing to 2 LSBs of pwm duty cycle in CCPCON register CCP2CON = (dutyValue >> 8); } .................................. bit setting void init(){ //T0CON: TIMER0 CONTROL REGISTER T0CONbits.TMR0ON = 1; //timer0 on/off control bit enable T0CONbits.T08BIT = 0; //config as 16-bit timer/counter T0CONbits.T0CS = 0; //internal instruction cycle clock CLKOUT T0CONbits.T0SE = 0; //increment low-to high on T0CKI pin T0CONbits.PSA = 0; //timer0 prescale is assign T0CONbits.T0PS2 = 1; //prescale value 1:256 T0CONbits.T0PS1 = 1; T0CONbits.T0PS0 = 1; /////////timer0 interrupt INTCON//////////////////////////////////// INTCONbits.GIE = 1; //enable global interrupt INTCONbits.PEIE = 1; //enable pheripheral interrupt INTCONbits.TMR0IE = 1; // Enables the TMR0 overflow interrupt (clear before re-entering in the interrupt) INTCONbits.INT0IE = 1; //enable the INT0 external interrupt INTCONbits.RBIE = 1; //enable the IOCx port change interrupt INTCONbits.TMR0IF = 0; // TMR0 register has overflowed (must be cleared in software) INTCONbits.RBIF = 0; //none of the (rb<7:4>) pins have changed state ///////////////*INTCON2 INTERRUPT CONTROL 2 REGISTER*//////////////////// INTCON2bits.INTEDG0 = 0; //interrpt 0 edge select bit INTCON2bits.INTEDG1 = 0; //interrupt on falling edge INTCON2bits.INTEDG2 = 0; //interrupt on falling egde INTCON2bits.TMR0IP = 1; //TMR0 Overflow Interrupt Priority bit INTCON2bits.RBIP = 1; //high interrupt priority ////////////*INTCON3 INTERRUPT REGISTER*/ INTCON3bits.INT2IP = 1; //high priority INTCON3bits.INT1IP = 1; //int1 high priority INTCON3bits.INT2IE = 1; //enable int2 external interrupt INTCON3bits.INT1IE = 1; //disables int1 external interrupt INTCON3bits.INT2IF = 0; //clear int2 interrupt INTCON3bits.INT1IF = 0; //int1 interrupt not set /*PIR1 PHERIPHERAL INTERRUPT REQUEST FLAG*/ PIR1bits.ADIF =0; //a/d conversion clear flag PIR1bits.TX1IF=0; //transmit interrupt flag PIR1bits.SSP1IF =0; //master synchrous trans/receive PIR1bits.CCP1IF =0;//ccp1 interrupt flag PIR1bits.TMR2IF =0;//tmr2 to pr2 match interrupt flag PIR1bits.TMR1IF =0; //tmr1 overflow flag /*PIR2 PHERIPHERAL INTERRUPT REQUEST FLAG*/ PIR2bits.CCP2IF =0; // not used in pwm /*PIR4 PHERIPHERAL INTERRUPT REQUEST FLAG*/ PIR4bits.CCP3IF =0; //ECCP3 Interrupt Flag bits /*PIE1 PHERIPHERAL INTERRUPT ENABLE*/ PIE1bits.ADIE =0; PIE1bits.RC1IE =0; PIE1bits.TX1IE =0; PIE1bits.SSP1IE =0; PIE1bits.CCP1IE =1; //Enables the CCP1 interrupt PIE1bits.TMR2IE =1; PIE1bits.TMR1IE =0; /*PIE2 PHERIPHERAL INTERRUPT ENABLE*/ PIE2bits.CCP2IE = 1; //CCP2 Interrupt Enable bit /*PIE4 PHERIPHERAL INTERRUPT ENABLE*/ PIE4bits.CCP3IE =1; //CCP3IE: CCP3 Interrupt Enable bit /*IPR1 PHERIPHERAL INTERRUPT PRIORITY REGISTER 1*/ IPR1bits.ADIP =0; IPR1bits.RC1IP =0; IPR1bits.TX1IP =0; IPR1bits.SSP1IP =0; IPR1bits.CCP1IP =1; //CCP1 Interrupt Priority bit IPR1bits.TMR2IP =1; //TMR2 to PR2 Match Interrupt Priority bit IPR1bits.TMR1IP =0; //low priority /*IPR2 PHERIPHERAL INTERRUPT PRIORITY REGISTER 1*/ IPR2bits.CCP2IP =1; //CCP2 Interrupt Priority bit /*IPR4 PHERIPHERAL INTERRUPT PRIORITY REGISTER 4*/ IPR4bits.CCIP3IP =1; //CCP3 Interrupt Priority bit // PWM2CONbits.P2DC = 0xE8D4A510000;//Number of FOSC/4 (4 * TOSC) // PWM Delay Count bits CM2CON1bits.C2SYNC =0; //C2 output is asynchronous CM2CON1bits.C1SYNC =0; /*T2CON CONTROL REGISTER*/ //11 = Prescaler is 16 & 0000 = 1:1 Postscaler T2CONbits.T2OUTPS = 0b0000; T2CONbits.TMR2ON = 1; //timer2 on T2CONbits.T2CKPS = 0b11; }//end init() |
|
相关推荐
19个回答
|
|
你没有看门狗或LVP,是吗?(显示您的配置)
以上来自于百度翻译 以下为原文 You don't have watchdog or LVP enabled, do you? (show your CONFIGs) |
|
|
|
看门狗是启用WDTEN=on,但不是LVP。
以上来自于百度翻译 以下为原文 Watchdog is enable WDTEN = ON but not LVP. Thanks in advanced. // CONFIG1H #pragma config FOSC = INTIO67 // Oscillator Selection bits (Internal oscillator block) #pragma config PLLCFG = OFF // 4X PLL Enable (Oscillator multiplied by 4) #pragma config PRICLKEN = ON // Primary clock enable bit (Primary clock is always enabled) #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 PWRTEN = ON // Power-up Timer Enable bit (Power up timer enabled) #pragma config BOREN = OFF // Brown-out Reset Enable bits (Brown-out Reset disabled in hardware and software) #pragma config BORV = 285 // Brown Out Reset Voltage bits (VBOR set to 2.85 V nominal) // CONFIG2H #pragma config WDTEN = ON // Watchdog Timer Enable bits (WDT is always enabled. SWDTEN bit has no effect) #pragma config WDTPS = 32768 // Watchdog Timer Postscale Select bits (1:32768) // CONFIG3H #pragma config CCP2MX = PORTC1 // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1) #pragma config PBADEN = OFF // PORTB A/D Enable bit (PORTB<5:0> pins are configured as digital I/O on Reset) #pragma config CCP3MX = PORTE0 // P3A/CCP3 Mux bit (P3A/CCP3 input/output is mulitplexed with RE0) #pragma config HFOFST = ON // HFINTOSC Fast Start-up (HFINTOSC output and ready status are not delayed by the oscillator stable status) #pragma config T3CMX = PORTC0 // Timer3 Clock input mux bit (T3CKI is on RC0) #pragma config P2BMX = PORTC0 // ECCP2 B output mux bit (P2B is on RC0) #pragma config MCLRE = EXTMCLR // 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 disabled) #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 Block 0 (Block 0 (000800-003FFFh) not code-protected) #pragma config CP1 = OFF // Code Protection Block 1 (Block 1 (004000-007FFFh) not code-protected) #pragma config CP2 = OFF // Code Protection Block 2 (Block 2 (008000-00BFFFh) not code-protected) #pragma config CP3 = OFF // Code Protection Block 3 (Block 3 (00C000-00FFFFh) 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 Block 0 (Block 0 (000800-003FFFh) not write-protected) #pragma config WRT1 = OFF // Write Protection Block 1 (Block 1 (004000-007FFFh) not write-protected) #pragma config WRT2 = OFF // Write Protection Block 2 (Block 2 (008000-00BFFFh) not write-protected) #pragma config WRT3 = OFF // Write Protection Block 3 (Block 3 (00C000-00FFFFh) not write-protected) // CONFIG6H #pragma config WRTC = ON // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) 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 Block 0 (Block 0 (000800-003FFFh) not protected from table reads executed in other blocks) #pragma config EBTR1 = OFF // Table Read Protection Block 1 (Block 1 (004000-007FFFh) not protected from table reads executed in other blocks) #pragma config EBTR2 = OFF // Table Read Protection Block 2 (Block 2 (008000-00BFFFh) not protected from table reads executed in other blocks) #pragma config EBTR3 = OFF // Table Read Protection Block 3 (Block 3 (00C000-00FFFFh) 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) |
|
|
|
所以你的PIC每一次都可能重置:尝试在主循环中添加一个CrrWdTh()
以上来自于百度翻译 以下为原文 So your PIC is possibly resetting every that time: try adding a ClrWdt() inside your main loop |
|
|
|
|
|
|
|
我禁用配置狗TooFor中的看门狗,它工作得很好,谢谢帮助。
以上来自于百度翻译 以下为原文 I disable watchdog in the config too #pragma config WDTEN = OFF instead of #pragma config WDTEN = ON And It work perfectly thanks for the help #CinziaG. Now how can I go around to implement pwm effects on individual eccp leds pins, thanks for any ideas. |
|
|
|
好!是的,我也是HTTP://www. McCHIP.COM/FUMMS/M9844 18.ASPX。
以上来自于百度翻译 以下为原文 good! and yep, me too http://www.microchip.com/forums/m984418.aspx |
|
|
|
|
|
|
|
我想写的8位MSB的10位PWM占空比在CCPR2L和2 LMB的PWM占空比在DC2B&LT;1:0&GT位的CCP2CON寄存器。我不确定这是否会写2个LMB位的占空比到第5th-4位置的CCP2 CON寄存器。感谢先进的任何想法。
以上来自于百度翻译 以下为原文 I want to write the 8 m*** of the 10 bit pwm duty cycle in the CCPR2L and the 2 lmb of the pwm duty cycle in the DC2B<1:0> bits of the CCP2CON register. I am not sure if this will write 2 lmb bits of the duty cycle to the 5th-4th position of the CCP2CON register. Thanks in advanced for any ideas. void eccp2Set_dutyValue(uint16_t dutyValue){ // Writing to 8 MSBs of pwm duty cycle in CCPR2L register CCPR2L = (dutyValue << 2); // Writing to 2 LSBs of pwm duty cycle in CCPCON register int temp = ((dutyValue >> 8) << 4); //store in 5th-4th position of ccp2con in DC2B<1:0> CCP2CON = (CCP2CON & temp); //temp temporal variable } |
|
|
|
更像
以上来自于百度翻译 以下为原文 More like void eccp2Set_dutyValue(uint16_t dutyValue){ // Writing to 8 MSBs of pwm duty cycle in CCPR2L register CCPR2L = (dutyValue >> 2); // Writing to 2 LSBs of pwm duty cycle in CCPCON register int temp = (dutyValue << 4); //store in 5th-4th position of ccp2con in DC2B<1:0> CCP2CON = ((CCP2CON & 0b11001111) | temp); //temp temporal variable } |
|
|
|
|
|
|
|
或者只是让编译器为YouEdgDe:添加缺少的“位”:
以上来自于百度翻译 以下为原文 Or just let the compiler do the legwork for you CCP2CONbits.DC2B = dutyValue & 0b11; Edit: added the missing "bits" :) |
|
|
|
|
|
|
|
|
|
|
|
(我只是不确定那个结构成员的名字!)
以上来自于百度翻译 以下为原文 (I was just not sure of that struct member name!) |
|
|
|
PWM占空比仅更新ECCP引脚一次,我认为占占空比的50%。我指派了DutyValue= 31,这大概是50%,当我运行程序时,引脚立即增加到亮度。是的PWM工作,但我的目标是当我按下开关按钮和按住按钮,ECCP应该增加一些时间延迟到亮度。我不知道如何关闭来自国家的LED。我怀疑我错过了其他的东西。感谢先进的任何想法。
以上来自于百度翻译 以下为原文 The pwm duty cycle update the eccp pins just once which i think is at 50% of the duty cycle. I assigned dutyValue =31, which is supposedly to the 50% and when i run the program the pins instantly increase to brightness. Yes the pwm is working but my goal is when i pressed switch button and hold button, the eccps should increase with some time delays to brightness. I fail how to turn off the leds from on state. I suspect am missing something else . Thanks in advanced for any ideas. global variable uint16_t dutyValue = 31; // load just once main void main(void){ .................................................................................... ///BUTTON//////////////////////////////////////////// TRISBbits.RB2 = 1; //INPUT PORTBbits.RB2 = 1; /////////////////////////////////////////////////////// ////Interrupt Priority Enable //////////////////////////////////////////////////// RCONbits.IPEN = 1; //Enable priority levels on interrupts TMR0H = 0xC2; // preset for Timer0 MSB register TMR0L = 0xF6; // preset for Timer0 LSB register init(); ClrWdt(); /*WPUB WEAK PULL-UP*/ WPUBbits.WPUB2 =1; /*PR2 TIMER2 PERIOD REGISTER*/ PR2 =0x0F; //load with pwm period value 15 /*TIMER2 REGISTER*/ TMR2 =0; //INITIAL eccp1Set(); eccp2Set(); eccp3Set(); __delay_ms(500); while(1){ /*I thinks this is logically right not sure */ if(TMR2 > PR2){ //check if the tmr2 is > pr2 and turn on leds LED0_OUT =0; LED1_OUT =0; LED2_OUT =0; } else if(TMR2 == PR2){ //off led LED0_OUT =1; LED1_OUT =1; LED2_OUT =1; }else{ //tmr2 < pr2 off led LED0_OUT =1; LED1_OUT =1; LED2_OUT =1; } } //return (0); } interrupt void interrupt high_priority__isr_handler(void){ if(INTCONbits.TMR0IF){ INTCONbits.TMR0IF =0; if(WPUBbits.WPUB2 && INTCON2bits.TMR0IP && buttonPressed != PORTBbits.RB2){ WPUBbits.WPUB2 =0; //pull up disable TMR0H =0; TMR0L =0; } } if(PIR1bits.TMR2IF){ PIR1bits.TMR2IF =0; TMR2 = 0; //initialize timer 2 /*I failed to turn off the leds after they are on can't think straight for now*/ if(!PORTBbits.RB2){ //button pressed ++dutyValue; }else{ --dutyValue; } if(dutyValue < 1023){ //if the duty cycle is less than 1024 increase the brightness eccp1Set_dutyValue(dutyValue); eccp2Set_dutyValue(dutyValue); eccp3Set_dutyValue(dutyValue); dutyValue++; //increment }else if(dutyValue ==1023){ //here the code is doing nothing and am surprise!! eccp1Set_dutyValue(dutyValue); eccp2Set_dutyValue(dutyValue); eccp3Set_dutyValue(dutyValue); dutyValue =0; } } } Duty Cycle void eccp2Set_dutyValue(uint16_t dutyValue){ // Writing to 8 MSBs of pwm duty cycle in CCPR2L register CCPR2L = (dutyValue >> 2); // Writing to 2 LSBs of pwm duty cycle in CCPCON register CCP2CONbits.DC2B = dutyValue & 0b11; //store in 5th-4th position of ccp2con in DC2B<1:0> } |
|
|
|
MMMM你有硬件PWM模块(S),你正在做一个SW-PWM出来吗?
以上来自于百度翻译 以下为原文 mmmm you have hardware PWM module(s) and you're making a SW PWM out of it? |
|
|
|
是的,我已经设置了硬件板PIC18,并使软件PWM控制三增强型PWM ECCP RB5,RC1和RC2引脚。
以上来自于百度翻译 以下为原文 Yes I have setup the hardware pic18 on breadboard and making the software PWM to control three enhanced pwm eccp RB5, RC1 and RC2 pins. |
|
|
|
请想象如下:按钮按下,dutyValue= 1021:-dutyValue++& gt;1022(dutyValue& lt;1023)-gt;true= & gt;dutyValue++& gt;1023,现在来自下一个ISR调用的两个选项:1)按钮仍然被按下:-dutyValue++& gt;1024(您应该处理您的(10位)dutyValueValabl的溢出)结果:DutyValuy将从YouReCpxStasdDutyValuy(DutyValuy)内读取为0。但它从不被调用,因为duty值既不持有(x& lt;1023)也不(x==1023)。2按钮释放:- duty-Value- -gt;1022 -(dutyValuth& lt;1023)-& gt;true= & gt;dutyValue++& gt;1023结果:除非按钮再次按下,否则duty值将永远保持1022。以前的if或if语句都是真的。有些情况下你会输入这个语句,但是当你可能期望它时,0将不会传递给你的ReCurrPcxStxdutyValk(DutyValk)函数。首先你设置输出1023,然后将变量重置为0。R增加到1,导致(dutyValue & lt;1023)变为真,或者递减到65535(下溢)。要关闭你的LED尝试如下:-按下按钮直到亮度不再增加。-然后释放按钮并等待几个周期,直到忠实值从0溢出到65535,可能少一些LSB。-再次按下按钮,等到DutyValuy溢出到0(DutyValue& Lt.;1023)一个周期,此时你的LED应该关闭。我真的怀疑这是你想要达到的。你能更详细地解释一下你长时间按下/释放按钮后会发生什么吗?费边主义
以上来自于百度翻译 以下为原文 if(PIR1bits.TMR2IF){ PIR1bits.TMR2IF =0; TMR2 = 0; //initialize timer 2 /*I failed to turn off the leds after they are on can't think straight for now*/ if(!PORTBbits.RB2){ //button pressed ++dutyValue; }else{ --dutyValue; } if(dutyValue < 1023){ //if the duty cycle is less than 1024 increase the brightness eccp1Set_dutyValue(dutyValue); eccp2Set_dutyValue(dutyValue); eccp3Set_dutyValue(dutyValue); dutyValue++; //increment }else if(dutyValue ==1023){ //here the code is doing nothing and am surprise!! eccp1Set_dutyValue(dutyValue); eccp2Set_dutyValue(dutyValue); eccp3Set_dutyValue(dutyValue); dutyValue =0; } } Please imagine the following: Button pressed, dutyValue = 1021: - dutyValue++ -> 1022 - (dutyValue < 1023) -> true => dutyValue++ -> 1023 Now two options from the next ISR call: 1) Button remains pressed: - dutyValue++ -> 1024 (you should handle overflow of your (10 bit) dutyValue variable) Result: dutyValue would read as 0 from within your eccpxSet_dutyValue(dutyValue). But it is never called since dutyValue holds neither of (x < 1023) or (x == 1023). 2) Button released: - dutyValue-- -> 1022 - (dutyValue < 1023) -> true => dutyValue++ -> 1023 Result: dutyValue will remain 1022 forever unless the button is pressed again. Note: Your else if(dutyValue ==1023) won't be entered if any of the previous if or else if statements were true. There are situations where you will enter this statement, but 0 will not be passed to your eccpxSet_dutyValue(dutyValue) function when you would probably expect it. First you set the output 1023, then you reset the variable to 0. Next call you either increment to 1, leading to (dutyValue < 1023) become true or you decrement to 65535 (underflow). To turn off you LEDs try the following: - press the button until the brightness does not increase anymore. - then release the button and wait a few cycles till dutyValue faithfully underflowed from 0 to 65535, maybe a few LSBs less. - press the button again and wait till dutyValue overflows to 0 being less (dutyValue < 1023) For one cycle your LEDs should be off at this point. I honestly doubt that this is what you want to achieve. Can you please in more detail explain what you expect to happen once you long-press/release the button? Regards Fabian |
|
|
|
谢谢你,法比安,其实目标是当按钮被按下一次LED应该打开,但不是在最大亮度。然后再长时间按下,LED的亮度应该增加到完全亮度,即使在按钮被释放后保持接通状态。如果按钮再次被按下,第二次LED应该立即关掉。如果在LED完全亮后释放后,再进行第二次长时间按压,LED的亮度会降低到低亮度,我会尽快在晚上工作。你的贡献是有帮助的。我希望现在对我所期待的描述有点清楚。
以上来自于百度翻译 以下为原文 Thank you #Fabian actually the goal is when the button is pressed just once the LED's should turn on but not at maximum brightness. And then further long pressing, the led's should increase until full brightness is attain and keep the on state even after the button is released. If button is pressed once again second time the led's should instantly toggle off. If after the released when the led's are at full brightness follow by second long pressing and hold the led's should faint to low brightness. I will work on it in the evening as soon as possible.... your contribution is helpful. I hope the description of what am expecting is bit clear now. |
|
|
|
只有小组成员才能发言,加入小组>>
5248 浏览 9 评论
2037 浏览 8 评论
1957 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
3218 浏览 3 评论
请问电源和晶体值之间有什么关系吗?PIC在正常条件下运行4MHz需要多少电压?
2265 浏览 5 评论
788浏览 1评论
680浏览 1评论
有偿咨询,关于MPLAB X IPE烧录PIC32MX所遇到的问题
608浏览 1评论
PIC Kit3出现目标设备ID(00000000)与预期的设备ID(02c20000)不匹配。是什么原因
685浏览 0评论
582浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-28 10:41 , Processed in 1.778221 second(s), Total 112, Slave 96 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号