Patrick Fitzgerald
Published © GPL3+

AVR C++ code for LS74HC595 Shift Register and 7 Segment LED

Interface hardware display device using C++ and AVR

AdvancedProtip2 hours93

Things used in this project

Hardware components

ATmega328
Microchip ATmega328
×1
Shift Register- Serial to Parallel
Texas Instruments Shift Register- Serial to Parallel
×1
Resistor 100 ohm
Resistor 100 ohm
×8
7 Segment LED Display, InfoVue
7 Segment LED Display, InfoVue
×1

Software apps and online services

Arduino IDE
Arduino IDE

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

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

Credits

Patrick Fitzgerald

Patrick Fitzgerald

100 projects • 28 followers

Comments