标题:
一个简单的单片机SPI通讯小程序
[打印本页]
作者:
oldspring
时间:
2018-10-22 11:00
标题:
一个简单的单片机SPI通讯小程序
以下是一个简单的SPI通讯小程序,希望大家能够喜欢。
// DAC module connections
sbit DAC_CS at P3_4_bit;
// End DAC module connections
unsigned int value;
// DAC increments (0..4095) --> output voltage (0..Vref)
void DAC_Output(unsigned int valueDAC) {
char temp;
DAC_CS = 0; // select MCP4921
// Send High Byte
temp = (valueDAC >> 8) & 0x0F; // Store valueDAC[11..8] to temp[3..0]
temp |= 0x30; // Define DAC setting http://ww1.microchip.com/downloads/en/DeviceDoc/21897B.pdf#page=18&zoom=100
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
DAC_CS = 1; // deselect MCP4921
}
void main() {
DAC_CS = 1; // deselect MCP4921
SPI1_Init(); // Initialize SPI module
value = 2048; // When program starts, DAC gives
// the output in the mid-range
while (1) { // Endless loop
if ((!P0_0_bit) && (value < 4095)) { // If P0.0 button is pressed
value++; // increment value
}
else {
if ((!P0_1_bit) && (value > 0)) { // If P0.1 button is pressed
value--; // decrement value
}
}
DAC_Output(value); // Send value to DAC chip
Delay_ms(1); // Slow down key repeat pace
}
}
复制代码
相关信息:
http://www.51hei.com/bbs/dpj-136722-1.html
spi.jpg
(59.08 KB, 下载次数: 32)
下载附件
2018-10-22 10:59 上传
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1