PCF8574 Projekte mit 8 LEDs

Atmel, PIC, VEX, Fischertechnik

Moderator: Moderatoren

Benutzeravatar
NXT pro
Super-Enorm-Viel-Schreiber
Super-Enorm-Viel-Schreiber
Beiträge: 930
Registriert: 27. Sep 2010 21:38

PCF8574 Projekte mit 8 LEDs

Beitragvon NXT pro » 25. Jun 2011 09:50

Hier kann man seine Projekte mit einem PCF8574 und 8 LEDs reinschreiben. Ich habe mir mal ein Programm zum einzelnen An- und Ausschalten der LEDs geschrieben.
Erstmal der Treiber von Adam Mokanszki, ich denke dass einige Programme diesen Trieber benutzen, deswegen will ich ihn hier nur einmal einfügen:

Code: Alles auswählen

/*************************************************************/
/*                                                           */
/*                     PCF8574Driver for NXC                 */
/*                                                           */
/*                 Programmed by: Adam Mokanszki             */
/*                                                           */
/*                 University of Debrecen Hungary            */
/*                                                           */
/*                            ENJOY! :)                      */
/*                                                           */
/*************************************************************/
#ifndef _PCF8574_
#define _PCF8574_

#define  PCF8574ID  0x40    //The address of your PCF8574- Hier muss 0x40 hin.
#define  PCF8574Port  S1   //This is the port on the NXT where the device is connected

void WritePort(char input);  //Writing a single port the input is the port number 1-8
void DeletePort(char input); //Deleting a single port the input is the port number 1-8
void WritePorts(byte input); //Writing all ports with one byte
int ReadPort(char input);    //Reading a single port the input is the port number 1-8 returning with a logical value in integer: 0,1
int ReadPorts();             //Reading all ports returning with one byte
                             //That's all what you need to know about this file

void WritePort(char input){

    byte cnt = 0x02;
    byte I2CMsg[];
    byte inbuf[];
    byte Address = PCF8574ID;
    byte nByteReady = 0;
    int  result = -1;
    int loop;

    ArrayBuild(I2CMsg, Address);

    Wait(8);

    while (loop == STAT_COMM_PENDING) {

          loop = I2CStatus(PCF8574Port, nByteReady);

    }

    if (I2CBytes(PCF8574Port, I2CMsg, cnt, inbuf)) {

        result = inbuf[1];

    }

    byte Command = result | (1<<(input-1));

    ArrayBuild(I2CMsg, Address, Command);

    while (loop == STAT_COMM_PENDING) {
        loop = I2CStatus(PCF8574Port, nByteReady);
    }

    I2CWrite(S1, 1, I2CMsg);

    while (loop == STAT_COMM_PENDING) {

        loop = I2CStatus(PCF8574Port, nByteReady);

    }
}

void DeletePort(char input){

    byte cnt = 0x02;
    byte I2CMsg[];
    byte inbuf[];
    byte Address = PCF8574ID;
    byte nByteReady = 0;
    int  result = -1;
    int loop;

    ArrayBuild(I2CMsg, Address);

    Wait(8);

    while (loop == STAT_COMM_PENDING) {

          loop = I2CStatus(PCF8574Port, nByteReady);

    }

    if (I2CBytes(PCF8574Port, I2CMsg, cnt, inbuf)) {

        result = inbuf[1];

    }

    byte Command = result ^ (1<<(input-1));

    ArrayBuild(I2CMsg, Address, Command);

    while (loop == STAT_COMM_PENDING) {

        loop = I2CStatus(PCF8574Port, nByteReady);

    }

    I2CWrite(S1, 1, I2CMsg);

    while (loop == STAT_COMM_PENDING) {

        loop = I2CStatus(PCF8574Port, nByteReady);

    }

}

void WritePorts(byte input){

    byte I2CMsg[];
    byte Address = PCF8574ID;
    byte Command = input;
    byte nByteReady = 0;
    int loop;

    ArrayBuild(I2CMsg, Address, Command);

    while (loop == STAT_COMM_PENDING) {

        loop = I2CStatus(PCF8574Port, nByteReady);

    }

    I2CWrite(S1, 1, I2CMsg);

    while (loop == STAT_COMM_PENDING) {

        loop = I2CStatus(PCF8574Port, nByteReady);

    }
}

int ReadPort(char input){

    byte cnt = 0x02;
    byte I2CMsg[];
    byte inbuf[];
    byte Address = PCF8574ID;
    byte nByteReady = 0;
    int  result = -1;
    int loop;

    ArrayBuild(I2CMsg, Address);
    Wait(8);
    while (loop == STAT_COMM_PENDING) {

          loop = I2CStatus(PCF8574Port, nByteReady);

    }

    if (I2CBytes(PCF8574Port, I2CMsg, cnt, inbuf)) {

        result = inbuf[1];

    }

    if( result == (result | (1<<input-1)) ){

       result = 1;

    }
    else{

       result = 0;

    }

    if(result==-1){

     TextOut(0, LCD_LINE7, "Error: Check the");
     TextOut(0, LCD_LINE8, "   connection!");
     Wait(500);
     ClearScreen();
    }
    return result;
}

int ReadPorts(){

    byte cnt = 0x02;
    byte I2CMsg[];
    byte inbuf[];
    byte Address = PCF8574ID;
    byte nByteReady = 0;
    int  result = -1;
    int loop;

    ArrayBuild(I2CMsg, Address);

    Wait(8);

    while (loop == STAT_COMM_PENDING) {

          loop = I2CStatus(PCF8574Port, nByteReady);

    }

    if (I2CBytes(PCF8574Port, I2CMsg, cnt, inbuf)) {

        result = inbuf[1];

    }
    if(result==-1){

     TextOut(0, LCD_LINE7, "Error: Check the");
     TextOut(0, LCD_LINE8, "   connection!");
     Wait(500);
     ClearScreen();
    }
    return result;
}
#endif

Und dann mein eigenes Programm:

Code: Alles auswählen

#include "pcf8574llb.nxc"

task main()
{
 int port = 1;
 SetSensorLowspeed(PCF8574Port);
 WritePorts(0xFF);
 Wait(100);
 while(true)
 {
  if(ButtonState(BTNLEFT) != 0)
  {
   port --;
   Wait(300);
  }
  if(ButtonState(BTNRIGHT) != 0)
  {
   port ++;
   Wait(300);
  }
  if(port == 9)
  {
   port = 1;
  }
  if(port == 0)
  {
   port = 8;
  }
  if((ReadPort(port) == 1) && (ButtonState(BTNCENTER) != 0))
  {
   DeletePort(port);
   Wait(300);
  }
  if((ReadPort(port) == 0) && (ButtonState(BTNCENTER) != 0))
  {
   WritePort(port);
   Wait(300);
  }
  ClearScreen();
  if(ReadPort(port) == 0)
  {
   TextOut(38, LCD_LINE4, "AN");
   TextOut(26, LCD_LINE8, "Ausschalten");
  }
  if(ReadPort(port) == 1)
  {
   TextOut(38, LCD_LINE4, "AUS");
   TextOut(26, LCD_LINE8, "Anschalten");
  }
  NumOut(44, LCD_LINE2, port);
 }
}
Real coders don't comment their code - it was hard to write, it should be hard to understand!

mattallen37
Weniger als 15 Beiträge
Weniger als 15 Beiträge
Beiträge: 14
Registriert: 23. Jun 2011 00:07

Re: PCF8574 Projekte mit 8 LEDs

Beitragvon mattallen37 » 25. Jun 2011 20:41

D:Es wurde einige Zeit, da ich diese schrieb, aber sie sollten noch funktionieren.
E:It has been quite some time since I wrote these, but they should still work fine.

D:Hier ist die Funktions-Bibliothek, speichern Sie es als "My8574 lib.nxc".
E:Here is the function library, save it as "My8574 lib.nxc".

Code: Alles auswählen

void WritePins(byte port, byte address, byte byteout, bool invert){
  if(invert){
    byteout=byteout*(-1)+255;
  }
  byte I2CMsg[];
  byte nByteReady = 0;
  int loop;

  ArrayBuild(I2CMsg, address, byteout);

  while (loop == STAT_COMM_PENDING) {
    loop = I2CStatus(port, nByteReady);
  }

  I2CWrite(port, 1, I2CMsg);

  while (loop == STAT_COMM_PENDING) {
    loop = I2CStatus(port, nByteReady);
  }
}

int ReadPins(byte port, byte address){
  byte cnt = 2;
  byte I2CMsg[];
  byte inbuf[];
  byte nByteReady = 0;
  int  result = -1;
  int loop;

  ArrayBuild(I2CMsg, address);

  while (loop == STAT_COMM_PENDING) {
    loop = I2CStatus(port, nByteReady);
  }

  if (I2CBytes(port, I2CMsg, cnt, inbuf)) {
    result = inbuf[1];
  }
  return result;
}

D:Zur Verwendung der WritePins Funktion geben Sie den Port, I2C-Adresse, Ausgangs-Byte, und wenn Sie wollen, dass es die Byte invertieren. Verwenden Sie es wie folgt aus:
WritePins (Port, Adresse, byteout, Invert); (edited)

E:To use the WritePins function, you specify the port, I2C address, output byte, and if you want it to invert the byte. Use it like this:
WritePins(port, address, byteout, invert);

D:Und dann sind hier viele verschiedene Programme. Alle von ihnen verlangen, ein 10k-Potentiometer (oder Trimmer Widerstand) von Pin 1 des NXT-Draht (analog) auf 2 oder 3 (Masse) Pin. Dies ist nur zum Einstellen der Geschwindigkeit der Schleife. Sie müssen nicht das Potentiometer zu verwenden, aber dann werden sie alle langsam laufen.

E:And then here are a lot of miscellaneous programs. All of them require a 10k potentiometer (or trimmer resistor) from pin 1 of the NXT wire (analog) to pin 2 or 3 (ground). This is just for adjusting the speed of the loop. You don't have to use the potentiometer, but then they will all run slowly.

Code: Alles auswählen

#include "My8574 lib.nxc"
int scaled,LEDtoDrive;
bool LR;
task main()
{
  LR=1;
  LEDtoDrive=1;
  while (true)
  {
    SetSensorLowspeed(S1);

    WritePins(S1, 0x40, LEDtoDrive, 1);
    Wait (7);//normal 7 as minimum

    Wait (scaled);
    if (LR==1)
    {
      LEDtoDrive=LEDtoDrive*2;
      if (LEDtoDrive>128)
      {
        LEDtoDrive=128;
        LR=0;
      }
    }
    if (LR==0)
    {
      LEDtoDrive=LEDtoDrive/2;
      if (LEDtoDrive==1)
      {
        LR=1;
      }
    }

    ClearScreen();
    NumOut(0,LCD_LINE1,scaled);
    NumOut(0,LCD_LINE2,LEDtoDrive);

    SetSensorTouch(S1);
    SetSensorMode(S1, IN_MODE_RAW);
    ResetSensor(S1);
    scaled=SENSOR_1/2;
  }
}

Code: Alles auswählen

#include "My8574 lib.nxc"
int scaled,numtoacton,LEDstoDrive;
bool LR;
task main()
{
  LR=1;
  while (true)
  {
    SetSensorLowspeed(S1);
    WritePins(S1, 0x40, LEDstoDrive, 1);

    Wait (scaled);
   
    numtoacton++;
    if (numtoacton==7)
    {
      numtoacton=1;
    }

    if (numtoacton==1){LEDstoDrive=129;}//1,128
    else
    {
      if (numtoacton==2){LEDstoDrive=66;}//2,64
      else
      {
        if (numtoacton==3){LEDstoDrive=36;}//4,32
        else
        {
          if (numtoacton==4){LEDstoDrive=24;}//8,16
          else
          {
            if (numtoacton==5){LEDstoDrive=36;}//4,32
            else
            {
              if (numtoacton==6){LEDstoDrive=66;}//2,64
              /*else
              {
                if (numtoacton==7){LEDstoDrive=129;}
                /*else
                {
                  if (numtoacton==8){LEDstoDrive=17;}
                }
              }*/
            }
          }
        }
      }
    }

    Wait (4);

    if (scaled>=255){scaled=255;}

    ClearScreen();
    NumOut(0,LCD_LINE2,numtoacton);
    NumOut(0,LCD_LINE3,LEDstoDrive);

    SetSensorTouch(S1);
    SetSensorMode(S1, IN_MODE_RAW);
    ResetSensor(S1);
    scaled=SENSOR_1/2;
  }
}

Code: Alles auswählen

#include "My8574 lib.nxc"
int scaled,numtoacton,LEDstoDrive;
bool LR;
task main()
{
  LR=1;
  while (true)
  {
    SetSensorLowspeed(S1);
    WritePins(S1, 0x40, LEDstoDrive, 1);

    Wait (scaled);

    numtoacton++;
    if (numtoacton==7)
    {
      numtoacton=1;
    }

    if (numtoacton==1){LEDstoDrive=17;}//1,16
    else
    {
      if (numtoacton==2){LEDstoDrive=34;}//2,32
      else
      {
        if (numtoacton==3){LEDstoDrive=68;}//4,64
        else
        {
          if (numtoacton==4){LEDstoDrive=136;}//8,128
          else
          {
            if (numtoacton==5){LEDstoDrive=68;}//4,64
            else
            {
              if (numtoacton==6){LEDstoDrive=34;}//2,32
              /*else
              {
                if (numtoacton==7){LEDstoDrive=34;}
                else
                {
                  if (numtoacton==8){LEDstoDrive=17;}
                }
              }*/
            }
          }
        }
      }
    }
   
    Wait (7);

    ClearScreen();
    NumOut(0,LCD_LINE1,scaled);
    NumOut(0,LCD_LINE2,numtoacton);
    NumOut(0,LCD_LINE3,LEDstoDrive);

    SetSensorTouch(S1);
    SetSensorMode(S1, IN_MODE_RAW);
    ResetSensor(S1);
   
    scaled=SENSOR_1/2;
    if (scaled>=255){scaled=255;}
  }
}

Code: Alles auswählen

#include "My8574 lib.nxc"
int scaled,btetodesp;

task main()
{
  while (true)
  {
    SetSensorLowspeed(S1);
    WritePins(S1, 0x40, btetodesp, 1);
    Wait (7);

    btetodesp++;
    if (btetodesp>=256){btetodesp=0;}

    SetSensorTouch(S1);
    SetSensorMode(S1, IN_MODE_RAW);
    ResetSensor(S1);

    until (scaled<255)
    {
      scaled=SENSOR_1/2;
      if (scaled>=255){scaled=255;}
    }

    scaled=SENSOR_1/2;
    if (scaled>=255){scaled=255;}

    Wait (scaled);
   
    ClearScreen();
    NumOut(0,LCD_LINE1,scaled);
    NumOut(0,LCD_LINE2,btetodesp);
  }
}

Code: Alles auswählen

#include "My8574 lib.nxc"
int scaled,LEDtoDrive,random,read_pins_result;

task main()
{
  while (true)
  {
    SetSensorLowspeed(S1);

    WritePins(S1, 0x40, LEDtoDrive, 1);
    Wait (7);//normal 7 as minimum
    read_pins_result=ReadPins(S1, 0x40);
    Wait (scaled);

    SetSensorTouch(S1);
    SetSensorMode(S1, IN_MODE_RAW);
    ResetSensor(S1);

    scaled=SENSOR_1/2;

    if (scaled>=255){scaled=255;}

    random=Random(8);
    switch (random)
    {
      case 0:{LEDtoDrive=1;}break;
      case 1:{LEDtoDrive=2;}break;
      case 2:{LEDtoDrive=4;}break;
      case 3:{LEDtoDrive=8;}break;
      case 4:{LEDtoDrive=16;}break;
      case 5:{LEDtoDrive=32;}break;
      case 6:{LEDtoDrive=64;}break;
      case 7:{LEDtoDrive=128;}break;
    }

    ClearScreen();
    NumOut(0,LCD_LINE1,scaled);
    NumOut(0,LCD_LINE2,LEDtoDrive);
    NumOut(0,LCD_LINE3,read_pins_result);
  }
}
D:Diese beiden Programme zeigen die Potentiometer-Wert mit den LEDs.
E:These two programs display the potentiometer value with the LEDs.

Code: Alles auswählen

#include "My8574 lib.nxc"
int scaled;

task main()
{
  while (true)
  {
    SetSensorLowspeed(S1);
    if (scaled>=255){scaled=255;}
    WritePins(S1, 0x40, scaled, 1);
    Wait (7);

    SetSensorTouch(S1);
    SetSensorMode(S1, IN_MODE_RAW);
    ResetSensor(S1);

    scaled=SENSOR_1/2;

    ClearScreen();
    NumOut(0,LCD_LINE1,scaled);
  }
}

Code: Alles auswählen

#include "My8574 lib.nxc"
int scaled,LEDtoDrive;

task main()
{
  while (true)
  {
    SetSensorLowspeed(S1);
    WritePins(S1, 0x40, LEDtoDrive, 1);
    Wait (7);

    SetSensorTouch(S1);
    SetSensorMode(S1, IN_MODE_RAW);
    ResetSensor(S1);

    scaled=SENSOR_1/2;

    if (scaled>=255){scaled=255;}

    if (scaled>=224&&scaled<255){LEDtoDrive=1;}
    else
    {
      if (scaled>=192){LEDtoDrive=2;}
      else
      {
        if (scaled>=160){LEDtoDrive=4;}
        else
        {
          if (scaled>=128){LEDtoDrive=8;}
          else
          {
            if (scaled>=96){LEDtoDrive=16;}
            else
            {
              if (scaled>=64){LEDtoDrive=32;}
              else
              {
                if (scaled>=32){LEDtoDrive=64;}
                else
                {
                  if (scaled>=1){LEDtoDrive=128;}
                }
              }
            }
          }
        }
      }
    }
   
    if (scaled==255||scaled==0){LEDtoDrive=0;}
   
    ClearScreen();
    NumOut(0,LCD_LINE1,scaled);
    NumOut(0,LCD_LINE2,LEDtoDrive);
  }
}

D:Ich habe viel mehr Programme, die 8574 zu verwenden, aber einige von ihnen sind für eine andere Hardware.
E:I have many more programs that use the 8574, but some of them are for a different hardware setup.
Deutsch: Bitte vergib mir meine falsche Deutsch, ich bin mit Google für alles zu übersetzen.
English: Please forgive my improper German, I am using google translate for everything.

Matt

Benutzeravatar
HaWe
Administrator
Administrator
Beiträge: 5399
Registriert: 11. Jan 2006 21:01
Wohnort: ein kleiner Planet in der Nähe von Beteigeuze

Re: PCF8574 Projekte mit 8 LEDs

Beitragvon HaWe » 25. Jun 2011 21:06

cool,
danke für deine Beispielprogramme,
thanks for sharing your code! :)
Gruß,
HaWe
±·≠≈²³αβγδε∂ζλμνπξφωΔΦ≡ΠΣΨΩ∫√∀∃∈∉∧∨¬⊂⊄∩∪∅∞®
NXT NXC SCHACHROBOTER: https://www.youtube.com/watch?v=Cv-yzuebC7E


Zurück zu „allgemeine / Nicht-Lego-Robotik und Elektronik“

Wer ist online?

Mitglieder in diesem Forum: 0 Mitglieder und 5 Gäste

Lego Mindstorms EV3, NXT und RCX Forum : Haftungsauschluss