8 LED’s
The simplest program to write and verify for a microcontroller is to light up some LED’s, typically through an 8-bit parallel port. Following the discussion on “Only 1 LED”, it would be “Configure all the bits for port B as slow push-pull outputs and send 0x53 (01010011 binary) to port B to turn on 4 LED’s.” Even if there is still no LED connected to the microcontroller, the running of the program can still be verified by measuring the voltages at the port B pins, or rather the brought-out terminals. Actually, for a new microcontroller, I always do this as connecting the components take too much time and I would not want to do the connection unless I am sure a program can be downloaded to the chip and run as expected.
Port D is not used here as Port D bit 1 is the SWIM signal.
And here is the program:
// mainXX 8 LED’s
#include <iostm8s105c6.h>
int main( void ){
PB_ODR = 0x53 ; // = 0101 0011
PB_DDR = 0xFF ; // Output
PB_CR1 = 0xFF ; // Push Pull
PB_CR2 = 0x00 ; // Slow
return 0;
}
Once the program is verified by a voltmeter, the LED’s can be connected one by one and the result would appear LED by LED.
Even with LED’s, we can talk a lot.
The forward voltage across an LED varies widely, from about 1.7 V to 2.7 V. The current also varies. To limit the current and to control the brightness, a series resistor has to be connected to the LED. As we are using a 3.3V supply, the balanced voltage across the resistor then varies from about 0.6 V to 1.6 V. Hence the value of the resistance has to be chosen according to the actual LED used, possibly making some actual voltage measurement.
If the LED and the resistor are connected directly to the microcontroller, the current supplied by the microcontroller may be quite big. Some port pins of the STM8S105 may have high sink current, which would require the cathode of the LED to be connected to the microcontroller. This then requires a logic of ‘0’ to turn on the LED. In any case, if many LED’s are connected directly to the chip, it may become too hot. So it is advisable to drive the many LED’s through a buffer such as the 74HC245.
The following shows the connections without drawing a diagram.
STM8S105 74HC245
Vdd Vdd and Dir
Vss Vss and /OE
PB7, … PB0 A7, … A0
B7, … B0 to resistor to anode of LED
cathodes of LED’s grounded
These are quite a lot of connections. The Pin-Out diagram in the paragraph further down would be useful.
Once the circuit is done, it is quite easy to write programs to display various patterns on the LED’s.
Typically, an 8-bit number can be displayed on the LED’s, with its value increasing or decreasing gradually, with software delays.
Then only one LED may be turned on and the made to shift left, shift right etc.
GPIO
GPIO stands for General Purpose Input Output.
Traditionally, this is called the parallel port.
A microcontroller communicates with other devices through ports. The ports may be parallel or serial. However, most people ignore that serial ports are also I/O ports and call parallel ports just I/O ports. A parallel port can change all the values at the port pins at the same time. But this is seldom required, and each bit of a parallel port is set or clear individually and work rather independently. Thus the newer description of such parallel ports is GPIO.
PinOut
The terminal pins of the microcontroller are brought out to pins at the pitch of 0.1 inch. This is suitable for wire-wrapping. It also allows an extension PCB to be connected to the starter kit. The signals for the pins are given in tables in the manual. It would be easier if they are given in a diagram.
PD6 PD4 PD2 PD0 PE1 PE3
PD7 PD5 PD3 PD1 PE0 PE2
Reset OscIn PG1 PG0
OscOut Vssio_1 PC7 PC6
Vss Vcap Vddio_2 Vssio_2
Vdd Vddio_1 PC5 PC4
PA3 PA4 PC3 PC2
PA5 PA6 PC1 PE5
Vssa PB6 PB4 PB2 PB0 PE6
Vdda PB7 PB5 PB3 PB1 PE7
PC5=SClk
PC6=MOSI
Connector 1 is on the left with Reset as pin 1
Connector 2 is on the right with PE5 as pin 1
Connector 3 is on the bottom with Vdda as pin 1
Connector 4 is on the top with PE3 as pin 1