Pic Lab, PIC16, Experiment #4: 7 segment display

It was 2012 year, I was just starting to learn how to work with the PIC microcontrollers, so the code is far away from optimal.

uC: PIC16F628a

Compiler: Hi-TECH PICC

Goal: To connect 7 segment indicator and display numbers from 0 to 9

7 segment indicator connection

In that experiment I decided to spare all pins and use just single IO, to do so the shift register is a great help, I used 74hc164 chip, now we need instead of 8 pins just 4:

1. Clocking

2. Reset

3. Data

4. For the buffer transistor

The economy is obvious, the principle of work could be found in the datasheet, it should be the first place all the time! First of all, let’s make a testbench schematic, then write the code in which we will sweep all digits with 1s period, like a stopwatch. The testbench schematic is already shown in the first picture.

The button has been added to determine the start of the sequence. The R11 resistor also not required in a real life, but there was some kind of the bug in Proteus tool, and without this resistor it was not possible to make show running 🙂 Quite simple schematic, quite simple code, but again please bare with it – it was 2012, I just started my hobby, I know how horrible arrays have been used there, but I still will left everything as is, to remember it thru the time (:

#include htc.h
#define _XTAL_FREQ 4000000
#define CK RB2
#define RESET RB1
#define DATA RB3
#define TRIG RB7
__CONFIG(MCLREN &UNPROTECT & WDTDIS);
void tick() {
             CK = 1;
             __delay_us(100);
             CK = 0;
             __delay_us(100);
             }
void vpih(unsigned char digit[8]) {
unsigned char i=0;
TRIG = 0;
for (i=0;i<8;i++) {
                  DATA = digit[i];
                  tick();
                  }
TRIG = 1;
}
const char point[8] = {1,0,0,0,0,0,0,0};    // Точка
const char one[8] = {0,0,0,0,0,1,1,0};      // Один
const char two[8] = {0,1,0,1,1,0,1,1};      // Два
const char three[8] = {0,1,0,0,1,1,1,1};    // Три
const char four[8] = {0,1,1,0,0,1,1,0};     // Четыре
const char five[8] = {0,1,1,0,1,1,0,1};     // Пять
const char six[8] = {0,1,1,1,1,1,0,1};      // Шесть
const char seven[8] = {0,0,0,0,0,1,1,1};    // Семь
const char eight[8] = {0,1,1,1,1,1,1,1};      // Восемь
const char nine[8] = {0,1,1,0,1,1,1,1};      // Девять
const char zero[8] = {0,0,1,1,1,1,1,1};      // Ноль
void main() {
unsigned char i=0;
TRISB = 0b00000001;
DATA = 0;
RESET = 0;
tick();
RESET = 1;
for (;;) {
          if (RB0 == 0) {
                         __delay_ms(30); if (RB0 == 0) {
                         vpih(zero);
                         for(i=0;i<10;i++){ __delay_ms(100);    }
                         vpih(one);
                         for(i=0;i<10;i++){ __delay_ms(100);    }
                         vpih(two);
                         for(i=0;i<10;i++){ __delay_ms(100);    }
                         vpih(three);
                         for(i=0;i<10;i++){ __delay_ms(100);    }
                         vpih(four);
                         for(i=0;i<10;i++){ __delay_ms(100);    }
                         vpih(five);
                         for(i=0;i<10;i++){ __delay_ms(100);    }
                         vpih(six);
                         for(i=0;i<10;i++){ __delay_ms(100);    }
                         vpih(seven);
                         for(i=0;i<10;i++){ __delay_ms(100);    }
                         vpih(eight);
                         for(i=0;i<10;i++){ __delay_ms(100);    }
                         vpih(nine);
                         for(i=0;i<10;i++){ __delay_ms(100);    }
                   }
          }
  }
}

The source can be found here

Leave a Reply

Your email address will not be published. Required fields are marked *