This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:examples:display:segment_display [2010/04/13 10:31] ā raivo.sell | en:examples:display:segment_display [2020/07/20 12:00] (current) ā external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | < | ||
| + | ====== 7-segment LED display ====== | ||
| + | //Necessary knowledge: | ||
| + | [HW] [[en: | ||
| + | [LIB] [[en: | ||
| + | [PRT] [[en: | ||
| + | |||
| + | ===== Theory ===== | ||
| + | |||
| + | [{{ : | ||
| + | |||
| + | 7-segmented LED number-indicator is a display which consists of 7 LEDs positioned in the shape of number 8. By lighting or switching off the corresponding LEDs (segments), it is possible to display numbers from 0 to nine as well as some letters. | ||
| + | |||
| + | Electrically all anodes of the LEDs are connected to one anode pin //ca//. LEDs are lit by switching their cathodes (//a, b, c...//). Exists also reversed connections, | ||
| + | |||
| + | [{{ : | ||
| + | [{{ : | ||
| + | |||
| + | LED number-indicators are easy to use, they can be controlled directly from the pins of the microcontroller, | ||
| + | |||
| + | * Latch-signal | ||
| + | * Clock-signal | ||
| + | * Data-signal | ||
| + | |||
| + | ===== Practice ===== | ||
| + | |||
| + | There is one 7-segment LED number-indicator on the Digital i/o module. It is controlled through a driver with serial interface. | ||
| + | For displaying the numbers on the HomeLabs Digital i/o module indicator, the following functionality is written to the library of the HomeLab. | ||
| + | |||
| + | |||
| + | <code c> | ||
| + | // Marking card | ||
| + | // The bits are marking the segments. Lower ranking is A, higher ranking is DP | ||
| + | const unsigned char __attribute__ ((weak)) segment_char_map[11] = | ||
| + | { | ||
| + | 0b00111111, | ||
| + | 0b00000110, | ||
| + | 0b01011011, | ||
| + | 0b01001111, | ||
| + | 0b01100110, | ||
| + | 0b01101101, | ||
| + | 0b01111100, | ||
| + | 0b00000111, | ||
| + | 0b01111111, | ||
| + | 0b01100111, | ||
| + | 0b01111001 | ||
| + | }; | ||
| + | |||
| + | // Start-up of the 7-segment indicator | ||
| + | void segment_display_init(void) | ||
| + | { | ||
| + | // Set latch, data out and clock pins as output | ||
| + | pin_setup_output(segment_display_latch); | ||
| + | pin_setup_output(segment_display_data_out); | ||
| + | pin_setup_output(segment_display_clock); | ||
| + | } | ||
| + | |||
| + | // Displaying number on the 7-segment indicator | ||
| + | void segment_display_write(unsigned char digit) | ||
| + | { | ||
| + | unsigned char map; | ||
| + | signed char i; | ||
| + | |||
| + | // Check-up of the number | ||
| + | if (digit > 9) | ||
| + | { | ||
| + | digit = 10; | ||
| + | } | ||
| + | |||
| + | // Number as the card of the segments | ||
| + | map = segment_char_map[digit]; | ||
| + | |||
| + | // Latch-signal off | ||
| + | pin_clear(segment_display_latch); | ||
| + | |||
| + | // Sending he bits. Higher ranking goes first | ||
| + | for (i = 7; i >= 0; i--) | ||
| + | { | ||
| + | // Setting the pin according to the value of the bit of the card | ||
| + | pin_set_to(segment_display_data_out, | ||
| + | |||
| + | // The clock-signal as high for a moment | ||
| + | pin_set(segment_display_clock); | ||
| + | _delay_us(1); | ||
| + | |||
| + | // The clock-signal as low | ||
| + | pin_clear(segment_display_clock); | ||
| + | _delay_us(1); | ||
| + | } | ||
| + | |||
| + | // Latch-signal on | ||
| + | pin_set(segment_display_latch); | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | For displaying numbers and the letter āEā, is created a " | ||
| + | |||
| + | The following is a more concrete example of a program for using the number-indicator. | ||
| + | |||
| + | |||
| + | <code c> | ||
| + | // The example program of the 7-segment LED indicator of the HomeLab' | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | // Main program | ||
| + | int main(void) | ||
| + | { | ||
| + | int counter = 0; | ||
| + | |||
| + | // Set-up of the 7-segment indicator | ||
| + | segment_display_init(); | ||
| + | |||
| + | // Endless loop | ||
| + | while (true) | ||
| + | { | ||
| + | // Displaying the values of the counter | ||
| + | segment_display_write(counter % 10); | ||
| + | |||
| + | // Counting up to 10 | ||
| + | counter++; | ||
| + | if (counter> | ||
| + | |||
| + | // Delay for 1 second | ||
| + | sw_delay_ms(1000); | ||
| + | } | ||
| + | } | ||
| + | </ | ||