//Encoder
#include "rskr8c27def.h"
#include "ior8c26_27.h"
#include "digitIO.h"
#define encoder0PinA 15 //INT1
#define encoder0PinB 33 //INT3
#define RawPinA p1_5
#define RawPinB p3_3
int encoder0Pos ;
void encoder_init()
{
//**************************************
// INT1 Interrupt Setting
//**************************************
//Note: Set INT1 hardware interrupt pin to input
//Note: By default, INT1 pin is at P1_7 pin
//To change to INT1 pin at P1_5 pin, set tiosel_traioc=1
//To change to INT1 pin at P3_6 pin, set int1sel=1
int1ic=0x04;
//Set interrupt priority level and active edge(int1ic bit 0, 1, 2 and 4)
int1en=1;
//Input enabled(inten bit 2)
int1pl=1;
//Both edges selected(inten bit 3)
int1f0=1;int1f1=0;
//Filter with f1 sampling selected(intf bit 2 and 3)
tiosel_traioc=1;
//**************************************
// INT3 Interrupt Setting
//**************************************
//Note: Set INT3 hardware interrupt pin to input
int3ic=0x04;
//Set interrupt priority level and active edge(int3ic bit 0, 1, 2 and 4)
int3en=1;
//Input enabled(inten bit 6)
int3pl=1;
//Both edges selected(inten bit 7)
int3f0=1;int3f1=0;
//Filter with f1 sampling selected(intf bit 6 and 7)
encoder0Pos = 0;
}
void encoder_stop()
{
int1en=0;
int3en=0;
}
void encoder_start()
{
int1en=1;
int3en=1;
}
void encoder_reset()
{
encoder0Pos = 0;
}
#pragma vector= INT1
__interrupt void INT1_ISR(void)
{
// look for a low-to-high on channel A
//if (digitalRead(encoder0PinA) == HIGH) {
if (RawPinA == 1) {
if (RawPinB == 0) {
encoder0Pos = encoder0Pos + 1; // CW
}
else {
encoder0Pos = encoder0Pos - 1; // CCW
}
}
else // must be a high-to-low edge on channel A
{
if (RawPinB == 1) {
encoder0Pos = encoder0Pos + 1; // CW
}
else {
encoder0Pos = encoder0Pos - 1; // CCW
}
}
}
#pragma vector= INT3
__interrupt void INT3_ISR(void)
{
if (RawPinB == 1) {
if (RawPinA == 1) {
encoder0Pos = encoder0Pos + 1; // CW
}
else {
encoder0Pos = encoder0Pos - 1; // CCW
}
}
// Look for a high-to-low on channel B
else {
if (RawPinA == 0) {
encoder0Pos = encoder0Pos + 1; // CW
}
else {
encoder0Pos = encoder0Pos - 1; // CCW
}
}
}
via Arduino
#include "rskr8c27def.h"
#include "ior8c26_27.h"
#include "digitIO.h"
#define encoder0PinA 15 //INT1
#define encoder0PinB 33 //INT3
#define RawPinA p1_5
#define RawPinB p3_3
int encoder0Pos ;
void encoder_init()
{
//**************************************
// INT1 Interrupt Setting
//**************************************
//Note: Set INT1 hardware interrupt pin to input
//Note: By default, INT1 pin is at P1_7 pin
//To change to INT1 pin at P1_5 pin, set tiosel_traioc=1
//To change to INT1 pin at P3_6 pin, set int1sel=1
int1ic=0x04;
//Set interrupt priority level and active edge(int1ic bit 0, 1, 2 and 4)
int1en=1;
//Input enabled(inten bit 2)
int1pl=1;
//Both edges selected(inten bit 3)
int1f0=1;int1f1=0;
//Filter with f1 sampling selected(intf bit 2 and 3)
tiosel_traioc=1;
//**************************************
// INT3 Interrupt Setting
//**************************************
//Note: Set INT3 hardware interrupt pin to input
int3ic=0x04;
//Set interrupt priority level and active edge(int3ic bit 0, 1, 2 and 4)
int3en=1;
//Input enabled(inten bit 6)
int3pl=1;
//Both edges selected(inten bit 7)
int3f0=1;int3f1=0;
//Filter with f1 sampling selected(intf bit 6 and 7)
encoder0Pos = 0;
}
void encoder_stop()
{
int1en=0;
int3en=0;
}
void encoder_start()
{
int1en=1;
int3en=1;
}
void encoder_reset()
{
encoder0Pos = 0;
}
#pragma vector= INT1
__interrupt void INT1_ISR(void)
{
// look for a low-to-high on channel A
//if (digitalRead(encoder0PinA) == HIGH) {
if (RawPinA == 1) {
if (RawPinB == 0) {
encoder0Pos = encoder0Pos + 1; // CW
}
else {
encoder0Pos = encoder0Pos - 1; // CCW
}
}
else // must be a high-to-low edge on channel A
{
if (RawPinB == 1) {
encoder0Pos = encoder0Pos + 1; // CW
}
else {
encoder0Pos = encoder0Pos - 1; // CCW
}
}
}
#pragma vector= INT3
__interrupt void INT3_ISR(void)
{
if (RawPinB == 1) {
if (RawPinA == 1) {
encoder0Pos = encoder0Pos + 1; // CW
}
else {
encoder0Pos = encoder0Pos - 1; // CCW
}
}
// Look for a high-to-low on channel B
else {
if (RawPinA == 0) {
encoder0Pos = encoder0Pos + 1; // CW
}
else {
encoder0Pos = encoder0Pos - 1; // CCW
}
}
}
via Arduino
Comments