標題: PIC單片機的一個簡單的SPI小程序 [打印本頁]

作者: oldspring    時間: 2018-10-23 08:44
標題: PIC單片機的一個簡單的SPI小程序
這里介紹一個簡單的PIC單片機SPI測試小程序,希望大家能夠喜歡。
  1. // DAC module connections
  2. sbit Chip_Select at RC0_bit;
  3. sbit Chip_Select_Direction at TRISC0_bit;
  4. // End DAC module connections

  5. unsigned int value;                     

  6. void InitMain() {
  7.   TRISA0_bit = 1;                        // Set RA0 pin as input
  8.   TRISA1_bit = 1;                        // Set RA1 pin as input
  9.   Chip_Select = 1;                       // Deselect DAC
  10.   Chip_Select_Direction = 0;             // Set CS# pin as Output
  11.   SPI1_Init();                           // Initialize SPI module
  12. }

  13. // DAC increments (0..4095) --> output voltage (0..Vref)
  14. void DAC_Output(unsigned int valueDAC) {
  15.   char temp;

  16.   Chip_Select = 0;                       // Select DAC chip
  17.   
  18.   // Send High Byte                                         
  19.   temp = (valueDAC >> 8) & 0x0F;         // Store valueDAC[11..8] to temp[3..0]
  20.   temp |= 0x30;                          // Define DAC setting, see MCP4921 datasheet
  21.   SPI1_Write(temp);                      // Send high byte via SPI
  22.   
  23.   // Send Low Byte
  24.   temp = valueDAC;                       // Store valueDAC[7..0] to temp[7..0]
  25.   SPI1_Write(temp);                      // Send low byte via SPI
  26.   
  27.   Chip_Select = 1;                       // Deselect DAC chip
  28. }

  29. void main() {
  30.   ANSEL = 0;
  31.   ANSELH = 0;
  32.   InitMain();                            // Perform main initialization

  33.   value = 2048;                          // When program starts, DAC gives
  34.                                          //   the output in the mid-range
  35.                                           
  36. while (1) {                             // Endless loop

  37.     if ((RA0_bit) && (value < 4095)) {   // If RA0 button is pressed
  38.       value++;                           //   increment value
  39.       }
  40.     else {
  41.       if ((RA1_bit) && (value > 0)) {    // If RA1 button is pressed
  42.         value--;                         //   decrement value
  43.         }
  44.       }
  45.     DAC_Output(value);                   // Send value to DAC chip
  46.     Delay_ms(1);                         // Slow down key repeat pace
  47.   }
  48. }
復(fù)制代碼
詳細內(nèi)容,請參考:http://www.torrancerestoration.com/bbs/dpj-138111-1.html





作者: 1273539886    時間: 2018-12-12 17:06
你好!pic的spi怎么檢測發(fā)送完成呀,能貼一下你的SPI1_Write代碼嗎?謝謝




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1