Code: Select all
program SEVENSEGMENT
' * Project name:
' Seven Segment Display
' * Copyright:
' (c) Mikroelektronika, 2008.
' * Revision History:
' 20080930:
' - initial release
' * Description:
' This code demonstrates displaying number on four 7-segment display (common
' cathode), in multiplex mode. All 7-segment displays are connected to PORTC
' (PC0..PC7, segment A to PC0, segment B to PC1, etc.) with refresh via pins
' PB0..PB3 on PORTB (board specific).
' * Test configuration:
' MCU: ATmega16
' Dev.Board: EasyAVR5A
' Oscillator: External Clock 08.0000 MHz
' Ext. Modules: -
' SW: mikroBasic for AVR PRO
' * NOTES:
' - Turn 7seg display switches ON, see SW6 on EasyAVR5A (board specific).
' *
dim shifter, digit, port_index as byte
number as word
port_array as byte[5]
bypass as byte
sub procedure Interrupt() org 0x04
PORTC = 0 ' Disable all 7seg. displays
PORTB = port_array[port_index] ' Turn on appropriate segments
PORTC = shifter ' Enable appropriate 7seg. display
' move shifter to next digit
shifter = shifter << 1
if (shifter > 8) then
shifter = 1 ' prepare mask for shifter
end if
' increment portd_index
Inc(port_index)
if (port_index > 3) then
port_index = 0 ' turn on 1st, turn off 2nd 7seg.
end if
end sub'~
'-------------- Returns mask for common cathode 7-seg. display
sub function mask(dim num as byte) as byte
select case num
case 0
result = 0x3F
case 1
result = 0x06
case 2
result = 0x5B
case 3
result = 0x4F
case 4
result = 0x66
case 5
result = 0x6D
case 6
result = 0x7D
case 7
result = 0x07
case 8
result = 0x7F
case 9
result = 0x6F
case else
result = 0xAA
end select 'case end
end sub'~
main:
DDRB = DDRB or 0x0F ' Set PB3..PB0 as output
DDRC = 0xFF ' Set PORTC as output
digit = 0 '
port_index = 0 ' Initial
shifter = 1 ' starting Delay_us
PORTB = 0 '
PORTC = 0 '
SREG_I_bit = 1 ' Interrupt enable
TOIE1_bit = 1 ' Timer1 interrupt enable
TCCR0B = 2 ' Start timer with 8 prescaler
number = 6789
while TRUE
digit = word(number div 1000) ' extract thousands digit
port_array[3] = (mask(digit)) ' and store it to port_array
digit = word((number div 100) mod 10) ' extract hundreds digit
port_array[2] = (mask(digit)) ' and store it to port_array
digit = word((number div 10) mod 10) ' extract tens digit
port_array[1] = (mask(digit)) ' and store it to port_array
digit = word(number mod 10) ' extract ones digit
port_array[0] = (mask(digit)) ' and store it to port_array
Delay_ms(200) ' 200 mili second delay
Inc(number) ' increment counter
if (number > 9999) then ' when counter reaches "9999"
number = 0
end if
wend
end.'~