C Library for mbedWSE project based single board computer for hardware peripherals

Dependents:   Lab7_wse Lab7_wse_student mbed_WSEPRJSBC_EncoderTest mbed_WSEPRJSBC_ADC_test ... more

Weapons and Systems Engineering mbed based single board computer

/media/uploads/jebradshaw/mbedwseprojectsbc_final_small.jpg

Contains C library function listing for hardware peripheral support. Includes functions for +/-10V 12-bit 8-Channel analog-to-digital converter (ADC), 10-bit 2-Channel digital-to-analog converter (DAC), motor control/servo ports, and quadrature encoder ports. Board includes physical Ethernet, USB Host, and CAN bus interfaces. Also includes xbee socket and external I/O pins straight to processor for on-board I2C, SPI, ADC, and DAC.

Schematic

/media/uploads/jebradshaw/mbed_wseprojsbc_schematic_20150211.jpg

Description

This single board computer was designed for the Weapons and Systems Engineering department at the US Naval Academy. The board encompasses a multitude of functional capability for interfacing to a variety of sensors and actuators used in embedded control systems. The board can be powered from a 7-36V DC / AC power supply using a 2.1mm standard barrel connector. The on board bridge rectifier composed of individual diodes allows either polarity on the connector power supply. The four primary external integrated circuits on the board all use the SPI bus for communication. The four primary components are the analog to digital converter, the digital to analog converter, and the two dedicated encoder/counter IC's. The encoder/counter IC's off-load the micro-processor from the burden of external interrupts during high frequency edge detection. The IC's offer 32-bit resolution counting/quadrature decoding, rollover and status information, and index detection for absolute position encoder interfacing. The MAX1270 analog to digital converter (ADC) was chosen for its internal reference and bi-polar voltage input range and scaling program-ability (0-5V, 0-10V, +/-5V, +/-10V). The ADC provides 12-bit resolution at these voltage ranges and are individually channel select-able. Note that careful attention must be used when driving the ADC inputs when in Bipolar operation. An Op-Amp with insufficient current drive capability (milli-amps) will be unable to overcome the internal voltage reference on the input pin due to impedance mismatch/ output current drive capability. This should be checked with an oscilloscope on the input pin if discrepancies between measured voltage values and ADC results exist.

The MAX522 DAC provides 0-5V 10-bit voltage resolution. The range is dependent on the selected 5V board voltage reference source (external 5V switching regulator vs. USB power supply). The board power supply 5V reference can be read by connecting the supply to an external ADC channel and reading the voltage on the 0-10V range.

/media/uploads/jebradshaw/mbed_wseprojsbc_schematic_20140922.pdf - Schematic in PDF

Test Program

/media/uploads/jebradshaw/main_mbedwsesbctest.cpp - Program for testing the library, control the SBC with serial commands

http://developer.mbed.org/users/jebradshaw/code/mbed_WSEPRJSBC_EncoderTest/ - Program for testing the Quadrature Encoder Channels

http://developer.mbed.org/users/jebradshaw/code/mbed_WSEPRJSBC_ADC_test/ - Program to read two channels from the MAX1270 ADC in +/- 10V operation.

http://mbed.org/handbook/Windows-serial-configuration - Link to Serial Driver Installation instructions

Datasheets

/media/uploads/jebradshaw/oki-78sr.pdf - 5V switching regulator

/media/uploads/jebradshaw/ls7366r_w_program.pdf - Encoder IC's

/media/uploads/jebradshaw/max1270.pdf - ADC 8-Channel 12-bit 5V to 10V single/bi-polar input

/media/uploads/jebradshaw/max522.pdf - DAC 2-Channel 0-5V 10-bit

/media/uploads/jebradshaw/mcp2551_can_transceiver.pdf - CAN Transceiver

/media/uploads/jebradshaw/0821-1x1t-36-f.pdf - Ethernet Jack

/media/uploads/jebradshaw/190-009-263r001.pdf - CAN connector

Printed Circuit Board (ExpressPCB)

/media/uploads/jebradshaw/mbedprjv10_20140916.pcb

Partslist (partial)

/media/uploads/jebradshaw/mbedprjv10_partslist.pdf

Committer:
jebradshaw
Date:
Mon Nov 03 20:02:00 2014 +0000
Revision:
8:b74ce788d87e
Parent:
1:85cd6d500385
Removed PWM period decleration from SBC Init() function.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jebradshaw 8:b74ce788d87e 1 /* C Library for the WSE-PROJ-SBC
jebradshaw 8:b74ce788d87e 2 J Bradshaw
jebradshaw 8:b74ce788d87e 3 20140912
jebradshaw 8:b74ce788d87e 4 20140918 J Bradshaw - Found CS mistake in Encoder routines
jebradshaw 8:b74ce788d87e 5 Added comments in Init function, encoder functions
jebradshaw 8:b74ce788d87e 6 */
jebradshaw 0:dbd8b5c35d0f 7
jebradshaw 8:b74ce788d87e 8
jebradshaw 8:b74ce788d87e 9 // LS7366 ENCODER IC DEFINITIONS
jebradshaw 0:dbd8b5c35d0f 10 //=============================================================================
jebradshaw 0:dbd8b5c35d0f 11 // Four commands for the Instruction Register (B7,B6) - LS7366
jebradshaw 0:dbd8b5c35d0f 12 //=============================================================================
jebradshaw 0:dbd8b5c35d0f 13 #define CLR 0x00 //Clear Instruction
jebradshaw 0:dbd8b5c35d0f 14 #define RD 0x01 //Read Instruction
jebradshaw 0:dbd8b5c35d0f 15 #define WR 0x02 //Write Instruction
jebradshaw 0:dbd8b5c35d0f 16 #define LOAD 0x03 //Load Instruction
jebradshaw 0:dbd8b5c35d0f 17
jebradshaw 0:dbd8b5c35d0f 18 //=============================================================================
jebradshaw 0:dbd8b5c35d0f 19 // Register to Select from the Instruction Register (B5,B4,B3) - LS7366
jebradshaw 0:dbd8b5c35d0f 20 //=============================================================================
jebradshaw 0:dbd8b5c35d0f 21 #define NONE 0x00 //No Register Selected
jebradshaw 0:dbd8b5c35d0f 22 #define MDR0 0x01 //Mode Register 0
jebradshaw 0:dbd8b5c35d0f 23 #define MDR1 0x02 //Mode Register 1
jebradshaw 0:dbd8b5c35d0f 24 #define DTR 0x03 //Data Transfer Register
jebradshaw 0:dbd8b5c35d0f 25 #define CNTR 0x04 //Software Configurable Counter Register
jebradshaw 0:dbd8b5c35d0f 26 #define OTR 0x05 //Output Transfer Register
jebradshaw 0:dbd8b5c35d0f 27 #define STR 0x06 //Status Register
jebradshaw 0:dbd8b5c35d0f 28 #define NONE_REG 0x07 //No Register Selected
jebradshaw 0:dbd8b5c35d0f 29
jebradshaw 0:dbd8b5c35d0f 30 // Set-up hardwired IO
jebradshaw 0:dbd8b5c35d0f 31 SPI spi_max1270(p5, p6, p7);
jebradshaw 0:dbd8b5c35d0f 32 SPI spi(p5, p6, p7);
jebradshaw 8:b74ce788d87e 33 DigitalOut max1270_cs(p8); //CS for MAX1270 ADC (U3)
jebradshaw 8:b74ce788d87e 34 DigitalOut max522_cs(p11); //CS for MAX522 DAC (U5)
jebradshaw 0:dbd8b5c35d0f 35
jebradshaw 8:b74ce788d87e 36 DigitalOut ls7166_cs1(p19); //CS for LS7366-1 (U8)
jebradshaw 8:b74ce788d87e 37 DigitalOut ls7166_cs2(p20); //CS for LS7366-2 (U9)
jebradshaw 0:dbd8b5c35d0f 38
jebradshaw 8:b74ce788d87e 39 DigitalOut mot1_ph1(p21);
jebradshaw 0:dbd8b5c35d0f 40 DigitalOut mot1_ph2(p22);
jebradshaw 0:dbd8b5c35d0f 41 PwmOut mot_en1(p23);
jebradshaw 0:dbd8b5c35d0f 42
jebradshaw 0:dbd8b5c35d0f 43 DigitalOut mot2_ph1(p24);
jebradshaw 0:dbd8b5c35d0f 44 DigitalOut mot2_ph2(p25);
jebradshaw 0:dbd8b5c35d0f 45 PwmOut mot_en2(p26);
jebradshaw 0:dbd8b5c35d0f 46
jebradshaw 0:dbd8b5c35d0f 47 DigitalOut led1(LED1);
jebradshaw 0:dbd8b5c35d0f 48 DigitalOut led2(LED2);
jebradshaw 0:dbd8b5c35d0f 49 DigitalOut led3(LED3);
jebradshaw 0:dbd8b5c35d0f 50 DigitalOut led4(LED4);
jebradshaw 0:dbd8b5c35d0f 51
jebradshaw 0:dbd8b5c35d0f 52 Serial pc(USBTX, USBRX); // tx, rx for serial USB interface to pc
jebradshaw 0:dbd8b5c35d0f 53 Serial xbee(p13, p14); // tx, rx for Xbee
jebradshaw 0:dbd8b5c35d0f 54 Timer t; // create timer instance
jebradshaw 0:dbd8b5c35d0f 55
jebradshaw 0:dbd8b5c35d0f 56 // ------ Prototypes -----------
jebradshaw 0:dbd8b5c35d0f 57 int read_max1270(int chan, int range, int bipol);
jebradshaw 0:dbd8b5c35d0f 58 float read_max1270_volts(int chan, int range, int bipol);
jebradshaw 0:dbd8b5c35d0f 59 void mot_control(int drv_num, float dc);
jebradshaw 0:dbd8b5c35d0f 60 void LS7366_cmd(int inst, int reg);
jebradshaw 0:dbd8b5c35d0f 61 long LS7366_read_counter(int chan_num);
jebradshaw 0:dbd8b5c35d0f 62 void LS7366_quad_mode_x4(int chan_num);
jebradshaw 0:dbd8b5c35d0f 63 void LS7366_reset_counter(int chan_num);
jebradshaw 0:dbd8b5c35d0f 64 void LS7366_write_DTR(int chan_num,long enc_value);
jebradshaw 0:dbd8b5c35d0f 65 void write_max522(int chan, float volts);
jebradshaw 0:dbd8b5c35d0f 66
jebradshaw 0:dbd8b5c35d0f 67 //---- Function Listing -------------------------------
jebradshaw 0:dbd8b5c35d0f 68 int read_max1270(int chan, int range, int bipol){
jebradshaw 0:dbd8b5c35d0f 69 int cword=0x80; //set the start bit
jebradshaw 0:dbd8b5c35d0f 70
jebradshaw 0:dbd8b5c35d0f 71 spi_max1270.frequency(10000000);
jebradshaw 0:dbd8b5c35d0f 72 spi_max1270.format(8, 0); // 8 data bits, CPOL0, and CPHA0 (datasheet Digital Interface)
jebradshaw 0:dbd8b5c35d0f 73
jebradshaw 0:dbd8b5c35d0f 74 cword |= (chan << 4); //shift channel
jebradshaw 0:dbd8b5c35d0f 75 cword |= (range << 3);
jebradshaw 0:dbd8b5c35d0f 76 cword |= (bipol << 2);
jebradshaw 0:dbd8b5c35d0f 77
jebradshaw 0:dbd8b5c35d0f 78 max1270_cs = 0;
jebradshaw 0:dbd8b5c35d0f 79
jebradshaw 0:dbd8b5c35d0f 80 spi_max1270.write(cword);
jebradshaw 0:dbd8b5c35d0f 81 wait_us(15); //15us
jebradshaw 0:dbd8b5c35d0f 82 spi_max1270.format(12, 3);
jebradshaw 0:dbd8b5c35d0f 83
jebradshaw 0:dbd8b5c35d0f 84 int result = spi_max1270.write(0);
jebradshaw 0:dbd8b5c35d0f 85
jebradshaw 0:dbd8b5c35d0f 86 max1270_cs = 1;
jebradshaw 0:dbd8b5c35d0f 87 spi_max1270.format(8, 0);
jebradshaw 0:dbd8b5c35d0f 88 return result;
jebradshaw 0:dbd8b5c35d0f 89 }
jebradshaw 0:dbd8b5c35d0f 90
jebradshaw 0:dbd8b5c35d0f 91 float read_max1270_volts(int chan, int range, int bipol){
jebradshaw 0:dbd8b5c35d0f 92 float rangevolts=0.0;
jebradshaw 0:dbd8b5c35d0f 93 float volts=0.0;
jebradshaw 0:dbd8b5c35d0f 94 int adc_res;
jebradshaw 0:dbd8b5c35d0f 95
jebradshaw 0:dbd8b5c35d0f 96 //read the ADC converter
jebradshaw 0:dbd8b5c35d0f 97 adc_res = read_max1270(chan, range, bipol) & 0xFFF;
jebradshaw 0:dbd8b5c35d0f 98
jebradshaw 0:dbd8b5c35d0f 99 //Determine the voltage range
jebradshaw 0:dbd8b5c35d0f 100 if(range) //RNG bit
jebradshaw 0:dbd8b5c35d0f 101 rangevolts=10.0;
jebradshaw 0:dbd8b5c35d0f 102 else
jebradshaw 0:dbd8b5c35d0f 103 rangevolts=5.0;
jebradshaw 0:dbd8b5c35d0f 104
jebradshaw 0:dbd8b5c35d0f 105 //bi-polar input range
jebradshaw 0:dbd8b5c35d0f 106 if(bipol){ //BIP is set, input is +/-
jebradshaw 0:dbd8b5c35d0f 107 if(adc_res < 0x800){ //if result was positive
jebradshaw 0:dbd8b5c35d0f 108 volts = ((float)adc_res/0x7FF) * rangevolts;
jebradshaw 0:dbd8b5c35d0f 109 }
jebradshaw 0:dbd8b5c35d0f 110 else{ //result was negative
jebradshaw 0:dbd8b5c35d0f 111 volts = -(-((float)adc_res/0x7FF) * rangevolts) - (rangevolts * 2.0);
jebradshaw 0:dbd8b5c35d0f 112 }
jebradshaw 0:dbd8b5c35d0f 113 }
jebradshaw 0:dbd8b5c35d0f 114 else{ //input is positive polarity only
jebradshaw 0:dbd8b5c35d0f 115 volts = ((float)adc_res/0xFFF) * rangevolts;
jebradshaw 0:dbd8b5c35d0f 116 }
jebradshaw 0:dbd8b5c35d0f 117
jebradshaw 0:dbd8b5c35d0f 118 return volts;
jebradshaw 0:dbd8b5c35d0f 119 }
jebradshaw 0:dbd8b5c35d0f 120
jebradshaw 0:dbd8b5c35d0f 121 //Motor control routine for PWM on 5 pin motor driver header
jebradshaw 0:dbd8b5c35d0f 122 // drv_num is 1 or 2 (defaults to 1, anything but 2)
jebradshaw 0:dbd8b5c35d0f 123 // dc is signed duty cycle (+/-1.0)
jebradshaw 0:dbd8b5c35d0f 124
jebradshaw 0:dbd8b5c35d0f 125 void mot_control(int drv_num, float dc){
jebradshaw 0:dbd8b5c35d0f 126 if(dc>1.0)
jebradshaw 0:dbd8b5c35d0f 127 dc=1.0;
jebradshaw 0:dbd8b5c35d0f 128 if(dc<-1.0)
jebradshaw 0:dbd8b5c35d0f 129 dc=-1.0;
jebradshaw 0:dbd8b5c35d0f 130
jebradshaw 0:dbd8b5c35d0f 131 if(drv_num != 2){
jebradshaw 0:dbd8b5c35d0f 132 if(dc > 0.0){
jebradshaw 0:dbd8b5c35d0f 133 mot1_ph2 = 0;
jebradshaw 0:dbd8b5c35d0f 134 mot1_ph1 = 1;
jebradshaw 0:dbd8b5c35d0f 135 mot_en1 = dc;
jebradshaw 0:dbd8b5c35d0f 136 }
jebradshaw 0:dbd8b5c35d0f 137 else if(dc < -0.0){
jebradshaw 0:dbd8b5c35d0f 138 mot1_ph1 = 0;
jebradshaw 0:dbd8b5c35d0f 139 mot1_ph2 = 1;
jebradshaw 0:dbd8b5c35d0f 140 mot_en1 = abs(dc);
jebradshaw 0:dbd8b5c35d0f 141 }
jebradshaw 0:dbd8b5c35d0f 142 else{
jebradshaw 0:dbd8b5c35d0f 143 mot1_ph1 = 0;
jebradshaw 0:dbd8b5c35d0f 144 mot1_ph2 = 0;
jebradshaw 0:dbd8b5c35d0f 145 mot_en1 = 0.0;
jebradshaw 0:dbd8b5c35d0f 146 }
jebradshaw 0:dbd8b5c35d0f 147 }
jebradshaw 0:dbd8b5c35d0f 148 else{
jebradshaw 0:dbd8b5c35d0f 149 if(dc > 0.0){
jebradshaw 0:dbd8b5c35d0f 150 mot2_ph2 = 0;
jebradshaw 0:dbd8b5c35d0f 151 mot2_ph1 = 1;
jebradshaw 0:dbd8b5c35d0f 152 mot_en2 = dc;
jebradshaw 0:dbd8b5c35d0f 153 }
jebradshaw 0:dbd8b5c35d0f 154 else if(dc < -0.0){
jebradshaw 0:dbd8b5c35d0f 155 mot2_ph1 = 0;
jebradshaw 0:dbd8b5c35d0f 156 mot2_ph2 = 1;
jebradshaw 0:dbd8b5c35d0f 157 mot_en2 = abs(dc);
jebradshaw 0:dbd8b5c35d0f 158 }
jebradshaw 0:dbd8b5c35d0f 159 else{
jebradshaw 0:dbd8b5c35d0f 160 mot2_ph1 = 0;
jebradshaw 0:dbd8b5c35d0f 161 mot2_ph2 = 0;
jebradshaw 0:dbd8b5c35d0f 162 mot_en2 = 0.0;
jebradshaw 0:dbd8b5c35d0f 163 }
jebradshaw 0:dbd8b5c35d0f 164 }
jebradshaw 0:dbd8b5c35d0f 165 }
jebradshaw 0:dbd8b5c35d0f 166
jebradshaw 0:dbd8b5c35d0f 167 //----- LS7366 Encoder/Counter Routines --------------------
jebradshaw 0:dbd8b5c35d0f 168 void LS7366_cmd(int inst, int reg){
jebradshaw 0:dbd8b5c35d0f 169 char cmd;
jebradshaw 0:dbd8b5c35d0f 170
jebradshaw 0:dbd8b5c35d0f 171 spi.format(8, 0);
jebradshaw 0:dbd8b5c35d0f 172 spi.frequency(2000000);
jebradshaw 0:dbd8b5c35d0f 173 cmd = (inst << 6) | (reg << 3);
jebradshaw 0:dbd8b5c35d0f 174 // printf("\r\ncmd=0X%2X", cmd);
jebradshaw 0:dbd8b5c35d0f 175 spi.write(cmd);
jebradshaw 0:dbd8b5c35d0f 176 }
jebradshaw 0:dbd8b5c35d0f 177
jebradshaw 0:dbd8b5c35d0f 178 long LS7366_read_counter(int chan_num){
jebradshaw 0:dbd8b5c35d0f 179 union bytes{
jebradshaw 0:dbd8b5c35d0f 180 char byte_enc[4];
jebradshaw 0:dbd8b5c35d0f 181 long long_enc;
jebradshaw 0:dbd8b5c35d0f 182 }counter;
jebradshaw 0:dbd8b5c35d0f 183
jebradshaw 0:dbd8b5c35d0f 184 counter.long_enc = 0;
jebradshaw 0:dbd8b5c35d0f 185
jebradshaw 0:dbd8b5c35d0f 186 spi.format(8, 0);
jebradshaw 0:dbd8b5c35d0f 187 spi.frequency(2000000);
jebradshaw 0:dbd8b5c35d0f 188
jebradshaw 0:dbd8b5c35d0f 189 if(chan_num!=2){
jebradshaw 0:dbd8b5c35d0f 190 ls7166_cs1 = 0;
jebradshaw 0:dbd8b5c35d0f 191 wait_us(1);
jebradshaw 0:dbd8b5c35d0f 192 LS7366_cmd(LOAD,OTR);//cmd = 0xe8, LOAD to OTR
jebradshaw 0:dbd8b5c35d0f 193 ls7166_cs1 = 1;
jebradshaw 0:dbd8b5c35d0f 194 wait_us(1);
jebradshaw 0:dbd8b5c35d0f 195 ls7166_cs1 = 0;
jebradshaw 0:dbd8b5c35d0f 196 }
jebradshaw 0:dbd8b5c35d0f 197 else{
jebradshaw 0:dbd8b5c35d0f 198 ls7166_cs2 = 0;
jebradshaw 0:dbd8b5c35d0f 199 wait_us(1);
jebradshaw 0:dbd8b5c35d0f 200 LS7366_cmd(LOAD,OTR);//cmd = 0xe8, LOAD to OTR
jebradshaw 0:dbd8b5c35d0f 201 ls7166_cs2 = 1;
jebradshaw 0:dbd8b5c35d0f 202 wait_us(1);
jebradshaw 0:dbd8b5c35d0f 203
jebradshaw 0:dbd8b5c35d0f 204 ls7166_cs2 = 0;
jebradshaw 0:dbd8b5c35d0f 205 }
jebradshaw 0:dbd8b5c35d0f 206 wait_us(1);
jebradshaw 0:dbd8b5c35d0f 207 LS7366_cmd(RD,CNTR); //cmd = 0x60, READ from CNTR
jebradshaw 0:dbd8b5c35d0f 208 counter.byte_enc[3] = spi.write(0x00);
jebradshaw 0:dbd8b5c35d0f 209 counter.byte_enc[2] = spi.write(0x00);
jebradshaw 0:dbd8b5c35d0f 210 counter.byte_enc[1] = spi.write(0x00);
jebradshaw 0:dbd8b5c35d0f 211 counter.byte_enc[0] = spi.write(0x00);
jebradshaw 0:dbd8b5c35d0f 212
jebradshaw 0:dbd8b5c35d0f 213 if(chan_num!=2){
jebradshaw 0:dbd8b5c35d0f 214 ls7166_cs1 = 1;
jebradshaw 0:dbd8b5c35d0f 215 }
jebradshaw 0:dbd8b5c35d0f 216 else{
jebradshaw 0:dbd8b5c35d0f 217 ls7166_cs2 = 1;
jebradshaw 0:dbd8b5c35d0f 218 }
jebradshaw 0:dbd8b5c35d0f 219
jebradshaw 0:dbd8b5c35d0f 220 return counter.long_enc; //return count
jebradshaw 0:dbd8b5c35d0f 221 }
jebradshaw 0:dbd8b5c35d0f 222
jebradshaw 0:dbd8b5c35d0f 223 void LS7366_quad_mode_x4(int chan_num){
jebradshaw 0:dbd8b5c35d0f 224
jebradshaw 0:dbd8b5c35d0f 225 spi.format(8, 0);
jebradshaw 0:dbd8b5c35d0f 226 spi.frequency(2000000);
jebradshaw 0:dbd8b5c35d0f 227
jebradshaw 0:dbd8b5c35d0f 228 if(chan_num!=2){
jebradshaw 0:dbd8b5c35d0f 229 ls7166_cs1 = 0;
jebradshaw 0:dbd8b5c35d0f 230 }
jebradshaw 0:dbd8b5c35d0f 231 else{
jebradshaw 0:dbd8b5c35d0f 232 ls7166_cs2 = 0;
jebradshaw 0:dbd8b5c35d0f 233 }
jebradshaw 0:dbd8b5c35d0f 234 wait_us(1);
jebradshaw 0:dbd8b5c35d0f 235 LS7366_cmd(WR,MDR0);// Write to the MDR0 register
jebradshaw 8:b74ce788d87e 236 wait_us(1);
jebradshaw 0:dbd8b5c35d0f 237 spi.write(0x03); // X4 quadrature count mode
jebradshaw 0:dbd8b5c35d0f 238 if(chan_num!=2){
jebradshaw 0:dbd8b5c35d0f 239 ls7166_cs1 = 1;
jebradshaw 0:dbd8b5c35d0f 240 }
jebradshaw 0:dbd8b5c35d0f 241 else{
jebradshaw 0:dbd8b5c35d0f 242 ls7166_cs2 = 1;
jebradshaw 0:dbd8b5c35d0f 243 }
jebradshaw 0:dbd8b5c35d0f 244 }
jebradshaw 0:dbd8b5c35d0f 245
jebradshaw 8:b74ce788d87e 246 void LS7366_reset_counter(int chan_num){
jebradshaw 8:b74ce788d87e 247 spi.format(8, 0); // set up SPI for 8 data bits, mode 0
jebradshaw 8:b74ce788d87e 248 spi.frequency(2000000); // 2MHz SPI clock
jebradshaw 0:dbd8b5c35d0f 249
jebradshaw 8:b74ce788d87e 250 if(chan_num!=2){ // activate chip select
jebradshaw 0:dbd8b5c35d0f 251 ls7166_cs1 = 0;
jebradshaw 0:dbd8b5c35d0f 252 }
jebradshaw 0:dbd8b5c35d0f 253 else{
jebradshaw 0:dbd8b5c35d0f 254 ls7166_cs2 = 0;
jebradshaw 0:dbd8b5c35d0f 255 }
jebradshaw 8:b74ce788d87e 256 wait_us(1); // short delay
jebradshaw 8:b74ce788d87e 257 LS7366_cmd(CLR,CNTR); // Clear the counter register
jebradshaw 8:b74ce788d87e 258 if(chan_num!=2){ // de-activate chip select
jebradshaw 0:dbd8b5c35d0f 259 ls7166_cs1 = 1;
jebradshaw 0:dbd8b5c35d0f 260 }
jebradshaw 0:dbd8b5c35d0f 261 else{
jebradshaw 8:b74ce788d87e 262 ls7166_cs2 = 1;
jebradshaw 0:dbd8b5c35d0f 263 }
jebradshaw 8:b74ce788d87e 264 wait_us(1); // short delay
jebradshaw 0:dbd8b5c35d0f 265
jebradshaw 8:b74ce788d87e 266 if(chan_num!=2){ // activate chip select
jebradshaw 0:dbd8b5c35d0f 267 ls7166_cs1 = 0;
jebradshaw 0:dbd8b5c35d0f 268 }
jebradshaw 0:dbd8b5c35d0f 269 else{
jebradshaw 0:dbd8b5c35d0f 270 ls7166_cs2 = 0;
jebradshaw 0:dbd8b5c35d0f 271 }
jebradshaw 8:b74ce788d87e 272 wait_us(1); // short delay
jebradshaw 8:b74ce788d87e 273 LS7366_cmd(LOAD,CNTR); // load counter reg
jebradshaw 8:b74ce788d87e 274 if(chan_num!=2){ // de-activate chip select
jebradshaw 0:dbd8b5c35d0f 275 ls7166_cs1 = 1;
jebradshaw 0:dbd8b5c35d0f 276 }
jebradshaw 0:dbd8b5c35d0f 277 else{
jebradshaw 8:b74ce788d87e 278 ls7166_cs2 = 1;
jebradshaw 0:dbd8b5c35d0f 279 }
jebradshaw 0:dbd8b5c35d0f 280 }
jebradshaw 0:dbd8b5c35d0f 281
jebradshaw 8:b74ce788d87e 282 void LS7366_write_DTR(int chan_num, long enc_value){
jebradshaw 8:b74ce788d87e 283 union bytes // Union to speed up byte writes
jebradshaw 0:dbd8b5c35d0f 284 {
jebradshaw 0:dbd8b5c35d0f 285 char byte_enc[4];
jebradshaw 0:dbd8b5c35d0f 286 long long_enc;
jebradshaw 0:dbd8b5c35d0f 287 }counter;
jebradshaw 0:dbd8b5c35d0f 288
jebradshaw 8:b74ce788d87e 289 spi.format(8, 0); // set up SPI for 8 data bits, mode 0
jebradshaw 8:b74ce788d87e 290 spi.frequency(2000000); // 2MHz SPI clock
jebradshaw 0:dbd8b5c35d0f 291
jebradshaw 8:b74ce788d87e 292 counter.long_enc = enc_value; // pass enc_value to Union
jebradshaw 0:dbd8b5c35d0f 293
jebradshaw 8:b74ce788d87e 294 if(chan_num!=2){ // activate chip select
jebradshaw 0:dbd8b5c35d0f 295 ls7166_cs1 = 0;
jebradshaw 0:dbd8b5c35d0f 296 }
jebradshaw 0:dbd8b5c35d0f 297 else{
jebradshaw 0:dbd8b5c35d0f 298 ls7166_cs2 = 0;
jebradshaw 0:dbd8b5c35d0f 299 }
jebradshaw 8:b74ce788d87e 300 wait_us(1); // short delay
jebradshaw 8:b74ce788d87e 301 LS7366_cmd(WR,DTR); // Write to the Data Transfer Register
jebradshaw 8:b74ce788d87e 302 spi.write(counter.byte_enc[3]); // Write the 32-bit encoder value
jebradshaw 0:dbd8b5c35d0f 303 spi.write(counter.byte_enc[2]);
jebradshaw 0:dbd8b5c35d0f 304 spi.write(counter.byte_enc[1]);
jebradshaw 0:dbd8b5c35d0f 305 spi.write(counter.byte_enc[0]);
jebradshaw 8:b74ce788d87e 306 if(chan_num!=2){ // de-activate the chip select
jebradshaw 0:dbd8b5c35d0f 307 ls7166_cs1 = 1;
jebradshaw 0:dbd8b5c35d0f 308 }
jebradshaw 0:dbd8b5c35d0f 309 else{
jebradshaw 0:dbd8b5c35d0f 310 ls7166_cs2 = 1;
jebradshaw 0:dbd8b5c35d0f 311 }
jebradshaw 0:dbd8b5c35d0f 312
jebradshaw 8:b74ce788d87e 313 wait_us(1); // short delay
jebradshaw 8:b74ce788d87e 314 if(chan_num!=2){ // activate chip select
jebradshaw 0:dbd8b5c35d0f 315 ls7166_cs1 = 0;
jebradshaw 0:dbd8b5c35d0f 316 }
jebradshaw 0:dbd8b5c35d0f 317 else{
jebradshaw 0:dbd8b5c35d0f 318 ls7166_cs2 = 0;
jebradshaw 0:dbd8b5c35d0f 319 }
jebradshaw 8:b74ce788d87e 320 wait_us(1); // short delay
jebradshaw 8:b74ce788d87e 321 LS7366_cmd(LOAD,CNTR); // load command to the counter register from DTR
jebradshaw 8:b74ce788d87e 322 if(chan_num!=2){ // de-activate chip select
jebradshaw 0:dbd8b5c35d0f 323 ls7166_cs1 = 1;
jebradshaw 0:dbd8b5c35d0f 324 }
jebradshaw 0:dbd8b5c35d0f 325 else{
jebradshaw 0:dbd8b5c35d0f 326 ls7166_cs2 = 1;
jebradshaw 0:dbd8b5c35d0f 327 }
jebradshaw 0:dbd8b5c35d0f 328 }
jebradshaw 0:dbd8b5c35d0f 329
jebradshaw 0:dbd8b5c35d0f 330 //------- MAX522 routines ---------------------------------
jebradshaw 0:dbd8b5c35d0f 331 void write_max522(int chan, float volts){
jebradshaw 0:dbd8b5c35d0f 332 int cmd=0x20; //set UB3
jebradshaw 0:dbd8b5c35d0f 333 int data_word = (int)((volts/5.0) * 256.0);
jebradshaw 0:dbd8b5c35d0f 334 if(chan != 2)
jebradshaw 0:dbd8b5c35d0f 335 cmd |= 0x01; //set DAC A out
jebradshaw 0:dbd8b5c35d0f 336 else
jebradshaw 0:dbd8b5c35d0f 337 cmd |= 0x02; //set DACB out
jebradshaw 0:dbd8b5c35d0f 338
jebradshaw 0:dbd8b5c35d0f 339 // pc.printf("cmd=0x%4X data_word=0x%4X \r\n", cmd, data_word);
jebradshaw 0:dbd8b5c35d0f 340
jebradshaw 0:dbd8b5c35d0f 341 spi.format(8, 0);
jebradshaw 0:dbd8b5c35d0f 342 spi.frequency(2000000);
jebradshaw 0:dbd8b5c35d0f 343 max522_cs = 0;
jebradshaw 0:dbd8b5c35d0f 344 spi.write(cmd & 0xFF);
jebradshaw 0:dbd8b5c35d0f 345 spi.write(data_word & 0xFF);
jebradshaw 0:dbd8b5c35d0f 346 max522_cs = 1;
jebradshaw 0:dbd8b5c35d0f 347 }
jebradshaw 0:dbd8b5c35d0f 348
jebradshaw 1:85cd6d500385 349 void mbedWSEsbcInit(unsigned long pcbaud){
jebradshaw 8:b74ce788d87e 350 led1 = 0; //Initialize all LEDs as off
jebradshaw 8:b74ce788d87e 351 led2 = 0;
jebradshaw 8:b74ce788d87e 352 led3 = 0;
jebradshaw 8:b74ce788d87e 353 led4 = 0;
jebradshaw 8:b74ce788d87e 354 max1270_cs = 1; //Initialize all chip selects as off
jebradshaw 0:dbd8b5c35d0f 355 max522_cs = 1;
jebradshaw 0:dbd8b5c35d0f 356 ls7166_cs1 = 1;
jebradshaw 0:dbd8b5c35d0f 357 ls7166_cs2 = 1;
jebradshaw 0:dbd8b5c35d0f 358
jebradshaw 0:dbd8b5c35d0f 359 wait(.2); //delay at beginning for voltage settle purposes
jebradshaw 0:dbd8b5c35d0f 360
jebradshaw 8:b74ce788d87e 361 // mot_en1.period_us(50); //20KHz for DC motor control PWM
jebradshaw 1:85cd6d500385 362 pc.baud(pcbaud); //Set up serial port baud rate
jebradshaw 8:b74ce788d87e 363 pc.printf("\r\n");
jebradshaw 0:dbd8b5c35d0f 364 xbee.baud(9600);
jebradshaw 0:dbd8b5c35d0f 365
jebradshaw 0:dbd8b5c35d0f 366 LS7366_reset_counter(1);
jebradshaw 0:dbd8b5c35d0f 367 LS7366_quad_mode_x4(1);
jebradshaw 0:dbd8b5c35d0f 368 LS7366_write_DTR(1,0);
jebradshaw 0:dbd8b5c35d0f 369
jebradshaw 0:dbd8b5c35d0f 370 LS7366_reset_counter(2);
jebradshaw 0:dbd8b5c35d0f 371 LS7366_quad_mode_x4(2);
jebradshaw 0:dbd8b5c35d0f 372 LS7366_write_DTR(2,0);
jebradshaw 0:dbd8b5c35d0f 373
jebradshaw 0:dbd8b5c35d0f 374 t.start(); // Set up timer
jebradshaw 0:dbd8b5c35d0f 375 }//mbedWSEsbc_init()