Re: Απάντηση: Υλοποίηση preamplifier PGA23xx
Προσπαθώ να ανεβάσω τους κώδικες από τα arduino.
//Selector7
// include the library code:
#include <LiquidCrystal595.h>
// initialize the library with the numbers of the interface pins + the row count
// datapin, latchpin, clockpin, num_lines
LiquidCrystal595 lcd(10,11,12);
//these pins can not be changed 2/3 are special pins
int encoderPin1 = 2;
int encoderPin2 = 3;
int SER_Pin = 7; //pin 14 on the 75HC595
int RCLK_Pin = 8; //pin 12 on the 75HC595
int SRCLK_Pin = 9; //pin 11 on the 75HC595
volatile int lastEncoded = 0;
volatile long encoderValue = 0;
long lastencoderValue = 0;
int counter = 0;
int lastCounter = 0;
int lastMSB = 0;
int lastLSB = 0;
int buttonPin = A0;
int buttonPinState = 0;
int lastButtonPinState = 0;
int muteAll = 0;
const int ledPin = 13; // the number of the LED pin
// Variables will change:
int ledState = LOW; // ledState used to set the LED
long previousMillis = 0;
long interval = 4000; // interval at which to blink (milliseconds)
//How many of the shift registers - change this
#define number_of_74hc595s 2
//do not touch
#define numOfRegisterPins number_of_74hc595s * 8
boolean registers[numOfRegisterPins];
void setup()
{
lcd.begin(20, 4);
// Print a message to the LCD.
lcd.setCursor(0,0);
lcd.print("Input: 1");
lcd.setCursor(0, 1);
lcd.print("OUTPUT: ON");
pinMode(encoderPin1, INPUT);
pinMode(encoderPin2, INPUT);
pinMode(buttonPin, INPUT);
digitalWrite(encoderPin1, HIGH); //turn pullup resistor on
digitalWrite(encoderPin2, HIGH); //turn pullup resistor on
pinMode(SER_Pin, OUTPUT);
pinMode(RCLK_Pin, OUTPUT);
pinMode(SRCLK_Pin, OUTPUT);
//reset all register pins
clearRegisters();
writeRegisters();
setRegisterPin(counter, HIGH);
setRegisterPin(12, HIGH);
writeRegisters();
//call updateEncoder() when any high/low changed seen
//on interrupt 0 (pin 2), or interrupt 1 (pin 3)
attachInterrupt(0, updateEncoder, CHANGE);
attachInterrupt(1, updateEncoder, CHANGE);
}
//set all register pins to LOW
void clearRegisters(){
for(int i = numOfRegisterPins - 1; i >= 0; i--){
registers = LOW;
}
}
//Set and display registers
//Only call AFTER all values are set how you would like (slow otherwise)
void writeRegisters(){
digitalWrite(RCLK_Pin, LOW);
for(int i = numOfRegisterPins - 1; i >= 0; i--){
digitalWrite(SRCLK_Pin, LOW);
int val = registers;
digitalWrite(SER_Pin, val);
digitalWrite(SRCLK_Pin, HIGH);
}
digitalWrite(RCLK_Pin, HIGH);
}
//set an individual pin HIGH or LOW
void setRegisterPin(int index, int value){
registers[index] = value;
}
void loop(){
//Do stuff here
if (lastButtonPinState == 0) {
lastCounter = counter;
}
if (lastencoderValue != encoderValue) {
if (lastencoderValue < encoderValue) {
counter++;
if (counter == 12) {
counter = 0;
}
}
else if (lastencoderValue > encoderValue) {
counter--;
if (counter == -1) {
counter = 11;
}
}
//Serial.println(lastencoderValue);
//Serial.println(encoderValue);
//Serial.println(counter);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Input: ");
lcd.print(lastCounter+1);
lcd.setCursor(0,1);
lcd.print("NewInput: ");
lcd.print(counter+1);
lcd.setCursor(0,3);
lcd.print("Press to select");
lastButtonPinState = 1;
delay(100); //just here to slow down the output, and show it will work even during a delay
unsigned long currentMillis = millis();
previousMillis = currentMillis;
lastencoderValue = encoderValue;
}
// here is where you'd put code that needs to be running all the time.
// check to see if it's time to blink the LED; that is, if the
// difference between the current time and last time you blinked
// the LED is bigger than the interval at which you want to
// blink the LED.
if (lastButtonPinState = 1){
unsigned long currentMillis = millis();
//previousMillis = currentMillis;
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
lcd.setCursor(0, 1);
lcd.print("OUTPUT: ON ");
lcd.setCursor(0,3);
lcd.print(" ");
lastButtonPinState = 0;
counter = lastCounter;
//ledState = HIGH;}
//else {
//ledState = LOW;
//}
}
}
buttonPinState=digitalRead(buttonPin);
if(buttonPinState == lastButtonPinState && buttonPinState == HIGH) {
setRegisterPin(lastCounter, LOW);
setRegisterPin(12, LOW);
writeRegisters();
lastCounter = counter;
setRegisterPin(lastCounter, HIGH);
setRegisterPin(12, HIGH);
writeRegisters();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Input: ");
lcd.print(lastCounter+1);
lcd.setCursor(0, 1);
lcd.print("OUTPUT: ON");
lastButtonPinState = 0;
}
//lcd.setCursor(3,1);
//lcd.print(" ");
//lcd.print(buttonPinState);
//lcd.setCursor(8,1);
//lcd.print(lastButtonPinState);
}
void updateEncoder(){
int MSB = digitalRead(encoderPin1); //MSB = most significant bit
int LSB = digitalRead(encoderPin2); //LSB = least significant bit
int encoded = (MSB << 1) |LSB; //converting the 2 pin value to single number
int sum = (lastEncoded << 2) | encoded; //adding it to the previous encoded value
if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) encoderValue ++;
if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) encoderValue --;
lastEncoded = encoded; //store this value for next time
//lastencoderValue = encoderValue;
}
Είναι το τελευταίο που χρησιμοποίησα πριν αρχίσω να δουλεύω σε πλακέτες.
Θέλει βέβαια δουλειά σχετικά με την σίγαση, αλλά το είχα αφήσει γιά αργότερα για να δουλεύω σε πιό πραγματικές συνθήκες.