Arduino in 74HC595 2x8 bit

Arduino je pojav za katerega bi težko rekli, da opisuje samo HW ali samo SW, zato mu tu namenajm poseben prostor.

Moderator: tilz0R

Arduino in 74HC595 2x8 bit

OdgovorNapisal/-a Peter123 » 13 Jan 2020, 13:05

Živjo.
Po vzoru: https://www.arduino.cc/en/Tutorial/ShiftOut
Sem spisal tole kodo:
Koda: Izberi vse
/* Yaesu FT 817, še shift out https://www.arduino.cc/en/tutorial/ShiftOut
1,8MHz - 0,33V - 1
3,5MHz - 0,67V - 2
7MHz - 1,00V - 3
10MHz - 1,33V - 4
14MHz - 1,67V - 5
18MHz - 2,00V - 6
21MHz - 2,33V - 7
24,5MHz - 2,67V - 8
28MHz - 3,00V - 9
50MHz - 3,33V - 10
144MHz - 3,67V - 11
430MHz - 4,00V - 12
*/
// PINOUTS
const int ADPin = A3; //Pin za merjenje napetosti Band ali ACC
const int PttDetectorPin = 2;     // PTT in - [interrupt]
const int VoltagePin = A0;        // measure input voltage
const int PttOffPin = 6;          // PTT out OFF switch
const int ShiftOutDataPin = 7;    // DATA
const int ShiftOutLatchPin = 8;   // LATCH
const int ShiftOutClockPin = 9;   // CLOCK

int BAND = 0;
bool PTT = false;
float DCinVoltage = 0;
float ACC = 0;

int band_no = 0;
String band_name = "NI_SIGNALA";
byte data_band_no;
byte dataArrayband_no[16];

void setup()
{
  Serial.begin(9600, SERIAL_8N2);
  Serial.println("Init...");
  analogReference(EXTERNAL);
  pinMode(VoltagePin, INPUT);
   digitalWrite(PttDetectorPin, HIGH);
  pinMode(ADPin, INPUT);
  pinMode(PttDetectorPin, INPUT);
  pinMode(PttOffPin, OUTPUT);
  pinMode(ShiftOutLatchPin, OUTPUT);

  //Arduino doesn't seem to have a way to write binary straight into the code
  //so these values are in HEX.  Decimal would have been fine, too.
  dataArrayband_no[0] = 0x0000; //00000000 00000000
  dataArrayband_no[1] = 0x0080; //00000000 10000000
  dataArrayband_no[2] = 0x0040; //00000000 01000000
  dataArrayband_no[3] = 0x0020; //00000000 00100000
  dataArrayband_no[4] = 0x0010; //00000000 00010000
  dataArrayband_no[5] = 0x0008; //00000000 00001000
  dataArrayband_no[6] = 0x0004; //00000000 00000100
  dataArrayband_no[7] = 0x0002; //00000000 00000010
  dataArrayband_no[8] = 0x0001; //00000000 00000001 ****************
  dataArrayband_no[9] = 0x8000; //10000000 00000000
  dataArrayband_no[10] = 0x4000; //01000000 00000000
  dataArrayband_no[11] = 0x2000; //00100000 00000000
  dataArrayband_no[12] = 0x1000; //00010000 00000000
  dataArrayband_no[13] = 0x0800; //00001000 00000000
  dataArrayband_no[14] = 0x0400; //00000100 00000000
  dataArrayband_no[15] = 0x0200; //00000010 00000000
  dataArrayband_no[16] = 0x0100; //00000001 00000000
 
  Serial.println("Init finished, running...");
}

void loop()
{

   
 
  int i = 0;
  DCinVoltage = 0;
  ACC = 0;
  while( i < 10 )
  {
    DCinVoltage = analogRead(VoltagePin) / 37.043;
    delay(10);
    ACC = analogRead(ADPin) / 33.727;
    delay(10);
    i++;
    //Serial.print("_1_");
  }
  i = 0;
  Serial.print("Vhodna napetost: ");
  Serial.print(DCinVoltage, 3);
  Serial.print(" V; ACC: ");
  Serial.print(ACC, 4);
  Serial.print(" V; Band no.: ");

  band_no = band_decode();
  Serial.print(band_no);
  Serial.print("; Band name: ");
  band_name = band_name_();
  Serial.println(band_name);

  data_band_no = dataArrayband_no[band_no];
  Serial.print(data_band_no);
  Serial.print(" ");
  Serial.print(dataArrayband_no[band_no]);
  digitalWrite(ShiftOutLatchPin, 0);
  shiftOut(ShiftOutDataPin, ShiftOutClockPin, data_band_no);
  digitalWrite(ShiftOutLatchPin, 1);
  delay (1000);

}


int band_decode()//byte band_no, String band_name
  {
    if (0.2 > ACC ) return 0;
    if (0.2 <= ACC && ACC < 0.5) return 1;
    if (0.5 <= ACC && ACC < 0.8) return 2;
    if (0.8 <= ACC && ACC < 1.2) return 3;
    if (1.2 <= ACC && ACC < 1.5) return 4;
    if (1.5 <= ACC && ACC < 1.8) return 5;
    if (1.8 <= ACC && ACC < 2.2) return 6;
    if (2.2 <= ACC && ACC < 2.5) return 7;
    if (2.5 <= ACC && ACC < 2.9) return 8;
    if (2.9 <= ACC && ACC < 3.2) return 9;
    if (3.2 <= ACC && ACC < 3.5) return 10;
    if (3.5 <= ACC && ACC < 3.8) return 11;
    if (3.8 <= ACC ) return 12;
    return 0;
  }
  //-------------------------------------------------------------------------------
String band_name_()
  {
    if (band_no == 0) return "NI_SIGNALA";
    if (band_no == 1) return "1,8MHz";
    if (band_no == 2) return "3,5MHz";
    if (band_no == 3) return "7MHz";
    if (band_no == 4) return "10MHz";
    if (band_no == 5) return "14MHz";
    if (band_no == 6) return "18MHz";
    if (band_no == 7) return "21MHz";
    if (band_no == 8) return "24,5MHz";
    if (band_no == 9) return "28MHz";
    if (band_no == 10) return "50MHz";
    if (band_no == 11) return "144MHz";
    if (band_no == 12) return "430MHz";
    return "NI_SIGNALA";
  }
 //-------------------------------------------------------------------------------
  void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
  // This shifts 8 bits out MSB first,
  //on the rising edge of the clock,
  //clock idles low

  //internal function setup
  int i=0;
  int pinState;
  pinMode(myClockPin, OUTPUT);
  pinMode(myDataPin, OUTPUT);

  //clear everything out just in case to
  //prepare shift register for bit shifting
  digitalWrite(myDataPin, 0);
  digitalWrite(myClockPin, 0);

  //for each bit in the byte myDataOut�
  //NOTICE THAT WE ARE COUNTING DOWN in our for loop
  //This means that %00000001 or "1" will go through such
  //that it will be pin Q0 that lights.
  for (i=16; i>=0; i--)  {
    digitalWrite(myClockPin, 0);

    //if the value passed to myDataOut and a bitmask result
    // true then... so if we are at i=6 and our value is
    // %11010100 it would the code compares it to %01000000
    // and proceeds to set pinState to 1.
    if ( myDataOut & (1<<i) ) {
      pinState= 1;
    }
    else { 
      pinState= 0;
    }

    //Sets the pin to HIGH or LOW depending on pinState
    digitalWrite(myDataPin, pinState);
    //register shifts bits on upstroke of clock pin 
    digitalWrite(myClockPin, 1);
    //zero the data pin after shift to prevent bleed through
    digitalWrite(myDataPin, 0);
  }

  //stop shifting
  digitalWrite(myClockPin, 0);
}


In sicer Shift deluje ampak samo do št. 8, ko pride na "dataArrayband_no[9] = 0x8000;" dobim rezultat 0
Iščem a ne najdem napake...
Na terminalu dobim tole:
Koda: Izberi vse
1 1Vhodna napetost: 9.664 V; ACC: 2.8760 V; Band no.: 8; Band name: 24,5MHz
1 1Vhodna napetost: 9.664 V; ACC: 3.2615 V; Band no.: 10; Band name: 50MHz
0 0Vhodna napetost: 9.664 V; ACC: 3.2615 V; Band no.: 10; Band name: 50MHz

Ja vmes je 1 cikel zakasnitve, kar pa za delovanje ni moteče.
Hvala za pomoč.
LP
P.
Peter123
 
Prispevkov: 1075
Pridružen: 09 Feb 2015, 14:20
Kraj: 1117 Lj.
Zahvalil se je: 23 krat
Prejel zahvalo: 144 krat
Uporabnika povabil: jmivsek
Število neizkoriščenih povabil: 29

Re: Arduino in 74HC595 2x8 bit

OdgovorNapisal/-a igo » 13 Jan 2020, 20:59

void shiftOut(int myDataPin, int myClockPin, byte myDataOut) { ...

Mislim, da se problem skriva v 8-bitnem
byte myDataOut

ki bi moral biti 16-bitna spremenljivka.
Lahko je unsigned int ali kaj podobnega.

V bistvu bi morala biti myDataPin in myClockPin tipa byte,
myDataOut pa unsigned int.
Teoretično je praksa posledica teorije, praktično je pa ravno obratno. (igo 2001)
LP, Igor
igo
 
Prispevkov: 1431
Pridružen: 11 Apr 2015, 13:38
Kraj: Krško
Zahvalil se je: 172 krat
Prejel zahvalo: 350 krat
Uporabnika povabil: DusanK
Število neizkoriščenih povabil: 34

Re: Arduino in 74HC595 2x8 bit

OdgovorNapisal/-a Peter123 » 14 Jan 2020, 09:27

Sem probal:
Koda: Izberi vse
void shiftOut(byte myDataPin, byte myClockPin, unsigned int myDataOut) {

in
Koda: Izberi vse
void shiftOut(int myDataPin, int myClockPin, unsigned int myDataOut) {

Pa je rezultat isti. Do 9 deluje, ko prestopiš na 9 ali več je stanje 0.
Peter123
 
Prispevkov: 1075
Pridružen: 09 Feb 2015, 14:20
Kraj: 1117 Lj.
Zahvalil se je: 23 krat
Prejel zahvalo: 144 krat
Uporabnika povabil: jmivsek
Število neizkoriščenih povabil: 29

Re: Arduino in 74HC595 2x8 bit

OdgovorNapisal/-a sundancer » 14 Jan 2020, 10:03

Povsod v kodi popravi spremenljivke. Namrec tudi array sprejema samo byte.
Koda: Izberi vse
byte dataArrayband_no[16];

V kodi imas pa dva byta:
Koda: Izberi vse
  dataArrayband_no[0] = 0x0000; //00000000 00000000
  dataArrayband_no[1] = 0x0080; //00000000 10000000
Dyslexic man walks into a bra.
Uporabniški avatar
sundancer
 
Prispevkov: 583
Pridružen: 16 Jan 2015, 23:36
Kraj: Domžale
Zahvalil se je: 204 krat
Prejel zahvalo: 307 krat
Uporabnika povabil: Vrtni palček
Število neizkoriščenih povabil: 41

Re: Arduino in 74HC595 2x8 bit

OdgovorNapisal/-a Peter123 » 14 Jan 2020, 10:29

Če upoštevam oba predloga
Koda: Izberi vse
unsigned int data_band_no;
unsigned int dataArrayband_no[16];
void shiftOut(byte myDataPin, byte myClockPin, unsigned int myDataOut) {

Zadeva deluje.
Hvala za predloge.
Peter123
 
Prispevkov: 1075
Pridružen: 09 Feb 2015, 14:20
Kraj: 1117 Lj.
Zahvalil se je: 23 krat
Prejel zahvalo: 144 krat
Uporabnika povabil: jmivsek
Število neizkoriščenih povabil: 29


Vrni se na Arduino

Kdo je na strani

Po forumu brska: 0 registriranih uporabnikov in 0 gostov