LED Bar Graph sketch conversion to the Teensy 3.
SeeedStudio came up with an unique bargraph using only two
wires (data/ clock) and multiple color bar graphs that can be ganged together.
These handy 10 position LED bar graph have a special driver on
the back of the Grove LED Bar which controls the LED brightness and
multicolor LED bar display. The unit runs at 3 VDC (for Teensy 3) or 5 VDC.
http://www.seeedstudio.com/depot/-p-1178.html?cPath=156_157
SeeedStudio wrote a sketch using an Arduino and "port" shifted out a bit
pattern, from the Arduino 8 bit port, to the bar graph.
Since the Teensy 3 ARM is 32 bit microcontroller how would you modify the
following sketch so it would work on the 32 bit Teensy 3?
Any help is appreciated.
Update ...I was able to re-write the LED bar graph code snippet to work (see below), on the Arduino 328P, without using portB and by using alternative functions. Shiftout and digitalWrite made it possible to port the code to the Teensy 3. Since the Teensy 3 is running faster than the 16 MHz Arduino then it is only a matter of obtaining a digital logic analyzer and get the timing correct.
SeeedStudio came up with an unique bargraph using only two
wires (data/ clock) and multiple color bar graphs that can be ganged together.
These handy 10 position LED bar graph have a special driver on
the back of the Grove LED Bar which controls the LED brightness and
multicolor LED bar display. The unit runs at 3 VDC (for Teensy 3) or 5 VDC.
http://www.seeedstudio.com/depot/-p-1178.html?cPath=156_157
SeeedStudio wrote a sketch using an Arduino and "port" shifted out a bit
pattern, from the Arduino 8 bit port, to the bar graph.
Since the Teensy 3 ARM is 32 bit microcontroller how would you modify the
following sketch so it would work on the 32 bit Teensy 3?
Any help is appreciated.
Code:
// SeeedStudio Grove LED Bar graph sketch
/* Version 1.0 April, 2012
Supply voltage 5V
Module maximum working voltage 5.5 V
Module minimum working voltage 3 V
Module maximum working current 222.6 mA <-----<<<<<
Module Minimum working current 5.37 mA
Chip full load working temperature 52 ℃
works on the SeeedStudio 328P <------<<<<
* Copyright 2012 Joinj
* http://www.seeedstudio.com
*/
#define PORT_Data PORTB
#define PORT_Clk PORTB
#define DATA_Pin 8 //DATA IN
#define CLK_Pin 9 //CLK IN
#define BIT_Data 0x01
#define BIT_Clk 0x02
#define CmdMode 0x0000 //Work on 8-bit mode
#define ON 0x00ff //8-bit 1 data
#define SHUT 0x0000 //8-bit 0 data
//Send 16_bit data
void send16bitData(unsigned int data)
{
for(unsigned char i=0;i<16;i++)
{
if(data&0x8000)
{
PORT_Data |= BIT_Data;
}
else
{
PORT_Data &=~ BIT_Data;
}
PORT_Clk ^= BIT_Clk;
data <<= 1;
}
}
//latch routine for MY9221 data exchange
void latchData(void)
{
//PORT_Data &=~ BIT_Data;
PORT_DATAx &=~ BIT_Data;
delayMicroseconds(10);
for(unsigned char i=0;i<8;i++)
{
PORT_Data ^= BIT_Data;
}
}
//Initializing pins
void setup()
{
pinMode(DATA_Pin,OUTPUT); //Data pin
pinMode(CLK_Pin,OUTPUT); //CLK pin
}
//Send 12 road led brightness data
void sendLED(unsigned int LEDstate)
{
unsigned char i;
for(i=0;i<12;i++)
{
if(LEDstate&0x0001)
send16bitData(ON);
else
send16bitData(SHUT);
// if(i!=11)
LEDstate=LEDstate>>1;
}
}
//If you want turn on the first red led,you can do it like this: sendLED(0x0001);
//The second led: sendLED(0x0002);
void loop()
{
unsigned int i=0x0000;
while(i<=0x03ff)
{
send16bitData(CmdMode); //set first LED Bar mode
sendLED(i); //send first LED Bar data
send16bitData(CmdMode); //set second LED Bar mode,if you do not use two LED Bar work together(connect one by one),you can delete this line.
sendLED(i); //send second LED Bar data,if you do not use two LED Bar work together(connect one by one),you can delete this line.
latchData(); //make it come into effect
i=i*2+1;
delay(100);
}
}
Update ...I was able to re-write the LED bar graph code snippet to work (see below), on the Arduino 328P, without using portB and by using alternative functions. Shiftout and digitalWrite made it possible to port the code to the Teensy 3. Since the Teensy 3 is running faster than the 16 MHz Arduino then it is only a matter of obtaining a digital logic analyzer and get the timing correct.
Code:
/* Version 1.0 April, 2012
Supply voltage 5V
Module maximum working voltage 5.5 V
Module minimum working voltage 3 V
Module maximum working current 222.6 mA <-----<<<<<
Module Minimum working current 5.37 mA
Chip full load working temperature 52 ℃
works on the SeeedStudio 328P <------<<<<
// not working yet on the T3
// alt send16bitData_1(data) <-------<<<<< works arduino
// alt latchData_1() <-------<<<<< works arduino
* Copyright 2012 Joinj
* http://www.seeedstudio.com
*/
//#define PORT_Data PORTB
//#define PORT_Clk PORTB
#define DATA_Pin 2 //DATA IN
#define CLK_Pin 3 //CLK IN
#define BIT_Data 0x01
#define BIT_Clk 0x02
#define CmdMode 0x0000 //Work on 8-bit mode
#define ON 0x00ff //8-bit 1 data
#define SHUT 0x0000 //8-bit 0 data
//Send 16_bit data
void send16bitData(unsigned int data)
{
for(unsigned char i=0;i<16;i++)
{
if(data&0x8000)
{
//PORT_Data |= BIT_Data;
}
else
{
//PORT_Data &=~ BIT_Data;
}
//PORT_Clk ^= BIT_Clk;
data <<= 1;
}
}
//Send 16_bit data
void send16bitData_1(unsigned int data){ // <-------<<<<< Alternate
// Do this for MSBFIRST serial
// shift out highbyte
shiftOut(DATA_Pin, CLK_Pin, MSBFIRST, (data >> 8));
// shift out lowbyte
shiftOut(DATA_Pin, CLK_Pin, MSBFIRST, data);
}
// License GNU Lesser General Public License 2.1 original material copyright David A. Mellis, changes copyright Scott Penrose
// New shiftOut, but backwards compatible, accepts
// shiftOut( dataPin, clockPin, bitOrder, val);
// shiftOut( dataPin, clockPin, bitOrder, val, bits);
// shiftOut (dataPin, clockPin, bitOrder, val, bits, delayMicroSeconds);
void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, unsigned int val, uint8_t bits = 8, uint8_t del = 0)
{
uint8_t i;
for (i = 0; i < bits; i++) {
if (bitOrder == MSBFIRST)
digitalWrite(DATA_Pin, !!(val & (1 << i)));
else
digitalWrite(DATA_Pin, !!(val & (1 << ((bits - 1 - i)))));
digitalWrite(CLK_Pin, HIGH);
delayMicroseconds(del);
digitalWrite(CLK_Pin, LOW);
}
}
void send16bitData_2(unsigned int data){
shiftOut(DATA_Pin, CLK_Pin, MSBFIRST, data, 16, 0);
}
//latch routine for MY9221 data exchange
void latchData(void)
{
// PORT_Data &=~ BIT_Data;
delayMicroseconds(10);
for(unsigned char i=0;i<8;i++)
{
//PORT_Data ^= BIT_Data; // toggle
}
}
//latch routine for MY9221 data exchange <-------<<<<< Alternate
void latchData_1(void)
{
//PORT_Data &=~ BIT_Data; // set
digitalWrite(DATA_Pin, HIGH);
delayMicroseconds(10); // 10 at 16 Hhz
for(unsigned char i=0;i<8;i++)
{
//PORT_Data ^= BIT_Data; // toggle
int x = digitalRead(DATA_Pin);
if (x){
digitalWrite(DATA_Pin, LOW);
}
else
{
digitalWrite(DATA_Pin, HIGH);
}
}
}
//Initializing pins
void setup()
{
pinMode(DATA_Pin,OUTPUT); //Data pin
pinMode(CLK_Pin,OUTPUT); //CLK pin
}
//Send 12 road led brightness data
void sendLED(unsigned int LEDstate)
{
unsigned char i;
for(i=0;i<12;i++)
{
if(LEDstate&0x0001)
send16bitData(ON);
else
send16bitData(SHUT);
// if(i!=11)
LEDstate=LEDstate>>1;
}
}
//If you want turn on the first red led,you can do it like this: sendLED(0x0001);
//The second led: sendLED(0x0002);
void loop()
{
unsigned int i=0x0000;
while(i<=0x03ff)
{
send16bitData_1(CmdMode); //set first LED Bar mode
//sendLED(i); //send first LED Bar data
sendLED(0x0001); // 0-3ff 0x100 0x200 0x3ff
//sendLED(B11111100); // need hex to addr last 2 bars
//sendLED(0x0002);
//send16bitData(CmdMode); //set second LED Bar mode,if you do not use two LED Bar work together(connect one by one),you can delete this line.
//sendLED(i); //send second LED Bar data,if you do not use two LED Bar work together(connect one by one),you can delete this line.
latchData_1(); //make it come into effect
while(1){}
i=i*2+1;
delay(100);
}
}
Last edited: