完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
我正在测试一个程序,通过旋转旋转编码器旋钮顺时针或逆时针来增加或减少数字,并在LCD1602A上显示数字;但是下面的代码使数字只增加了一个方向(不能减少),不管编码器是逆时针还是顺时针旋转。顺时针方向,请有经验的眼睛帮我拾起我做错的事,谢谢!RA0,RA2连接到编码器的CLK和DT引脚;
以上来自于百度翻译 以下为原文 I'm test a program to increase or decrease the number by turning the rotary encoder knob clockwise or counter-clockwise; and display number on the LCD1602A; but the following code made the number just increased one direction(can't be decreased), regardless of turning the encoder knob clockwise or counter-clockwise, please the experienced eyes help to pick up what I'm doing wrong, Thanks! RA0, RA2 connected to CLK and DT pins of encoder; #include // BEGIN CONFIG #pragma config FEXTOSC = HS // Oscillator Selection bits (HS oscillator) #pragma config FCMEN = OFF // when external osc fail , turn on the internal osc monitor #pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT enabled) #pragma config PWRTS = OFF // 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 is digital I/O, HV on MCLR must be used for programming #pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off) #pragma config CSWEN = ON //clock switch writing enable bit //END CONFIG #define _XTAL_FREQ 20000000 #include "lcddisplay.h" #define REA LATAbits.LATA0 // Rotary encoder pin definition #define REB LATAbits.LATA2 void pbchange(void); //portb change routine this routine is being called by the interrrupt serivice routine on portb change interrupts __interrupt() void callISR(void) { if ( IOCAF!= 0x00 ) //check for PortB change interrupt { pbchange(); //call the routine } } unsigned char count; //this variable will incremented or decremented on encoder rotation void main() { TRISC = 0x00; // LCD Pins as Output TRISAbits.TRISA0 = 1; // set rotary encoder pins to input TRISAbits.TRISA2 = 1; ANSELAbits.ANSA0 = 0; ANSELAbits.ANSA2 = 0; LATC = 0x00; LATA= 0x00; lcdInit(); // inilized the LCD prints("Count = "); numToLcd(count); // display count to lcd IOCAF = 0x00; // clear the interrupt flag INTCONbits.GIE = 1; // enable the global interrupt PIE0bits.IOCIE = 1; // enable PORTB change interrupt IOCAPbits.IOCAP0 = 1; IOCANbits.IOCAN0 = 1; IOCAPbits.IOCAP2 = 1; IOCANbits.IOCAN2 = 1; while (1) { } } void pbchange(void ) { unsigned char state; static unsigned char oldstate; // this variable need to be static as it has to retain the value between calls __delay_ms(1); // delay for 1ms here for debounce state= REB<<1 | REA; // combine the pin status and assign to state variable if(oldstate==0b00){ if( state == 0b01) { count--; //decrement the count gotoXy(8,0); //goto proper position on the LCD screen numToLcd(count); //display the count value on to LCD }else if( state == 0b10) { count++; //decrement the count gotoXy(8,0); //goto proper position on the LCD screen numToLcd(count); //display the count value on to LCD } } oldstate = state; // store the current state value to oldstate value this value will be used in next call PORTA = PORTA; // read or Any read or write of PORTA,This will end the mismatch condition IOCAF = 0x00; // clear the porta change intrrupt flag } |
|
相关推荐
3个回答
|
|
幸运的是,你感觉到了任何变化。我猜你试着从一个更老的PIC中修改一些“更改代码”。你正在检查错误的位。使用LATX寄存器进行输出,PORTX寄存器用于输入。因此,您应该摆脱的唯一原因是由于这种虚假的指令,在这个PIC中,IOC外围设备不需要它,但在这样做时,您意外地将PATA值复制到LATA寄存器。LUWIN指令,清除IOCAF寄存器,就是在这个PIC中清除“不匹配”的东西。
以上来自于百度翻译 以下为原文 It's only by luck that your are sensing any changes at all. I guess you tried to adapt some "interrupt on change" code from a much older PIC. You are checking the wrong bits. Use LATx registers for output, PORTX registers for input. So, #define REA LATAbits.LATA0 // Rotary encoder pin definition #define REB LATAbits.LATA2 should be #define REA PORTAbits.RA0 // Rotary encoder pin definition #define REB PORTAbits.RA2 The only reason you are getting away with it is because of this spurious instruction PORTA = PORTA; // read or Any read or write of PORTA,This will end the mismatch condition You don't need that for the IOC peripheral in this PIC, but in doing it you are accidentally copying the PORTA value to the LATA register. (The following instruction, clearing the IOCAF register, is what "clears the mismatch" in this PIC.) |
|
|
|
另外,我不确定你的检测逻辑是否正确。如何在LCD上显示“状态”和“OLDSTATE”变量,看看它们是否是你所期望的。将LCD输出代码移出ISR是一个好主意。理想的是,你不应该做多于两个的事情。ISR内部的指令。你想尽快离开ISR以避免丢失的事件。
以上来自于百度翻译 以下为原文 Also, I'm not sure your detection logic is correct. How about displaying the "state" and "oldstate" variables on the LCD, and see if they are actually what you expect. It would be a good idea to move the LCD output code out of the ISR. Ideally you should never do things that take more than a couple of instructions inside an ISR. You want to get out of an ISR as quickly as possible to avoid missing events. |
|
|
|
你好:QYB:你有超凡的洞察力!是的,我这样做是为了匹配一个旧的PIC程序,但事实上我不能理解“PATA= PORTA”的含义,所以我试图使它变灰;但是程序不能正常运行,然后我必须重新添加它;现在我修改了“Laxax”到“Poxx”,然后灰色化了“PoTa= PORTA”,程序正在运行。!谢谢!我发现造成“数字减少”的原因不是我使用了RA0/ICSPDAT,而这个PIXIT3使用的PIN同时,我认为一旦程序建立和负载从计算机完成,RA0将不再被PACKIT3占用,但是这是错误的,PIN的状态将是现在,我把它们改成RA4和RA5(这是用于外部晶体的两个端口,现在必须使用内部振荡器,因为这个PIC只有14个引脚),那么编码器旋钮控制可以适当地上下创建数字;谢谢您的建议!你太棒了!
以上来自于百度翻译 以下为原文 Hi: qyb: You have superb insight! yes I did this to match an old pic program, but in fact I can't understand what's meaning of "PORTA = PORTA" , so I tried to gray it out; but the program could't run properly, then I had to add it back again; Now I revised the "LATAX" to "PORTX", and gray out the "PORTA = PORTA", program is running ! thanks! And I found the reason caused the "number decrease" didn't happen is I used the RA0/ICSPDAT, and this pin used by pickit3 at the same time, I thought once the program build and load from computer completed, the RA0 will not be occupied by pickit3 anymore, but that's wrong, the status of pin will be interfered ; Now I changed them to RA4 & RA5(this two port used for external crystal, have to use the internal oscillator now, because this pic only have 14 pins), then the encoder knob control can create number going up and down properly; thanks for your advise! you are superb! |
|
|
|
只有小组成员才能发言,加入小组>>
5204 浏览 9 评论
2016 浏览 8 评论
1942 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
3188 浏览 3 评论
请问电源和晶体值之间有什么关系吗?PIC在正常条件下运行4MHz需要多少电压?
2244 浏览 5 评论
755浏览 1评论
641浏览 1评论
有偿咨询,关于MPLAB X IPE烧录PIC32MX所遇到的问题
552浏览 1评论
PIC Kit3出现目标设备ID(00000000)与预期的设备ID(02c20000)不匹配。是什么原因
654浏览 0评论
554浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-6 03:17 , Processed in 2.209325 second(s), Total 49, Slave 42 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号