User manual

396
mikoBasic PRO for PIC32
MikroElektronika
dim value as word
sub procedure InitMain()
TRISB0_bit = 1 ‘ Set RB0 pin as input
TRISB1_bit = 1 ‘ Set RB1 pin as input
Chip_Select = 1 ‘ Deselect DAC
Chip_Select_Direction = 0 ‘ Set CS# pin as Output
Soft_Spi_Init() ‘ Initialize Soft_SPI
end sub
‘ DAC increments (0..4095) --> output voltage (0..Vref)
sub procedure DAC_Output(dim valueDAC as word)
dim temp as byte volatile
Chip_Select = 0 ‘ Select DAC chip
‘ Send High Byte
temp = word(valueDAC >> 8) and 0x0F ‘ Store valueDAC[11..8] to temp[3..0]
temp = temp or 0x30 ‘ Dene DAC setting, see MCP4921 datasheet
Soft_SPI_Write(temp) ‘ Send high byte via Soft SPI
‘ Send Low Byte
temp = valueDAC ‘ Store valueDAC[7..0] to temp[7..0]
Soft_SPI_Write(temp) ‘ Send low byte via Soft SPI
Chip_Select = 1 ‘ Deselect DAC chip
end sub
main:
CHECON = 0x32
AD1PCFG = 0xFFFF ‘ Congure AN pins as digital
InitMain() ‘ Perform main initialization
value = 2048 ‘ When program starts, DAC gives
‘ the output in the mid-range
while (TRUE) ‘ Endless loop
if ((RB0_bit) and (value < 4095)) then ‘ If RB0 button is pressed
Inc(value) ‘ increment value
else
if ((RB1_bit) and (value > 0)) then ‘ If RB1 button is pressed
Dec(value) ‘ decrement value
end if
end if
DAC_Output(value) ‘ Send value to DAC chip
Delay_ms(1) ‘ Slow down key repeat pace
wend
end.