Electronics > Projects, Designs, and Technical Stuff
STM32 blinking 30 LED's!
(1/6) > >>
bjdhjy888:
I'm sure my STM32 min. system is working, as I have tested it before. My Keil 5 generated .hex file could be flashed to blink an LED.

Now, I've added 29 more LED's in total.

Group A: 10 LED's in blue
Group B: 10 LED's in red
Group C: 10 LED's in yellow

My goal is to display:
    - the letter I with the blue LED's
    - the shape heart with the red LED's
    - and the letter U with the yellow LED's. In other words, 30 LED's are used to visually show: I heart U.

Would this be possible to do in theory?

Do I simply modify my current codes from:
      GPIO_WriteBit(LEDPORT,LED1,(BitAction)(1)); 
      delay_us(50000); 
      GPIO_WriteBit(LEDPORT,LED1,(BitAction)(0)); 
      delay_us(50000); 
to:
29 more code blocks added to the above one?

I fear PA0~PC13 cannot be controlled to finish this task.

Is my logic right?

Please help. Thank you!


Renate:
No, the type of code that you are showing is clearly demo code.

It's my personal prejudice, but I hate seeing the word "delay".

Here's some very fake code for how I would write it:


--- Code: ---THIS IS NOT REAL CODE!

void main(void)
{
   init_timer(); // how about a 1 mSec timer?
   // enable interrupts
   for(;;)
      sleep();
}

uint8_t prescaler;
uint8_t state;

void timer_ISR()
{
   // I could check switches here or something

   prescaler++;
   if (prescaler<250) return;
   prescaler=0;

   switch(state)
   {
     // turn on some LEDs, turn off some LEDs depending on what state
   }
   state++;
   if (state<NSTATES) return;
   state=0;
}
--- End code ---
You could do this if you kept to one pin per LED.
If you multiplexed your LEDs you would save pins, but you would have to rotate through separate banks of LEDs every 1 mS (or whatever).

I don't do STM32, but aren't there better examples?
james_s:
If you're trying to display things with 3 groups of LEDs do you even need each LED in a group to be individually controlled?
OM222O:
if you need individual LED control with as few pins as possible, you should use charliplexing. Most MCUs don't support the tri-state mode but it should still give you a very reasonable pin count (15 rather than 30). the only downside is you can't have both LEDs on at the same time, so if you want to have both of them on at the same time, a trick is to control with PWM and use a smaller value resistor, so the brightness looks the same.

About coding, you can do a fake "real time timer" by creating variables. I haven't written code for STM32 but the general idea is the same. you create a variable which keeps the time and you use multiple if statements, here is an example from arduino:


--- Code: ---unsigned long lastMillis;
int i=0;
void setup() {
  pinMode(2,INPUT);
  pinMode(3,OUTPUT);
  pinMode(4,OUTPUT);
  pinMode(LED_BUILTIN,OUTPUT);
  Serial.begin(9600);
  lastMillis=millis();
}

void loop() {
  if ((millis() -lastMillis) > 5000){
    i++;
    lastMillis=millis();
    if (i==1){
      digitalWrite(3,1);
      digitalWrite(4,0);
      Serial.println("3 is on");
    } else if(i==2) {
      digitalWrite(3,0);
      digitalWrite(4,1);
      Serial.println("4 is on");
    } else if(i==3) {
      digitalWrite(3,1);
      digitalWrite(4,1);
      Serial.println("3 and 4 are on");
    } else {
      digitalWrite(3,0);
      digitalWrite(4,0);
      i=0;
      Serial.println("reset");
    }
  }
  digitalWrite(LED_BUILTIN,digitalRead(2));
}

--- End code ---

it's a psudo state machine, where "i" is the state and at each state the digital outputs change. this allows the line outside the "timer" (digitalWrite(LED_BUILTIN,digitalRead(2));) to be updated at maximum frequency (you can test it by connecting the digital pin 2, to a high or low state; that should update the LED status "instantly"), while the state update is "delayed" for 5 seconds. I'm sure you can convert it to work with STM32. A lot of details are missing from your project description, but I hope this helped  :-+

The timing accuracy will be a bit off here, but I don't think you require precision timing for driving LEDs anyways.
daqq:

--- Quote ---Most MCUs don't support the tri-state mode however but it should still give you a very reasonable pin count (15 rather than 30).
--- End quote ---
Which current generation MCU does not support tri-state GPIOs?
Navigation
Message Index
Next page
There was an error while thanking
Thanking...

Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod