Patrick Fitzgerald
Published © GPL3+

Arduino and LS74HC595 Shift Register for 7 Segment LED

You can do this with your Arduino board or use a simulator program.

IntermediateProtip2 hours429

Things used in this project

Story

Read more

Code

count7simple.ino

Arduino
Arduino sketch that requires ShiftDisplay library
will count from 0 to 9 and repeat on 7 segment LED connected with shift register circuit
/* 
 *  count m = 0 to 9 on seven segment display Arduino and LS74HC595 shift register
*/
#include <ShiftDisplay.h>
#define SCLK  11  // SCLK STC portb.3 pin11 clock
#define RCLK  12  // RCLK SHC portb.4 pin12 latch
#define DIO   9   // Data input to shift register
ShiftDisplay display(SCLK, RCLK, DIO, COMMON_CATHODE, 1);
void setup() { }
void loop() {
	static int m = 0;
	display.set(m, ALIGN_LEFT);
		int pos = m < 10 ? 0 : 1;
		display.setDot(pos, true);
		display.show(200);
		display.setDot(pos, false);
		display.show(200);
	m++;
}

hc595_mega328.asm

Assembly x86
SimulIDE has a simulation with an ATmega328 connected to a 74hc595 shift register with a 7segment LED display. Compiled hex file shifts a bit from Q0->Q7 to light up bars and dot on display.

Arduino connections to 74hc595
portb.1 pin9 DS data
portb.2 pin10 OE output enable
portb.3 pin11 STC clock
portb.4 pin12 SHC latch
portb.5 pin13 MR reset

Files are in folder \SimulIDE\examples\Micro\Avr\mega328\m328_hc595\gcb_code\
hc595_mega328.asm - compiles with both Avra and Microchip assemblers
hc595_mega328.gcb - Great Cow Basic
provides accurate values for SER OE RCLK SRCLK SRCLR
portb.1 portb.2 portb.3 portb.4 portb.5
74hc595.gcb - Great Cow Basic incomplete, no comments, no pin numbers

Erratum!: the simulation uses leds to show bit shifting from Q0->Q7 apologies
; hc595_mega328.asm from Simulide Arduino with 595 shift register LEDs
;  cycles a to g segments and dot 
;  from \SimulIDE\examples\Micro\Avr\mega328\m328_hc595\gcb_code\
;   file hc595_mega328.asm   compiles with avra and microchip 
;Chip Model: MEGA328P
;Assembler header file
.INCLUDE "m328pdef.inc"

;SREG bit names (for AVR Assembler compatibility, GCBASIC uses different names)
#define I 7
#define T 6
#define H 5
#define S 4
#define V 3
#define N 2
#define Z 1
#define C 0

;********************************************************************************

;Set aside memory locations for variables
.EQU	BITOUT=256
.EQU	INDEX=257

;********************************************************************************

;Register variables
.DEF	DELAYTEMP=r25
.DEF	DELAYTEMP2=r26
.DEF	SysBitTest=r5
.DEF	SysCalcTempA=r22
.DEF	SysValueCopy=r21
.DEF	SysWaitTempMS=r29
.DEF	SysWaitTempMS_H=r30
.DEF	SysTemp1=r0

;********************************************************************************

;Vectors
;Interrupt vectors
	nop
	rjmp	BASPROGRAMSTART ;Reset
	nop
	reti	;INT0
	nop
	reti	;INT1
	nop
	reti	;PCINT0
	nop
	reti	;PCINT1
	nop
	reti	;PCINT2
	nop
	reti	;WDT
	nop
	reti	;TIMER2_COMPA
	nop
	reti	;TIMER2_COMPB
	nop
	reti	;TIMER2_OVF
	nop
	reti	;TIMER1_CAPT
	nop
	reti	;TIMER1_COMPA
	nop
	reti	;TIMER1_COMPB
	nop
	reti	;TIMER1_OVF
	nop
	reti	;TIMER0_COMPA
	nop
	reti	;TIMER0_COMPB
	nop
	reti	;TIMER0_OVF
	nop
	reti	;SPI_STC
	nop
	reti	;USART_RX
	nop
	reti	;USART_UDRE
	nop
	reti	;USART_TX
	nop
	reti	;ADC
	nop
	reti	;EE_READY
	nop
	reti	;ANALOG_COMP
	nop
	reti	;TWI
	nop
	reti	;SPM_READY

;********************************************************************************

;Start of program memory page 0
nop
BASPROGRAMSTART:
;Initialise stack
	ldi	SysValueCopy,high(RAMEND)
	out	SPH, SysValueCopy
	ldi	SysValueCopy,low(RAMEND)
	out	SPL, SysValueCopy
;Call initialisation routines
	rcall	INITSYS

;Start of the main program
	rcall	INIT595
SysDoLoop_S1:
	ldi	SysValueCopy,1
	sts	BITOUT,SysValueCopy
	rcall	SHIFTBIT
	ldi	SysWaitTempMS,244
	ldi	SysWaitTempMS_H,1
	rcall	Delay_MS
	ldi	SysValueCopy,0
	sts	INDEX,SysValueCopy
SysForLoop1:
	lds	SysTemp1,INDEX
	inc	SysTemp1
	sts	INDEX,SysTemp1
	ldi	SysValueCopy,0
	sts	BITOUT,SysValueCopy
	rcall	SHIFTBIT
	ldi	SysWaitTempMS,244
	ldi	SysWaitTempMS_H,1
	rcall	Delay_MS
	lds	SysCalcTempA,INDEX
	cpi	SysCalcTempA,7
	brlo	SysForLoop1
SysForLoopEnd1:
	rjmp	SysDoLoop_S1
SysDoLoop_E1:
BASPROGRAMEND:
	sleep
	rjmp	BASPROGRAMEND

;********************************************************************************

Delay_MS:
	inc	SysWaitTempMS_H
DMS_START:
	ldi	DELAYTEMP2,254
DMS_OUTER:
	ldi	DELAYTEMP,20
DMS_INNER:
	dec	DELAYTEMP
	brne	DMS_INNER
	dec	DELAYTEMP2
	brne	DMS_OUTER
	dec	SysWaitTempMS
	brne	DMS_START
	dec	SysWaitTempMS_H
	brne	DMS_START
	ret

;********************************************************************************

INIT595:
	sbi	DDRB,5
	sbi	DDRB,4
	sbi	DDRB,3
	sbi	DDRB,2
	sbi	DDRB,1
	cbi	PORTB,5
	cbi	PORTB,4
	cbi	PORTB,3
	cbi	PORTB,2
	cbi	PORTB,1
	sbi	PORTB,5
	ret

;********************************************************************************

INITSYS:
	ldi	SysValueCopy,0
	out	PORTB,SysValueCopy
	ldi	SysValueCopy,0
	out	PORTC,SysValueCopy
	ldi	SysValueCopy,0
	out	PORTD,SysValueCopy
	ret

;********************************************************************************

SHIFTBIT:
	cbi	PORTB,3
	cbi	PORTB,4
	cbi	PORTB,1
	lds	SysBitTest,BITOUT
	sbrc	SysBitTest,0
	sbi	PORTB,1
	sbi	PORTB,4
	sbi	PORTB,3
	ret

;********************************************************************************

hc595_mega328.gcb

Plain text
SimulIDE has a simulation with an ATmega328 connected to a 74hc595 shift register with a 7segment LED display. Compiled hex file shifts a bit from Q0->Q7 to light up bars and dot on display.

Arduino connections to 74hc595
portb.1 pin9 DS data
portb.2 pin10 OE output enable
portb.3 pin11 STC clock
portb.4 pin12 SHC latch
portb.5 pin13 MR reset

Files are in folder \SimulIDE\examples\Micro\Avr\mega328\m328_hc595\gcb_code\
hc595_mega328.asm - compiles with both Avra and Microchip assemblers
hc595_mega328.gcb - Great Cow Basic
provides accurate values for SER OE RCLK SRCLK SRCLR
portb.1 portb.2 portb.3 portb.4 portb.5
74hc595.gcb - Great Cow Basic incomplete, no comments, no pin numbers

Erratum!: the simulation uses leds to show bit shifting from Q0->Q7 apologies
// sample program hc595_mega328.gcb from 
//  \SimulIDE\examples\Micro\Avr\mega328\m328_hc595\gcb_code\hc595_mega328.gcb
// the example does shift bit Q0->Q7 to drive 7 segment + dot
// the example hex file may be compiled from hc595_mega328.asm
// 
#chip mega328P, 16

#include "74hc595.gcb"

#define SER     portb.1
#define OE      portb.2
#define RCLK    portb.3
#define SRCLK   portb.4
#define SRCLR   portb.5

init595
do
    shiftBit( 1 )
    wait 500 ms
    
    for index=1 to 7
        shiftBit( 0 )
        wait 500 ms
    next
loop

74hc595.gcb

Plain text
SimulIDE has a simulation with an ATmega328 connected to a 74hc595 shift register with a 7segment LED display. Compiled hex file shifts a bit from Q0->Q7 to light up bars and dot on display.

Arduino connections to 74hc595
portb.1 pin9 DS data
portb.2 pin10 OE output enable
portb.3 pin11 STC clock
portb.4 pin12 SHC latch
portb.5 pin13 MR reset

Files are in folder \SimulIDE\examples\Micro\Avr\mega328\m328_hc595\gcb_code\
hc595_mega328.asm - compiles with both Avra and Microchip assemblers
hc595_mega328.gcb - Great Cow Basic
provides accurate values for SER OE RCLK SRCLK SRCLR
portb.1 portb.2 portb.3 portb.4 portb.5
74hc595.gcb - Great Cow Basic incomplete, no comments, no pin numbers

Erratum!: the simulation uses leds to show bit shifting from Q0->Q7 apologies
'#define SER
'#define OE
'#define RCLK
'#define SRCLK
'#define SRCLR

'#define delay595

#define clear595 SRCLR = 0

Sub init595
    Dir SRCLR   out
    Dir SRCLK   out
    Dir RCLK    out
    Dir OE      out
    Dir SER     out
    SRCLR = 0           'Clear shiht register
    SRCLK = 0           '
    RCLK  = 0           '
    OE    = 0           'Enable outputs
    SER   = 0
    
    SRCLR = 1
End Sub  

Sub shiftByte( in byteout as byte )    'LSB first
    RCLK = 0
    for num=0 to 7
        SRCLK = 0
        SER   = byteout.0
        SRCLK = 1
        rotate byteout right
        #ifdef delay595
        wait delay595 
        #endif
    next
    RCLK = 1
End Sub

Sub shiftBit( in bitout as bit )
    RCLK  = 0
    SRCLK = 0
    SER   = bitout.0
    SRCLK = 1
    RCLK  = 1
End Sub

Credits

Patrick Fitzgerald

Patrick Fitzgerald

117 projects • 39 followers

Comments