Bluepill Examples

User contributed example programs for the cheap chinese BLUE PILL board

See also

The Blue Pill starter kit and my anti Blue Pill rant especially if you have not yet bought one ?

Traffic Lights

By Michael Jeffery.

_images/traffic.gif _images/traffic_schematic.png

DOWNLOAD traffic.txt

\ Traffic Light, By Michael Jeffery Copyright 2019, licensed under the GPL.
\ For use with Mecrisp-Stellaris by Matthias Koch. https://sourceforge.net/projects/mecrisp/
\ The original shell of this was taken from the Matthias Koch 'Blinky' and reworked.
\ Thanks also to Terry Porter.
\ Traffic Light, uses PB13, PB14, PB15 on a Blue Pill Module running Forth - Mecrisp Stellaris.
\ This will run a Traffic Light sequence, around 12 sec for each of the Green and Red LEDs.
\ You can change the Timing sequence in the Loop, 25000000 0 or 4200000 0 for a different result.
\ In Terminal you should see the word 'STOP' then 'AMBER' then 'GO' printed with four or so spaces
\ after each word on every new step.
\ As the delay loops are Long, 'key? until' only works at the end of each cycle, so you may have 
\ to wait a little for press 'any key' to end the program. An interrupt would work better here.
\ Copy and paste this text into your Terminal, hit enter, then type Traffic when it's done.
\ If you get errors, set the EOL (End Of Line) delay to 200ms in your Terminal.
\ Type Traffic to Start the sequence.


$40010C00 constant GPIOB

GPIOB $00 + constant GPIOB_CRL \ Reset value: $44444444 PB0-PB7 are configured in CRL, PB8-PB15 in CRH.
GPIOB $04 + constant GPIOB_CRH \ Reset value: $44444444 1: Push-Pull output, 4: Floating input.
GPIOB $08 + constant GPIOB_IDR
GPIOB $0C + constant GPIOB_ODR         \ Configuration Settings for GPIO Port B
GPIOB $10 + constant GPIOB_BSRR        \ on the Blue Pill Module.
GPIOB $14 + constant GPIOB_BRR
GPIOB $18 + constant GPIOB_LCKR

: Traffic ( -- )          \ Start of Traffic 'Word'.
$11144444 GPIOB_CRH !     \ PB15: PB14: PB13: sets Push-Pull Output.
begin                     \ Start Begin - Until loop.
1 13 lshift GPIOB_ODR !   \ GREEN, GO ON, Set Pin13 ON. 
." GO    "                \ Prints GO on the Terminal.
25000000 0 do loop        \ Delay Loop, XXX to 0.
1 14 lshift GPIOB_ODR !   \ AMBER, Wait ON, Set Pin14 ON.
." AMBER    "             \ Prints AMBER on the Terminal.
4200000 0 do loop         \ Delay Loop, XXX to 0.
1 15 lshift GPIOB_ODR !   \ RED, STOP ON, Set Pin15 ON.
." STOP    "              \ Prints STOP on the Terminal.
25000000 0 do loop        \ Delay Loop, XXX to 0.
1 14 lshift GPIOB_ODR !   \ AMBER, Wait ON, Set Pin14 ON.  
." AMBER    "             \ Prints AMBER on the Terminal.     
4200000 0 do loop         \ Delay Loop, XXX to 0.   
key? until                \ Ends if any Key pressed, or Loops forever.
;                         \ End of traffic 'Word'.



\ So here we created a single new Word called Traffic, that's basically how Forth works.
\ Every time you type this Word the Code will now run (as long as you save it, see below).
\ You will need to include the Header first to configure GPIOB as well, if not previously saved.
 
 
\ If you are in the UK you may want to leave everything 'as is'.
\ If you are in the USA you may want to remove the last AMBER set, so Green goes directly to
\ Red. There is no Red and Amber at once before Green though, that's the advanced version ~ :o)
\ If you want this Word 'Traffic' to remain after a reset you could include 'compiletoflash'
\ above the Header, without the quotation marks of course and it will then be included 
\ in your dictionary, along with the Header config for GPIOB.

 
\ Wiring, any LED Resistor value from around 120 Ohms to 2K2 Ohms should work, LED Pos+ to the 
\ Blue Pill pin PB13, PB14 and PB15. Blue Pill output 'high' is 3.3V.
\ If in doubt use an online LED / Resistor Calculator, there's hundreds out there.
\ Other than that you probably won't get any further support on this part of the project.
\
\ Good Luck!




\ Swap Red for Green order, starts on RED vs Green, easy just swap each LED part.
\ Use the same Header as above also.
 
\ : Traffic ( -- ) 
\ $11144444 GPIOB_CRH !     \ PB15: PB14: PB13: Push-Pull Output.
\ begin                     \ Start Begin - Until loop.
\ 1 15 lshift GPIOB_ODR !   \ RED, STOP ON, Set Pin15 ON.
\ ." STOP    "              \ Prints STOP on the Terminal.
\ 25000000 0 do loop        \ Delay Loop, XXX to 0.
\ 1 14 lshift GPIOB_ODR !   \ AMBER, Wait ON, Set Pin14 ON. 
\ ." AMBER    "             \ Prints AMBER on the Terminal.
\ 4200000 0 do loop         \ Delay Loop, XXX to 0.
\ 1 13 lshift GPIOB_ODR !   \ GREEN, GO ON, Set Pin13 ON. 
\ ." GO    "                \ Prints GO on the Terminal.
\ 25000000 0 do loop        \ Delay Loop, XXX to 0.
\ 1 14 lshift GPIOB_ODR !   \ AMBER, Wait ON, Set Pin14 ON.  
\ ." AMBER    "             \ Prints AMBER on the Terminal.                
\ 4200000 0 do loop         \ Delay Loop, XXX to 0.   
\ key? until                \ Ends if any Key pressed, or Loops forever.
\ ;
\
\ Remove the First AMBER Set in this instance to sequence RED-to-GREEN, without AMBER.
\ TEST IT !
\
\ You can actually make the Red-Amber both briefly ON, before Green, it involves addressing
\ the GPIOB_ODR directly and with two bits set and you also adjust a couple of delay tmings.
\ If there's enough interest you may see a Part II.
\
\ And you can have this Traffic word run from Boot-Up, Power ON, without a PC but it requires 
\ a little more information so you don't get into trouble first. The advanced lesson again.
\ You could try the Common Problems page here, (Search Index) pay attention to the warning 
\ or you may end up having to re-flash your Blue Pill with Mecrisp Stellaris all over again.
\ For now this should get you started using FORTH on this Traffic Light project.
\
\ Enjoy.