⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 seg7.bas

📁 ATMEGA16使用的5位数码管动态扫描程序。
💻 BAS
字号:
'/////////////////////////////////////////////////////////////////////////
'///工程: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 + -