top of page

MCP4725, 12bit DAC TWI 사용하기

  • 작성자 사진: 홍근 정
    홍근 정
  • 2015년 11월 3일
  • 2분 분량

// atmega128로 구현한 i2c DAC 쓰기

// MCP4725.C

//++++++++++++++++++++++++++++++++++++++++++++

#ifndef _MCP4725_C_ #define _MCP4725_C_

#include "MCP4725.H"

void Tx_TWI(byte Command_Type, word w){ twi_send(Command_Type); twi_send((byte)(w>>4)); twi_send((byte)(w<<4)); }

byte MCP4725_DAC_Register_TWI(word DAC, byte Choice_I2C, byte Command_Type){ twi_start();

if(twi_send(_MCP4725_Write_)){ TWCR = 0; return 1; } Tx_TWI(Command_Type, DAC);

if(Command_Type == _DAC_and_EEPROM_) Tx_TWI(Command_Type, DAC); twi_stop();

TWCR = 0; Delay_xMs(1); return 0; }

byte MCP4725_DAC_EEPROM(word DAC, byte Choice_I2C, byte Command_Type){ byte error=0; startCommunication(Choice_I2C);

if (error = sendByte(_MCP4725_Write_, Choice_I2C)){ stopCommunication(Choice_I2C); return error; }

if ( error = sendByte(Command_Type, Choice_I2C )){ stopCommunication(Choice_I2C); return error; }

if (error = sendByte(((byte)(DAC>>4)), Choice_I2C)){ stopCommunication(Choice_I2C); return error; }

if (error = sendByte(((byte)(DAC<<4)), Choice_I2C)){ stopCommunication(Choice_I2C); return error; } /////////////////////////////////////////////////////////////////////////////////////////////// if(Command_Type == _DAC_and_EEPROM_){ if ( error = sendByte(Command_Type, Choice_I2C )){ stopCommunication(Choice_I2C); return error; } if (error = sendByte(((byte)(DAC>>4)), Choice_I2C)){ stopCommunication(Choice_I2C); return error; } if (error = sendByte(((byte)(DAC<<4)), Choice_I2C)){ stopCommunication(Choice_I2C); return error; } }

stopCommunication(Choice_I2C); return 0; }

void TWI_GPIO_DAC_Test(void){ // GPIO를 이용한 TWI DAC 구현 while(1){ if(_g_DAC < 0x0fff) _g_DAC++; else _g_DAC = 0;

// MCP4725_FastMode(_g_DAC, _MCP4725_DAC_); MCP4725_DAC_EEPROM(_g_DAC, _MCP4725_DAC_, _DAC_and_EEPROM_); _WDR(); Delay_xMs(1); } }

void TWI_Register_DAC_Test(byte CMD){ // 레지스터를 이용한 TWI DAC 구현 while(1){ if(_g_DAC < 0x0fff) _g_DAC++; else _g_DAC = 0;

_g_i = MCP4725_DAC_Register_TWI(_g_DAC, _MCP4725_DAC_, CMD); Delay_xMs(1); } }

byte MCP4725_FastMode(word DAC, byte Choice_I2C){ byte error=0; startCommunication(Choice_I2C);

if (error = sendByte(_MCP4725_Write_, Choice_I2C)){ stopCommunication(Choice_I2C); return error; }

if (error = sendByte((_MCP4725_PowerDownNormal_<<4) + ((byte)(DAC>>8)), Choice_I2C)){ stopCommunication(Choice_I2C); return error; }

if (error = sendByte(((byte)(DAC&0x00ff)), Choice_I2C)){ stopCommunication(Choice_I2C); return error; }

if (error = sendByte(_MCP4725_PowerDownNormal_ + ((byte)(DAC>>8)), Choice_I2C)){ stopCommunication(Choice_I2C); return error; } ///////////////////////////////////////////////////////////////////////////////////// if (error = sendByte(((byte)(DAC&0x00ff)), Choice_I2C)){ stopCommunication(Choice_I2C); return error; }

stopCommunication(Choice_I2C); return 0; }

#endif //--------------------------------------------------------------------------------------

// MCP4725.H

//++++++++++++++++++++++++++++++++++++++++++++

#ifndef _MCP4725_H_ #define _MCP4725_H_

extern signed int _g_DAC; extern byte _g_i;

#define _MCP4725_DAC_ 3

#define _MCP4725_Write_ 0xC0 #define _MCP4725_Read_ 0xC1 #define _MCP4725_PowerDownNormal_ 0 #define _MCP4725_PowerDown_1k_ 1 #define _MCP4725_PowerDown_100k_ 2 #define _MCP4725_PowerDown_500K_ 3

#define _DAC_Only_ 0x40 #define _DAC_and_EEPROM_ 0x60

byte MCP4725_DAC_Register_TWI(word DAC, byte Choice_I2C, byte Command_Type); byte MCP4725_DAC_EEPROM(word DAC, byte Choice_I2C, byte Command_Type); void TWI_GPIO_DAC_Test(void); void TWI_Register_DAC_Test(byte CMD); byte MCP4725_FastMode(word DAC, byte Choice_I2C); void Tx_TWI(byte Command_Type, word w);

extern byte twi_start(void); extern void twi_stop(void); extern byte twi_send(byte data); extern void startCommunication(byte cs); extern void stopCommunication(byte ch); extern byte sendByte(char b, byte ch); extern void Delay_xMs(word i);

#endif //--------------------------------------------------------------------------------------

 
 
 

Comments


bottom of page