Testing the bit bang serial transmission to connect the Trinket with the Serial LCD Module 16×2 from NKC Electronics.
The first Sketch with string functions use 3,634 bytes, the second Sketch, avoiding string functions, use 1,362 bytes.
Sketch with string functions:
/* Test_LCD.ino Arduining.com 16 FEB 2014 Binary sketch size: 3,634 bytes (of a 5,310 byte maximum) A timer is implemented presenting minutes and seconds elapsed since start. Bit Banging Serial transmission at 9600 bauds. Example using the Trinket from Adafruit (5V, 8 Mhz) and Serial LCD Module 16x2 from NKC Electronics. */ //----- defining bit banging serial values:------------------------------------ #define TXDATA 1 // Pin used to transmit data. #define BITTIME 93 // Adjusted with osciloscope to obtain 9600 bauds. //----------variables:--------------------------------------------------------- int seconds= 0; //seconds counter String number; //String to be sent to the display. //============================================================================= void setup() { pinMode(TXDATA, OUTPUT); // initialize the TXDATA pin as an output. digitalWrite(TXDATA,HIGH); // TXDATA = 5V when is resting. //---LCD Display initialisation--- delay(500); sendChar(0xFE); sendChar(0x41); //display on. sendChar(0xFE); sendChar(0x51); //clear display. sendChar(0xFE); sendChar(0x4C); //cursor blinking Off. sendChar(0xFE); sendChar(0x48); //cursor Off. sendChar(0xFE); sendChar(0x46); //cursor at home. sendChar(0xFE); sendChar(0x53); //backlight brightness command. sendChar(4); //brightness 1-8 sendChar(0xFE); sendChar(0x52); //contrast command. sendChar(45); //contrast 1-50 show(" Arduining.com"); } //================= Main loop ================================================= void loop() { if ((millis() % 1000)<2){ //Skip until the next second. sendChar(0xFE); sendChar(0x45); // Set cursor at sendChar(0x45); // line 2 pos 6 number = String (seconds/60); //elapsed minutes while(number.length()<2)number= "0" + number; //format to 2 characters. show(number); //Show minutes in the display sendChar(':'); number = String (seconds % 60); //elapsed seconds while(number.length()<2)number= "0" + number; //format to 2 characters. show(number); //Show seconds in the display seconds++; if (seconds >= 3600) seconds=0; //roll over } } //----------------------------------------------------------------------------- void sendChar(char c){ delayMicroseconds(BITTIME*2); // wait 2 Stop bits. digitalWrite(TXDATA,LOW); // TXDATA=0. delayMicroseconds(BITTIME); // wait Start bit. for (int i=0; i<8;i++){ digitalWrite(TXDATA,bitRead(c, i)); // bit out. delayMicroseconds(BITTIME); // wait bit } digitalWrite(TXDATA,HIGH); //TXDATA=1. } //----------------------------------------------------------------------------- void show(String s){ int lenght=s.length(); for(int i=0;i<lenght;i++){ sendChar(s[i]); } }
Sketch without string functions:
/* Test_LCD_01.ino Arduining.com 17 FEB 2014 Based in Test_LCD.ino (2,278 bytes less avoiding string functions). Binary sketch size: 1,356 bytes (of a 5,310 byte maximum) A timer is implemented presenting minutes and seconds elapsed since start. Bit Banging Serial transmission at 9600 bauds. Example using the Trinket from Adafruit (5V, 8 Mhz) and Serial LCD Module 16x2 from NKC Electronics. */ //----- defining bit banging serial values:------------------------------------ #define TXDATA 1 // Pin used to transmit data. #define BITTIME 93 // Adjusted with osciloscope to obtain 9600 bauds. //----------variables:--------------------------------------------------------- int seconds= 0; //seconds counter //============================================================================= void setup() { pinMode(TXDATA, OUTPUT); // initialize the TXDATA pin as an output. digitalWrite(TXDATA,HIGH); // TXDATA = 5V when is resting. //---LCD Display initialisation--- delay(500); sendChar(0xFE); sendChar(0x41); //display on. sendChar(0xFE); sendChar(0x51); //clear display. sendChar(0xFE); sendChar(0x4C); //cursor blinking Off. sendChar(0xFE); sendChar(0x48); //cursor Off. sendChar(0xFE); sendChar(0x46); //cursor at home. sendChar(0xFE); sendChar(0x53); //backlight brightness command. sendChar(4); //brightness 1-8 sendChar(0xFE); sendChar(0x52); //contrast command. sendChar(45); //contrast 1-50 show(" Arduining.com"); } //================= Main loop ================================================= void loop() { if ((millis() % 1000)<2){ //Skip until the next second. sendChar(0xFE); sendChar(0x45); // Set cursor at sendChar(0x45); // line 2 pos 6 sendChar((seconds/600)+'0'); //show tens of minutes sendChar(((seconds/60) % 10)+'0'); //show units of minutes sendChar(':'); sendChar(((seconds % 60)/10)+'0'); //show tens of seconds sendChar((seconds % 10)+'0'); //show units of seconds seconds++; if (seconds >= 3600) seconds=0; //roll over } } //----------------------------------------------------------------------------- void sendChar(char c){ delayMicroseconds(BITTIME*2); // wait 2 Stop bits. digitalWrite(TXDATA,LOW); // TXDATA=0. delayMicroseconds(BITTIME); // wait Start bit. for (int i=0; i<8;i++){ digitalWrite(TXDATA,bitRead(c, i)); // bit out. delayMicroseconds(BITTIME); // wait bit } digitalWrite(TXDATA,HIGH); //TXDATA=1. } //----------------------------------------------------------------------------- void show(char s[]){ int i=0; while(s[i]!=0){ //looking for the string end. sendChar(s[i]); i++ ; } }
Not so much as comment on your superb projects, but a question: what are those (spring) connectors/probes in the photo with the serial LCD? I haven’t seen those before (I’m in the UK) and they look handy.
Mike
minigrabber test clips , use google images. Found them in Amazon. Very usefull for quick tests…