标题: 一个简单的单片机SPI通讯小程序 [打印本页]

作者: oldspring    时间: 2018-10-22 11:00
标题: 一个简单的单片机SPI通讯小程序
以下是一个简单的SPI通讯小程序,希望大家能够喜欢。
  1. // DAC module connections
  2. sbit DAC_CS at P3_4_bit;
  3. // End DAC module connections

  4. unsigned int value;

  5. // DAC increments (0..4095) --> output voltage (0..Vref)
  6. void DAC_Output(unsigned int valueDAC) {
  7.   char temp;

  8.   DAC_CS = 0;                            // select MCP4921

  9.   // Send High Byte
  10.   temp = (valueDAC >> 8) & 0x0F;         // Store valueDAC[11..8] to temp[3..0]
  11.   temp |= 0x30;                          // Define DAC setting http://ww1.microchip.com/downloads/en/DeviceDoc/21897B.pdf#page=18&zoom=100
  12.   SPI1_Write(temp);                      // Send high byte via SPI

  13.   // Send Low Byte
  14.   temp = valueDAC;                       // Store valueDAC[7..0] to temp[7..0]
  15.   SPI1_Write(temp);                      // Send low byte via SPI

  16.   DAC_CS = 1;                            // deselect MCP4921
  17. }

  18. void main() {

  19.   DAC_CS = 1;                            // deselect MCP4921
  20.   SPI1_Init();                           // Initialize SPI module
  21.   
  22.   value = 2048;                          // When program starts, DAC gives
  23.                                          //   the output in the mid-range

  24.   while (1) {                            // Endless loop

  25.     if ((!P0_0_bit) && (value < 4095)) { // If P0.0 button is pressed
  26.       value++;                           //   increment value
  27.     }
  28.     else {
  29.       if ((!P0_1_bit) && (value > 0)) {  // If P0.1 button is pressed
  30.         value--;                         //   decrement value
  31.       }
  32.     }
  33.    
  34.     DAC_Output(value);                   // Send value to DAC chip
  35.     Delay_ms(1);                         // Slow down key repeat pace
  36.    
  37.   }
  38. }
复制代码
相关信息:http://www.51hei.com/bbs/dpj-136722-1.html







欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1