Hello Community,
I’m officially now at my wits end with this project. If anyone can please help me I would be so grateful as I’m 40+ hours in and at a standstill of what I thought would be the easy part.
Project goal:
Using a Mega2560 to read the SPI communications between my car radio and its LCD Screen powered by a NJU6623 15-CHARACTER 1-LINE DOT MATRIX LCD CONTROLLER DRIVER with OUTPUT PORT
The aim is to scan for the characters Ex: and insert my own string “TEST” for example.
Status:
Bench testing. I have the Mega running the below code connected to the screen and I am able to write to the screen correctly and it displays the text I enter “TEST”.
#include <SPI.h>
#include <Time.h>
#include <Wire.h>
#include "RTClib.h"
RTC_DS3231 rtc;
//PORTB
#define DATAOUT 51 // MOSI
#define SPICLOCK 52 //sck
#define ssPin 53 // PB0
//PORT L
#define mode0 49
#define mode1 48
/**********************************************
// User configurable
**********************************************/
String Oil = "TEST";
void setup() {
// Control pins for the display SPI
pinMode(mode0, OUTPUT);
pinMode(mode1, OUTPUT);
pinMode(ssPin, OUTPUT);
SPI.begin();
SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE3));
Serial2.begin (4800, SERIAL_8E1);
Serial.begin(9600);
}
void loop()
{
populateFixedLcdDisplay(Oil);
}
void populateFixedLcdDisplay(String _lcdInput)
{
_lcdInput.remove(12);
if (_lcdInput.length() < 12)
{
for (byte i = _lcdInput.length() + 1; i <= 12; i++)
{
_lcdInput.concat(' ');
}
}
byte _start = 0x80;
PORTB &= ~(1 << PB0);
SPI.transfer(0x80);
PORTB |= (1 << PB0);
for (byte i = 0; i < _lcdInput.length(); i++)
{
PORTL |= (1 << PL0);
PORTB &= ~(1 << PB0);
SPI.transfer(_lcdInput.charAt(i));
PORTB |= (1 << PB0);
delayMicroseconds(42);
PORTL &= ~(1 << PL0);
}
}
This code was taken from Tony Chatfield - S1-RX8-AC-Display-controller project. His aim was to entirely replace the radio section of the SPI controls. The Wiki has a lot of detail and compiles 90% of my knowledge on the subject.
I have an Uno set as an SPI slave device to read from the master. I used the tutorial/example from Here to test communications from slave to master. I made slight adjustments to match the SPI mode between slave and master.
Altering the master code void loop to:
//populateFixedLcdDisplay(Oil)
char c;
digitalWrite(SS, LOW); // enable Slave Select
// send test string
for (const char * p = "Hello, World\r" ; c = *p; p++)
{
SPI.transfer (c);
Serial.print(c);
}
digitalWrite(SS, HIGH); // disable Slave Select
delay(2000);
and using the Slave code on an Uno:
#include <SPI.h>
byte ssPin = 10;
char buff [50];
volatile byte indx;
volatile boolean process;
void setup (void) {
Serial.begin (9600);
SPI.setDataMode(SPI_MODE3);
pinMode(MISO, OUTPUT); // have to send on master in so it set as output
SPCR |= _BV(SPE); // turn on SPI in slave mode
indx = 0; // buffer empty
process = false;
SPI.attachInterrupt(); // turn on interrupt
}
ISR (SPI_STC_vect) // SPI interrupt routine
{
byte c = SPDR; // read byte from SPI Data Register
if (indx < sizeof buff) {
buff [indx++] = c; // save data in the next index in the array buff
if (c == '\r') //check for the end of the word
process = true;
}
}
void loop (void) {
if (process) {
process = false; //reset the process
Serial.println (buff); //print the array on serial monitor
indx= 0; //reset button to zero
}
}
This gives me valid communication from master to slave. Printing Hello, World in the serial monitor.
If I enable populateFixedLcdDisplay(Oil) in the master code I get the below on repeat in the slave serial monitor.
⸮TEST Hello, World
From this I can see my write to the LCD screen is being picked up by the slave. I don’t understand why the ⸮ is there or the large spacing between TEST and Hello, World.
I understand that the slave code is looking for the last part of the string from the master. In this case \r.
my first question is how do I get it to look for the first character instead of the last and still print he message?
My second question is why if I try to run the MEGA/Master code on an UNO do I get the error PORTL is undefined? Its not defined in either but is fine on the Mega.
I hope from being able to understand the answer to question 1 I can make my own progress and will give the boost needed to continue as I’m on the verge of giving up.