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@5:1b2dc43e8947, 2015-11-06 (annotated)
- Committer:
- baxterja
- Date:
- Fri Nov 06 19:28:49 2015 +0000
- Revision:
- 5:1b2dc43e8947
- Parent:
- 4:9fd291254686
Dewayne, Use this one
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
baxterja | 2:3771b3195c7b | 1 | #include "mbed.h" |
baxterja | 2:3771b3195c7b | 2 | #include "pause.cpp" |
baxterja | 2:3771b3195c7b | 3 | #include "Sample/adc.h" |
baxterja | 2:3771b3195c7b | 4 | #include "Sample/pdb.h" |
baxterja | 3:a85b742be262 | 5 | #include "SignalProcessing.h" |
baxterja | 2:3771b3195c7b | 6 | // for debug purposes |
baxterja | 2:3771b3195c7b | 7 | Serial pc(USBTX, USBRX); |
baxterja | 2:3771b3195c7b | 8 | DigitalOut led_red(LED_RED); |
baxterja | 2:3771b3195c7b | 9 | DigitalOut led_green(LED_GREEN); |
baxterja | 2:3771b3195c7b | 10 | DigitalOut led_blue(LED_BLUE); |
baxterja | 2:3771b3195c7b | 11 | |
baxterja | 2:3771b3195c7b | 12 | Timer t1; |
baxterja | 2:3771b3195c7b | 13 | using namespace std; |
baxterja | 2:3771b3195c7b | 14 | |
baxterja | 4:9fd291254686 | 15 | |
baxterja | 4:9fd291254686 | 16 | |
baxterja | 4:9fd291254686 | 17 | |
baxterja | 4:9fd291254686 | 18 | #define PDB_DACINTC0_TOE 0x01 // 0x01 -> PDB DAC interal trigger enabled |
baxterja | 4:9fd291254686 | 19 | |
baxterja | 4:9fd291254686 | 20 | #define DAC0_DAT0 (uint16_t *)0x400CC000 // DAC word buffer base address |
baxterja | 4:9fd291254686 | 21 | |
baxterja | 4:9fd291254686 | 22 | uint16_t *p1; |
baxterja | 4:9fd291254686 | 23 | void setUpDac() |
baxterja | 4:9fd291254686 | 24 | { |
baxterja | 4:9fd291254686 | 25 | SIM_SCGC2 |= SIM_SCGC2_DAC0_MASK; // turn on clock to the DAC |
baxterja | 4:9fd291254686 | 26 | SIM_SCGC6 |= SIM_SCGC6_DAC0_MASK; // turn on clock to the DAC |
baxterja | 4:9fd291254686 | 27 | DAC0_C0 |= DAC_C0_DACEN_MASK ; // enable the DAC; must do before any of the following |
baxterja | 5:1b2dc43e8947 | 28 | DAC0_C2 =9;//cycle through the first 10 values in the buffer |
baxterja | 5:1b2dc43e8947 | 29 | DAC0_C1 = 1;//enable the dac buffer |
baxterja | 4:9fd291254686 | 30 | p1 = DAC0_DAT0; |
baxterja | 5:1b2dc43e8947 | 31 | for (int i = 0; i < 16; i++)//fill the buffer |
baxterja | 4:9fd291254686 | 32 | { |
baxterja | 4:9fd291254686 | 33 | *p1++ = (uint16_t) (cos(3.14159265359 * 2 * 10000 * .00001 * i) * 460.0 + 2870.0); // 3351.0 |
baxterja | 4:9fd291254686 | 34 | //printf("Pointer: %d\tValue: %d\n\r", (uint32_t)p1,(int) (cos(3.14159265359 * 2 * 10000 * .00001 * i) * 800.0 + 3103.0)); |
baxterja | 4:9fd291254686 | 35 | } |
baxterja | 5:1b2dc43e8947 | 36 | |
baxterja | 4:9fd291254686 | 37 | } |
baxterja | 4:9fd291254686 | 38 | |
baxterja | 3:a85b742be262 | 39 | int main() |
baxterja | 3:a85b742be262 | 40 | { |
baxterja | 4:9fd291254686 | 41 | |
baxterja | 2:3771b3195c7b | 42 | led_blue = 1; |
baxterja | 2:3771b3195c7b | 43 | led_green = 1; |
baxterja | 2:3771b3195c7b | 44 | led_red = 1; |
baxterja | 3:a85b742be262 | 45 | pre_compute_tables(); |
baxterja | 3:a85b742be262 | 46 | |
baxterja | 2:3771b3195c7b | 47 | |
baxterja | 4:9fd291254686 | 48 | // t1.reset(); |
baxterja | 4:9fd291254686 | 49 | // for (int t = 0; t<500000; t++) |
baxterja | 4:9fd291254686 | 50 | // { |
baxterja | 4:9fd291254686 | 51 | // int input1 = 5000*cos(3.14159265359 * 2*10000*t*.00001)+2500; |
baxterja | 4:9fd291254686 | 52 | // int input2 = 2500*sin(3.14159265359 * 2*10000*t*.00001)+2500; |
baxterja | 4:9fd291254686 | 53 | // t1.start(); |
baxterja | 4:9fd291254686 | 54 | // filter100K(input1, input2); |
baxterja | 4:9fd291254686 | 55 | // t1.stop(); |
baxterja | 4:9fd291254686 | 56 | // } |
baxterja | 4:9fd291254686 | 57 | // printf("FINAL TIME: %f\n\r",t1.read()); |
baxterja | 4:9fd291254686 | 58 | |
baxterja | 5:1b2dc43e8947 | 59 | pc.printf("Starting...\r\n"); |
baxterja | 2:3771b3195c7b | 60 | for(int i = 0; i < 86; i++) |
baxterja | 2:3771b3195c7b | 61 | { |
baxterja | 2:3771b3195c7b | 62 | if(NVIC_GetPriority((IRQn_Type) i) == 0) NVIC_SetPriority((IRQn_Type) i, 2); |
baxterja | 2:3771b3195c7b | 63 | } |
baxterja | 2:3771b3195c7b | 64 | |
baxterja | 2:3771b3195c7b | 65 | // Give hardware associated with |
baxterja | 2:3771b3195c7b | 66 | // sampling the highest priority |
baxterja | 2:3771b3195c7b | 67 | NVIC_SetPriority(ADC1_IRQn,0); |
baxterja | 2:3771b3195c7b | 68 | NVIC_SetPriority(ADC0_IRQn,0); |
baxterja | 2:3771b3195c7b | 69 | NVIC_SetPriority(PDB0_IRQn,0); |
baxterja | 2:3771b3195c7b | 70 | NVIC_SetPriority(DMA0_IRQn,0); |
baxterja | 2:3771b3195c7b | 71 | NVIC_SetPriority(DMA1_IRQn,0); |
baxterja | 2:3771b3195c7b | 72 | NVIC_SetPriority(DMA2_IRQn,0); |
baxterja | 2:3771b3195c7b | 73 | |
baxterja | 2:3771b3195c7b | 74 | NVIC_SetPriority(ENET_1588_Timer_IRQn,1); |
baxterja | 2:3771b3195c7b | 75 | NVIC_SetPriority(ENET_Transmit_IRQn,1); |
baxterja | 2:3771b3195c7b | 76 | NVIC_SetPriority(ENET_Receive_IRQn,1); |
baxterja | 2:3771b3195c7b | 77 | NVIC_SetPriority(ENET_Error_IRQn,1); |
baxterja | 2:3771b3195c7b | 78 | |
baxterja | 2:3771b3195c7b | 79 | adc_init(); // initialize ADCs (always initialize adc before dma) |
baxterja | 4:9fd291254686 | 80 | setUpDac(); |
baxterja | 2:3771b3195c7b | 81 | dma_init(); // initializes DMAs |
baxterja | 2:3771b3195c7b | 82 | pdb_init(); // initialize PDB0 as the timer for ADCs and DMA2 |
baxterja | 2:3771b3195c7b | 83 | |
baxterja | 2:3771b3195c7b | 84 | // flash green led indicating startup complete |
baxterja | 2:3771b3195c7b | 85 | led_red = 1; |
baxterja | 2:3771b3195c7b | 86 | led_blue = 1; |
baxterja | 2:3771b3195c7b | 87 | led_green = 0; |
baxterja | 2:3771b3195c7b | 88 | pause_ms(500); |
baxterja | 2:3771b3195c7b | 89 | led_green = 1; |
baxterja | 2:3771b3195c7b | 90 | pause_ms(200); |
baxterja | 2:3771b3195c7b | 91 | led_green = 0; |
baxterja | 4:9fd291254686 | 92 | pause_ms(500); |
baxterja | 2:3771b3195c7b | 93 | led_green = 1; |
baxterja | 2:3771b3195c7b | 94 | pdb_start(); |
baxterja | 2:3771b3195c7b | 95 | int startAddress = (int)&sample_array0[0]; |
baxterja | 4:9fd291254686 | 96 | int destinationIndex = (DMA_TCD0_DADDR-startAddress)/2; |
baxterja | 4:9fd291254686 | 97 | int currentIndex; |
baxterja | 2:3771b3195c7b | 98 | while (1) |
baxterja | 2:3771b3195c7b | 99 | { |
baxterja | 4:9fd291254686 | 100 | |
baxterja | 4:9fd291254686 | 101 | |
baxterja | 5:1b2dc43e8947 | 102 | destinationIndex = (DMA_TCD0_DADDR-startAddress)/2;//-1; |
baxterja | 5:1b2dc43e8947 | 103 | //if (destinationIndex<0) |
baxterja | 5:1b2dc43e8947 | 104 | // destinationIndex = 1999; |
baxterja | 4:9fd291254686 | 105 | while (currentIndex!=destinationIndex) |
baxterja | 2:3771b3195c7b | 106 | { |
baxterja | 4:9fd291254686 | 107 | filter100K(sample_array0[currentIndex], sample_array1[currentIndex]); |
baxterja | 4:9fd291254686 | 108 | currentIndex++; |
baxterja | 4:9fd291254686 | 109 | if (currentIndex>=2000) |
baxterja | 4:9fd291254686 | 110 | currentIndex = 0; |
baxterja | 4:9fd291254686 | 111 | |
baxterja | 2:3771b3195c7b | 112 | } |
baxterja | 2:3771b3195c7b | 113 | |
baxterja | 2:3771b3195c7b | 114 | } |
baxterja | 4:9fd291254686 | 115 | |
baxterja | 4:9fd291254686 | 116 | |
baxterja | 4:9fd291254686 | 117 | } |