Problems controlling 74HC595 from a Library, Logic Error Somewhere? Please Help
i'm trying take code works expected , turn library can reuse throughout projects. have taken code , created library compiles fine, gives unexpected results leds. can see i'm doing wrong? please if can. thanks!
the code works expected:
here code library , sketch :
the code works expected:
quote
int ser_pin = 11; //pin 14 on 75hc595 // data pin
int rclk_pin = 8; //pin 12 on 75hc595 // latch pin
int srclk_pin = 12; //pin 11 on 75hc595 // clock pin
//number of shift registers p resent.
#define number_of_74hc595s 1
//define number of output pins based on number of shift registers present.
#define numofregisterpins number_of_74hc595s * 8
// accelerometer connected analog pins 0, 1, 2 (x, y, z)
#define xpin 0
#define ypin 1
#define zpin 2
boolean registers[numofregisterpins];
void setup(){
serial.begin(9600);
pinmode(ser_pin, output);
pinmode(rclk_pin, output);
pinmode(srclk_pin, output);
pinmode(xpin, input);
pinmode(ypin, input);
pinmode(zpin, input);
//reset register pins
clearregisters();
writeregisters();
}
//set register pins low
void clearregisters(){
for(int = numofregisterpins - 1; >= 0; i--){
registers = low;
}
}
//set , display registers
//only call after values set how (slow otherwise)
void writeregisters(){
digitalwrite(rclk_pin, low); // unlatch can write changes
for(int = numofregisterpins - 1; >= 0; i--){
digitalwrite(srclk_pin, low);
int val = registers;
digitalwrite(ser_pin, val);
digitalwrite(srclk_pin, high);
}
digitalwrite(rclk_pin, high);
}
//set individual pin high or low
void setregisterpin(int index, boolean value){
registers[index] = value;
}
void loop(){
setregisterpin(0, high);
setregisterpin(1, low);
setregisterpin(2, high);
setregisterpin(3, low);
setregisterpin(4, high);
setregisterpin(5, low);
setregisterpin(6, high);
setregisterpin(7, low);
writeregisters(); //must called display changes
//only call after values set how need.
serial.print(analogread(xpin));
serial.print(",");
serial.print(analogread(ypin));
serial.print(",");
serial.println(analogread(zpin));
delay(500);
clearregisters();
setregisterpin(0, low);
setregisterpin(1, high);
setregisterpin(2, low);
setregisterpin(3, high);
setregisterpin(4, low);
setregisterpin(5, high);
setregisterpin(6, low);
setregisterpin(7, high);
writeregisters();
delay(500);
}
here code library , sketch :
code: [select]
/*
sr74hc595.cpp - library working 74hc595 shift register
*/
#include <arduino.h>
#include <sr74hc595.h>
sr74hc595::sr74hc595(int datapin, int latchpin, int clockpin, int numofregisterpins)
{
_data_pin = datapin;
_latch_pin = latchpin;
_clock_pin = clockpin;
numberofregisterpins = numofregisterpins;
registers[numberofregisterpins];
pinmode(_data_pin, output);
pinmode(_latch_pin, output);
pinmode(_clock_pin, output);
}
void sr74hc595::clearregisters()
{
for(int = numberofregisterpins-1; >= 0; i--)
{
registers[i] = low;
}
}
void sr74hc595::writeregisters()
{
digitalwrite(_latch_pin, low); // unlatch can write changes
for(int = numberofregisterpins-1; >= 0; i--){
digitalwrite(_clock_pin, low);
int val = registers[i];
digitalwrite(_data_pin, val);
digitalwrite(_clock_pin, high);
}
digitalwrite(_latch_pin, high);
}
void sr74hc595::setregisterpin(int index, boolean value)
{
registers[index] = value;
}
code: [select]
/*
sr74hc595.h - library working 74hc595 shift register
*/
#ifndef sr74hc595_h
#define sr74hc595_h
#include <arduino.h>
class sr74hc595
{
private:
int _data_pin; // pin 14 on 75hc595
int _latch_pin; // pin 12 on 75hc595
int _clock_pin; // pin 11 on 75hc595
int numberofregisterpins;
public:
boolean registers[];
sr74hc595(int datapin, int latchpin, int clockpin, int numofregisterpins);
void clearregisters();
void writeregisters();
void setregisterpin(int index, boolean value);
};
#endif
quote
#include <sr74hc595.h>
#define data_pin 11
#define latch_pin 8
#define clock_pin 12
#define numberofregisterpins 8
sr74hc595 shiftregister(data_pin, latch_pin, clock_pin, numberofregisterpins);
void setup()
{
serial.begin(9600);
shiftregister.clearregisters();
}
void loop()
{
shiftregister.setregisterpin(0, high);
shiftregister.setregisterpin(1, low);
shiftregister.setregisterpin(2, high);
shiftregister.setregisterpin(3, low);
shiftregister.setregisterpin(4, high);
shiftregister.setregisterpin(5, low);
shiftregister.setregisterpin(6, high);
shiftregister.setregisterpin(7, low);
shiftregister.writeregisters(); //must called display changes
//only call after values set how need.
delay(500);
shiftregister.clearregisters();
shiftregister.setregisterpin(0, low);
shiftregister.setregisterpin(1, high);
shiftregister.setregisterpin(2, low);
shiftregister.setregisterpin(3, high);
shiftregister.setregisterpin(4, low);
shiftregister.setregisterpin(5, high);
shiftregister.setregisterpin(6, low);
shiftregister.setregisterpin(7, high);
shiftregister.writeregisters();
delay(500);
}
whoops, solved problem. looks overflowing array. anyways guys/gals!
Arduino Forum > Using Arduino > Programming Questions > Problems controlling 74HC595 from a Library, Logic Error Somewhere? Please Help
arduino
Comments
Post a Comment