?? tmp.tmp
字號:
'/////////////////////////////////////////////////////////////////////////
'///工程:FastAVR Basic WORD型變量變換成5字節BCD碼并對5位數碼管掃描
'///芯片:mega16
'///簡介:數碼管每位點亮時間2ms
'///編譯:通過,499 words
'///實踐:使用本站mega16精品試驗板測試通過
'///作者:agui2008
'///時間:2006/5/7
'///版本:V2.0
'//////////////////////////////////////////////////////////////////////////
$Device= m16 '聲明使用mega16芯片
$Stack = 32 '聲明堆棧深度32byte
$Clock = 7.3728 '聲明芯片時鐘頻率
'$Lcd = PORTB.4, RS=PORTC.6, EN=PORTC.7 , 16, 2 'lcd config
$Timer0=Timer,Prescale=256,Compare=DisConnect,Clear 'timer0為定時器,預分頻256,比較輸出斷開,匹配就清零定時器
$Source= On
Declare Interrupt Oc0() ' 定義oc0中斷
Declare Sub word_bcd() '
Dim n As Word
Dim a As Byte
Dim i As Byte
Dim dxs(5) As Byte
Dim bcd_1 As Byte
Dim bcd_2 As Byte
Dim bcd_3 As Byte
Dim bcd_4 As Byte
Dim bcd_5 As Byte
Dim tab(10) As Flash Byte
DDRB=255
DDRA=DDRA Or &b11110000
Enable Interrupts '允許全局中斷
ocr0=&h3A 'timer0比較匹配中斷時間=2ms
Enable Oc0 '允許timer0比較匹配中斷
Start Timer0 '啟動定時器0
Do
If i>1 Then
word_bcd() '將變量n轉換成BCD碼
i=0
Start Adc, Vref=Int'啟動ADC轉換,使用內部電壓基準
n=Adc(0) '獲得ADC0的值
Stop Adc '停止ADC
dxs(1)=tab(bcd_1) '查表獲得個位7段碼
dxs(2)=tab(bcd_2) '查表獲得十位7段碼
dxs(3)=tab(bcd_3) '查表獲得百位7段碼
dxs(4)=tab(bcd_4) '查表獲得千位7段碼
dxs(5)=tab(bcd_5) '查表獲得萬位7段碼
PORTA=PORTA And &b00001111 '關閉所有的顯示位而不影響其它位
Select Case a
Case 1
PORTA=PORTA Or &b00010000:PORTB=dxs(1) '顯示個位
Case 2
PORTA=PORTA Or &b00100000:PORTB=dxs(2) '顯示十位
Case 3
PORTA=PORTA Or &b01000000:PORTB=dxs(3) '顯示百位
Case 4
PORTA=PORTA Or &b10000000:PORTB=dxs(4) '顯示千位
'Case 5
' PORTA=PORTA And &b00000000:PORTB=dxs(5) '顯示萬位
End Select
Else
End If
Loop
'//////////////定時器0比較匹配中斷服務程序///////////////////////////
Interrupt Oc0(),Save 1 'timer0比較匹配中斷服務無需裝初值
i=255 '設置時間標志
a=a+1 '控制數碼管各位輪流顯示
If a>5 Then a=1 '顯示到第5位回到第一位
Enable Interrupts 'AVR在進入中斷之后會關閉全局中斷,所以在這里要重新打開全局中斷
End Interrupt '中斷服務程序結束
'//////////////WORD轉換成5字節BCD碼//////////////////////////////////
Sub word_bcd()
word_bcd1:
bcd_5=255
word_bcd2:
Incr bcd_5
If n > 9999 Then
n=n-10000 :GoTo word_bcd2
Else
bcd_4=255
End If
word_bcd3:
Incr bcd_4
If n > 999 Then
n=n-1000 :GoTo word_bcd3
Else
bcd_3=255
End If
word_bcd4:
Incr bcd_3
If n > 99 Then
n=n-100 :GoTo word_bcd4
Else
bcd_2=255
End If
word_bcd5:
Incr bcd_2
If n > 9 Then
n=n-10 :GoTo word_bcd5
Else
bcd_1=n
End If
End Sub
tab=192,249,164,176,153,146,130,248,128,144 '數碼管7段碼碼表
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -