void set_current_limit(float value) { #ifdef HARDWARE_TRIP unsigned short d; if (value == 9999) { /* disable current trip */ d = 65535; trip_disable = 1; write_adc(REG_IOCONTROL, (1 << 4)); // P1DIR=1,P1DAT=0 } else { /* remove reset */ trip_disable = 0; write_adc(REG_IOCONTROL, (1 << 4) | (1 << 0)); // P1DIR=1,P1DAT=1 /* "inverse" calibration */ value /= user_data[0].cur_gain; value += user_data[0].cur_offset; /* omit cur_vgain calibration, because statistical mean of unbalanced dividers is zero */ if (value < 0) value = 0; /* convert current to voltage */ value = value / DIVIDER * CUR_MULT * RCURR / 1E6; /* convert to DAC units */ d = (unsigned short) ((value / 2.5 * 65535) + 0.5); } /* write current dac */ write_dac(5, d); #else // HARDWARE_TRIP if (value == 9999) trip_disable = 1; else trip_disable = 0; #endif // !HARDWARE_TRIP }
byte set_voltage(float v, float* vset) { // Sets the PVS voltage. Reads it back and applies correction in a loop. u16 k, iv, isv, goal; if ((v < 0) || (v > 5.0)) return INVARG; goal = (int)(v / dacm + 0.5); iv = goal; for(k=0; k < 15; ++k) { if(write_dac(iv)) return COMERR; if(read_adc(12, &isv)) return COMERR; // Read channel 12. if (abs(isv-goal) <= 1) break; if (isv > goal) iv -= 1; else if(isv < goal) iv += 1; } *vset = m12[12] * isv + c[12]; //The voltage actually set return 0; }
//------------------------------------------------------------------------------ // MAIN FUNCTION //------------------------------------------------------------------------------ void main (void) { unsigned char index, voltage_out; initialize_system(); printf("\n\rKeil Software, Inc.\n\r"); // Display starup message printf("MCB517 MCU I²C Example Test Program\n\r"); printf("Version 1.0.0\n\r"); printf("Copyright 2000 - Keil Software, Inc.\n\r"); printf("All rights reserved.\n\n\r"); printf("P8591 Test Program....Reading from ADC channel 0, Writing to DAC!\n\r"); while (TRUE) { for (voltage_out = 0; voltage_out < 0xFF; voltage_out++) { write_dac(voltage_out); // Write voltage value to DAC // Blink LEDs in sequence for (index = 0x01; index < 0x80; index <<=1) { P4 = index; delay_time(DELAY_BLINK); } for (index = 0x80; index > 0x01; index >>=1) { P4 = index; delay_time(DELAY_BLINK); } // Read voltage (ADC 0) and display results printf("DAC output: %3bu ADC Channel 0: %3bu\n\r", voltage_out, read_adc_channel(0x00)); } } }
void main() { // Stop watchdog timer to prevent time out reset WDTCTL = WDTPW + WDTHOLD; IO_init(); write_dac(0xff); for(;;) { write_dac(0xff); delay_ms(1); write_dac(0xc0); delay_ms(1); write_dac(0x7f); delay_ms(1); write_dac(0x3f); delay_ms(1); write_dac(0x00); delay_ms(1); } }
// // Do digital to analogue to digital conversion // void main(void) { int d, dac_val, v, s, i; printf ("These are the connections for the digital to analogue to digital test:\n"); printf ("jumper connecting GP11 to SCLK\n"); printf ("jumper connecting GP10 to MOSI\n"); printf ("jumper connecting GP9 to MISO\n"); printf ("jumper connecting GP8 to CSnA\n"); printf ("jumper connecting GP7 to CSnB\n"); printf ("jumper connecting DA1 on J29 to AD0 on J28\n"); printf ("When ready hit enter.\n"); (void) getchar(); // Map the I/O sections setup_io(); // activate SPI bus pins setup_gpio(); // Setup SPI bus setup_spi(); // The value returned by the A to D can jump around quite a bit, so // simply printing out the value isn't very useful. The bar graph // is better because this hides the noise in the signal. printf ("dig ana\n"); for (d=0; d <= 256; d+= 32) { if (d == 256) dac_val = 255 * 16; else dac_val = d * 16; write_dac(1, dac_val); v= read_adc(0); // v should be in range 0-1023 // map to 0-63 s = v >> 4; printf("%3x %04d ", dac_val, v); // show horizontal bar for (i = 0; i < s; i++) putchar('#'); for (i = 0; i < 64 - s; i++) putchar(' '); putchar('\n'); short_wait(); } // repeated write/read for (d=224; d >= 0; d-= 32) { dac_val = d * 16; write_dac(1, dac_val); v= read_adc(0); // v should be in range 0-1023 // map to 0-63 s = v >> 4; printf("%3x %04d ", dac_val, v); // show horizontal bar for (i = 0; i < s; i++) putchar('#'); for (i = 0; i < 64 - s; i++) putchar(' '); putchar('\n'); short_wait(); } // repeated write/read printf("\n"); restore_io(); } // main