紙上談兵的程序如下:
$regfile = "m8def.dat"
$crystal = 8000000
'$baud = 9600
'*******************端口B用于編碼器及開關(guān)*********************
ddrb=&B00000000
Portb = &B11111111 ' 激活上拉寄存器
'*******************端口D用于LED指示燈*********************
ddrd=&b11111111
Portd = &B00000000
'*******************設(shè)置"SSD1306"*******************
'Config Clockdiv = 1 ' 確保芯片以8 MHz運行
Config Scl = Portc.5 ' 使用i2c引腳
Config Sda = Portc.4
Config Twi = 400000 ' i2c 速度
I2cinit
$lib "i2c_twi.lbx" ' 不使用模擬軟I2C,使用硬TWI
$lib "glcdSSD1306-I2C.lib" ' 用這個特殊的lib覆蓋默認(rèn)lib
Config Graphlcd = Custom , Cols = 128 , Rows = 64 , Lcdname = "SSD1306"
'****************設(shè)置定時器**************************
Config Timer0 = Timer , Prescale = 256 'timer0 以 256uS tick 自動啟動
Stop Timer0 '所以現(xiàn)在停止它
On Ovf0 checkidle '每 20 毫秒檢查一次空閑按鈕
'********************** ADS1110的I2C 地址*********************
' -------------
' ADS1110的I2C地址是1001aaa,其中aaa是出廠時默認(rèn)設(shè)置。ADS1110有8種不同類型,每種類型都有不同的I2C地址。封裝上,
' ADS1110的每種類型都以EDx為標(biāo)識,其中x表示地址變量。
' 部件 設(shè)備I2C設(shè)備地址 部件
' 型號 BIN HEX DEC 標(biāo)記
' --------- ------- ---- --- -------
' ADS1110A0 1001000 0x48 72 ED0
' ADS1110A1 1001001 0x49 73 ED1
' ADS1110A2 1001010 0x4A 74 ED2
' ADS1110A3 1001011 0x4B 75 ED3
' ADS1110A4 1001100 0x4C 76 ED4
' ADS1110A5 1001101 0x4D 77 ED5
'ADS1110A6 1001110 0x4E 78 ED6
' ADS1110A7 1001111 0x4F 79 ED7
' ----------------------------
Const ADS1110_DIZHI = &B10010001 '選擇ED0標(biāo)識的芯片ADS1110A0 地址:1001000 + 位0(讀/寫)
'Const Addressw = &B10010000 '沒使用
'*******************變量
Dim idleflag As Bit '標(biāo)記待機模式
dim switchevent as bit '空閑開關(guān)防抖
Dim it As word
Dim ut As word
Dim srt_temp As word
Dim Bt As Byte'因為我們需要保持編碼器的狀態(tài)
Dim dianya_zhuanhuan As word'Single '計算電壓轉(zhuǎn)換變量
Dim Pga As Integer '增益變量
Dim CE_LIANG_H As Byte , CE_LIANG_l As Byte '數(shù)據(jù)測量變量
green alias portd.0 '綠色LED指示降溫引腳
red alias portd.1 '紅色LED指示升溫引腳
dc alias portd.2 'dc加熱器引腳
idleswitch alias pinB.5 '待機開關(guān)引腳
'*************初始化***************
green = 0
red = 0
dc = 0
Pga = 1
:it=0
:ut=0
:srt_temp=90
Tcnt0 = 178 '設(shè)置 timer0,因此溢出需要 78 個刻度.即: 256-78 = 178 (78*256uS=20mS)
Enable Timer0 ' 啟用定時器中斷
Enable Interrupts '允許中斷發(fā)生
start timer0
'
Cls
Setfont Font12x16
'*************編碼器程序***************
set123:
waitms 50
Do
Bt = Encoder(pinb.0 , Pinb.1 , decline , rise , 0)
Lcdat 2 , 2 ,"SET:"; srt_temp;" C " ;
Waitms 10
'if PINB.3=0 then Exit Do
Debounce PINb.3 , 0 , gh
Loop
'*************主程序*************
Do
'***********查表ad值
gh:
It=srt_temp-1
Waitms 10
ut = Lookup(it, AD1)
Waitms 200
Gosub duqu
Waitms 100
Debounce PINb.4 , 0 , set123
Waitms 50
Loop
End
'------------------------------------------------------------------------------
duqu: '測量讀取計算子程序
I2cstart
I2cwbyte ADS1110_DIZHI '讀取地址
I2crbyte CE_LIANG_H , Ack '測量讀數(shù) 如果要讀取更多字節(jié),請指定ACK。
I2crbyte CE_LIANG_l , Nack '如果NACK是要讀取的最后一個字節(jié),則指定NACK
I2cstop '停止位
dianya_zhuanhuan = CE_LIANG_H * 256 '左移至高8位
dianya_zhuanhuan = dianya_zhuanhuan + CE_LIANG_l '添加低8位
'**************模式選擇及顯示********************
waitms 3
if idleflag = 1 then ut = 2003 '如果選擇了空閑模式,則設(shè)置空閑溫度對應(yīng)105攝氏度
if idleflag = 1 then Lcdat 2 , 1 , "IDLE100 C"
if idleflag = 0 then ut = ut
if idleflag = 0 then Lcdat 2 , 1 , "SET:"; srt_temp;" C " ;
'****************溫度控制信號執(zhí)行****************
waitms 50
if dianya_zhuanhuan > ut then
red = 1 '打開紅色 LED
green = 0 '關(guān)閉綠色 LED
dc = 1 '并打開絡(luò)鐵的電源
Lcdat 2 , 112 ,chr(127)
endif
if dianya_zhuanhuan <= ut then
green = 1
red = 0
dc = 0 '關(guān)閉電源
Lcdat 2 , 112 ,chr(126)
endif
waitms 500
Return
end
'-------------------------------------------------------------------------------------------
$include "../Font12x16.font" ' 字體文件
checkidle: 'timer0 溢出中斷例程,每 20mS 調(diào)用一次
Tcnt0 = 178 '下次重新加載計時器
if idleswitch = 0 then '是否按下了空閑開關(guān)?
toggle switchevent '是的,所以去抖動
if switchevent = 0 then '去抖動,所以必須真正按下開關(guān)
stop timer0 '等一下,我們把這一切解決掉
while idleswitch = 0
wend '等待用戶釋放開關(guān)
start timer0 '現(xiàn)在我們可以下次重新啟動計時器
toggle idleflag '指示開關(guān)已按下
endif
else
switchevent = 0 '如果上次是1,但20毫秒后不是,那就不是誤報了。
endif
Return
'************************NTC溫度轉(zhuǎn)換ADC值表1----400****************************
AD1:
Data 24704% ,24395% ,24079% ,23757% ,23429% ,23096% ,22757% ,22414% ,22066% ,21713% ,
Data 21357% ,20997% ,20635% ,20270% ,19902% ,19533% ,19162% ,18791% ,18419% ,18047% ,
Data 17675% ,17304% ,16934% ,16566% ,16200% ,15836% ,15475% ,15116% ,14761% ,14410% ,
Data 14062% ,13718% ,13379% ,13045% ,12715% ,12390% ,12071% ,11757% ,11448% ,11145% ,
Data 10848% ,10556% ,10271% ,9991% ,9717% ,9449% ,9187% ,8931% ,8681% ,8437% ,
Data 8199% ,7967% ,7741% ,7520% ,7305% ,7096% ,6892% ,6694% ,6501% ,6313% ,
Data 6131% ,5954% ,5782% ,5614% ,5452% ,5294% ,5140% ,4992% ,4847% ,4707% ,
Data 4571% ,4439% ,4311% ,4187% ,4066% ,3950% ,3836% ,3727% ,3620% ,3517% ,
Data 3417% ,3320% ,3226% ,3134% ,3046% ,2960% ,2877% ,2797% ,2719% ,2643% ,
Data 2570% ,2499% ,2430% ,2363% ,2298% ,2236% ,2175% ,2116% ,2058% ,2003% ,
Data 1949% ,1897% ,1847% ,1797% ,1750% ,1704% ,1659% ,1616% ,1573% ,1533% ,
Data 1493% ,1454% ,1417% ,1381% ,1346% ,1312% ,1278% ,1246% ,1215% ,1185% ,
Data 1155% ,1127% ,1099% ,1072% ,1046% ,1020% ,995% ,971% ,948% ,925% ,
Data 903% ,882% ,861% ,841% ,821% ,802% ,783% ,765% ,747% ,730% ,
Data 713% ,697% ,681% ,666% ,651% ,636% ,622% ,608% ,595% ,582% ,
Data 569% ,557% ,545% ,533% ,521% ,510% ,499% ,489% ,478% ,468% ,
Data 458% ,449% ,439% ,430% ,421% ,413% ,404% ,396% ,388% ,380% ,
Data 373% ,365% ,358% ,351% ,344% ,337% ,330% ,324% ,318% ,311% ,
Data 305% ,300% ,294% ,288% ,283% ,277% ,272% ,267% ,262% ,257% ,
Data 252% ,248% ,243% ,239% ,234% ,230% ,226% ,222% ,218% ,214% ,
AD2:
Data 210% ,207% ,203% ,199% ,196% ,192% ,189% ,186% ,183% ,180% ,
Data 176% ,173% ,171% ,168% ,165% ,162% ,159% ,157% ,154% ,152% ,
Data 149% ,147% ,144% ,142% ,140% ,137% ,135% ,133% ,131% ,129% ,
Data 127% ,125% ,123% ,121% ,119% ,117% ,115% ,114% ,112% ,110% ,
Data 109% ,107% ,105% ,104% ,102% ,101% ,99% ,98% ,96% ,95% ,
Data 93% ,92% ,91% ,89% ,88% ,87% ,86% ,84% ,83% ,82% ,
Data 81% ,80% ,79% ,78% ,76% ,75% ,74% ,73% ,72% ,71% ,
Data 70% ,69% ,69% ,68% ,67% ,66% ,65% ,64% ,63% ,62% ,
Data 62% ,61% ,60% ,59% ,58% ,58% ,57% ,56% ,55% ,55% ,
Data 54% ,53% ,53% ,52% ,51% ,51% ,50% ,50% ,49% ,48% ,
Data 48% ,47% ,47% ,46% ,45% ,45% ,44% ,44% ,43% ,43% ,
Data 42% ,42% ,41% ,41% ,40% ,40% ,39% ,39% ,39% ,38% ,
Data 38% ,37% ,37% ,36% ,36% ,36% ,35% ,35% ,34% ,34% ,
Data 34% ,33% ,33% ,33% ,32% ,32% ,32% ,31% ,31% ,31% ,
Data 30% ,30% ,30% ,29% ,29% ,29% ,28% ,28% ,28% ,27% ,
Data 27% ,27% ,27% ,26% ,26% ,26% ,26% ,25% ,25% ,25% ,
Data 25% ,24% ,24% ,24% ,24% ,23% ,23% ,23% ,23% ,22% ,
Data 22% ,22% ,22% ,22% ,21% ,21% ,21% ,21% ,21% ,20% ,
Data 20% ,20% ,20% ,20% ,19% ,19% ,19% ,19% ,19% ,19% ,
Data 18% ,18% ,18% ,18% ,18% ,18% ,17% ,17% ,17% ,17% ,
'************************************************
'編碼器子程序
decline:
srt_temp=srt_temp-10
if srt_temp<20 then srt_temp=10
Return
rise:
srt_temp=srt_temp+10
if srt_temp>390 then srt_temp=400
Return
|