Mikroelektronika MIKROE-742 데이터 시트

다운로드
페이지 532
Library Example
The code demonstrates how to use SPI library functions for communication between
SPI module of the MCU and Microchip's MCP4921 12-bit D/A converter
program SPI;
// DAC module connections
var Chip_Select : sbit at PORTB.B0;
Chip_Select_Direction : 
sbit at DDRB.B0;
// End DAC module connections
var value : word;
procedure InitMain();
begin
DDA0_bit := 0;                 
// Set PA0 pin as input
DDA1_bit := 0;               
// Set PA1 pin as input
Chip_Select := 1;    
// Deselect DAC
Chip_Select_Direction := 1; 
// Set CS# pin as Output
SPI1_Init();                   
// Initialize SPI1 module
end;
// DAC increments (0..4095) --> output voltage (0..Vref)
procedure DAC_Output( valueDAC : word);
var temp : byte;
begin
Chip_Select := 0;                        // Select DAC chip
// Send High Byte
temp := word(valueDAC 
shr 8) and 0x0F;  // Store valueDAC[11..8]
to temp[3..0]
temp := temp 
or 0x30;             // Define DAC setting, see
MCP4921 datasheet
SPI1_Write(temp);                  // Send high byte via SPI
// Send Low Byte
temp := valueDAC;             // Store valueDAC[7..0] to temp[7..0]
SPI1_Write(temp);         // Send low byte via SPI
Chip_Select := 1;                       // Deselect DAC chip
end;
begin
InitMain();                     // Perform main initialization
value := 2048;                 // When program starts, DAC gives
377
MIKROELEKTRONIKA
- SOFTWARE AND HARDWARE SOLUTIONS FOR EMBEDDED WORLD
Libraries
mikroPASCAL PRO for AVR
CHAPTER 6