本帖最后由 taotie 于 2020-9-3 13:13 編輯
屏幕截圖(41).png (192.45 KB, 下載次數: 106)
下載附件
2020-9-3 13:11 上傳
rem Main.bas file generated by New Project wizard
rem
rem Created: 周二 8月 25 2020 TAOTIE
rem Processor: ATmega8
rem Compiler: BASCOM-AVR
rem Write your code here
$RegFile = "m8def.dat"
$Crystal = 8000000
'$Baud = 19200
$HWstack = 40
$SWstack = 8
$FrameSize = 40
Declare Sub Adc_isr()
'配置單模式和自動預分頻器設置
'單模式必須與GETADC()函數一起使用
'預分頻器將內部時鐘除以2、4、8、16、32、64或128
'因為ADC需要一個50-200千赫的時鐘
'自動功能將選擇可能的最高時鐘頻率
Config Adc = Free , Prescaler = Auto , Reference = avcc 'Internal
'*******************OLED配置*********************************************************************************
Config Scl = Portc.5 ' 用I2C引腳Scl = Portc.5 ,Sda = Portc.4
Config Sda = Portc.4
Config Twi = 400000 ' i2c 的速度
I2cinit
$lib "i2c_twi.lbx" ' 不使用模擬I2c的軟件,而是使用twi
$lib "glcdSSD1306-I2C.lib" ' 用glcdSSD1306-I2C庫替換默認庫要添加在bascom avr庫中
#if _build < 20784
Dim ___lcdrow As Byte , ___lcdcol As Byte ' 老版本變量格式進行編譯
#endif
Config Graphlcd = Custom , Cols = 128 , Rows = 64 , Lcdname = "SSD1306" '配置圖形顯示器
'*********現在給芯片供電*********************
On ADC Adc_isr Nosave 'Nosave指定的中斷發生時執行子程序。
Enable ADC
Enable Interrupts
'*******變量***********************
Dim W As long , Channel As Byte
Dim i As long
Dim d As string*5
Channel = 0
'************主程序************************
Do
Channel = 0 '現在從通道0讀取A/D值
'閑置將使微型計算機進入睡眠狀態。.
'一個中斷就會喚醒微型計算機.
Start ADC
Idle
nop
nop
Stop ADC
waitms 10
i=w*5000 '分度轉換
i=i/1023
i=i-1250
i=i/5
'**********誤差校準*************
i=i-1
if i>10 then i=i+1
if i>73 then i=i-1
if i>124 then i=i-1
if i>139 then i=i+1
if i>=183 then i=i+1
if i>221 then i=i-1
if i>226 then i=i+1
if i>267 then i=i-1
if i>269 then i=i+1
if i>296 then i=i-1
if i>314 then i=i+1'太煩。。。后面不管了
'************************************
d=str(i) '數值轉字符串
d= format(d, "+000") '輸出結果格式化------覆蓋顯示模式避免顯示跳動
Setfont Font12x16
Lcdat 2, 2 , "Channel " ;": "; Channel
Lcdat 6, 2 ,"value";": "; d
Loop
End
Sub Adc_isr()
$asm
push r26 '在堆棧上推送寄存器 STACK ← Rr
push r27
push r24
in r24,SREG '進入 Por Rd ← P
push r24
push r25
$End Asm
W = GetADC(Channel)
$Asm
pop r25 '從堆棧彈出寄存器 Rd ← STACK
pop r24
Out SREG,r24
pop r24
pop r27
pop r26
$end Asm
End Sub
$include "../Font12x16.font"
|