I have to say it sounds easy enough unless your like me and just play with C on the weekend
Needless to say after pulling my hair this afternoon I have managed to get 2 Teensy 3.1's talking to each other at 4.8Mhz.
My large scale plan is to send data from a motor controller on my EGoCart to a touch screen mounted on the steering wheel and I needed a little more then the standard Serial Baud rates could do, I will be using RS485 to make up for the cable lengths. Adding RS485 to the testing will come soon.
Below is the Transmit(motor controller) portion stripped down and the Receiving side(ILI9341 LCD w/touch) in its current WIP glory. Right now I am only transmitting 2 bytes, which will be 1 of several analog values im reading and sending in the final build along with a dozen or so other pieces of data.
I think the Serial can go faster but I will leave it for another day.... Like tomorrow.
Questions, comments, "hey this is lame do this instead". all welcome.
Transmit
Receive
P.S. not sure if this would be better in Tech Support
Needless to say after pulling my hair this afternoon I have managed to get 2 Teensy 3.1's talking to each other at 4.8Mhz.
My large scale plan is to send data from a motor controller on my EGoCart to a touch screen mounted on the steering wheel and I needed a little more then the standard Serial Baud rates could do, I will be using RS485 to make up for the cable lengths. Adding RS485 to the testing will come soon.
Below is the Transmit(motor controller) portion stripped down and the Receiving side(ILI9341 LCD w/touch) in its current WIP glory. Right now I am only transmitting 2 bytes, which will be 1 of several analog values im reading and sending in the final build along with a dozen or so other pieces of data.
I think the Serial can go faster but I will leave it for another day.... Like tomorrow.
Questions, comments, "hey this is lame do this instead". all welcome.
Transmit
Code:
/////////Donziboy2/////////
#define SERIAL_BAUD_RATE 4800000
void setup() {
Serial.begin(9600);
Serial1.begin( SERIAL_BAUD_RATE, SERIAL_8N1 );
delay(200);
}
elapsedMillis serialtesting; //
int batterySOC = 1234; //serialtestvalue
byte buffer[1] = {0};
void loop() {
int i;
if(serialtesting > 200) { //10s
buffer[1] = (byte) (batterySOC & 0xFF);
buffer[0] = (byte) ((batterySOC >> 8) & 0xFF);
Serial1.write(buffer[0]);
Serial1.write(buffer[1]);
serialtesting = 0;
batterySOC = batterySOC + 1;
if(batterySOC > 9999){
batterySOC = 0;
}
}
}
Receive
Code:
/***************************************************
Adafruit graphicstest used as a base.
****************************************************/
//////////////////////////////////////////////////////
//Modified by Donziboy2
//////////////////////////////////////////////////////
#include "SPI.h"
#include "ILI9341_t3.h"
// For the Adafruit shield, these are the default.
#define TFT_DC 9
#define TFT_CS 10
#define TFT_RST 8
ILI9341_t3 tft = ILI9341_t3(TFT_CS, TFT_DC, TFT_RST);
#define SERIAL_BAUD_RATE 4800000
//Test perams
int frequency2 = 259;
float BatterySOC = 0; // percentage of charge (assumed)
float BatterySOCold = 0;
volatile int32_t executiontime = 0;
byte boxset = 0;
byte drivestate = 1;
byte drivestateold = 0;
byte Redfull = 0;
byte Yellowfull = 0;
byte Greenfull = 0;
int a = 0;
int b = 0;
float xold = 0;
float yold = 0;
float zold = 0;
float aold = 10;
float bold = 10;
float cold = 10;
float dold = 9;
int h = 0;
int i = 0;
byte buffer[1] = {0};
void setup() {
tft.begin();
tft.fillScreen(ILI9341_BLACK);
tft.setRotation(3);
tft.setTextColor(ILI9341_YELLOW);
tft.setTextSize(3);
tft.println("Initializing.....");
delay(1000);
tft.fillScreen(ILI9341_BLACK);
tft.fillRect(5, 5, 20, 230, ILI9341_WHITE); //blank battery bar once
mainScreen();
Serial.begin( 9600 );
Serial1.begin( SERIAL_BAUD_RATE );
}
void loop() {
if (Serial1.available() > 1){
buffer[0] = Serial1.read();
buffer[1] = Serial1.read();
int a = buffer[0] << 8;
a = a + buffer[1];
BatterySOC = a;
}
refreshtime();
mainScreen();
// delay(50);
// frequency2++;
h++;
// i++;
if(h == 30){
if(frequency2 >= 999){
a = 1;
}
if(frequency2 <= 0){
a = 0;
}
if(a == 0){
frequency2 = frequency2 + 1;
}
else
{
frequency2 = frequency2 - 1;
}
h = 0;
}
/*
if(i == 2){
if(BatterySOC >= 9999){
b = 1;
}
if(BatterySOC <= 0){
b = 0;
}
if(b == 0){
BatterySOC = BatterySOC + 1;
}
else
{
BatterySOC = BatterySOC - 1;
}
i = 0;
}*/
}
unsigned long refreshtime(){
tft.fillRect(35, 110, 75, 15, ILI9341_BLACK);
tft.setCursor(35, 110);
tft.setTextColor(ILI9341_WHITE); tft.setTextSize(2);
tft.print(executiontime);
tft.setCursor(35, 130);
tft.print("Microseconds");
}
unsigned long mainScreen() {
elapsedMicros elapsedtesttime;
if(boxset < 1){ //removes redraw for no reason
tft.drawRect(145, 5, 170, 30, ILI9341_GREEN); //drive status box
tft.drawRect(35, 185, 65, 50, ILI9341_WHITE);
tft.drawRect(105, 185, 65, 50, ILI9341_WHITE);
tft.drawRect(175, 185, 65, 50, ILI9341_WHITE);
tft.drawRect(245, 185, 65, 50, ILI9341_WHITE);
tft.drawRect(5, 5, 20, 230, ILI9341_WHITE); //battery bar outline
tft.setTextColor(ILI9341_WHITE); tft.setTextSize(2);
tft.setCursor(74, 5);
tft.print(".");
tft.setCursor(110, 5);
tft.print("%");
tft.setTextColor(ILI9341_WHITE); tft.setTextSize(2);
tft.setCursor(50, 22);
tft.print("Charge");
tft.setTextColor(ILI9341_BLUE); tft.setTextSize(4);
tft.setCursor(240, 150);
tft.println("MPH");
tft.setTextColor(ILI9341_YELLOW); tft.setTextSize(5);
tft.setCursor(255, 110);
tft.print(".");
boxset = 1;
}
if(BatterySOCold > BatterySOC){
float w = (1 - (BatterySOC / 10000)) * 228;
tft.fillRect(6, 6, 18, w, ILI9341_WHITE); //blank battery bar
Redfull = 0;
Yellowfull = 0;
}
if(BatterySOC <= 3333 && BatterySOC != 0){ //red battery bar only
float x;
float y;
x = BatterySOC / 3333;
y = x * 76; //rectangle size
x = 235 - y; //rectangle starting position
tft.fillRect(6, x, 18, y, ILI9341_RED);
Redfull = 0;
Yellowfull = 0;
}
if(BatterySOC <= 6666 && BatterySOC > 3333){ //red and yellow battery bars only
float x;
float y;
x = BatterySOC - 3333; //remove red region
x = x / 3333;
y = x * 76; //rectangle size
x = 159 - y; //rectangle starting postion
if(Redfull == 0){
tft.fillRect(6, 158, 18, 76, ILI9341_RED);
Redfull = 1;
}
tft.fillRect(6, x, 18, y, ILI9341_YELLOW);
Yellowfull = 0;
}
if(BatterySOC <= 10000 && BatterySOC > 6666){ //red, yellow and green battery bars only
float x;
float y;
x = BatterySOC - 6666; //remove red and yellow region
x = x / 3333;
y = x * 76; //rectangle size
x = 83 - y; //rectangle starting postion
if(Redfull == 0){
tft.fillRect(6, 158, 18, 76, ILI9341_RED);
Redfull = 1;
}
if(Yellowfull == 0){
tft.fillRect(6, 82, 18, 76, ILI9341_YELLOW);
Yellowfull = 1;
}
tft.fillRect(6, x, 18, y, ILI9341_GREEN);
}
// tft.fillRect(50, 5, 59, 14, ILI9341_BLACK); //clears % value
// tft.setTextColor(ILI9341_BLACK); tft.setTextSize(2);
// tft.setCursor(50, 5);
// tft.print((float)BatterySOCold * 100,2); tft.print("%");
//1234 as example
float a = BatterySOC / 1000; //1.234
float b = BatterySOC / 100; //12.34
float c = BatterySOC / 10; //123.4
int aa = a; //1
int bb = b; //12
int cc = c; //123
float aaa = aa; //1
float bbb = bb; //12
float ccc = cc; //123
a = aaa; //1
b = bbb - (aaa * 10); //2
c = ccc - (bbb * 10); //3
float d = BatterySOC - (ccc * 10); //4
tft.setTextSize(2);
if(a != aold){
tft.setTextColor(ILI9341_BLACK);
tft.setCursor(50, 5);
tft.print(aold, 0);
tft.setTextColor(ILI9341_WHITE);
tft.setCursor(50, 5);
tft.print(a, 0);
aold = a;
}
if(b != bold){
tft.setTextColor(ILI9341_BLACK);
tft.setCursor(62, 5);
tft.print(bold, 0);
tft.setTextColor(ILI9341_WHITE);
tft.setCursor(62, 5);
tft.print(b, 0);
bold = b;
}
////decimal is printed once with rest of boxes
if(c != cold){
tft.setTextColor(ILI9341_BLACK);
tft.setCursor(86, 5);
tft.print(cold, 0);
tft.setTextColor(ILI9341_WHITE);
tft.setCursor(86, 5);
tft.print(c, 0);
cold = c;
}
if(d != dold){
tft.setTextColor(ILI9341_BLACK);
tft.setCursor(98, 5);
tft.print(dold, 0);
tft.setTextColor(ILI9341_WHITE);
tft.setCursor(98, 5);
tft.print(d, 0);
dold = d;
}
BatterySOCold = BatterySOC;
// tft.setCursor(50, 5);
// tft.setTextColor(ILI9341_WHITE); tft.setTextSize(2);
// tft.print((float)BatterySOC * 100,2); tft.print("%");
// tft.fillRect(195, 110, 115, 35, ILI9341_BLACK); //clears MPH Value
//239 as example
float x = frequency2 / 100; //2.39
float y = (frequency2 / 10); //23.9
int xx = x; //2
int yy = y; //23
float xxx = xx; //2
float yyy = yy; //23
x = xxx; //2
y = yyy - (x * 10); //3
float z = frequency2 - (yyy * 10); //9
tft.setTextSize(5);
if(x != xold){
tft.setTextColor(ILI9341_BLACK);
tft.setCursor(195, 110);
tft.print(xold, 0);
tft.setTextColor(ILI9341_YELLOW);
tft.setCursor(195, 110);
tft.print(x, 0);
xold = x;
}
if(y != yold){
tft.setTextColor(ILI9341_BLACK);
tft.setCursor(225, 110);
tft.print(yold, 0);
tft.setTextColor(ILI9341_YELLOW);
tft.setCursor(225, 110);
tft.print(y, 0);
yold = y;
}
////decimal is printed once with rest of boxes
if(z != zold){
tft.setTextColor(ILI9341_BLACK);
tft.setCursor(285, 110);
tft.print(zold, 0);
tft.setTextColor(ILI9341_YELLOW);
tft.setCursor(285, 110);
tft.print(z, 0);
zold = z;
}
//tft.print((float)frequency2, 1); // this rounds 25.98765 to 26.0 :(
if(drivestateold != drivestate){ //only change is drivestate changes
tft.setTextColor(ILI9341_RED); tft.setTextSize(2);
tft.setCursor(170, 12);
tft.println("DriveStatus");
drivestateold = 1;
}
executiontime = elapsedtesttime;
}
P.S. not sure if this would be better in Tech Support