📄 dac.pbas
字号:
'*******************************************************************************
' microcontroller: P16F877A
'
' Project name: DAC
'
' This project is designed to work with PIC 16F877A; with minor adjustments,
' it should work with any other PIC MCU that has the SPI interface.
'
' This is a sample program which demonstrates the use of the Microchip's MCP4921
' 12-bit D/A converter with PIC mcu's. This device accepts digital input (number
' from 0..4095) and transforms it to the output voltage, ranging from 0..Vref.
' In this example the D/A is connected to PORTC and communicates with PIC through
' the SPI. The reference voltage on the mikroElektronika's DAC module is 5 V.
' NOTE: in this example, the entire DAC's resolution range (12bit -> 4096
' increments) is covered, meaning you'll need to hold a button for about
' 7 minutes to get from mid-range to the end-of-range.
'
' Tested on P16F877A.
'*******************************************************************************
program dac
const CHIP_SELECT = 0
dim value as word
sub procedure Init
SetBit(TRISB,0)
SetBit(TRISB,1)
SPI_init
ClearBit(TRISC,CHIP_SELECT)
end sub
' DAC increments (0..4095) --> output voltage (0..Vref)
sub procedure DAC_Output(dim Value as word)
dim temp as byte
ClearBit(PORTC,CHIP_SELECT) ' Prepare for data transfer
temp = hi(Value) and $0F ' Prepare hi-byte for transfer
temp = temp or $30 ' It's a 12-bit number, so only
SPI_Write(temp) ' lower nibble of high byte is used
temp = lo(Value) ' Prepare lo-byte for transfer
SPI_Write(temp)
SetBit(PORTC,CHIP_SELECT)
end sub
'main procedure
main:
Init
DAC_Output(2048) ' When program starts, DAC gives
value = 2048 ' the output in the mid-range
while true ' Main loop
if (Button(portb,0,1,1)=true)
and (value < 4095) then ' Test button on B0 (increment)
Inc(value)
else
if (Button(portb,1,1,1)=true) ' If RB0 is not active then test
and (value > 0) then ' RB1 (decrement)
Dec(value)
end if
end if
DAC_Output(value) ' Perform output
Delay_ms(200) ' Slow down key repeat pace
wend
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -