
نوشته اصلی توسط
pouriyar مهمان عزیز شما حق دیدن لینک ها را ندارید
عضویت
ممنونم از لطفتون
تعدادش حداکثر عددی باشه که میشه با سون سگمنت نشون داد یعنی همون 9999
دوست عزیز این کد اولیه اش
( جاهایی که نیاز داری هم خالی گذاشتم خودت متغیر مد نظرت رو تعریف کن )
کد:
. program LED_7_Seg_Cnt
' * Project name:
' Seven Segment
' * Copyright:
' (c) Amir.s.m, 2023
' * Revision History:
' 20230126:
' - initial release (PRE);
' * Description:
' This code demonstrates how to display number on one 7-segment display
' (common cathode). Display is connected to PORTB (PB0..PB7, segment A to
' PB0, segment B to PB1, etc); common cathode is connected to the pin PC2 to PC5 on
'
' * Test configuration:
' MCU: ATMEGA8
' Oscillator: Internal Clock 02.0000 MHz
' Ext. Modules: None.
' SW: mikroBasic PRO for AVR
' * NOTES:
'
' *
dim shifter, portd_index , digit as byte
g_time , number as word
portd_array as byte[4]
_led as sbit at DDRD.4
_up as sbit at PIND0_bit
_down as sbit at PIND1_bit
_ok as sbit at PIND2_bit
sub procedure show
PORTC = 0 ' turn off all 7seg displays
PORTB = portd_array[portd_index] ' bring appropriate value to PORTB
PORTC = shifter ' turn on appropriate 7seg. display
' move shifter to next digit
shifter= shifter << 1
if (shifter > 32) then
shifter = 4
end if
' increment portd_index
Inc(portd_index)
if (portd_index > 3) then
portd_index = 0 ' turn on 1st, turn off 2nd 7seg.
end if
end sub
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
end select
end sub
sub procedure refresh
digit = number / 1000 ' extract thousands digit
portd_array[3] = mask(digit) ' and store it to PORTB array
digit = (number / 100) mod 10 ' extract hundreds digit
portd_array[2] = mask(digit) ' and store it to PORTB array
digit = (number / 10) mod 10 ' extract tens digit
portd_array[1] = mask(digit) ' and store it to PORTB array
digit = number mod 10 ' extract ones digit
portd_array[0] = mask(digit) ' and store it to PORTB array
' Inc(number) ' increment number
if ( number > 9999 ) then
number = 9999
end if
if ( number < 1 ) then
number = 1
end if
if ( GICR.6 = 0 ) then 'Check INT0 STS
GICR.6 = 1 'Enable INT0
end if
end sub
sub procedure run(dim num as word)
end sub
sub procedure KeyPress() iv IVT_ADDR_INT0 ics ICS_AUTO
dim _key , _time as byte
GICR.6 = 0 'Disable INT0
g_time = 1 'reset general time
_time = 11 'set debunce time
while ( g_time < _time )
_key = PORTD and 0x07 'Read PD.2,1,0
wend '10ms debunce elapsed
select case _key
case 0 'Up AND Down
exit 'cancel change
case 1 'Down
dec(number)
case 2 'Up
inc(number)
case 3 'Ok
run(number)
case else
exit 'Release Keys
end select
end sub
sub procedure Timer0Overflow_ISR() org IVT_ADDR_TIMER0_OVF '(1,024ms)
inc(g_time)
if ( g_time>= 58594) then '(60K ms)
g_time = 0
end if
if (g_time mod 10 = 0) then '10ms interval time
show 'show multiplex segments
if (g_time mod 100 = 0) then '100ms interval time
refresh 'refresh parameters
end if
end if
end sub
sub procedure boot_init
DDRB = 0xFF ' Configure PORTB as output on all pins
PORTB = 0 ' Clear PORTB
DDRC = 0x3C ' Configure PORTC as output on PC.5,4,3,2
PORTC = 0 ' Clear PORTC
DDRD.4 = 1 ' Configure PD.4 as output LED
PORTD = 7 ' Pull up on PD.2,1,0
g_time = 0
digit = 0
portd_index = 0
shifter = 4
number = 1234 ' Initial number value
TCCR0 = 0x02 ' ClkI/O/8 (From prescaler)
MCUCR.0 = 0
MCUCR.1 = 0 'Low level IRQ INT0 for Keys
GICR.6 = 1 'Enable INT0
TOIE0_bit = 1 ' Timer0 overflow interrupt enable
SREG_I_bit = 1 ' General Interrupt enable
end sub
main:
boot_init()
while true
nop
wend
end.
اینم شماتیک سیستم
مهمان عزیز شما حق دیدن لینک ها را ندارید
عضویت