Maple Lesson 02 - Multiple LED Blinking
ObjectiveUse the Microduino-CoreSTM32 to control a LED light, and use the Maple IDE to program for the Microduino-CoreSTM32, just like programming in the Arduino IDE. EquipmentMicroduino-CoreSTM32 is an ARM development board using STM32F103CBT6 chip. It use special Upin7 interface, the size is similar with a coin and fully compatible other Microduino modules.
Experiment schematic
This experiment will use the first method, you can try another method. Programint led = 13; //Define the LED control pin
void setup() {
pinMode(led, OUTPUT); //Define the pin as output
}
void loop() {
digitalWrite(led, HIGH); //Output higt voltage
delay(1000); // Delay 1s
digitalWrite(led, LOW); //Output low voltage
delay(1000); // Delay 1s
}
{| style="width: 800px;"
|-
|
==Objective==
We have known how to control one LED by learning the first experiment. In this lesson we will learn to control multiple LED.
==Equipment==
*[[ Microduino-CoreSTM32]]
Microduino-CoreSTM32 is an ARM development board using STM32F103CBT6 chip. It use special Upin7 interface, the size is similar with a coin and fully compatible other Microduino modules.
*Other hardware equipment
**Breadboard Jumper one box
**Breadboard one piece
**LED Light-emitting diodes Eight
**220ohm resistor Eight
**USB Data cable one
[[File:STM32-lesson2All.jpg|600px|center|thumb]]
==Experiment schematic==
We use eight LEDs in this experiment, so need eight digit I/O ports.
[[File: STM32-lesson2Setup.jpg|600px|center|thumb]]
LED cathode connects the GND of Microduino-CoreSTM32, and anode connects to Microduino digital I/O port D3 ~ D10, which is the high-level light led. We can realize the flash LED by setting the output high/low voltage.
==Program==
*Program 1:
<source lang="cpp">
void setup() {
//Define D3 ~ D10 as output
for(int i=3;i<11;i++)
pinMode(i, OUTPUT);
}
void loop() {
for(int i=3;i<11;i++)
{
digitalWrite(i, HIGH); // D3 ~ D10 outputs high voltage
delay(50); //Delay 50ms
digitalWrite(i, LOW); //// D3 ~ D10 outputs low voltage
delay(50); //Delay 50ms
}
}
Program description: Use the for() loop logic, pinMode() function defines the D3 ~ D10 as output, then use the for() to light every LED.
Use hexadecimal array, and sending the shift data to each I/O port. /*===============================================
ox(High-Low:10,9,8,7)(High-Low:6,5,4,3)
For example:
0x81:10000001
10,9,8,7,6,5,4,3
↓ ↓ ↓ ↓↓ ↓↓ ↓
1 0 0 0 0 0 0 1
In common cathode that all led cathode are connected together, 1 on behalf of on, 0 for off,
then use number in the array to control the LED.
=================================================*/
long data[]=
{
0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,//From left to right light
0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01,//From right to left light
0x81,0x42,0x24,0x18,0x18,0x24,0x42,0x81,//Both sides to the middle and middle to both sides bright light
0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff,//Light from left to right
0xff,0x7f,0x3f,0x1f,0x0f,0x07,0x03,0x01,//From right to left extinguished
};
void setup()
{
for(int x=3;x<11;x++)
{
pinMode(x,OUTPUT);//Define the output pin
}
}
void loop()
{
for(int x=0;x<40;x++)//Reading different pattern's lights
{
leddisplay(data[x]);
delay(200); //Every state displays 200ms
}
leddisplay(0x00);//Cycle is completed, then all extinguished
delay(200);
}
void leddisplay(int num) // Mapping the pattern matrix to the port display
{
/*====================================================================
Firstly right shift the hexadecimal number x bits (num >> x),
x represents the microduino I/O port corresponding hexadecimal bit,
0 is the lowest bit, bit 7 is the highest bit.
Then the shifted data with 0x01 bitwise AND operation and you can get a certain hexadecimal data (0 or 1), and then the value assigned to Microduino-CoreSTM32 of I/O port.
====================================================================*/
digitalWrite(3, ((num>>0)&0x01));
digitalWrite(4, ((num>>1)&0x01));
digitalWrite(5, ((num>>2)&0x01));
digitalWrite(6, ((num>>3)&0x01));
digitalWrite(7, ((num>>4)&0x01));
digitalWrite(8, ((num>>5)&0x01));
digitalWrite(9,((num>>6)&0x01));
digitalWrite(10,((num>>7)&0x01));
}
Assign the hexadecimal array values to the I/O port to light the LED. Such as 0x18 is equivalent to binary 00011000, the corresponding value to "1" on, "0" off. You can write all kinds of tricks randomly. Compared two experiments, the first experiment looks simple, single effect, and the output port must be continuous, and the limitation is very big. The second experiment is optimized, using an array, the hexadecimal number's each bit is assigned to the specified I/O port, can realize water light designs. Debug
Result
From left to right in turn cycle lit each LED.
You can see five pattern effect:From left to right light, From right to left light, Both sides to the middle and middle to both sides bright light, Light from left to right and From right to left extinguished.|} |