Sunday, October 17, 2010

GCBASIC Tutorial 2 part 2: 3 wires 7 segment LED display using open source PIC compiler



As in part 1 I have show how to program a single 7 segments with 3 wires. In this tutorial I have modify the last circuit by add-on another two 7 segments and two IC 4094 Shift register.

The three 7 segment and three IC 4094 Shift register have been connected as picture above. Take note on the CLK and STB pin of the three IC 4094. There are connecting each others with RB1 and RB2 of PIC respectively. The purpose of connection is to sharing Clock and Strobe signal output from PIC.

However the data output from PIC is only connected to pin2 of U2. At the same time pin 2 of U3 and U4 are cascading by connecting QS of the previous 4094. This can be explained that the 8 bit data of U2 is cascading to U3 and so on.

'A program to flash two LEDs on PORTB, bits 0 and 1

'Chip model
#chip 16F84a, 4

#define data PORTB.0 'set as data
#define clock PORTB.1 'set as clock
#define strobe PORTB.2 'set as strobe

dim x as byte

'Set the pin directions
dir PORTB out


'Main routine
Start:

set data off 'put data low
for loopcounter = 1 to 8
PulseOut clock, 10 ms
PulseOut strobe, 10 ms
next


for x = 1 to 10 ‘#1
readtable tsegment2,x,segment
gosub display
next

table tsegment
b'01111110 '#0
b'00110000 '#1
b'01101101 '#2
b'01111001 '#3
b'00110011 '#4
b'01011011 '#5
b'01011111 '#6
b'01110000 '#7
b'01111111 '#8
b'01111011 '#9
end table

end

display: #2
for loopcounter = 1 to 7
nsegment = segment % 2
data = nsegment
PulseOut clock, 10 ms
segment = segment / 2
next

data = segment
PulseOut clock, 10 ms
PulseOut strobe, 100 ms
return

The program above is identical with previous program but only two parts was add-on. At #1 I use for..next cycle to have 7 segment to shows number from 0 to 9. It could produce only single number at one time. The readtable function is used to call a number arrangement which is stored in tsegment table. The number arrangements are in binary and it’s responding to the arrangement of the 7 segment.

At #2 I used label to display number on 7 segments. The name of the label is Display. The line inside of display label is identical line from previous program at #4. The GOSUB function had used inside for..next loop at #1 in order to jump into display label and return back after finish their job.

When you run the program it will produce the number at first 7 segments only. A few second after that the number will shift to second 7 segments. However first 7 segment will produce a new number. Once again a few second after that the number at second 7 segments will shift to third 7 segments. The second 7 segment will replace with the number previously at first 7 segments.

The shift number can be explained due to 4094 are connected in cascading. Actually the PIC sends 8 bit data to the U2 only. The U2 then shift the previous data into U3 after received new data from PIC. The U3 will store the data and display on 7 segments when STB is high. It then repeated the process for the rest of 4094.

Saturday, October 9, 2010

GCBASIC Tutorial 2 part 1: 3 wires 7 segment LED display using open source PIC compiler



Last tutorial I has give a simple example how to use open source PIC basic using GCBasic. This tutorial I used 7 segments to display a number using PIC16F84A. Usually 7 segments need about 8 wires to operate using PIC but in this tutorial I only used 3 wires.

Seven segments display have 7 elements of LED to show Arabic numerals. Each element of LED can be ON or OFF and they can be combining in order to produce representation of the number. They also can be produce letters but it limits letters A to G only. The elements of LED is arrange as below.
Typically 7 segments have two types. The types are common cathode and common anode. The common cathode is referring to negative terminals and anode is positive terminal. It means the negative or positive terminals of the each LED elements are connected together and brought to common pin. Hence a 7 segments plus decimal point package will only require nine pins.

As mention earlier I used only 3 wires to operate the 7 segments. In order to have that IC 4094 shift register was used.

The 4094 is an 8 bit SIPO shift latch register. The device consist of 8 bit shift register and an 8 bit latch with 3 state output. It also have 3 importance input. The inputs are strobe, serial in and clock. The circuit connection between the PIC16F84A, IC 4094 and 7 segments were connected as below.


The Data, clock and strobe are connected to RB0, RB1 and RB2 respectively. The 7 segments are connected to Q0 to Q7 pins of 4094 with A to G pins and DP pin of 7 segments. The type of 7 segments is common cathode. The OE is connected to 5V. It makes sure the IC is always ready to operate.

The data will send by PIC16F84A to 4094 trough pin of RB0. The 8 bit data of the PIC will shift serially to the 4094. The IC then shift the data to shift registers at the positive transition of the clock input. The clock input also trigger by the PIC. When the strobe input is give high by the PIC the data will propagates to the output pins of Q0 to Q7.

The output of the 4094 then will ON or OFF the 7 segments LED depending on the data input. The 7 segments will display a number responding of it. Below is the program of the microcontroller.

'A program to flash two LEDs on PORTB, bits 0 and 1

'Chip model
#chip 16F84a, 4

#define data PORTB.0 'set as data #1
#define clock PORTB.1 'set as clock
#define strobe PORTB.2 'set as strobe

'Set the pin directions #2
dir PORTB out


'Main routine
Start:

set data off 'put data low #3
for loopcounter = 1 to 8
PulseOut clock, 10 ms
PulseOut strobe, 10 ms
next

segment = b'11111100 ' #4

for loopcounter = 1 to 7
nsegment = segment % 2
data = nsegment
PulseOut clock, 10 ms
PulseOut strobe, 100 ms
segment = segment / 2
next

data = segment
PulseOut clock, 10 ms
PulseOut strobe, 10 ms
End

At #1 and #2 is to define the pin of the PIC16F84A and the flow of the direction. At #3 the data is set low to clean 7 segments from showing unwanted number.

At #4 the variable of segment is stored the data input. In this case it stored the data to display number 1.

After that variable of nsegment is used to get least significant bit (LSB) of the segment and send to 4094 trough data pin. It then followed by the positive transition of the clock about 10 milliseconds to have the 4094 shifted the data into shift register. The segment variable then will reduce the bit to 7 by dropping the LSB. It will repeat 7 times.

The last bit of the segment is not dividing by 2 because it only have one bit. The bit number will send to data pin to complete the process of sending data to 4094. At last the high output of strobe will send to 4094 in order to display a number on 7 segments.

Video for tutorial as below (please take note this video is no audio).