Home > Exploring Ideas, Trinket Projects > Serial bit banging with Arduino Trinket

Serial bit banging with Arduino Trinket

Serial transmission at 9600 bauds is possible with the Trinket ATtiny85 (5Volts, 8Mhz) from Adafruit.
The analog value (0~1023) presented at the analog pin 1 is serially transmited to the COM-11441 ( 7-segments Serial Display from Sparkfun).

wpid-20140122_215515.jpg

The Sketch:

/* Pot_Display7_02.ino
   Arduining.com 20 JAN 2014
   Serial transmission at 9600 bauds using bit banging.(Trinket (5V, 8mhz).
   Analog 1 in presented in the 7-Segment Serial Display COM-11441 from Sparkfun.
   Using delayMicroseconds() for serial timming.
   BITTIME = 93 was adjusted experimentally.
*/

//----- defining some values:-------------------------------------------------
#define TXDATA  0       // Pin used to transmit data.
#define POT     1       // pin used to read the analog value.
#define BITTIME 93      // adjusted to obtain 104 milliseconds delay (9600bauds).

//----------variables:--------------------------------------------------------
  int potVal=0;
//============================================================================
void setup() {                
  pinMode(TXDATA, OUTPUT);   // initialize the TXDATA pin as an output.
  digitalWrite(TXDATA,HIGH); // TXDATA = 5V when is resting.
  delay(10);                 //Wait for Serial Display startup.
  sendChar('v');             //Reset the display 0x76.
}

//----------------------------------------------------------------------------
void sendChar(char c){
  delayMicroseconds(BITTIME*2);          // wait 2 Stop bits before sending the char
  digitalWrite(TXDATA,LOW);              // low the line
  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);            //Return TXDATA pin to "1".
}

//-------------------------------------------------------------------------
void show(String s){
  for(int i=0;i<4;i++){
    sendChar(s[i]);
  }
}

//=========================================================================
void loop() {
  potVal= analogRead(POT);
  String strPot = String (potVal);
  while(strPot.length()<4)strPot= " " + strPot;   //format to 4 characters.
  show(strPot);
  delay(100);  
}

Then the potentiometer is replaced by a keypad 3×4 and some resistors.

wpid-20140122_010116.jpg

The resistor networw is designed to produce diferent voltage levels for each key pressed.

image

And this is the Sketch using the keypad:

/* AnalogKey_03.ino
   Arduining.com 23 JAN 2014
   Using an analog pin to read a 3x4 keypad and presenting the keyed data in the
   7-Segment Serial Display COM-11441 from Sparkfun.
   Notes:
     Implemented using the Arduino Trinket (5V, 8Mhz) from Adafruit. 
     Serial transmission at 9600 bauds using bit banging.
     BITTIME delay was determined using an oscilloscope.
     1% resistors are used in the keypad (1.62k,6.49k and 25.5k) 
*/

//----- defining some values:-------------------------------------------------
#define TXDATA    0         // Pin used to transmit data.
#define ADIN      1         // pin used to read the analog value.
#define BITTIME   93        // Adjusted experimentally to obtain 9600 bauds.
#define DEBOUNCE  5         // number of detections before key validation.

//----------variables:--------------------------------------------------------
  int lastkey= -1;          // No key.
  int key;                  // key value
  boolean released= true;   // No key pressed estatus.
  boolean  newline= true;   // flag to clean screen before new value entry.
  int validkey=0;           // used as debounce counter.
  int analog;               // value of the analog pin.
  char dispbuff[]= "-   ";
//============================================================================
void setup() {                
  pinMode(TXDATA, OUTPUT);   // initialize the TXDATA pin as an output.
  digitalWrite(TXDATA,HIGH); // TXDATA = 5V when is resting.
  delay(10);                 //Wait for Serial Display startup.
  sendChar('v');             //Reset the display 0x76.
}

//----------------------------------------------------------------------------
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){
  for(int i=0;i<4;i++){
    sendChar(s[i]);
  }
}

//--------------------------------------------------------------------------
void checkey(){
  analog= analogRead(ADIN);		
  key=-1;					        // no key pressed.
  if(analog<524) key=1;
  if(analog<508) key=4;
  if(analog<491) key=7;	
  if(analog<472) key=10;	// '*' key
  if(analog<453) key=2;
  if(analog<432) key=5;
  if(analog<409) key=8;
  if(analog<385) key=0;	
  if(analog<359) key=3;
  if(analog<330) key=6;
  if(analog<299) key=9;
  if(analog<265) key=11;	// '#' key

  if (key!=lastkey){
    validkey=0;
    lastkey=key;
   }
  validkey++;
  if(validkey > DEBOUNCE){
    validkey=0;
    if (key<0){
      released= true;
      return;
    }
    else if (released == true){
      released=false;
      return; 
    }
  }
  key=-1;
}

//--------------------------------------------------------------------------
void clrbuff(){
  dispbuff[0]= '-';
  dispbuff[1]= ' ';
  dispbuff[2]= ' ';
  dispbuff[3]= ' ';
  newline= false;
}
//--------------------------------------------------------------------------
void action(){
  dispbuff[0]= 'd';
  dispbuff[1]= 'o';
  dispbuff[2]= 'n';
  dispbuff[3]= 'e';
  newline= true;
}

//==========================================================================
void loop() {
  show(dispbuff);     //Show content of the array dispbuff in the display
  checkey();
  if(key>=0){
    if(key==10) clrbuff();
    if(key==11) action();
    if(key<10){
     if (newline==true) clrbuff();
     dispbuff[0]= dispbuff[1];    //buffer shift (display shift to the left)
     dispbuff[1]= dispbuff[2];
     dispbuff[2]= dispbuff[3];
     dispbuff[3]=char(key+'0');
    }
  }
  delay(5);         // experimental, 5 works fine. (10 is to slow).
}

The video is in YouTube:

Advertisements
  1. Alior
    February 9, 2014 at 12:56 AM

    e super mar interesa codul sursa

    • February 9, 2014 at 11:27 AM

      The code was added, thanks for asking.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: