Datasheet

Library Example
The code demonstrates how to use SPI library functions for communication between SPI
module of the MCU and MAX7219 chip. MAX7219 controls eight 7 segment displays.
// DAC module connections
sbit Chip_Select at PORTB.B0;
sbit Chip_Select_Direction at DDRB.B0;
// End DAC module connections
unsigned int value;
void InitMain() {
DDA0 = 0; // Set PA0 pin as input
DDA1 = 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
}
// DAC increments (0..4095) --> output voltage (0..Vref)
void DAC_Output(unsigned int valueDAC) {
char temp;
Chip_Select = 0; // Select DAC chip
// Send High Byte
temp = (valueDAC >> 8) & 0x0F; // Store valueDAC[11..8]
to temp[3..0]
temp |= 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
}
void main() {
InitMain(); // Perform main initialization
value = 2048; // When program starts, DAC gives
// the output in the mid-range
while (1) { // Endless loop
429
MIKROELEKTRONIKA - SOFTWARE AND HARDWARE SOLUTIONS FOR EMBEDDED WORLD
Libraries
mikroC PRO for AVR
CHAPTER 6