Last active
December 12, 2015 02:48
-
-
Save nayutaya/4701328 to your computer and use it in GitHub Desktop.
ArduinoにMCP4922(12ビットD/Aコンバータ)をSPIで接続し、シリアルコンソールから電圧を設定するスケッチ。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "pins_arduino.h" | |
#include <SPI.h> | |
const int MCP4922_LDAC = 9; | |
char buffer[16] = {0}; | |
unsigned char buffer_counter = 0; | |
int da0_value = 0; | |
void mcp4922_init() | |
{ | |
pinMode(MCP4922_LDAC, OUTPUT); | |
SPI.begin(); | |
SPI.setBitOrder(MSBFIRST); | |
SPI.setClockDivider(SPI_CLOCK_DIV8); // 16MHz / 8 | |
SPI.setDataMode(SPI_MODE0); | |
} | |
void mcp4922_write(const int value) | |
{ | |
digitalWrite(MCP4922_LDAC, HIGH); | |
digitalWrite(SS, LOW); | |
SPI.transfer(((value >> 8) & 0x0F) | 0x30); // DAC-A, Unbuffered, 1x, No shutdown | |
SPI.transfer(value & 0xFF); | |
digitalWrite(SS, HIGH); | |
digitalWrite(MCP4922_LDAC, LOW); | |
} | |
int hex2int(const char hex) | |
{ | |
if ( hex >= '0' && hex <= '9' ) return hex - '0'; | |
else if ( hex >= 'A' && hex <= 'F' ) return hex - 'A' + 10; | |
else if ( hex >= 'a' && hex <= 'f' ) return hex - 'a' + 10; | |
else return 0; | |
} | |
char int2hex(const int value) | |
{ | |
if ( value >= 0 && value <= 9 ) return '0' + value; | |
else if ( value >= 10 && value <= 15 ) return 'A' + (value - 10); | |
else return '_'; | |
} | |
void parse_command(const String command) | |
{ | |
Serial.println(command); | |
// "DA0" | |
if ( command.equals("DA0") ) | |
{ | |
Serial.print("DA0:"); | |
Serial.print(int2hex((da0_value >> 12) & 0x0F)); | |
Serial.print(int2hex((da0_value >> 8) & 0x0F)); | |
Serial.print(int2hex((da0_value >> 4) & 0x0F)); | |
Serial.print(int2hex((da0_value >> 0) & 0x0F)); | |
Serial.println(""); | |
} | |
// "DA0:FFFF" | |
else if ( command.startsWith("DA0:") && command.length() == 8 ) | |
{ | |
da0_value = 0; | |
da0_value |= hex2int(command.charAt(4)) << 12; | |
da0_value |= hex2int(command.charAt(5)) << 8; | |
da0_value |= hex2int(command.charAt(6)) << 4; | |
da0_value |= hex2int(command.charAt(7)); | |
Serial.print("DA0:"); | |
Serial.print(int2hex((da0_value >> 12) & 0x0F)); | |
Serial.print(int2hex((da0_value >> 8) & 0x0F)); | |
Serial.print(int2hex((da0_value >> 4) & 0x0F)); | |
Serial.print(int2hex((da0_value >> 0) & 0x0F)); | |
Serial.println(""); | |
mcp4922_write(da0_value); | |
} | |
} | |
void read_serial() | |
{ | |
if ( Serial.available() > 0 ) | |
{ | |
buffer[buffer_counter] = (char)Serial.read(); | |
buffer_counter++; | |
if ( buffer_counter >= sizeof(buffer) ) | |
{ | |
buffer_counter = 0; | |
} | |
if ( buffer[buffer_counter - 1] == '\n' ) | |
{ | |
buffer[buffer_counter - 1] = '\0'; | |
parse_command(String(buffer)); | |
buffer_counter = 0; | |
} | |
} | |
} | |
void setup() | |
{ | |
mcp4922_init(); | |
Serial.begin(9600); | |
Serial.println("SETUP"); | |
} | |
void loop() | |
{ | |
read_serial(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment