Go Down

Topic: Serielles LCD gestört? (Read 1 time) previous topic - next topic

kduin

Hallo.

Was ich vor habe:
Mit einer IR-Fernbedienung will ich PONG auf einem SparkFun 20x4 Seriellen Display spielen.

Momentan befinde ich mich noch ganz am Anfang.

Die einzelnen Sketches für den IR-Empfänger und für die Ansteuerung des Seriellen Displays habe ich schon entwickelt, diese laufen einzeln genau wie ich das will.
Für den IR-Empfänger habe ich den Code von Ken Shirriff genommen.
Fürs Display habe ich die Vorlage fürs SparkFun SerLCD aus dem Playground genommen. (LINK)

Jetzt habe ich die beiden Sketches zusammengeführt.

Mein Problem:
Im Setup-Teil des Sketches funktioniert das Display noch einwandfrei. (das habe ich zum debuggen getestet)
Aber sobald die IR-Funktionen laufen (scheint mit Interrupts zu funktionieren, da auch während eines 10-sec. Delays codes von der FB angenommen werden)
und ich dann was aufs Display schreibe, erscheinen auf dem Display nur merkwürdige Zeichen.
Das Display ist übrigens nicht an RX/TX sondern wird mit SoftwareSerial angesprochen.

Jemand eine Idee woran das liegen kann?



kduin

Hier mein Code:
Code: [Select]
/*
* IRremote: IRrecvDump - dump details of IR codes with IRrecv
* An IR detector/demodulator must be connected to the input RECV_PIN.
* Version 0.1 July, 2009
* Copyright 2009 Ken Shirriff
* http://arcfn.com
*/
#include <SoftwareSerial.h>
#include <IRremote.h>

#define txPin 12
SoftwareSerial LCD = SoftwareSerial(0, txPin);


/***************** LG REMOTE CODES ***************/
#define KEY_POWER 0x20DF10EF
#define KEY_OK 0x0DF22DD
#define KEY_LEFT 0x20DFE01F
#define KEY_RIGHT 0x20DF609F
#define KEY_UP 0x20DF02FD
#define KEY_DOWN 0x20DF827D
#define KEY_VOL_UP 0x20DF40BF
#define KEY_VOL_DOWN 0x20DFC03F
#define KEY_MUTE 0x20DF906F
#define KEY_PROG_UP 0x20DF00FF
#define KEY_PROG_DOWN 0x20DF807F
#define KEY_MENU 0x20DFC23D
#define KEY_0 0x20DF08F7
#define KEY_1 0x20DF8877
#define KEY_2 0x20DF48B7
#define KEY_3 0x20DFC837
#define KEY_4 0x20DF28D7
#define KEY_5 0x20DFA857
#define KEY_6 0x20DF6897
#define KEY_7 0x20DFE817
#define KEY_8 0x20DF18E7
#define KEY_9 0x20DF9867

   long KEY_codes[] = {0x20DF10EF, 0x20DF22DD, 0x20DFE01F, 0x20DF609F, 0x20DF02FD, 0x20DF827D, 0x20DF40BF, 0x20DFC03F, 0x20DF906F, 0x20DF00FF, 0x20DF807F, 0x20DFC23D, 0x20DF08F7, 0x20DF8877, 0x20DF48B7, 0x20DFC837, 0x20DF28D7, 0x20DFA857, 0x20DF6897, 0x20DFE817, 0x20DF18E7, 0x20DF9867};
   String KEY_buttons[] = {"POWER", "OK", "LEFT", "RIGHT", "UP", "DOWN", "VOL_UP", "VOL_DOWN", "MUTE", "PROG_UP", "PROG_DOWN", "MENU", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
   int KEY_array_size = (sizeof(KEY_codes)/sizeof(long));

String TranslateCode(long code)
{
 String giveback = "";
/*    
   long codes[] = {0x20DF10EF, 0x20DF22DD, 0x20DFE01F, 0x20DF609F, 0x20DF02FD, 0x20DF827D, 0x20DF40BF, 0x20DFC03F, 0x20DF906F, 0x20DF00FF, 0x20DF807F, 0x20DFC23D, 0x20DF08F7, 0x20DF8877, 0x20DF48B7, 0x20DFC837, 0x20DF28D7, 0x20DFA857, 0x20DF6897, 0x20DFE817, 0x20DF18E7, 0x20DF9867};
   String buttons[] = {"POWER", "OK", "LEFT", "RIGHT", "UP", "DOWN", "VOL_UP", "VOL_DOWN", "MUTE", "PROG_UP", "PROG_DOWN", "MENU", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
   int array_size = (sizeof(codes)/sizeof(long));
*/
   
   //check if code id in
   for(int xi = 0; xi < KEY_array_size; xi++ )
   {
     if(code == KEY_codes[xi])
     {
       return KEY_buttons[xi];
     }
   }
return "UNKNOWN";
}

/***************** END CODES **********************/
boolean CheckKeyPressedValid(long code)
{
   for(int xi = 0; xi < KEY_array_size; xi++ )
   {
     if(code == KEY_codes[xi])
     {
       return true;
     }
   }
   // else
return false;
}




int RECV_PIN = 3;

IRrecv irrecv(RECV_PIN);

decode_results results;



// Dumps out the decode_results structure.
// Call this after IRrecv::decode()
// void * to work around compiler issue
//void dump(void *v) {
//  decode_results *results = (decode_results *)v
void dump(decode_results *results) {
 int count = results->rawlen;
 if (results->decode_type == UNKNOWN) {
   ////////Serial.println("Could not decode message");
 }
 else {
   if (results->decode_type == NEC) {
     ////////Serial.print("Decoded NEC: ");
   }
   else if (results->decode_type == SONY) {
     ////////Serial.print("Decoded SONY: ");
   }
   else if (results->decode_type == RC5) {
     ////////Serial.print("Decoded RC5: ");
   }
   else if (results->decode_type == RC6) {
     ////////Serial.print("Decoded RC6: ");
   }
   ////////Serial.print(results->value, HEX);
   ////////Serial.print(" (");
   ////////Serial.print(results->bits, DEC);
   ////////Serial.println(" bits)");
 }
 ////////Serial.print("Raw (");
 ////////Serial.print(count, DEC);
 ////////Serial.print("): ");

 for (int i = 0; i < count; i++) {
   if ((i % 2) == 1) {
     ////////Serial.print(results->rawbuf[i]*USECPERTICK, HEX);
   }
   else {
     ////////Serial.print(-(int)results->rawbuf[i]*USECPERTICK, HEX);
   }
   ////////Serial.print(" ");
 }
 ////////Serial.println("");
}



char* StrToChar(String str)
{
      char str_c[str.length()+1];
     str.toCharArray(str_c, (str.length() + 1));
return str_c;
}
//#####################################################

kduin

... und der Rest:


Code: [Select]
void setup()
{
Serial.begin(9600);

  pinMode(txPin, OUTPUT);
  LCD.begin(9600);
  backlightSerLcd(70);
  delay(100);
displaySerLcdScreen("     WELCOME                                 PONG");
                    //0123456789012345678_L123456789012345678_L123456789012345678_L123456789012345678_L 
delay(3000);
  clearSerLcd();
 

  irrecv.enableIRIn(); // Start the receiver
}
void loop()
{
 

// ######################################################################## //
// ###### CHECK IF KEY PRESSED, IF SO, STORE CODE IN key_pressed ########## //
long key_pressed = 0;
if (irrecv.decode(&results))
  {
     if(CheckKeyPressedValid(results.value) == true)   
     {
      key_pressed = results.value;
      ////////Serial.println(TranslateCode(key_pressed));
     }
//  dump(&results); // ////////Serial.PRINT THE CODES
    irrecv.resume(); // Receive the next value
  }
// ###### END READING IR REMOTE CONTROL ################################### //
// ######################################################################## //
displaySerLcdScreen("TEST");
                    //0123456789012345678_L123456789012345678_L123456789012345678_L123456789012345678_L 
Serial.println(); Serial.println(); Serial.println("############");
Serial.print("free memory = ");
Serial.print(availableMemory());
Serial.print(" - memory used = ");
Serial.println(8192-availableMemory());
Serial.println("############");

}



//########################################
    int availableMemory()
    {
     int size = 8192;
     byte *buf;
     while ((buf = (byte *) malloc(--size)) == NULL);
     free(buf);
     return size;
    }


//############################################################################
//SerialLCD Functions
// NOTE, None of the following functions use each other. If all you need is one function,
// like for example, writing lines (displaySerLcdLine), you can delete all the other
// functions listed below to reduce the size of the code. They are not interdependent.
// displaySerLcdScreen("text for the entire screen")
// overwrites the entire screen, wraps the text as needed
// truncates after 80 characters
void displaySerLcdScreen(char *theText){
  int delayTime = 50;
  LCD.print(0xFE, BYTE);   // command flag
  delay(delayTime);
  LCD.print(128, BYTE);    // start position for line 1
  if (strlen(theText) < 80) {
    // less than 80 characters, print then and then
    LCD.print(theText);
    // pad the rest of the line with spaces
    for (int i = strlen(theText); i < 80; i++) {
      LCD.print(" ");
    }
  }
  else { 
    // 80 or more characters, just print the first 80
    for (int i = 0; i < 80; i++) {
      LCD.print(theText[i]);
    }
  }
  delay(delayTime);
}
// displaySerLcdLine( line number, "text for that line")
// writes to each line separately
// lineNum is an integer for the line number. valid values 1 through 4
// *theText is a string of text and it gets padded at the end with spaces
// to overwrite whatever is already showing on that line. If you send more than
// 20 characters, it truncates the text.
void displaySerLcdLine(int lineNum, char *theText){
  int delayTime = 50;
  int lcdPosition = 0;  // initialize lcdPosition and use to indicate value values

  // based upon the lineNum, set the position on the LCD
  if (lineNum==1){
    lcdPosition = 128;
  }
  else if (lineNum==2){
    lcdPosition = 192;
  }
  else if (lineNum==3){
    lcdPosition = 148;
  }
  else if (lineNum==4){
    lcdPosition = 212;
  }
  // don't write to the LCD if the lineNum value didn't generate a valid position
  if (lcdPosition > 0){
    LCD.print(0xFE, BYTE);   //command flag
    delay(delayTime);
    LCD.print(lcdPosition, BYTE);    //position

    if (strlen(theText) < 20) {
      // less than 20 characters, print then and then
      LCD.print(theText);
      // pad the rest of the line with spaces
      for (int i = strlen(theText); i < 20; i++) {
        LCD.print(" ");
      }
    }
    else { 
      // 20 or more characters, just print the first 20
      for (int i = 0; i < 20; i++) {
        LCD.print(theText[i]);
      }
    }
    delay(delayTime);
  }
}
// displaySerLcdChar(LCD line, position on line, 'the character to display')
// LCD line: integer 1 through 4
// position on line: integer 1 through 20
// character to display: a single character in single quotes
void displaySerLcdChar(int lineNum, int charNum, char theChar){
  int delayTime = 50;
  int lcdPosition = 0;  // initialize lcdPosition and use to indicate value values
  // charNum has to be within 1 to 20,
  // lineNum has to be within 1 to 4
  if (charNum > 0 && charNum < 21) {
    if (lineNum==1){
      lcdPosition = 128;
    }
    else if (lineNum==2){
      lcdPosition = 192;
    }
    else if (lineNum==3){
      lcdPosition = 148;
    }
    else if (lineNum==4){
      lcdPosition = 212;
    }
  }
  // don't write to the LCD if the lineNum and charNum values were not within range
  if (lcdPosition > 0){
    // add to start of line position to get the position to write to
    lcdPosition = lcdPosition + charNum - 1;

    LCD.print(0xFE, BYTE);   //command flag
    delay(delayTime);
    LCD.print(lcdPosition, BYTE);    //position
    LCD.print(theChar);
    delay(delayTime);
  }
}
void clearSerLcd(){
  LCD.print(0xFE, BYTE);   //command flag
  LCD.print(0x01, BYTE);   //clear command.
  delay(50);
}
void backlightSerLcd(int thePercentage){  //turns on the backlight
  LCD.print(0x7C, BYTE);   //command flag for backlight stuff
  int theValue = map(thePercentage, 0,100,128,157); // maps percentage to what SerLCD wants to see
  LCD.print(theValue, BYTE);    //light level.
  delay(50); 
}
//########################### END SERIAL LCD  FUNCTIONS  #########################
//################################################################################

Go Up