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

Quadrature input on PortB int on change

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



Joined: 27 Jan 2004
Posts: 12

View user's profile Send private message

Quadrature input on PortB int on change
PostPosted: Thu Jun 03, 2004 7:48 am     Reply with quote

I am trying to decode quadrature signals A & B on a Linear Probe using interrupt on portB. I may not be defining the structure correct. My code is as follows. Thanks for any comments in advance.

Code:


#include <18F242.h>
#fuses EC,NOPROTECT,NOWDT,NOLVP,PUT,BROWNOUT
#use delay(clock=40000000)                       

// DEFINE PORT PINS

#define led_in              PIN_C4
#define led_out            PIN_C5
#define plc_up             PIN_C2        // Up Counter
#define plc_down         PIN_C3        // Down Counter

struct LinearProbeStruct {
      int unused:4;                       // RB[0:3]
      int A:1;                            // RB4
      int B:1;                            // RB5
      int unused2:2;                      // RB6-7
};

struct LinearProbeStruct LinearProbe;

#BYTE PORTB = 0x06
#BYTE LinearProbe = 0x06           // Place structure right over PORTB at location 0x06

// DEFINE VARIABLES

byte status;
int16 t_value;
signed int16 count = 0;

// START OF INTERRUPT ROUTINE

#int_RB
RB_isr()

{
   static BYTE OldPortB;
   BYTE byTemp;
   static struct LinearProbeStruct OldProbe;

   if (OldPortB != (byTemp=(PORTB & 0x30))) {
      OldPortB = byTemp;

      if (LinearProbe.A == OldProbe.A) {
         if (LinearProbe.B == LinearProbe.A)
            --count;
         else
            ++count;
      } else {
         OldProbe.A = LinearProbe.A;
            if (LinearProbe.B == LinearProbe.A)
               ++count;
            else
               --count;
      }
   }
}
Guest








Re: Quadrature input on PortB int on change
PostPosted: Fri Jun 04, 2004 2:49 am     Reply with quote

whmeade10 wrote:
I am trying to decode quadrature signals A & B on a Linear Probe using interrupt on portB. I may not be defining the structure correct. My code is as follows. Thanks for any comments in advance.

Code:


#include <18F242.h>
#fuses EC,NOPROTECT,NOWDT,NOLVP,PUT,BROWNOUT
#use delay(clock=40000000)                       

// DEFINE PORT PINS

#define led_in              PIN_C4
#define led_out            PIN_C5
#define plc_up             PIN_C2        // Up Counter
#define plc_down         PIN_C3        // Down Counter

struct LinearProbeStruct {
      int unused:4;                       // RB[0:3]
      int A:1;                            // RB4
      int B:1;                            // RB5
      int unused2:2;                      // RB6-7
};

struct LinearProbeStruct LinearProbe;

#BYTE PORTB = 0x06
#BYTE LinearProbe = 0x06           // Place structure right over PORTB at location 0x06

// DEFINE VARIABLES

byte status;
int16 t_value;
signed int16 count = 0;

// START OF INTERRUPT ROUTINE

#int_RB
RB_isr()

{
   static BYTE OldPortB;
   BYTE byTemp;
   static struct LinearProbeStruct OldProbe;

   if (OldPortB != (byTemp=(PORTB & 0x30))) {
      OldPortB = byTemp;

      if (LinearProbe.A == OldProbe.A) {
         if (LinearProbe.B == LinearProbe.A)
            --count;
         else
            ++count;
      } else {
         OldProbe.A = LinearProbe.A;
            if (LinearProbe.B == LinearProbe.A)
               ++count;
            else
               --count;
      }
   }
}

I'd suggest using the #locate directive, rather than #byte to put the variable where you want it.
PortB, is _not_ at address 6, on a 18F chip...
You can verify if the layout is working correctly, by simply(!), looking at the generated assembler for just one instruction accessing the bits, and making sure that the instruction is talking to the right bit number.
What else if on the bits for B change?. If you don't use the other two bits of portB, you don't have to do the 'change' test that you start the code with, since the interrupt will only occur when the bits do change. Saves quite a bit of time.
The tests look 'odd' to me. Normally you simply look for which edge has changed, and the status of the opposite edge at this time. So (assuming that the other bits of portB don't change):
struct LinearProbeStruct {
int unused:4; // RB[0:3]
int A:1; // RB4
int B:1; // RB5
int unused2:2; // RB6-7
};

union dblock {
struct LinearProbeStruct LP;
int8 b;
};

#BYTE PORTB = 0xF81
#BIT INTRBC = 0xFF2.0

#int_RB noclear
RB_isr()

{
static union dblock OldPortB;
union dblock byTemp;
do {
byTemp.b=PORTB;
//Clear the interrupt here
INTRBC=0;
if (byTemp.LP.A != OldPortB.LP.A) {
//Here 'A' has changed
if (byTemp.LP.A) {
//Here rising edge on A
if (byTemp.LP.B) --position;
else ++position;
}
else {
//Here falling edge on A
if (byTemp.LP.B) ++position;
else --position;
}
}
else {
//Here B changed
if (byTemp.LP.B) {
//Here there was a rising edge on B
if (byTemp.LP.A) ++position;
else --position;
}
else {
//Here falling edge on B
if (byTemp.LP.A) --position;
else ++position;
}
}
OldPortB.b=byTemp.b;
}
while (INTRBC);
}
[/code]
Note also, that the port is read just once, and all comparisons are done on old/new copies of the port. Also, use the 'noclear' option, and clear the interrupt immediately after reading the port, then loop at the end, if the interrupt has recurred. This massively increases the ability to handle fast changes. Though it looks more laborious (doing the four tests seperately), the code generated is quick for each route, giving good handling of movements.

Best Wishes
prwatCCS



Joined: 10 Dec 2003
Posts: 70
Location: West Sussex, UK

View user's profile Send private message

PostPosted: Fri Jun 04, 2004 3:34 am     Reply with quote

I would also look carefully at how fast your signals change. Dnt forget the horribly long latency in servicing irqs, due in part to the CCS code needing to save a whole host of things. You may need to consider use of the FAST irq available on the 18F series PICS
_________________
Peter Willis
Development Director
Howard Eaton Lighting Ltd UK
Guest








Re: Quadrature input on PortB int on change
PostPosted: Fri Jun 04, 2004 8:23 am     Reply with quote

Anonymous wrote:
whmeade10 wrote:
I am trying to decode quadrature signals A & B on a Linear Probe using interrupt on portB. I may not be defining the structure correct. My code is as follows. Thanks for any comments in advance.

Code:


#include <18F242.h>
#fuses EC,NOPROTECT,NOWDT,NOLVP,PUT,BROWNOUT
#use delay(clock=40000000)                       

// DEFINE PORT PINS

#define led_in              PIN_C4
#define led_out            PIN_C5
#define plc_up             PIN_C2        // Up Counter
#define plc_down         PIN_C3        // Down Counter

struct LinearProbeStruct {
      int unused:4;                       // RB[0:3]
      int A:1;                            // RB4
      int B:1;                            // RB5
      int unused2:2;                      // RB6-7
};

struct LinearProbeStruct LinearProbe;

#BYTE PORTB = 0x06
#BYTE LinearProbe = 0x06           // Place structure right over PORTB at location 0x06

// DEFINE VARIABLES

byte status;
int16 t_value;
signed int16 count = 0;

// START OF INTERRUPT ROUTINE

#int_RB
RB_isr()

{
   static BYTE OldPortB;
   BYTE byTemp;
   static struct LinearProbeStruct OldProbe;

   if (OldPortB != (byTemp=(PORTB & 0x30))) {
      OldPortB = byTemp;

      if (LinearProbe.A == OldProbe.A) {
         if (LinearProbe.B == LinearProbe.A)
            --count;
         else
            ++count;
      } else {
         OldProbe.A = LinearProbe.A;
            if (LinearProbe.B == LinearProbe.A)
               ++count;
            else
               --count;
      }
   }
}

I'd suggest using the #locate directive, rather than #byte to put the variable where you want it.
PortB, is _not_ at address 6, on a 18F chip...
You can verify if the layout is working correctly, by simply(!), looking at the generated assembler for just one instruction accessing the bits, and making sure that the instruction is talking to the right bit number.
What else if on the bits for B change?. If you don't use the other two bits of portB, you don't have to do the 'change' test that you start the code with, since the interrupt will only occur when the bits do change. Saves quite a bit of time.
The tests look 'odd' to me. Normally you simply look for which edge has changed, and the status of the opposite edge at this time. So (assuming that the other bits of portB don't change):
struct LinearProbeStruct {
int unused:4; // RB[0:3]
int A:1; // RB4
int B:1; // RB5
int unused2:2; // RB6-7
};

union dblock {
struct LinearProbeStruct LP;
int8 b;
};

#BYTE PORTB = 0xF81
#BIT INTRBC = 0xFF2.0

#int_RB noclear
RB_isr()

{
static union dblock OldPortB;
union dblock byTemp;
do {
byTemp.b=PORTB;
//Clear the interrupt here
INTRBC=0;
if (byTemp.LP.A != OldPortB.LP.A) {
//Here 'A' has changed
if (byTemp.LP.A) {
//Here rising edge on A
if (byTemp.LP.B) --position;
else ++position;
}
else {
//Here falling edge on A
if (byTemp.LP.B) ++position;
else --position;
}
}
else {
//Here B changed
if (byTemp.LP.B) {
//Here there was a rising edge on B
if (byTemp.LP.A) ++position;
else --position;
}
else {
//Here falling edge on B
if (byTemp.LP.A) --position;
else ++position;
}
}
OldPortB.b=byTemp.b;
}
while (INTRBC);
}
[/code]
Note also, that the port is read just once, and all comparisons are done on old/new copies of the port. Also, use the 'noclear' option, and clear the interrupt immediately after reading the port, then loop at the end, if the interrupt has recurred. This massively increases the ability to handle fast changes. Though it looks more laborious (doing the four tests seperately), the code generated is quick for each route, giving good handling of movements.

Best Wishes


Thanks for the quick response, i am new to the PIC18 series and have never used the change on Port B interrupt. I have been put between a rock and a hard place with this project I have been given 2 days to complete. The code you provided has helped tremendously. I have also ordered and will receive some new 18F2331 PISc today. These PICs have quadrature inputs built in. Have you had any experience with these chips? Also, I have the data sheet for the PIC18FXX2 and was wondering where I can find the Port address of #BYTE PORTB = 0xF81. Thanks again for your help.
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Fri Jun 04, 2004 10:14 am     Reply with quote

0xF81 IS the address for PortB on this pic. The register addresses are on page 45 of the data sheet.

Ronald
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