View previous topic :: View next topic |
Author |
Message |
Josh Davis Guest
|
Duplicating A #INT_EXT in code. |
Posted: Fri Jan 02, 2004 1:03 am |
|
|
Hi all, Hope you had a good christmas and a great new year.
Im using a rotary encoder to increment / decrement a variable. The encoder routine is working real good for me using phase a connected to RB0 and phase b connected to rb3.
The encoder routine is called from main via a case statement that is
trigered when a button is pushed.
Here is a code snip.
void main(Void){
set_tris_b(0b00001101);
set_tris_c(0b11111100);
freq_Mhz=200; //Inital turn on frequency
freq_Khz=10;
message_number=0; //init message state to known
disable_interrupts(GLOBAL);
enable_interrupts(INT_EXT);
ext_int_edge(H_TO_L);
Lcd_Start(); //Call LCD Function
LCD_Cmd( LCD_LINE1 );
Write_LCD("TEST LINE 1");
LCD_Cmd( LCD_LINE2 );
Write_LCD("Press Enter To Start");
while (true){
if ( toggle == 0 ){
short_delay();
if ( toggle == 0){
message_number++;
switch(message_number){
case 1:
Lcd_Cmd( CLEAR_DISP );
Lcd_cmd( LCD_LINE1 );
Write_LCD("Frequency Setup:");
Lcd_cmd( LCD_LINE2 );
Write_LCD("Press Enter Key");
break;
case 2:
enable_interrupts(GLOBAL);
output_high(d3led);
encoder(); //Call encoder routine running a interrupt.
break;
case 3:
output_low(d3led);
LCD_Cmd( CLEAR_DISP );
Lcd_cmd( LCD_LINE1 );
Write_LCD("System Started Ok");
Lcd_cmd( LCD_LINE2 );
printf(Write_LCD, "FREQ: = %u.%2u Mhz", freq_Mhz,Freq_Khz);
disable_interrupts(GLOBAL);
break;
default: message_number = 0;
} // end of switch
while(toggle==0); // Wait until button NOT pushed
}
}
}
}
//////////////////////////////////////////////////////////////////////
Ok case 2 will take me to my rotary encoder routine that shall increment/
decrement a variable and in turn displays this on my lcd.
Here is that code. This is working ok
#INT_EXT
void encoder(void){ // Change on falling edge of RB
if( ENC2 == 0 ){ // Read encoder B Phase
if( ENC2 == 0 )
++freq_mhz;
if (freq_mhz > 240) //Bound upper freq limit
freq_mhz=240;
FREQMSG(); // DISPLAY RESULT
}
else
{
--freq_mhz;
if (freq_mhz < 145) //Bound Lower freq limit
freq_mhz=145;
FREQMSG(); // DISPLAY RESULT
}}
But this is where Im stuck. I have another variable called freq_khz
but I would like to use the same encoder routine to also increment
it. I have tried to do this a couple of ways but I guess my method is
screwed...
so to sum up What im trying to do is, after I update the freq_mhz
by twisting the encoder shaft I wish to press the button "" Toggle" and then update the variable freq_khz.
When i press the toggle button again I return to case statement 3.
Anybody got any methods for the above ???
Thanks for your help.
Josh. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Jan 04, 2004 3:49 pm |
|
|
I didn't really try to understand your code, but just browsing it
I spotted this:
encoder(); // Call encoder routine running a interrupt.
This is never done. You don't "call" an interrupt routine.
It's only "called" by the occurrence of a hardware event.
(I'm speaking in terms of good programming practice). |
|
|
Guest
|
|
Posted: Tue Jan 06, 2004 8:18 pm |
|
|
Hi PCM, I know what you mean, but I think it really depends on the application.
In my case the encoder routine is used in conjunction with the interrupt
RB0 I dont see anything wrong with that for this application. But would be interested in your opinion as to why its not a good idea in this case. Or perhaps you might know of a better way of implementing the same that
you would like to share.
Best Regards. Josh. |
|
|
|