BarGraph Tiva C series Launchpad
Tiva C series Launchpad-BarGraph
Hola! en este primera entrada del blog les mostrare como hacer la prueba del bargraph usando leds y un potenciometro al igual que se hace en Arduino uno.
Materiales:
-Tiva C serie TM4C123g
-Led's (use 8)
-Resistencias 470 ohm (mismo numero de los leds usados)
-Potenciometro 10 Kohm
-Protoboard
-Cables
La primera conexion que se debe hacer es la del potenciometro de la siguiente manera:
A0=PE3
GND
5V=VBUS
Figura 1. Conexion del Potenciometro con la Tiva C
Ahora el codigo a usar sera: File-Examples-display-barGraph.
Figura 2. Programa básico
Y el codigo incluyendo los 8 leds y el pin analogo A0:
// these constants won't change:
const int analogPin = 29; // the pin that the potentiometer is attached to
const int ledCount = 8; // the number of LEDs in the bar graph
int ledPins[] = { 4, 5, 6, 7,8,9,10,13 }; // an array of pin numbers to which LEDs are attached
const int numReadings = 8;
int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
int inputPin = A0;
void setup() {
// loop over the pin array and set them all to output:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
pinMode(ledPins[thisLed], OUTPUT);
}
// initialize serial communication with computer:
Serial.begin(9600);
// initialize all the readings to 0:
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;
}
void loop() {
// read the potentiometer:
int sensorReading = analogRead(analogPin);
// map the result to a range from 0 to the number of LEDs:
int ledLevel = map(sensorReading, 0, 1023, 0, ledCount);
// loop over the LED array:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
// if the array element's index is less than ledLevel,
// turn the pin for this element on:
if (thisLed < ledLevel) {
digitalWrite(ledPins[thisLed], HIGH);
}
// turn off all pins higher than the ledLevel:
else {
digitalWrite(ledPins[thisLed], LOW);
}
}
// subtract the last reading:
total= total - readings[index];
// read from the sensor:
readings[index] = analogRead(inputPin);
// add the reading to the total:
total= total + readings[index];
// advance to the next position in the array:
index = index + 1;
// if we're at the end of the array...
if (index >= numReadings)
// ...wrap around to the beginning:
index = 0;
// calculate the average:
average = total / numReadings;
// send it to the computer as ASCII digits
Serial.println(average);
delay(1);
}
El resultado debe verse algo así:
Para mas información pueden revisar mi siguiente entrada que se trata de este mismo proyecto usando Arduino uno.
Exitos!
Maryori Sabalza Mejía
Cartagena-Colombia
Excelente, gracias por compartirlo (Y)
ResponderBorrarHi, in the code, you use some objects that don't appear to be defined (like Serial and digitalWrite). Can you provide info on where those are defined? Thanks.
ResponderBorrar