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