cb-028 12-bit d/a converter module - jameco electronics · arduino uno. a voltmeter can then be...
TRANSCRIPT
_____________________________________________________________________________________
Rev 1.01
r e v . 1 . 0 1 145 Rt. 46 West E‐mail: sales@electro‐set.net Wayne, NJ 07470 Web: www.Electro‐Set.net
1
CB-028 12-Bit D/A Converter Module
September 04, 2014
Introduction
This application note will show two minimal code set ups to operate the 12-bit D/A converter module CB-028 one with an Arduino board and the other with Cardinal’s CB-026 Microchip microcontroller module.
The CB-028 has two output terminals, ground and the D/A output to which can be connected to a voltmeter or oscilloscope to observe the module’s output.
Arduino
The Arduino example will take consecutive values from an array and output them to the D/A converter. The array is initialized as a sine table. These values are also be presented to the Arduino serial monitor port.
Before proceeding any further, please have the Arduino software install to your computer. Visit Arduino’s user setup guide at http://arduino.cc/en/Guide/windows for step-by-step instructions.
Hardware
Parts List:
Arduino Uno USB type B connector CB-I-022 Cardinal Interface Board CB-028 D/A converter module
_____________________________________________________________________________________
Rev 1.01
r e v . 1 . 0 1 145 Rt. 46 West E‐mail: sales@electro‐set.net Wayne, NJ 07470 Web: www.Electro‐Set.net
2
Connect the components as shown in the picture below
Plug the CB-I-022 interface into the Arduino Uno board and the CB-028 D/A module into the interface’s Electro-Set 6 pin connector. Connect the USB cable between the computer and the Arduino Uno. A voltmeter can then be connected to the two terminals of the CB-028 module.
The computer will supply 5 volts through the USB cable which will be regulated on the Arduino interface board to 3.3 volts to run the CB-028 D/A module. The D/A module has an onboard 2.5 volt reference regulator which we will configure the D/A to use.
Two jumpers on the CB-028 can be used to change the slave address of the D/A. For this code we are using 0x10 as the 7-bit slave address so the two jumper should be populated towards the boards male connector as shown in the picture above.
_____________________________________________________________________________________
Rev 1.01
r e v . 1 . 0 1 145 Rt. 46 West E‐mail: sales@electro‐set.net Wayne, NJ 07470 Web: www.Electro‐Set.net
3
The Software
#include <Wire.h> // Wire I2C library
#define DA_ADDRESS 0x10 // 7-bit I2C DA_ADDRESS of D/A converter
byte da_MSB, da_LSB;
static int sine[] = { // 120 values from the sine table
0x7ff0, 0x86a0, 0x8d50, 0x93f0, 0x9a90, 0xa110, 0xa780, 0xadd0, 0xb400, 0xba10,
0xbff0, 0xc5a0, 0xcb20, 0xd080, 0xd590, 0xda70, 0xdf10, 0xe360, 0xe770, 0xeb40,
0xeec0, 0xf1f0, 0xf4d0, 0xf770, 0xf9a0, 0xfb90, 0xfd20, 0xfe50, 0xff30, 0xffc0,
0xfff0, 0xffc0, 0xff30, 0xfe50, 0xfd20, 0xfb90, 0xf9a0, 0xf770, 0xf4d0, 0xf1f0,
0xeec0, 0xeb40, 0xe770, 0xe360, 0xdf10, 0xda70, 0xd590, 0xd080, 0xcb20, 0xc5a0,
0xbff0, 0xba10, 0xb400, 0xadd0, 0xa780, 0xa110, 0x9a90, 0x93f0, 0x8d50, 0x86a0,
0x7ff0, 0x7940, 0x7290, 0x6bf0, 0x6550, 0x5ed0, 0x5860, 0x5210, 0x4be0, 0x45d0,
0x3ff0, 0x3a40, 0x34c0, 0x2f60, 0x2a50, 0x2570, 0x20d0, 0x1c80, 0x1870, 0x14a0,
0x1120, 0x0df0, 0x0b10, 0x0870, 0x0640, 0x0450, 0x02c0, 0x0190, 0x00b0, 0x0020,
0x0000, 0x0020, 0x00b0, 0x0190, 0x02c0, 0x0450, 0x0640, 0x0870, 0x0b10, 0x0df0,
0x1120, 0x14a0, 0x1870, 0x1c80, 0x20d0, 0x2570, 0x2a50, 0x2f60, 0x34c0, 0x3a40,
0x3ff0, 0x45d0, 0x4be0, 0x5210, 0x5860, 0x5ed0, 0x6550, 0x6bf0, 0x7290, 0x7940
};
int index = 0;
void setup()
{
Serial.begin(9600); // initialize serial monitor to 9600 baud
Serial.println("CB-028 Test"); // welcome message
_____________________________________________________________________________________
Rev 1.01
r e v . 1 . 0 1 145 Rt. 46 West E‐mail: sales@electro‐set.net Wayne, NJ 07470 Web: www.Electro‐Set.net
4
Wire.begin(); // initialize I2C port
Wire.beginTransmission(DA_ADDRESS); // start of I2C transmission to D/A
Wire.write(0x70); // first byte is a command byte, 0x70 selects to use the external reference
Wire.write(0x00); // dummy ignored by D/A
Wire.write(0x00); // dummy ignored by D/A
Wire.endTransmission(); // Complete the I2C transmission
}
void loop()
{
da_MSB=(sine[index]&0xff00)>>8; // select out the MSB part of the 16-bit hex sine value
da_LSB=(sine[index]&0x00ff); // select out the LSB part of the 16-bit hex sine value
Wire.beginTransmission(DA_ADDRESS); // start of I2C transmission to D/A
Wire.write(0x30); // command byte, 0x30 causes a data write and updates D/A output
Wire.write(da_MSB); // write the MSB
Wire.write(da_LSB); // write the LSB
Wire.endTransmission(); // Complete the I2C transmission
Serial.print(da_MSB,HEX);
Serial.print(da_LSB,HEX);
Serial.print(" ");
delay(200); // slow the update rate so a DVM attached to the D/A output can respond
if(++index >= 120) // increment the index, last array index is 119
index = 0; // wrap index back to first array location
}
Code Operation
The D/A module has a 7-bit I2C slave address as 0x10 which we make a #define.
_____________________________________________________________________________________
Rev 1.01
r e v . 1 . 0 1 145 Rt. 46 West E‐mail: sales@electro‐set.net Wayne, NJ 07470 Web: www.Electro‐Set.net
5
The setup() function is called when a sketch starts. The setup code, initializes the serial monitor baud rate at 9600, outputs a welcome message and then initializes the I2C port as a master. The D/A is also initialized to use the external on board reference by writing 0x70 as the command byte, which is the first of 3 bytes in an I2C write to the D/A. See the chip datasheet for more information.
.
After setup has completed sketch causes the function loop() to be repeatedly called.
The variable ‘index’ is used to access through the sine array sequentially. The integer sine value retrieved is split into two byte which are written into the D/A with the command byte 0x30. This command byte causes the two data byte to be written to the D/A input register and the D/A output to be updated with the new value.
A delay of 200mS helps aloow the output from the D/A to be observed which will change between 0 volts for a data code of 0x000 to the reference voltage of 2.5 volts for a data code of 0xfff0.
The index is incremented each time around the loop and tested for a value of greater than or equal to 200, which indicated the index has passed the end of the array and is wrapped back to zero to repeat the sequence again.
As compiled on Arduino 1.0.5 r2 and with the delay(200) and Serial.print lines commented out, the loop function take 432uS to complete. With 120 data points in one sine cycle the output sinusoidal period will be 0.432*120mS or
_____________________________________________________________________________________
Rev 1.01
r e v . 1 . 0 1 145 Rt. 46 West E‐mail: sales@electro‐set.net Wayne, NJ 07470 Web: www.Electro‐Set.net
6
51.8mS.
Microchip Module CB-026
The CB-026 example in PICBASIC PRO will write digital values to the D/A converter to form a saw tooth waveform and also output the values via the serial port to a terminal emulator such as hyperterminal.
Before proceeding any further, please have the MicroCode Studio PICBASIC PRO suite installed on your computer.
_____________________________________________________________________________________
Rev 1.01
r e v . 1 . 0 1 145 Rt. 46 West E‐mail: sales@electro‐set.net Wayne, NJ 07470 Web: www.Electro‐Set.net
7
Hardware
Parts List:
Electro-Set module CB-026 microcontroller USB type mini connector CB-028 12-bit D/A converter module
Connect the components as shown in the picture below
The Software
INCLUDE "modedefs.bas"
#CONFIG __config _HS_OSC & _WDT_ON & _LVP_OFF & _CP_OFF
_____________________________________________________________________________________
Rev 1.01
r e v . 1 . 0 1 145 Rt. 46 West E‐mail: sales@electro‐set.net Wayne, NJ 07470 Web: www.Electro‐Set.net
8
#ENDCONFIG
DEFINE OSC 20 ‘ Set for 20 MHz oscillator
SCL var PORTC.3 ‘ Set I2C pins
SDA var PORTC.4
Soutpin var PORTC.6 ‘ Set serial port pins
Sinpin var PORTC.7
baud1 var byte ‘ for serial port baud rate
testword var word ‘ 16-bit variable, for D/A values
baud1 = 6 ‘ 38400 baud, no inversion
Write_DA:
i2cwrite sda,scl,$20,[$70],nodata ‘ initialize D-A & Checks if module is connected'
' if not connected, goto nodata
Serout2 soutpin,baud1,["Writing to D-A",13,10] ‘ welcome message
serout2 soutpin,baud1,["Creating a 0 to 3.3 volt ramp function",13,10]
pause 2000 ‘ wait 2 seconds
for testword = $0010 to $AF00 step $FF ‘ increase testword for each loop
i2cwrite sda,scl,$20,[$30,testword.byte1,testword.byte0],nodata ‘ write testword to D/A
serout2 soutpin,baud1,["Set for ",dec testword/13500,".",dec2 (testword /135)," Volts",13,10]
pause 100
next testword
serout2 soutpin,baud1,["Done",13,10] ‘ report completed for loop iteration
pause 1000 ‘ wait 1 second
goto Write_DA
NoData:
_____________________________________________________________________________________
Rev 1.01
r e v . 1 . 0 1 145 Rt. 46 West E‐mail: sales@electro‐set.net Wayne, NJ 07470 Web: www.Electro‐Set.net
9
Serout2 soutpin,baud1,["D-A Module Not Connected",13,10] ‘ report D/A not responding
pause 500
goto Write_DA
end
Code Operation
The line
“# CONFIG __config _HS_OSC & _WDT_ON & _LVP_OFF & _CP_OFF”
includes configuration for the programmer in the output hex file so it does not necessary to manually change the entries in the programmer configuration window every time you come to write a new version of your program to the microcontroller.
The port pins for the I2C interface are set on PORTC pins 3 and 4. The CB-026 board wires these to the Electro-Set 6 pin connector.
The microcontroller serial port pins are set on PORTC pins 6 and 7, these connect to the FTDI chip on the CB-026 which couples through to the USB and so when plugged into a PC producing a virtual COMM port where the microcontroller’s serial data can be seen on a terminal emulator such as hyperterminal. The terminal emulator should be set to connect to the virtual COMM port assigned to the FTDI driver and connect at 38400,8,N,1.
We declare the variable baud1 for use in the serout2 to set the baud rate. For a baud rate of 38400, baud1 is set to 6, see the MicroChip documentation for other rates and options.
The first parameters for the PicBasicPro functions i2cread and i2cwrite uses and 8-bit slave address which is a one bit left shift of the 7-bit slave address, so the D/A module’s 8-bit slave address is 0x20.
The line
i2cwrite sda,scl,$20,[$70],nodata
initializes the D/A to use its external reference pin, which is supplied by the 2.5 volt regulator on the CB-028 module.
Next the welcome messages are sent out the serial port.
The for loop causes the variable ‘testword’ to increase from low to high. On each iteration of the for loop the value of ‘testword’ is written to the D/A’s internal register address 0x30 causing the D/A’s output to change accordingly, rising from low to high.
_____________________________________________________________________________________
Rev 1.01
r e v . 1 . 0 1 145 Rt. 46 West E‐mail: sales@electro‐set.net Wayne, NJ 07470 Web: www.Electro‐Set.net
10
i2cwrite sda,scl,$20,[$30,testword.byte1,testword.byte0],nodata
Also on each iteration a decimal representation of ‘testword’ is output to the serial port.
A scope connected to the D/A output shows the saw tooth waveform.