以前,做而論道寫過一篇關(guān)于?DAC0832?的博文
當時,為了直觀的說明?DAC0832?的基本功能,就使用了撥動開關(guān)來輸入數(shù)字量。
后來,時常有人留言或私信,提出怎樣利用單片機來驅(qū)動?DAC0832?輸出周期電壓波形、以及如何編程的問題。
其實,用單片機輸出數(shù)字量,還真是很簡單方便的。
電路,也不用像課本里面畫的那么復(fù)雜,用任意一個單片機的并行口連接到?DAC0832?的數(shù)據(jù)輸入口即可。
用?P1?口,驅(qū)動?0832?的電路如下:
按照這個電路,寫入、執(zhí)行下列的小程序,可以看到輸出的鋸齒波電壓的波形圖如下所示:
????ORG???0000H
????JMP???START
START:
;------------------------------------------
????MOV???R2,??#0
LOOP:
????MOV???P1,??R2??????;輸出
????CALL??DELAY????????;延時
????INC???R2
????SJMP??LOOP
;---------------
DELAY:?????????????????;延時子程序
????MOV???R6,??#2;1;127
DEL1:
????MOV???R7,??#200
DEL2:
????DJNZ??R7,??DEL2????;內(nèi)循環(huán)200遍
????DJNZ??R6,??DEL1????;外循環(huán)127遍
????RET
;------------------------------------------
END
看波形,還是稍微有些失真呵。
同樣還是這個電路,執(zhí)行下列的程序,看看,會出現(xiàn)什么波形?
????ORG???0000H
????JMP???START
START:
;------------------------------------------
SINE:
????MOV???R1,?#00H
????MOV???DPTR,?#SINTAB
LOOP:
????MOV???A,?R1
????MOVC??A,?@A?+?DPTR??;取數(shù)據(jù)
????MOV???P1,?A?????????;輸出,D/A?轉(zhuǎn)換
????INC???R1
????CALL??DELAY?????????;延時
????SJMP??LOOP
;---------------
SINTAB:?????????????????;正弦波數(shù)據(jù)表
????DB??07FH,?083H,?086H,?089H,?08CH,?08FH,?092H,?095H
????DB??098H,?09BH,?09EH,?0A1H,?0A4H,?0A7H,?0AAH,?0ADH
????DB??0B0H,?0B3H,?0B6H,?0B9H,?0BBH,?0BEH,?0C1H,?0C3H
????DB??0C6H,?0C9H,?0CBH,?0CEH,?0D0H,?0D2H,?0D5H,?0D7H
????DB??0D9H,?0DBH,?0DEH,?0E0H,?0E2H,?0E4H,?0E5H,?0E7H
????DB??0E9H,?0EBH,?0ECH,?0EEH,?0EFH,?0F1H,?0F2H,?0F4H
????DB??0F5H,?0F6H,?0F7H,?0F8H,?0F9H,?0FAH,?0FBH,?0FBH
????DB??0FCH,?0FDH,?0FDH,?0FDH,?0FEH,?0FEH,?0FEH,?0FEH
????DB??0FFH,?0FEH,?0FEH,?0FEH,?0FEH,?0FDH,?0FDH,?0FDH
????DB??0FCH,?0FBH,?0FBH,?0FAH,?0F9H,?0F8H,?0F7H,?0F6H
????DB??0F5H,?0F4H,?0F2H,?0F1H,?0EFH,?0EEH,?0ECH,?0EBH
????DB??0E9H,?0E7H,?0E5H,?0E4H,?0E2H,?0E0H,?0DEH,?0DBH
????DB??0D9H,?0D7H,?0D5H,?0D2H,?0D0H,?0CEH,?0CBH,?0C9H
????DB??0C6H,?0C3H,?0C1H,?0BEH,?0BBH,?0B9H,?0B6H,?0B3H
????DB??0B0H,?0ADH,?0AAH,?0A7H,?0A4H,?0A1H,?09EH,?09BH
????DB??098H,?095H,?092H,?08FH,?08CH,?089H,?086H,?083H
????DB??7FH,?7CH,?79H,?76H,?73H,?70H,?6DH,?6AH
????DB??67H,?64H,?61H,?5EH,?5BH,?58H,?55H,?52H
????DB??4FH,?4CH,?49H,?46H,?44H,?41H,?3EH,?3BH
????DB??39H,?36H,?34H,?31H,?2FH,?2CH,?2AH,?28H
????DB??26H,?23H,?21H,?1FH,?1DH,?1BH,?19H,?18H
????DB??16H,?14H,?12H,?11H,?0FH,?0EH,?0DH,?0BH
????DB??0AH,?09H,?08H,?07H,?06H,?05H,?04H,?03H
????DB??03H,?02H,?02H,?01H,?01H,?01H,?01H,?00H
????DB??00H,?00H,?01H,?01H,?01H,?01H,?02H,?02H
????DB??03H,?03H,?04H,?05H,?06H,?07H,?08H,?09H
????DB??0AH,?0BH,?0DH,?0EH,?0FH,?11H,?12H,?14H
????DB??16H,?18H,?19H,?1BH,?1DH,?1FH,?21H,?23H
????DB??26H,?28H,?2AH,?2CH,?2FH,?31H,?34H,?36H
????DB??39H,?3BH,?3EH,?41H,?44H,?46H,?49H,?4CH
????DB??4FH,?52H,?55H,?58H,?5BH,?5EH,?61H,?64H
????DB??67H,?6AH,?6DH,?70H,?73H,?76H,?79H,?7CH
;---------------
DELAY:??????????????????;延時子程序
????MOV???R6,???#1;127
DEL1:
????MOV???R7,???#200
DEL2:
????DJNZ??R7,???DEL2????;內(nèi)循環(huán)200遍
????DJNZ??R6,???DEL1????;外循環(huán)127遍
????RET
;------------------------------------------
END
確實是個正弦波。
想要改變波形的形狀,修改程序中的數(shù)據(jù)表內(nèi)容即可。
想要改變波形的周期,修改程序中的延時子程序即可。