本帖最后由 taotie 于 2020-9-3 13:13 编辑
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"
|