Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Fork of Chemical_Sensor_DMA by
Diff: main.cpp
- Revision:
- 4:9fd291254686
- Parent:
- 3:a85b742be262
- Child:
- 5:1b2dc43e8947
--- a/main.cpp Sat Oct 31 20:06:37 2015 +0000 +++ b/main.cpp Fri Nov 06 03:33:09 2015 +0000 @@ -1,92 +1,155 @@ -/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * - * - * Pinout for FRDM-k64f - * J2 - * X X - * X X - * X X - * J3 X X GND - * X X X X SCLK - * X X X X MISO - * X X X X MOSI - * X X X X CS - * X X GND X X - * GND X X X X - * GND X X - * 5Vin X X J1 - * X X - * J4 X X motorA - * X X X X motorB - * mic1 X X X X - * mic2 X X X X - * X X X X - * X X quadA X X - * X X quadB X X - * - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ #include "mbed.h" #include "pause.cpp" - -// Sampling #include "Sample/adc.h" #include "Sample/pdb.h" -#include "Sample/quad.h" - -#include "AngleEncoder.h" -#include "MotorControl.h" - #include "SignalProcessing.h" // for debug purposes Serial pc(USBTX, USBRX); DigitalOut led_red(LED_RED); DigitalOut led_green(LED_GREEN); DigitalOut led_blue(LED_BLUE); -//DigitalIn switch1(PTA4); -//DigitalIn switch2(PTC6); -//DigitalOut TotalInd(PTC4); -//DigitalOut SampleInd(PTC5); - -MotorControl motor(PTC2, PTA2, 2000, 25); // cw, ccw, period_us, safetyPeriod_us -AngleEncoder angle_encoder(PTD2, PTD3, PTD1, PTD0, 8, 0, 1000000); // mosi, miso, sclk, cs, bit_width, mode, hz -// defined in dma.cpp -extern int len; -extern uint16_t sample_array0[]; -extern uint16_t sample_array1[]; -extern uint16_t angle_array[]; -extern bool dma_done; -extern bool dma_half_done; -int motorVal = 10; - -// Declaration of functions void output_data(); Timer t1; using namespace std; + +// uint16_t out_val_pre[]; +// +// +// uint16_t phase_counter2 = 0; +// #define pre_compute_length 2000 +// +// void ISR_repeat() { +// WaveOut.write_u16(out_val_pre[phase_counter2]); //creates a wave that bounces between 0 & 3.3 V +// +// phase_counter2++; +// if (phase_counter2 >= pre_compute_length) phase_counter2 = 0; +//} //ISR_repeat +// +//Ticker timer0; +// + + + +#define PDB_DACINTC0_TOE 0x01 // 0x01 -> PDB DAC interal trigger enabled + +#define DAC0_DAT0 (uint16_t *)0x400CC000 // DAC word buffer base address + +uint16_t *p1; +void setUpDac() +{ + SIM_SCGC2 |= SIM_SCGC2_DAC0_MASK; // turn on clock to the DAC + SIM_SCGC6 |= SIM_SCGC6_DAC0_MASK; // turn on clock to the DAC + //DAC0_C0 = 0; + DAC0_C0 |= DAC_C0_DACEN_MASK ; // enable the DAC; must do before any of the following + //DAC0_C0 |= DAC_C0_REFSEL(0) + //DAC0_C0 &= 0x9f; + //DAC0_C0 |= DAC_C0_DACRFS_MASK; // 3.3V VDDA is DACREF_2 + //DAC0_C2 =9; + //DAC0_C2 |= 0x9; + //DAC0_C2 |= DAC_C2_DACBFUP(9); + DAC0_C2 =9; + //DAC0_C2 |= DAC_C2_DACBFUP(9); // resets to 15 but setting it anyway... + DAC0_C1 = 1; + //DAC0_C1 |= (0x01); // 0x01 enables the DAC buffer! See note above + + // prefill the DAC buffer with first 16 values from the lut + + p1 = DAC0_DAT0; + for (int i = 0; i < 16; i++) + { + *p1++ = (uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * i) * 460.0 + 2870.0); // 3351.0 + //printf("Pointer: %d\tValue: %d\n\r", (uint32_t)p1,(int) (cos(3.14159265359 * 2 * 10000 * .00001 * i) * 800.0 + 3103.0)); + } + printf ("data Low: %d\tdata High: %d\tTotal: %d\n\r",DAC0_DAT0L,DAC0_DAT0H,DAC0_DAT0L|DAC0_DAT0H<<8); + printf ("data Low: %d\tdata High: %d\tTotal: %d\n\r",DAC0_DAT1L,DAC0_DAT1H,DAC0_DAT1L|DAC0_DAT1H<<8); + printf ("data Low: %d\tdata High: %d\tTotal: %d\n\r",DAC0_DAT2L,DAC0_DAT2H,DAC0_DAT2L|DAC0_DAT2H<<8); + printf ("data Low: %d\tdata High: %d\tTotal: %d\n\r",DAC0_DAT3L,DAC0_DAT3H,DAC0_DAT3L|DAC0_DAT3H<<8); + printf ("data Low: %d\tdata High: %d\tTotal: %d\n\r",DAC0_DAT4L,DAC0_DAT4H,DAC0_DAT4L|DAC0_DAT4H<<8); + printf ("data Low: %d\tdata High: %d\tTotal: %d\n\r",DAC0_DAT5L,DAC0_DAT5H,DAC0_DAT5L|DAC0_DAT5H<<8); + printf ("data Low: %d\tdata High: %d\tTotal: %d\n\r",DAC0_DAT6L,DAC0_DAT6H,DAC0_DAT6L|DAC0_DAT6H<<8); + printf ("data Low: %d\tdata High: %d\tTotal: %d\n\r",DAC0_DAT7L,DAC0_DAT7H,DAC0_DAT7L|DAC0_DAT7H<<8); + printf ("data Low: %d\tdata High: %d\tTotal: %d\n\r",DAC0_DAT8L,DAC0_DAT8H,DAC0_DAT8L|DAC0_DAT8H<<8); + printf ("data Low: %d\tdata High: %d\tTotal: %d\n\r",DAC0_DAT9L,DAC0_DAT9H,DAC0_DAT9L|DAC0_DAT9H<<8); + //printf ("data High: %d\n\r",DAC0_DAT0H); + + //printf ("data Low: %d\n\r",DAC0_DAT1L); + //printf ("data High: %d\n\r",DAC0_DAT1H); + /* + p1 = DAC0_DAT0; + for (int i = 0; i < 16; i++) + { + *p1++; + printf("Pointer: %d\n\r", (uint32_t)*p1); + } + + DAC0_DAT0L =(uint8_t)(((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 0) * 4965.0 + 49650.0))&0xFF); + DAC0_DAT1L =(uint8_t)(((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 1) * 4965.0 + 49650.0))&0xFF); + DAC0_DAT2L =(uint8_t)(((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 2) * 4965.0 + 49650.0))&0xFF); + DAC0_DAT3L =(uint8_t)(((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 3) * 4965.0 + 49650.0))&0xFF); + DAC0_DAT4L =(uint8_t)(((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 4) * 4965.0 + 49650.0))&0xFF); + DAC0_DAT5L =(uint8_t)(((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 5) * 4965.0 + 49650.0))&0xFF); + DAC0_DAT6L =(uint8_t)(((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 6) * 4965.0 + 49650.0))&0xFF); + DAC0_DAT7L =(uint8_t)(((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 7) * 4965.0 + 49650.0))&0xFF); + DAC0_DAT8L =(uint8_t)(((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 8) * 4965.0 + 49650.0))&0xFF); + DAC0_DAT9L =(uint8_t)(((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 9) * 4965.0 + 49650.0))&0xFF); + + DAC0_DAT0H =(uint8_t)((((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 0) * 4965.0 + 49650.0))>>8)&0x0F); + DAC0_DAT1H =(uint8_t)((((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 1) * 4965.0 + 49650.0))>>8)&0x0F); + DAC0_DAT2H =(uint8_t)((((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 2) * 4965.0 + 49650.0))>>8)&0x0F); + DAC0_DAT3H =(uint8_t)((((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 3) * 4965.0 + 49650.0))>>8)&0x0F); + DAC0_DAT4H =(uint8_t)((((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 4) * 4965.0 + 49650.0))>>8)&0x0F); + DAC0_DAT5H =(uint8_t)((((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 5) * 4965.0 + 49650.0))>>8)&0x0F); + DAC0_DAT6H =(uint8_t)((((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 6) * 4965.0 + 49650.0))>>8)&0x0F); + DAC0_DAT7H =(uint8_t)((((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 7) * 4965.0 + 49650.0))>>8)&0x0F); + DAC0_DAT8H =(uint8_t)((((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 8) * 4965.0 + 49650.0))>>8)&0x0F); + DAC0_DAT9H =(uint8_t)((((uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * 9) * 4965.0 + 49650.0))>>8)&0x0F); + */ + /* + DAC0_SR = 0x00; + SIM_SCGC2 |= SIM_SCGC2_DAC0_MASK; + SIM_SCGC6 |= SIM_SCGC2_DAC0_MASK; + DAC0_C0 = 0; + //DAC0_C0 |= DAC_C0_DACTRGSEL_MASK; + DAC0_C0 |= DAC_C0_DACEN_MASK ; //The DAC system is enabled. + + DAC0_C1 = 0; + //DAC0_C0 |= DAC_C0_DACTRGSEL_MASK; + DAC0_C0 |= 0x80;//DAC_C0_DMAEN_MASK ; + DAC0_DAT0L = 0xFF; + DAC0_DAT0H = 0x00; + */ +} + int main() { + //DAC0_C2 =0; + led_blue = 1; led_green = 1; led_red = 1; pre_compute_tables(); - printf("STARTING\n\r"); - t1.reset(); - for (int t = 0; t<500000; t++) - { - int input1 = 5000*cos(3.14159265359 * 2*10000*t*.00001)+2500; - int input2 = 2500*sin(3.14159265359 * 2*10000*t*.00001)+2500; - t1.start(); - filter100K(input1, input2); - t1.stop(); - } - printf("FINAL TIME: %f\n\r",t1.read()); - /* - pc.baud(230400); +// t1.reset(); +// for (int t = 0; t<500000; t++) +// { +// int input1 = 5000*cos(3.14159265359 * 2*10000*t*.00001)+2500; +// int input2 = 2500*sin(3.14159265359 * 2*10000*t*.00001)+2500; +// t1.start(); +// filter100K(input1, input2); +// t1.stop(); +// } +// printf("FINAL TIME: %f\n\r",t1.read()); + + + +// pc.baud(230400); pc.printf("Starting...\r\n"); + for(int i = 0; i < 86; i++) { @@ -110,6 +173,11 @@ //quad_init(); // initialize FTM2 quadrature decoder //quad_invert(); // invert the direction of counting adc_init(); // initialize ADCs (always initialize adc before dma) + setUpDac(); + // DAC0_C1 |= 0x80; + // DAC0_DAT0L = 0xFF; + // DAC0_DAT0H = 0xFF; + // printf("Dac should be 3.3V"); dma_init(); // initializes DMAs pdb_init(); // initialize PDB0 as the timer for ADCs and DMA2 @@ -121,215 +189,39 @@ led_green = 1; pause_ms(200); led_green = 0; - pause_ms(500); + pause_ms(500); led_green = 1; pdb_start(); - t1.start(); - t1.reset(); + + //timer0.attach_us(&ISR_repeat, 10); int startAddress = (int)&sample_array0[0]; + //timer0.attach_us(&ISR_repeat, 100); + int destinationIndex = (DMA_TCD0_DADDR-startAddress)/2; + int currentIndex; + /* + printf("Dac Control0 Register: %X\n\r",DAC0_C0); + printf("Dac Control2 Register: %X\n\r",DAC0_C2); + printf("Dac Control1 Register: %X\n\r",DAC0_C1); + printf("Dac Control2 Register: %X\n\r\n\r",DAC0_C2); + //PDB0_DACINT0 = 0x257; + printf("Dac intc Register: %X\n\r",PDB0_DACINTC0); + printf("Dac int Register: %X\n\r",PDB0_DACINT0); + */ while (1) { - if (t1.read()>2) + + + destinationIndex = (DMA_TCD0_DADDR-startAddress)/2; + while (currentIndex!=destinationIndex) { - //pdb_start(); - pc.printf("%i\t%i\t%i\r\n", sample_array0[1], sample_array1[1],(DMA_TCD0_DADDR-startAddress)/2); - t1.reset(); + filter100K(sample_array0[currentIndex], sample_array1[currentIndex]); + currentIndex++; + if (currentIndex>=2000) + currentIndex = 0; + } - } -*/ - // while(1) { -// if(pc.readable() > 0) { -// char temp = pc.getc(); -// -// switch(temp) { -// case '1': // run motor and sample -// { -// // wait for angle to set to zero -// led_blue = 0; -// while(!angle_encoder.set_zero()) {pc.printf("AngleNotSet");} -// quad_reset(); -// led_blue = 1; -// -// // release mallet -// motor.releaseMallet(); -// -// // wait for mallet to drop to certin point before beginning to sample -// /* -// led_red = 0; -// int angleVal; -// do { -// angleVal = angle_encoder.absolute_angle(); -// } -// while(!(angleVal > 150 && angleVal < 400)); -// led_red = 1; -// */ -// -// // begin sampling -// pdb_start(); -// //pause_us(TOTAL_SAMPLES*10); -// //while(!dma_done) {led_green = 0;} -// led_green = 1; -// -// // reset mallet -// motor.reset(); -// output_data(); -// -// -// led_red = 1; -// led_blue = 1; -// led_green = 1; -// } -// break; -// -// case '2': -// { -// motor.releaseMallet(); -// pause(1); -// -// while(!angle_encoder.set_zero()) {} -// quad_reset(); -// -// pdb_start(); -// motor.clockwise(1); -// pause(1); -// motor.off(); -// output_data(); -// -// } -// break; -// -// case '3': -// { -// /* -// while(angleVar) -// { -// counter++; -// angleVar = angle_encoder.absolute_angle(); -// angleVar -= motorVal; -// if(angleVar == 0x00ff0000) {} // do nothing -// //else if(angleVar > 340) motorB.pulsewidth_us(8000); // max speed -// else if(angleVar < 43) { -// angleVar = 0; // exit loop -// //motorB.pulsewidth_us(0);} // min speed -// //else motorB.pulsewidth_us(angleVar*800/34); -// } -// motor.off(); -// */ -// } -// break; -// -// -// /******************************************************************** -// * The code below is for testing and troubleshooting. Don't delete. * -// ********************************************************************/ -// case 'B': -// case 'b': -// led_blue = !led_blue; -// pc.printf("Blue LED\r\n"); -// break; -// case 'R': -// case 'r': -// led_red = !led_red; -// pc.printf("Red LED\r\n"); -// break; -// -// // test sampling -// case 'Q': -// case 'q': -// quad_reset(); -// pdb_start(); -// -// while(!dma_half_done) { -// pc.printf("first half\r\n"); -// pause_ms(1); -// } -// -// while(!dma_done) { -// pc.printf("second half\r\n"); -// pause_ms(1); -// } -// for(int i = 0; i < len; i++) pc.printf("%i\t %i\t %i\r\n", sample_array0[i],sample_array1[i], angle_array[i]); -// pc.printf("\r\n"); -// -// led_red = 1; -// led_green = 1; -// led_blue = 1; -// -// -// /* -// while(!angle_encoder.set_zero()) {} -// quad_reset(); -// -// -// pdb_start(); -// pause_us(TOTAL_SAMPLES*10); -// output_data();*/ -// -// break; -// case 'W': -// case 'w': // reads 0 unless the counter is running -// pc.printf("PDB: %i\r\n",PDB0_CNT); -// break; -// -// // test angle encoder -// case 'A': -// case 'a': // set zero -// if(angle_encoder.set_zero()) { -// pc.printf("Zero set\r\n"); -// } -// else { -// pc.printf("Zero NOT set\r\n"); -// } -// break; -// case 'S': -// case 's': // perform "no operation", which returns 0xa5 -// { -// pc.printf("NOP: 0x%02x\r\n", angle_encoder.nop()); -// break; -// } -// case 'D': -// case 'd': // read absolute and relative angles -// { -// pc.printf("Angle: %i %i\r\n", angle_encoder.absolute_angle(), quad_read()); -// break; -// } -// case 'F': -// case 'f': -// pc.printf("Quad Cnt: %i\r\n", quad_read()); -// break; -// -// // test motor -// case 'Z': -// case 'z': // release the mallet -// motor.releaseMallet(); -// break; -// case 'X': -// case 'x': -// motor.hardReset(motorVal); -// break; -// case '+': -// case '=': -// motorVal++; -// if(motorVal > 8000) motorVal = 8000; -// pc.printf("MotorVal: %i\r\n", motorVal); -// break; -// case '-': -// { -// motorVal--; -// if(motorVal < 0) motorVal = 0; -// pc.printf("MotorVal: %i\r\n", motorVal); -// break; -// } -// } -// } -// } - -} - -void output_data() -{ - for (int n = 0; n < len; n++) { - pc.printf("%i\t%i\r\n", sample_array0[n], sample_array1[n]); } -} \ No newline at end of file + + +}