![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
whmeade10
Joined: 27 Jan 2004 Posts: 12
|
Quadrature input on PortB int on change |
Posted: Thu Jun 03, 2004 7:48 am |
|
|
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;
}
}
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
Re: Quadrature input on PortB int on change |
Posted: Fri Jun 04, 2004 2:49 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
prwatCCS
Joined: 10 Dec 2003 Posts: 67 Location: West Sussex, UK
|
|
Posted: Fri Jun 04, 2004 3:34 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
Re: Quadrature input on PortB int on change |
Posted: Fri Jun 04, 2004 8:23 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Fri Jun 04, 2004 10:14 am |
|
|
0xF81 IS the address for PortB on this pic. The register addresses are on page 45 of the data sheet.
Ronald |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|