TA的每日心情 | 开心 2015-6-9 15:02 |
---|
签到天数: 13 天 连续签到: 1 天 [LV.3]偶尔看看II
|
楼主 |
发表于 2014-10-28 12:45:45
|
显示全部楼层
单个通道的读写程序:- #include <SPI.h>
- unsigned char receiveBuffer[2] = {0, 0}; //receive data buffer
- unsigned short value = 0; //
- unsigned short convData;//conversion data,remove the hight 4 bits for channel inforamtion
- float volts = 0; //displayed voltage
- unsigned char ch; //voltage Channel
- float vRef=2.5;
- char tempString [12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
- #define CS 10
- void setup() {
- //for debug
- Serial.begin(9600);
-
- //set up the SPI protocol ref to the AD7298 datasheet
- SPI.setBitOrder(MSBFIRST);
- SPI.setDataMode(SPI_MODE2);
- SPI.begin();
- }
- void loop() {
-
- //the 1st cycle,write cycle
- digitalWrite(CS, LOW);
- SPI.transfer(0xA0); //the first Byte
- SPI.transfer(0x00); //the last byte
- digitalWrite(CS,HIGH);
- delay(10);
-
- //the 2nd cycle,wait for conversion cycle
- digitalWrite(CS, LOW);
- SPI.transfer(00);
- SPI.transfer(00);
- digitalWrite(CS,HIGH);
- delay(10);
- //the 3rd cycle,read voltage from the assigned channel in the write cyle
-
- digitalWrite(CS, LOW);
- receiveBuffer[0]=SPI.transfer(00);
- receiveBuffer[1]=SPI.transfer(00);
- digitalWrite(CS,HIGH);
- //analysis the data for display
- value = ((unsigned short)receiveBuffer[0] << 8) + receiveBuffer[1];
- convData = value & 0x0FFF; //adc raw data value
- ch = (value & 0xF000) >> 12; //adc channel raw data
- volts = 2*((float)convData * vRef) / 4096; //
-
- Serial.print("rawdata=");
- itoa(convData, tempString, 10);
- Serial.println(tempString);
-
- Serial.print("THE Vin0 input Voltage is:");
- Serial.print(volts,4);
-
- Serial.println('\n');
- delay(1000);
- }
复制代码 |
|