CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Not able to make a simple interrupt work

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
eyewonder300



Joined: 09 Jun 2004
Posts: 52

View user's profile Send private message

Not able to make a simple interrupt work
PostPosted: Fri Oct 02, 2015 3:32 pm     Reply with quote

PIC16F887, compiler 4.069

I am unable to make a simple RB change interrupt work.
Code:


#include "T:\Lube_Timer\Lube_Timer.h"
#include "T:\Lube_Timer\Noritake_1.h"

#int_RB
   void rb_isr(void)
   {
   vfd_putc("\fEncoder counted");
   disable_interrupts(int_rb);
   delay_ms(1500);
   vfd_putc("\fISR time out");
   delay_ms(1500);
   vfd_putc("\f");
   enable_interrupts(int_rb5);   
   }

void main()
{
    vfd_init();
   delay_ms(500);
   vfd_putc("Hello World");
  enable_interrupts(INT_RB);
   enable_interrupts(global);   
   while(true)
      {

      }
}


Code:
#include <16F887.h>
#device *=16
#device adc=8

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES HS                       //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES PUT                      //Power Up Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOCPD                    //No EE protection
#FUSES BROWNOUT                 //Reset when brownout detected
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOWRT                    //Program memory not write protected
#FUSES BORV40                   //Brownout reset at 4.0V

#use delay(clock=20000000)       //20MHz ceramic resonator


The VFD works, displays 'Hello World', but no change when I toggle bits RB5 or RB4 (only testing those two bits at this time). Scope shows pulses going from +5 to Gnd, 15us width.

Cheers,
Steve
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Oct 02, 2015 4:19 pm     Reply with quote

Here is a test program that I simplified from here:
http://www.ccsinfo.com/forum/viewtopic.php?t=42384&start=1
See if this works:
Code:

#include <16F887.h>
#fuses INTRC_IO, NOWDT, BROWNOUT, PUT, NOLVP
#use delay(clock=4M)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

short int dbutton1, dbutton2;

//-------------------------
// This function reads Port B without changing the TRIS.
int8 input_state_b(void)
{
#byte PortB = getenv("SFR:PORTB") 

return(PortB);
}

//-------------------------

#define HIGHTOLOW  FALSE
#define LOWTOHIGH  TRUE

int8 current, last;

#int_rb
void int_rb_isr(void)
{
current = input_state_b();

#if LOWTOHIGH
   if ((!bit_test(last,1))&&(bit_test(current,1)))
       dbutton1 = TRUE;
   if ((!bit_test(last,2))&&(bit_test(current,2)))
       dbutton2 = TRUE;
#elif HIGHTOLOW
   if((!bit_test(current,1))&&(bit_test(last,1)))
       dbutton1 = TRUE;
   if((!bit_test(current,2))&&(bit_test(last,2)))
       dbutton2 = TRUE;
#endif

last = current;

delay_ms(1);
}

//------------------------------
void init()
{
dbutton1=0;
dbutton2=0;

current = input_state_b();
last = current;

}

//=======================================     
void main()
{
init();

port_b_pullups(0b00110000); // Enable pullups on PortB bits 4, 5
delay_us(10);   // Allow time for pull-ups to initialize

enable_interrupts(INT_RB4);
enable_interrupts(INT_RB5);

input_state_b();   // Clear mismatch condition

clear_interrupt(INT_RB);

enable_interrupts(GLOBAL);

while(1)
  {
   if(dbutton1)
     {
      putc('1');
      dbutton1 = FALSE;
     }

   if(dbutton2)
     {
      putc('2');
      dbutton2 = FALSE;
     }
  }
}
 
eyewonder300



Joined: 09 Jun 2004
Posts: 52

View user's profile Send private message

PostPosted: Fri Oct 02, 2015 6:24 pm     Reply with quote

PCM,

I'm doing this project on a board I have already wire-wrapped. I will need to wire up a MAX232 chip to use between the PIC & PC - later tonight, or early tomorrow.

I had to change the chip clock to 20Mhz to match my hardware.

When I tried to compile I got an error:
"Undefined Identifier: INT_EXT_L2H"

Cheers,
Steve
eyewonder300



Joined: 09 Jun 2004
Posts: 52

View user's profile Send private message

PostPosted: Fri Oct 02, 2015 6:27 pm     Reply with quote

Oops,

I copied your code again, and it compiled - must have done something wrong.

Cheers,
Steve
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Oct 02, 2015 6:44 pm     Reply with quote

The code in the link has that line in it, but the code I posted above does not.
I removed all the INT_EXT code, since you only want interrupt-on-change.
Use the code I posted in this thread.
Ttelmah



Joined: 11 Mar 2010
Posts: 19488

View user's profile Send private message

PostPosted: Sat Oct 03, 2015 12:07 am     Reply with quote

As a comment, do a search here on the things that _must_ happen in an RB interrupt. Note that PCM programmer's code does one critical thing that yours doesn't. Without this, when the interrupt triggers, it'll effectively never exit.....
eyewonder300



Joined: 09 Jun 2004
Posts: 52

View user's profile Send private message

Update
PostPosted: Sat Oct 03, 2015 5:27 pm     Reply with quote

I finally got a MAX232 chip working on my wire-wrap board. I had to use another PC to receive the output from 'puts'. I wrote a prog to continuously send 'puts ("Sending to PC")' to the PC. That worked, confirming that I am sending out good 9600 Baud RS232 to the PC.

Back to the code in the 2nd post, changed clock to the 20MHz I am using, no other changes. Programmed the PIC, took it over to the receiving PC, toggled my RB4 & RB5 bits, and no output over the RS232 to the PC. Using logic probe, confirmed that RB4 & 5 were toggling.

So interrupts still not working. More ideas?

Cheers,
Steve
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Oct 03, 2015 11:06 pm     Reply with quote

You have an older version of the compiler. I'm not at a location where I
have all my older versions. Compile the following program and post the
.LST file. Set the build options so the .LST file is in "normal CCS format",
ie, numbered registers with no register names. I want to see if your
compiler is doing certain things correctly. Make sure your listing shows
the fuses at the end.
Code:

#include <16F887.h>
#device *=16
#device adc=8

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES HS                       //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES PUT                      //Power Up Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOCPD                    //No EE protection
#FUSES BROWNOUT                 //Reset when brownout detected
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOWRT                    //Program memory not write protected
#FUSES BORV40                   //Brownout reset at 4.0V

#use delay(clock=20000000)       //20MHz ceramic resonator


// This function reads Port B without changing the TRIS.
int8 input_state_b(void)
{
#byte PortB = getenv("SFR:PORTB") 

return(PortB);
}


#int_rb
void rb_isr(void)
{
int8 current;

current = input_state_b();
}

//==========================================
void main()
{
port_b_pullups(0b00110000); // Enable pullups on PortB bits 4, 5
delay_us(10);   // Allow time for pull-ups to initialize

enable_interrupts(INT_RB4);
enable_interrupts(INT_RB5);

input_state_b();   // Clear mismatch condition

clear_interrupt(INT_RB);

enable_interrupts(GLOBAL);

while(TRUE);
}



I did notice something a little odd about the listing below. For the line,
Code:
clear_interrupt(INT_RB); 

CCS is reading PortB. Properly, they should be clearing the RBIF flag.
They are clearing the change condition which sets RBIF, but they are
not clearing RBIF itself. That's a bug. The 16F887 data sheet says:
Quote:

The user, in the Interrupt Service Routine, clears the interrupt by:
a) Any read or write of PORTB. This will end the mismatch condition.
b) Clear the flag bit RBIF.

My expectation would be that clear_interrupt(INT_RB) would only clear
the RBIF flag. Reading PortB would be left to the user.

Here is the .LST file for vs. 5.049:
Code:

CCS PCM C Compiler, Version 5.049, xxxxx    03-Oct-15 21:51

               Filename:   C:\Program Files\PICC\Projects\PCM_Test\PCM_Test.lst

               ROM used:   101 words (1%)
                           Largest free fragment is 2048
               RAM used:   13 (4%) at main() level
                           14 (4%) worst case
               Stack used: 0 locations
               Stack size: 8

*
0000:  MOVLW  00
0001:  MOVWF  0A
0002:  GOTO   039
0003:  NOP
0004:  MOVWF  7F
0005:  SWAPF  03,W
0006:  CLRF   03
0007:  MOVWF  21
0008:  MOVF   0A,W
0009:  MOVWF  20
000A:  CLRF   0A
000B:  MOVF   04,W
000C:  MOVWF  22
000D:  MOVF   77,W
000E:  MOVWF  23
000F:  MOVF   78,W
0010:  MOVWF  24
0011:  MOVF   79,W
0012:  MOVWF  25
0013:  MOVF   7A,W
0014:  MOVWF  26
0015:  BCF    03.7
0016:  BCF    03.5
0017:  BTFSS  0B.3
0018:  GOTO   01B
0019:  BTFSC  0B.0
001A:  GOTO   02C
001B:  MOVF   22,W
001C:  MOVWF  04
001D:  MOVF   23,W
001E:  MOVWF  77
001F:  MOVF   24,W
0020:  MOVWF  78
0021:  MOVF   25,W
0022:  MOVWF  79
0023:  MOVF   26,W
0024:  MOVWF  7A
0025:  MOVF   20,W
0026:  MOVWF  0A
0027:  SWAPF  21,W
0028:  MOVWF  03
0029:  SWAPF  7F,F
002A:  SWAPF  7F,W
002B:  RETFIE
002C:  BCF    0A.3
002D:  BCF    0A.4
002E:  GOTO   032
.................... #include <16F887.h> 
... //////////// Standard Header file for the PIC16F887
(CCS boilerplate removed for brevity)
.................... #device PIC16F887
.................... 
.................... #list
.................... 
.................... #device *=16 
.................... #device adc=8 
.................... 
.................... #FUSES NOWDT                    //No Watch Dog Timer 
.................... #FUSES HS                       //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD) 
.................... #FUSES PUT                      //Power Up Timer 
.................... #FUSES NOPROTECT                //Code not protected from reading 
.................... #FUSES NOCPD                    //No EE protection 
.................... #FUSES BROWNOUT                 //Reset when brownout detected 
.................... #FUSES IESO                     //Internal External Switch Over mode enabled 
.................... #FUSES FCMEN                    //Fail-safe clock monitor enabled 
.................... #FUSES NODEBUG                  //No Debug mode for ICD 
.................... #FUSES NOWRT                    //Program memory not write protected 
.................... #FUSES BORV40                   //Brownout reset at 4.0V 
.................... 
.................... #use delay(clock=20000000)       //20MHz ceramic resonator 
.................... 
.................... 
.................... // This function reads Port B without changing the TRIS. 
.................... int8 input_state_b(void) 
.................... { 
.................... #byte PortB = getenv("SFR:PORTB")   
.................... 
.................... return(PortB); 
002F:  MOVF   06,W
0030:  MOVWF  78
0031:  RETURN
.................... } 
.................... 
.................... 
.................... #int_rb
.................... void rb_isr(void)
.................... {
.................... int8 current;
.................... 
.................... current = input_state_b(); 
0032:  CALL   02F
0033:  MOVF   78,W
0034:  MOVWF  28
0035:  BCF    0B.0
0036:  BCF    0A.3
0037:  BCF    0A.4
0038:  GOTO   01B
.................... }
.................... 
.................... //==========================================
.................... void main() 
0039:  MOVF   03,W
003A:  ANDLW  1F
003B:  MOVWF  03
003C:  BSF    03.5
003D:  BSF    03.6
003E:  MOVF   09,W
003F:  ANDLW  C0
0040:  MOVWF  09
0041:  BCF    03.6
0042:  BCF    1F.4
0043:  BCF    1F.5
0044:  MOVLW  00
0045:  BSF    03.6
0046:  MOVWF  08
0047:  BCF    03.5
0048:  CLRF   07
0049:  CLRF   08
004A:  CLRF   09
004B:  BCF    03.7
.................... { 
.................... port_b_pullups(0b00110000); // Enable pullups on PortB bits 4, 5 
004C:  MOVLW  30
004D:  BSF    03.5
004E:  BCF    03.6
004F:  MOVWF  15
0050:  BCF    01.7
.................... delay_us(10);   // Allow time for pull-ups to initialize 
0051:  MOVLW  10
0052:  MOVWF  77
0053:  DECFSZ 77,F
0054:  GOTO   053
0055:  NOP
.................... 
.................... enable_interrupts(INT_RB4); 
0056:  BCF    03.5
0057:  BSF    0B.3
0058:  BSF    03.5
0059:  BSF    16.4
.................... enable_interrupts(INT_RB5); 
005A:  BCF    03.5
005B:  BSF    0B.3
005C:  BSF    03.5
005D:  BSF    16.5
.................... 
.................... input_state_b();   // Clear mismatch condition 
005E:  BCF    03.5
005F:  CALL   02F
.................... 
.................... clear_interrupt(INT_RB); 
0060:  MOVF   06,W
.................... 
.................... enable_interrupts(GLOBAL); 
0061:  MOVLW  C0
0062:  IORWF  0B,F
.................... 
.................... 
.................... 
.................... while(TRUE); 
0063:  GOTO   063
.................... }
.................... 
0064:  SLEEP
.................... 
.................... 

Configuration Fuses:
   Word  1: 2FE2   HS NOWDT PUT MCLR NOPROTECT NOCPD BROWNOUT IESO FCMEN NOLVP NODEBUG
   Word  2: 0700   BORV40 NOWRT

eyewonder300



Joined: 09 Jun 2004
Posts: 52

View user's profile Send private message

Compile list
PostPosted: Sun Oct 04, 2015 9:13 am     Reply with quote

PCM,

Here is the listing from the code you posted.

Code:
CCS PCM C Compiler, Version 4.069, 42050               04-Oct-15 10:07

               Filename: Test_List.lst

               ROM used: 105 words (1%)
                         Largest free fragment is 2048
               RAM used: 15 (4%) at main() level
                         16 (4%) worst case
               Stack:    3 worst case (1 in main + 2 for interrupts)

*
0000:  MOVLW  00
0001:  MOVWF  0A
0002:  GOTO   03D
0003:  NOP
0004:  MOVWF  7F
0005:  SWAPF  03,W
0006:  CLRF   03
0007:  MOVWF  21
0008:  MOVF   0A,W
0009:  MOVWF  20
000A:  CLRF   0A
000B:  MOVF   04,W
000C:  MOVWF  22
000D:  MOVF   77,W
000E:  MOVWF  23
000F:  MOVF   78,W
0010:  MOVWF  24
0011:  MOVF   79,W
0012:  MOVWF  25
0013:  MOVF   7A,W
0014:  MOVWF  26
0015:  MOVF   7B,W
0016:  MOVWF  27
0017:  BCF    03.7
0018:  BCF    03.5
0019:  BTFSS  0B.3
001A:  GOTO   01D
001B:  BTFSC  0B.0
001C:  GOTO   030
001D:  MOVF   22,W
001E:  MOVWF  04
001F:  MOVF   23,W
0020:  MOVWF  77
0021:  MOVF   24,W
0022:  MOVWF  78
0023:  MOVF   25,W
0024:  MOVWF  79
0025:  MOVF   26,W
0026:  MOVWF  7A
0027:  MOVF   27,W
0028:  MOVWF  7B
0029:  MOVF   20,W
002A:  MOVWF  0A
002B:  SWAPF  21,W
002C:  MOVWF  03
002D:  SWAPF  7F,F
002E:  SWAPF  7F,W
002F:  RETFIE
0030:  BCF    0A.3
0031:  BCF    0A.4
0032:  GOTO   036
.................... #include <16F887.h>
.................... //////// Standard Header file for the PIC16F887 device ////////////////
.................... #device PIC16F887
.................... #list
.................... 
.................... #device *=16
.................... #device adc=8
.................... 
.................... #FUSES NOWDT                    //No Watch Dog Timer
.................... #FUSES HS                       //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
.................... #FUSES PUT                      //Power Up Timer
.................... #FUSES NOPROTECT                //Code not protected from reading
.................... #FUSES NOCPD                    //No EE protection
.................... #FUSES BROWNOUT                 //Reset when brownout detected
.................... #FUSES IESO                     //Internal External Switch Over mode enabled
.................... #FUSES FCMEN                    //Fail-safe clock monitor enabled
.................... #FUSES NODEBUG                  //No Debug mode for ICD
.................... #FUSES NOWRT                    //Program memory not write protected
.................... #FUSES BORV40                   //Brownout reset at 4.0V
.................... 
.................... #use delay(clock=20000000)       //20MHz ceramic resonator
.................... 
.................... 
.................... // This function reads Port B without changing the TRIS.
.................... int8 input_state_b(void)
.................... {
.................... #byte PortB = getenv("SFR:PORTB") 
.................... 
.................... return(PortB);
0033:  MOVF   06,W
0034:  MOVWF  78
.................... }
0035:  RETLW  00
.................... 
.................... 
.................... #int_rb
.................... void rb_isr(void)
.................... {
.................... int8 current;
.................... 
.................... current = input_state_b();
0036:  CALL   033
0037:  MOVF   78,W
0038:  MOVWF  29
.................... }
.................... 
.................... //==========================================
0039:  BCF    0B.0
003A:  BCF    0A.3
003B:  BCF    0A.4
003C:  GOTO   01D
.................... void main()
.................... {
003D:  CLRF   04
003E:  BCF    03.7
003F:  MOVLW  1F
0040:  ANDWF  03,F
0041:  BSF    03.5
0042:  BSF    03.6
0043:  MOVF   09,W
0044:  ANDLW  C0
0045:  MOVWF  09
0046:  BCF    03.6
0047:  BCF    1F.4
0048:  BCF    1F.5
0049:  MOVLW  00
004A:  BSF    03.6
004B:  MOVWF  08
004C:  BCF    03.5
004D:  CLRF   07
004E:  CLRF   08
004F:  CLRF   09
.................... port_b_pullups(0b00110000); // Enable pullups on PortB bits 4, 5
0050:  MOVLW  30
0051:  BSF    03.5
0052:  BCF    03.6
0053:  MOVWF  15
0054:  BCF    01.7
.................... delay_us(10);   // Allow time for pull-ups to initialize
0055:  MOVLW  10
0056:  MOVWF  77
0057:  DECFSZ 77,F
0058:  GOTO   057
0059:  NOP
.................... 
.................... enable_interrupts(INT_RB4);
005A:  BCF    03.5
005B:  BSF    0B.3
005C:  BSF    03.5
005D:  BSF    16.4
.................... enable_interrupts(INT_RB5);
005E:  BCF    03.5
005F:  BSF    0B.3
0060:  BSF    03.5
0061:  BSF    16.5
.................... 
.................... input_state_b();   // Clear mismatch condition
0062:  BCF    03.5
0063:  CALL   033
.................... 
.................... clear_interrupt(INT_RB);
0064:  BCF    0B.0
.................... 
.................... enable_interrupts(GLOBAL);
0065:  MOVLW  C0
0066:  IORWF  0B,F
.................... 
.................... while(TRUE);
0067:  GOTO   067
.................... }
0068:  SLEEP

Configuration Fuses:
   Word  1: 2FE2   HS NOWDT PUT MCLR NOPROTECT NOCPD BROWNOUT IESO FCMEN NOLVP NODEBUG
   Word  2: 3FFF   NOWRT BORV40


Cheers,
Steve
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Oct 04, 2015 10:14 am     Reply with quote

I don't see any important differences (that would cause your problem).
I'll test your code this evening with your hardware input conditions.
eyewonder300



Joined: 09 Jun 2004
Posts: 52

View user's profile Send private message

Port B pullups?
PostPosted: Sun Oct 04, 2015 5:09 pm     Reply with quote

In testing this problem, I enabled all Port B pull-ups, and then put a meter on the pins. They were reading 0v - do I not understand what the internal (weak) pull-ups should be doing?

Cheers,
Steve
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Oct 04, 2015 7:36 pm     Reply with quote

I got back my normal location and tested it in hardware and quickly
found my mistake. When I translated my test program to your setup,
I neglected to change the bits that are tested in #int_rb to bits 4 and 5.

The #int_rb routine should be as shown below:
Code:
#int_rb
void int_rb_isr(void)
{
current = input_state_b();

#if LOWTOHIGH
   if ((!bit_test(last,4))&&(bit_test(current,4)))
       dbutton1 = TRUE;
   if ((!bit_test(last,5))&&(bit_test(current,5)))
       dbutton2 = TRUE;
#elif HIGHTOLOW
   if((!bit_test(current,4))&&(bit_test(last,4)))
       dbutton1 = TRUE;
   if((!bit_test(current,5))&&(bit_test(last,5)))
       dbutton2 = TRUE;
#endif

last = current;

delay_ms(1);
}

As soon as I did that, it started working. If I press push-buttons on
either pin B4 or B5, I get '1' or '2' displayed in the TeraTerm window.
Here I have pushed the button on B4 several times, followed by B5:
Quote:
11111111122222

This was tested with vs. 4.069.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Oct 04, 2015 7:43 pm     Reply with quote

Quote:
In testing this problem, I enabled all Port B pull-ups, and then put a meter
on the pins. They were reading 0v - do I not understand what the internal
(weak) pull-ups should be doing?

If the PIC is held in reset by the ICD or Pickit programmer, you will
likely see 0v with a meter on the PortB pins. That's what I got just now.

Here's the test program I used:
Code:

#include <16F887.h>
#fuses HS, NOWDT, BROWNOUT, PUT, NOLVP
#use delay(clock=20M)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

//=======================================     
void main()
{
port_b_pullups(0xFF);   // Enable pull-ups on all PortB pins

while(TRUE);
}


If you click the "release from reset" button in MPLAB vs. 8.92, then you
will see some of the PortB pins go to a high level. But, if the programmer
is still connected to pins B6 and B7, you will see a voltage such as 0.92v.
That's because the programmer has 4.7K pull-downs on those pins.
Remove the programmer from the board, and measure pins B6 and B7
again. This time you will get 4.77v. At least that's what I got.


Last edited by PCM programmer on Sun Oct 04, 2015 7:47 pm; edited 1 time in total
eyewonder300



Joined: 09 Jun 2004
Posts: 52

View user's profile Send private message

PostPosted: Sun Oct 04, 2015 7:46 pm     Reply with quote

I'll test this tomorrow at work, and report back.

Cheers,
Steve
eyewonder300



Joined: 09 Jun 2004
Posts: 52

View user's profile Send private message

Success
PostPosted: Mon Oct 05, 2015 3:19 pm     Reply with quote

Thanks to all, I have my prog working as needed. Some of the problems were from my fuse selections (or lack thereof), and not understanding the interrupt enabling, & testing.

My hardware is a quadrature encoder chip, and it outputs a low going pulse on one pin for count up, and a low going pulse on another pin for count down. The chip prohibits both from being low at the same time, so whichever (port RB4 or RB5) is low is my count direction. If both are hi, then some other interrupt on the port was active.

Here is the code that worked for me:

Code:

// This function reads Port B without changing the TRIS.
//int8 input_state_b(void)
//   {
//   #byte PortB = getenv("SFR:PORTB")
//   return(PortB);
//   }


#int_RB
void  RB_isr(void)
   {
   current = input_b();
   disable_interrupts(global);
   if ((!bit_test(current,5)))    //LOW signal is COUNT UP pulse from encoder
      {
      counter_val = counter_val + 1;
      printf(vfd_putc,"\fCounter: %3u",counter_val);
      }
   if ((!bit_test(current,4)))    //LOW signal is COUNT DOWN pulse from encoder
      {
      counter_val = counter_val - 1;
      printf(vfd_putc,"\fCounter: %3u",counter_val);
      }
   }

void main()
   {
   counter_val = 0;
   vfd_init();
   vfd_putc("Hello, Steve");
   delay_ms(1000);
   vfd_putc("\fWaiting for ISR");
   delay_ms(1000);
   set_tris_b(0x3F);
   port_b_pullups(0b00111111);
   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
//   enable_interrupts(INT_RB);
   enable_interrupts(INT_RB4);
   enable_interrupts(INT_RB5);
   clear_interrupt(int_rb);
   enable_interrupts(GLOBAL);
//Setup_Oscillator parameter not selected from Intr Oscillator Config tab

   // TODO: USER CODE!!
   while(true){};
   }
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group