Hi. I desperately need your help.
A bit of a story first.
I have my Arduino reading data from a HCTL-2022 IC (decoder for rotary encoders) via MCP23016. The HCTL IC outputs 32 bits of data on its D0-D7 pins in 4 bytes (which ones - selected by 3 other pins). So, I use Port A of MCP23016 as output (three pins of it are connected to HCTL so I can control which byte is going to be received) and Port B is basically receiving data from HCTL.
At the same time another Arduino is emulating rotary encoder, generating input for HCTL. The expected result is forever increasing counter (HCTL_pos) withing its 32 bits memory allocation.
Here is the code (I know there are some unneeded variables, I left them for testing now):
#include "Wire.h"
//In this definitions:
//right-to-left: O/E pin, Sel1, Sel2, RST(RST active-low, so keep it high most of the time)
#define HCTL_read_step0 B00001001 //no reading
#define HCTL_read_step1 B00001100 //Read MSB
#define HCTL_read_step2 B00001110 //2nd byte
#define HCTL_read_step3 B00001000 //3rd byte
#define HCTL_read_step4 B00001010 //Read LSB
#define HCTL_read_step5 B00001001 //Complete logic
#define HCTL_reset B00000001 //Activate reset pin
uint8_t HCTL_MSB;
uint8_t HCTL_2ND;
uint8_t HCTL_3RD;
uint8_t HCTL_LSB;
uint32_t HCTL_pos;
uint32_t HCTL_MSB1;
uint32_t HCTL_2ND1;
uint32_t HCTL_3RD1;
uint32_t HCTL_LSB1;
//MCP extender parameters
#define MCP_addr 0x20
#define IODIR0 0x06
#define IODIR1 0x07
#define GPIO0 0x00
#define GPIO1 0x01
#define DS3232_I2C_ADDRESS 0x68 // RTC for 32kHz clock signal
void setup(){
Serial.begin(9600); //Start serial communication
Wire.begin(); // wake up I2C bus
// set I/O pins to outputs
Wire.beginTransmission(MCP_addr);
Wire.write(IODIR0); // IODIRA register
Wire.write(B00000000); // set all of port 0 to outputs
Wire.endTransmission();
MCPWrite(HCTL_read_step0);
//Run 32k CLK
Wire.beginTransmission(DS3232_I2C_ADDRESS);
Wire.write(0x0F); // move register pointer to control register
Wire.write(0x08); // set to 32 kHz
Wire.endTransmission();
}
void loop(){
GetHCTLPosition(); //Get current counter position
delay (1000);
}
void GetHCTLPosition(){
HCTL_MSB=0;
HCTL_2ND=0;
HCTL_3RD=0;
HCTL_LSB=0;
HCTL_pos=0;
HCTLRead(); //read counter (via MCP)
HCTL_MSB1=0;
HCTL_2ND1=0;
HCTL_3RD1=0;
HCTL_LSB1=0;
HCTL_MSB1=HCTL_MSB;
HCTL_MSB1=HCTL_MSB1 << 24;
HCTL_2ND1=HCTL_2ND;
HCTL_2ND1=HCTL_2ND1 << 16;
HCTL_3RD1=HCTL_3RD;
HCTL_3RD1=HCTL_3RD1 << 8;
HCTL_LSB1=HCTL_LSB;
HCTL_pos = HCTL_MSB1 | HCTL_2ND1 | HCTL_3RD1 | HCTL_LSB1; //combine 4 separate bytes into one big number
Serial.print(HCTL_MSB, BIN);
Serial.print(" ");
Serial.print(HCTL_2ND, BIN);
Serial.print(" ");
Serial.print(HCTL_3RD, BIN);
Serial.print(" ");
Serial.println(HCTL_LSB, BIN);
Serial.print(HCTL_pos, BIN);
Serial.print(" ");
Serial.print(HCTL_pos);
Serial.println();
}
byte MCPRead(){
//Read data from Port B of MCP
uint8_t data;
Wire.beginTransmission(MCP_addr);
Wire.write(GPIO1); // set MCP23017 memory pointer to GPIOB address
Wire.endTransmission();
Wire.requestFrom(MCP_addr, 1); // request one byte of data from MCP20317
data=Wire.read(); // store the incoming byte into "inputs"
return data;
}
void MCPWrite(uint8_t data){
//Write data to Port A of MCP (effectively talking to HCTL)
Wire.beginTransmission(MCP_addr);
Wire.write(GPIO0); // GPIOA
Wire.write(data); // port A
Wire.endTransmission();
}
void HCTLRead(){
//Talk to HCTL via MCP, sending and reading sequences
MCPWrite(HCTL_read_step1);
delayMicroseconds(20);
HCTL_MSB = MCPRead();
MCPWrite(HCTL_read_step2);
delayMicroseconds(5);
HCTL_2ND = MCPRead();
MCPWrite(HCTL_read_step3);
delayMicroseconds(5);
HCTL_3RD = MCPRead();
MCPWrite(HCTL_read_step4);
delayMicroseconds(5);
HCTL_LSB = MCPRead();
MCPWrite(HCTL_read_step5);
}
The trouble is, my serial output is really dodgy… I get incrementing numbers but at some random (?) moment I get my counter dropped to zero.
Here is an example:
0 0 10010 11101110
1001011101110 4846
0 0 10010 11111000
1001011111000 4856
0 0 10011 11
1001100000011 4867
0 0 10011 1101
1001100001101 4877
0 0 10011 10111
1001100010111 4887
0 0 0 0
0 0
0 0 0 0
0 0
0 0 0 1001
1001 9
0 0 0 10011
10011 19
0 0 0 11101
11101 29
What am I doing wrong? It’s possibly something small and stupid, but I can’t catch it… Heeeelp…
P.S.: Here are the links to datasheets for IC I’m using: http://docs-asia.electrocomponents.com/webdocs/04e5/0900766b804e53e3.pdf and http://docs-asia.electrocomponents.com/webdocs/0d77/0900766b80d770fa.pdf