7-segment on easyAVR5A

General discussion on mikroBasic PRO for AVR.
Post Reply
Author
Message
edg962
Posts: 27
Joined: 06 Dec 2008 04:46

7-segment on easyAVR5A

#1 Post by edg962 » 08 Sep 2010 04:43

Need your help. I am trying to follow the example on 7-segment and compile it. But it does not work. I am using ATMEGA644 which has interrupt vector 0x04 and TCCR0B which I set to 2 (prescaler 8) to follow the example. It seems pretty straight forward but I get nothing on the 4 7-segment LEDS. Any help would be great. Thanks

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.'~
Edited by Administrator: Added Code Tag

User avatar
tihomir.losic
mikroElektronika team
Posts: 2138
Joined: 02 Dec 2009 14:16
Location: Serbia
Contact:

Re: 7-segment on easyAVR5A

#2 Post by tihomir.losic » 20 Sep 2010 14:58

Hello,

please, try this code:

Code: Select all

program Display7seg

dim shifter, digit, port_index as byte
    number as word
    port_array as byte[5]
    bypass as byte

sub procedure Interrupt() org 0x24

  PORTB = 0                         ' Disable all 7seg. displays
  PORTC = port_array[port_index]    ' Turn on appropriate segments
  PORTB = 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
  PORTB       =   0    '
  PORTC       =   0    '
  
  SREG_I_bit = 1     ' Interrupt enable
  TOIE0_bit  = 1     ' Timer0 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.'~
Best regards,

Losic Tihomir
mikroElektronika [Support team]

Post Reply

Return to “mikroBasic PRO for AVR General”