Hardware Reference
In-Depth Information
#include “c:\cwHCS12\include\spi0util.h”
// include spi0util.c in the project
void sendMCP4922(char x1, char x2);
void openSPI0(void);
void main(void) {
DDRK |
5
BIT7;
// configure PK7 pin for output
openSPI0();
while(1) {
sendMCP4922(0x34,0xCD);
// generate 1.5 V from V
OUTA
pin
delayby1ms(1);
// wait for 1 ms
sendMCP4922(0x39,0xAA);
// generate 3.0 V from V
OUTA
delayby1ms(1);
sendMCP4922(0x3E,0x66);
// generate 4.5 V from V
OUTA
delayby1ms(1);
}
}
void openSPI0(void) {
SPI0BR
5
0x10;
// Set SPI0 baud rate to 6 MHz
SPI0CR1
5
0x50;
// enable SPI0, SCK idle low & shift on rising edge
SPI0CR2
5
0x02;
// disable bidirectional mode, stop SPI in wait mode
WOMS
5
0;
// enable port S pull-up
}
void sendMCP4922(char x1, char x2) {
PORTK
&
5
0x7F;
// enable SPI transfer to MCP4922
putcspi0(x1);
// send out upper byte
putcspi0(x2);
// send out lower byte
PTK
|
5
BIT7;
// start DAC operation
}
▲
Example 10.8
▼
Write a program to generate the waveform shown in Figure 10.21 using the DAC circuit
shown in Figure 10.19.
V
OUTA
Time
2 ms
2 ms
2 ms
2 ms
V
OUTB
1 ms
1 ms
1 ms
1 ms
1 ms
1 ms
1 ms
1 ms
Figure 10.21
■
Waveform to be generated
Search WWH ::
Custom Search