Hey Guys,
I'm using a ProMini to run a small rotary encoder counter and display project. The encoder pulses are going to an LS7366 counter via SPI. I Initially set this up using the SPI counter and an I2C OLED display and everything functioned perfectly but the display is unreadable when using in bright outdoors. So I wanted to switch to this JLX12864G-08602 SPI COG display using the U8G2 library.
I had no issues with my code when running the counter with the I2C display but I cannot figure out what I'm doing wrong with connecting multiple SPI devices.
I think I have everything wired up correctly because I can get a string to appear whenever I do not call the LS7366 to connect.
It looks like whenever I initialize the encoder chip and SPI.begin(), I loose the LCD communication.
Is there an Issue with using the u8g2 library and my LS7366 chip?
I would really appreciate any help on this... Been working days trying to figure this one out.
Here is my sketch below:
#include <Arduino.h>
#include <U8g2lib.h>
#include <SPI.h>
int chipSelectPin1=10;
//-===============================DISPLAY SETTINGS =======================================
U8G2_UC1701_MINI12864_F_4W_SW_SPI u8g2(U8G2_R0, /* clock=*/ 13, /* data=*/ 11, /* cs=*/ 2, /* dc=*/ 3, /* reset=*/ 4);
void setup()
{
u8g2.begin();
pinMode(chipSelectPin1, OUTPUT);
digitalWrite(chipSelectPin1, HIGH); // Pin for chip select SPI for encoder
LS7366_Init();
delay(100);
}
void loop()
{
long encoder1Value;
encoder1Value = getEncoderValue(1); // Get the pulse count from the encoder
delay(50);
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_logisoso30_tn); // choose a suitable font
u8g2.drawStr(0,50,encoder1Value); // write something to the internal memory
u8g2.sendBuffer(); // transfer internal memory to the display
delay(50);
}//end void loop
//*****************************************************
long getEncoderValue(int encoder)
//*****************************************************
{
unsigned int count1Value, count2Value, count3Value, count4Value;
long result;
selectEncoder(encoder);
SPI.transfer(0x60); // Request count
count1Value = SPI.transfer(0x00); // Read highest order byte
count2Value = SPI.transfer(0x00);
count3Value = SPI.transfer(0x00);
count4Value = SPI.transfer(0x00); // Read lowest order byte
deselectEncoder(encoder);
result= ((long)count1Value<<24) + ((long)count2Value<<16) + ((long)count3Value<<8) + (long)count4Value;
return result;
}
void selectEncoder(int encoder)
{
digitalWrite(chipSelectPin1,LOW);
}
void deselectEncoder(int encoder)
{
digitalWrite(chipSelectPin1,HIGH);
}
void LS7366_Init(void) // LS7366 Initialization and configuration
{
// SPI initialization
SPI.begin();
//SPI.setClockDivider(SPI_CLOCK_DIV16); // SPI at 1Mhz (on 16Mhz clock)
delay(10);
digitalWrite(chipSelectPin1,LOW);
SPI.transfer(0x88); // Enable write to first byte of chip- MDR0
SPI.transfer(0x01); // set quadrature mode
digitalWrite(chipSelectPin1,HIGH);
}