3 changed files with 196 additions and 0 deletions
			
			
		@ -0,0 +1,138 @@ | 
				
			|||||
 | 
					//----------------------------------------------------------------------------------------------------------------------------
 | 
				
			||||
 | 
					// Project:     Uppsense
 | 
				
			||||
 | 
					// Name:        dac101c085.cpp
 | 
				
			||||
 | 
					// Author:      Maximilian Stiefel
 | 
				
			||||
 | 
					// Date:        08.08.2017
 | 
				
			||||
 | 
					//
 | 
				
			||||
 | 
					// Description:
 | 
				
			||||
 | 
					//
 | 
				
			||||
 | 
					//----------------------------------------------------------------------------------------------------------------------------
 | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					//-------------------------------------Libraries------------------------------------------------------------------------------
 | 
				
			||||
 | 
					#include "dac101c085.h" | 
				
			||||
 | 
					#include <SmingCore/Wire.h> | 
				
			||||
 | 
					#include <SmingCore/SmingCore.h> | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					//-------------------------------------Namespaces-----------------------------------------------------------------------------
 | 
				
			||||
 | 
					namespace rijnfel { | 
				
			||||
 | 
					namespace dac { | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					//-------------------------------------Constructor----------------------------------------------------------------------------
 | 
				
			||||
 | 
					cDAC101C085::cDAC101C085(uint8_t i_number, uint8_t i_address): | 
				
			||||
 | 
						m_number(i_number), m_address(i_address) | 
				
			||||
 | 
					{} | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					//-------------------------------------Destructor-----------------------------------------------------------------------------
 | 
				
			||||
 | 
					cDAC101C085::~cDAC101C085() | 
				
			||||
 | 
					{} | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					//-------------------------------------checkDev-------------------------------------------------------------------------------
 | 
				
			||||
 | 
					void cDAC101C085::checkDev() | 
				
			||||
 | 
					{ | 
				
			||||
 | 
						// Vars
 | 
				
			||||
 | 
						byte error = 0; | 
				
			||||
 | 
						// Action
 | 
				
			||||
 | 
						#if DEBUG_LEVEL == 1 | 
				
			||||
 | 
						Serial.print("DAC: Scanning address 0x"); | 
				
			||||
 | 
						Serial.print(m_address, HEX); | 
				
			||||
 | 
						Serial.println(".");	 | 
				
			||||
 | 
						#endif | 
				
			||||
 | 
						Wire.beginTransmission(m_address); | 
				
			||||
 | 
						error = Wire.endTransmission(); | 
				
			||||
 | 
						I2CError(error); // Pass to error parser
 | 
				
			||||
 | 
					} | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					//-------------------------------------changeSettings-------------------------------------------------------------------------
 | 
				
			||||
 | 
					uint8_t cDAC101C085::changeSettings(enum eOpMode mode, uint16_t val) | 
				
			||||
 | 
					{ | 
				
			||||
 | 
						// Vars
 | 
				
			||||
 | 
						uint16_t settings = 0; | 
				
			||||
 | 
						// Action
 | 
				
			||||
 | 
						if( val > 1023) | 
				
			||||
 | 
							return 0; // This is illegal
 | 
				
			||||
 | 
						settings = val << D_BITS_POS; // First two bits have to be 0
 | 
				
			||||
 | 
						switch(mode) | 
				
			||||
 | 
						{ | 
				
			||||
 | 
							case NORMAL: | 
				
			||||
 | 
							       		settings += 0b00 << PD_BITS_POS;	 | 
				
			||||
 | 
									break; | 
				
			||||
 | 
							case PULL_DOWN_2K5: | 
				
			||||
 | 
									settings += 0b01 << PD_BITS_POS;	 | 
				
			||||
 | 
									break; | 
				
			||||
 | 
							case PULL_DOWN_100K: | 
				
			||||
 | 
									settings += 0b10 << PD_BITS_POS;	 | 
				
			||||
 | 
									break; | 
				
			||||
 | 
							case HIGH_IMPEDANCE: | 
				
			||||
 | 
									settings += 0b11 << PD_BITS_POS;	 | 
				
			||||
 | 
									break; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
						} | 
				
			||||
 | 
						return WriteSettings(settings);	 | 
				
			||||
 | 
					} | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					//-------------------------------------WriteSettings--------------------------------------------------------------------------
 | 
				
			||||
 | 
					uint8_t cDAC101C085::WriteSettings(uint16_t settings) | 
				
			||||
 | 
					{ | 
				
			||||
 | 
						// Vars
 | 
				
			||||
 | 
						uint8_t error = 0; | 
				
			||||
 | 
						// Action
 | 
				
			||||
 | 
						Wire.beginTransmission(m_address); | 
				
			||||
 | 
						Wire.write( (uint8_t) (settings >> 8) ); // Most significant byte
 | 
				
			||||
 | 
						Wire.write( (uint8_t) (settings & 0xFF) ); // Least significant byte
 | 
				
			||||
 | 
						error = Wire.endTransmission(); | 
				
			||||
 | 
						Serial.print(settings); | 
				
			||||
 | 
						return I2CError(error); // Pass to error parser	
 | 
				
			||||
 | 
					} | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					//-------------------------------------ReadSettings--------------------------------------------------------------------------
 | 
				
			||||
 | 
					uint16_t cDAC101C085::ReadSettings(void) | 
				
			||||
 | 
					{ | 
				
			||||
 | 
						// Vars
 | 
				
			||||
 | 
						uint8_t number = 0; | 
				
			||||
 | 
						uint16_t settings = 0; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
						// Action
 | 
				
			||||
 | 
						number = Wire.requestFrom(m_address, (uint8_t) 2); | 
				
			||||
 | 
						if( number != 2) | 
				
			||||
 | 
						{ | 
				
			||||
 | 
						        Serial.print("DAC: Trying to read 2 bytes from 0x"); | 
				
			||||
 | 
						        Serial.print(m_address, HEX); | 
				
			||||
 | 
						        Serial.println(" went wrong."); | 
				
			||||
 | 
							return 0; | 
				
			||||
 | 
						} | 
				
			||||
 | 
						settings = (uint16_t) Wire.read() << 8; // Most significant byte
 | 
				
			||||
 | 
						settings += (uint16_t) Wire.read(); // Least significant byte
 | 
				
			||||
 | 
						return settings; | 
				
			||||
 | 
					} | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					//-------------------------------------I2CError-------------------------------------------------------------------------------
 | 
				
			||||
 | 
					uint8_t cDAC101C085::I2CError(uint8_t error) | 
				
			||||
 | 
					{ | 
				
			||||
 | 
						switch(error) | 
				
			||||
 | 
						{ | 
				
			||||
 | 
							case 0: | 
				
			||||
 | 
								#if DEBUG_LEVEL == 1 | 
				
			||||
 | 
								Serial.println("DAC: I2C device found. Transmission successful."); | 
				
			||||
 | 
								#endif | 
				
			||||
 | 
								break; | 
				
			||||
 | 
							case 2: | 
				
			||||
 | 
								Serial.print("DAC: Received NACK after address from 0x."); | 
				
			||||
 | 
								Serial.print(m_address, HEX); | 
				
			||||
 | 
								Serial.println("."); | 
				
			||||
 | 
								break; | 
				
			||||
 | 
							case 3: | 
				
			||||
 | 
								Serial.print("DAC: Received NACK after data from 0x."); | 
				
			||||
 | 
								Serial.print(m_address, HEX); | 
				
			||||
 | 
								Serial.println("."); | 
				
			||||
 | 
								break; | 
				
			||||
 | 
							case 4: | 
				
			||||
 | 
								Serial.print("DAC: Unknow error at 0x."); | 
				
			||||
 | 
								Serial.print(m_address, HEX); | 
				
			||||
 | 
								Serial.println("."); | 
				
			||||
 | 
								break; | 
				
			||||
 | 
						}	 | 
				
			||||
 | 
						return ( (error == 0) ? 1 : 0 ); | 
				
			||||
 | 
					} | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					} | 
				
			||||
 | 
					} | 
				
			||||
@ -0,0 +1,48 @@ | 
				
			|||||
 | 
					//----------------------------------------------------------------------------------------------------------------------------
 | 
				
			||||
 | 
					// Project:    	Uppsense
 | 
				
			||||
 | 
					// Name:	dac101c085.h
 | 
				
			||||
 | 
					// Author:	Maximilian Stiefel
 | 
				
			||||
 | 
					// Date:	08.08.2017
 | 
				
			||||
 | 
					//
 | 
				
			||||
 | 
					// Description:
 | 
				
			||||
 | 
					//
 | 
				
			||||
 | 
					//----------------------------------------------------------------------------------------------------------------------------
 | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					#ifndef APP_DAC101C085_H_ | 
				
			||||
 | 
					#define APP_DAC101C085_H_ | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					//-------------------------------------Libraries------------------------------------------------------------------------------
 | 
				
			||||
 | 
					#include <stdint.h> | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					//-------------------------------------Defines--------------------------------------------------------------------------------
 | 
				
			||||
 | 
					#define DEBUG_LEVEL 1 // 1 a lot debugging output, else rare debugging output
 | 
				
			||||
 | 
					#define PD_BITS_POS 14 // Position of power down bits
 | 
				
			||||
 | 
					#define D_BITS_POS 2 // Position of data bits
 | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					//-------------------------------------Namespaces-----------------------------------------------------------------------------
 | 
				
			||||
 | 
					namespace rijnfel { | 
				
			||||
 | 
					namespace dac { | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					//-------------------------------------Enums----------------------------------------------------------------------------------
 | 
				
			||||
 | 
					enum eOpMode {NORMAL, PULL_DOWN_2K5, PULL_DOWN_100K, HIGH_IMPEDANCE}; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					//-------------------------------------cDAC101085-----------------------------------------------------------------------------
 | 
				
			||||
 | 
					class cDAC101C085 | 
				
			||||
 | 
					{ | 
				
			||||
 | 
					public: | 
				
			||||
 | 
						cDAC101C085(uint8_t number, uint8_t i_address); // Constructor
 | 
				
			||||
 | 
						~cDAC101C085(); // Destructor
 | 
				
			||||
 | 
						void checkDev(); // Check if device is available and alive
 | 
				
			||||
 | 
						uint16_t ReadSettings(void); | 
				
			||||
 | 
						uint8_t WriteSettings(uint16_t settings); | 
				
			||||
 | 
						uint8_t changeSettings(enum eOpMode mode, uint16_t val); | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					private:	 | 
				
			||||
 | 
						uint8_t m_number; // Which of the dacs is it?
 | 
				
			||||
 | 
						uint8_t m_address; | 
				
			||||
 | 
						uint8_t I2CError(uint8_t error); | 
				
			||||
 | 
					}; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					} | 
				
			||||
 | 
					} | 
				
			||||
 | 
					#endif /* APP_DAC101C085_H_ */ | 
				
			||||
					Loading…
					
					
				
		Reference in new issue