Permalink
Browse files

Works with Due and Zero

shiftOut runs too fast on Due and Zero.  Wrote a shift loop and slowed
it down.
Also cleaned up non-avr conditional compilation
  • Loading branch information...
1 parent 097d4ba commit d40d9d832be8a6ce26d6462e85de3dcf439942fc @driverblock driverblock committed Oct 6, 2015
Showing with 59 additions and 17 deletions.
  1. +28 −1 ST7565/ST7565.cpp
  2. +10 −4 ST7565/examples/gol/gol.pde
  3. +13 −10 ST7565/examples/st7565lcd/st7565lcd.pde
  4. +8 −2 ST7565/glcdfont.c
View
@@ -23,8 +23,20 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
//#include <Wire.h>
+#ifdef __AVR__
#include <avr/pgmspace.h>
#include <util/delay.h>
+#endif
+
+#ifndef _delay_ms
+ #define _delay_ms(t) delay(t)
+#endif
+
+#ifndef _BV
+ #define _BV(bit) (1<<(bit))
+#endif
+
+
#include <stdlib.h>
#include "ST7565.h"
@@ -433,7 +445,22 @@ void ST7565::st7565_init(void) {
}
inline void ST7565::spiwrite(uint8_t c) {
- shiftOut(sid, sclk, MSBFIRST, c);
+
+#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_)
+ shiftOut(sid, sclk, MSBFIRST, c);
+#else
+ int8_t i;
+ for (i=7; i>=0; i--) {
+ digitalWrite(sclk, LOW);
+ delayMicroseconds(5); //need to slow down the data rate for Due and Zero
+ if (c & _BV(i))
+ digitalWrite(sid, HIGH);
+ else
+ digitalWrite(sid, LOW);
+ // delayMicroseconds(5); //need to slow down the data rate for Due and Zero
+ digitalWrite(sclk, HIGH);
+ }
+#endif
/*
int8_t i;
for (i=7; i>=0; i--) {
@@ -7,7 +7,7 @@
int ledPin = 13; // LED connected to digital pin 13
// the LCD backlight is connected up to a pin so you can turn it on & off
-#define BACKLIGHT_LED 10
+#define BACKLIGHT_LED 4
// pin 9 - Serial data out (SID)
// pin 8 - Serial clock out (SCLK)
@@ -31,10 +31,12 @@ extern uint8_t st7565_buffer[1024];
void setup() {
Serial.begin(9600);
- Serial.println(freeRam());
+#ifdef __AVR__
+ Serial.print(freeRam());
+#endif
pinMode(BACKLIGHT_LED, OUTPUT);
- digitalWrite(BACKLIGHT_LED, HIGH);
+ digitalWrite(BACKLIGHT_LED, LOW);
glcd.st7565_init();
glcd.st7565_command(CMD_DISPLAY_ON);
@@ -45,7 +47,9 @@ void setup() {
glcd.clear();
+#ifdef __AVR__
Serial.println(freeRam());
+#endif
// A little of Conway's game of Life
drawGabrielsp138();
@@ -58,6 +62,7 @@ void loop()
glcd.display();
}
+#ifdef __AVR__
// this handy function will return the number of bytes currently free in RAM, great for debugging!
int freeRam(void)
{
@@ -72,6 +77,7 @@ int freeRam(void)
}
return free_memory;
}
+#endif
// http://www.conwaylife.com/wiki/index.php?title=Gabriel%27s_p138
void drawGabrielsp138() {
@@ -136,4 +142,4 @@ void lifeNextGen(ST7565 *g) {
}
g->setpixel(x-1, y-1, pix);
}
-}
+}
@@ -3,7 +3,7 @@
int ledPin = 13; // LED connected to digital pin 13
// the LCD backlight is connected up to a pin so you can turn it on & off
-#define BACKLIGHT_LED 10
+#define BACKLIGHT_LED 4
// pin 9 - Serial data out (SID)
// pin 8 - Serial clock out (SCLK)
@@ -24,11 +24,13 @@ const static unsigned char __attribute__ ((progmem)) logo16_glcd_bmp[]={
void setup() {
Serial.begin(9600);
+#ifdef __AVR__
Serial.print(freeRam());
-
+#endif
+
// turn on backlight
pinMode(BACKLIGHT_LED, OUTPUT);
- digitalWrite(BACKLIGHT_LED, HIGH);
+ digitalWrite(BACKLIGHT_LED, LOW);
// initialize and set the contrast to 0x18
glcd.begin(0x18);
@@ -93,6 +95,7 @@ void setup() {
void loop()
{}
+#ifdef __AVR__
// this handy function will return the number of bytes currently free in RAM, great for debugging!
int freeRam(void)
{
@@ -107,7 +110,7 @@ int freeRam(void)
}
return free_memory;
}
-
+#endif
#define NUMFLAKES 10
#define XPOS 0
@@ -116,13 +119,13 @@ int freeRam(void)
void testdrawbitmap(const uint8_t *bitmap, uint8_t w, uint8_t h) {
uint8_t icons[NUMFLAKES][3];
- srandom(666); // whatever seed
+ randomSeed(666); // whatever seed
// initialize
for (uint8_t f=0; f< NUMFLAKES; f++) {
- icons[f][XPOS] = random() % 128;
+ icons[f][XPOS] = random(128);
icons[f][YPOS] = 0;
- icons[f][DELTAY] = random() % 5 + 1;
+ icons[f][DELTAY] = random(5) + 1;
}
while (1) {
@@ -140,9 +143,9 @@ void testdrawbitmap(const uint8_t *bitmap, uint8_t w, uint8_t h) {
icons[f][YPOS] += icons[f][DELTAY];
// if its gone, reinit
if (icons[f][YPOS] > 64) {
- icons[f][XPOS] = random() % 128;
+ icons[f][XPOS] = random(128);
icons[f][YPOS] = 0;
- icons[f][DELTAY] = random() % 5 + 1;
+ icons[f][DELTAY] = random(5) + 1;
}
}
}
@@ -192,4 +195,4 @@ void testdrawline() {
for (uint8_t i=0; i<64; i+=4) {
glcd.drawline(127, i, 0, 0, WHITE);
}
-}
+}
View
@@ -1,9 +1,15 @@
// 5x7 LCD font 'flipped' for the ST7565 - public domain
+#ifdef __AVR__
#include <avr/io.h>
#include <avr/pgmspace.h>
-
-const uint8_t font[] PROGMEM = {
+#endif
+
+#ifndef __AVR__
+#define PROGMEM
+#endif
+
+const unsigned char font[] PROGMEM = {
0x0, 0x0, 0x0, 0x0, 0x0, // Ascii 0
0x7C, 0xDA, 0xF2, 0xDA, 0x7C, //ASC(01)
0x7C, 0xD6, 0xF2, 0xD6, 0x7C, //ASC(02)

0 comments on commit d40d9d8

Please sign in to comment.