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
main.cpp
- Committer:
- baxterja
- Date:
- 2015-11-06
- Revision:
- 4:9fd291254686
- Parent:
- 3:a85b742be262
- Child:
- 5:1b2dc43e8947
File content as of revision 4:9fd291254686:
#include "mbed.h" #include "pause.cpp" #include "Sample/adc.h" #include "Sample/pdb.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); 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(); // 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++) { if(NVIC_GetPriority((IRQn_Type) i) == 0) NVIC_SetPriority((IRQn_Type) i, 2); } // Give hardware associated with // sampling the highest priority NVIC_SetPriority(ADC1_IRQn,0); NVIC_SetPriority(ADC0_IRQn,0); NVIC_SetPriority(PDB0_IRQn,0); NVIC_SetPriority(DMA0_IRQn,0); NVIC_SetPriority(DMA1_IRQn,0); NVIC_SetPriority(DMA2_IRQn,0); NVIC_SetPriority(ENET_1588_Timer_IRQn,1); NVIC_SetPriority(ENET_Transmit_IRQn,1); NVIC_SetPriority(ENET_Receive_IRQn,1); NVIC_SetPriority(ENET_Error_IRQn,1); //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 // flash green led indicating startup complete led_red = 1; led_blue = 1; led_green = 0; pause_ms(500); led_green = 1; pause_ms(200); led_green = 0; pause_ms(500); led_green = 1; pdb_start(); //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) { destinationIndex = (DMA_TCD0_DADDR-startAddress)/2; while (currentIndex!=destinationIndex) { filter100K(sample_array0[currentIndex], sample_array1[currentIndex]); currentIndex++; if (currentIndex>=2000) currentIndex = 0; } } }