A PPM driver for the K64F that uses the CMT module
PPMout.h@2:4570e00696b9, 2016-09-06 (annotated)
- Committer:
- Padman
- Date:
- Tue Sep 06 16:54:00 2016 +0000
- Revision:
- 2:4570e00696b9
- Parent:
- 0:04365c9c9994
Changed frame parameters
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Padman | 0:04365c9c9994 | 1 | /** |
Padman | 0:04365c9c9994 | 2 | * @file PPMout.h |
Padman | 0:04365c9c9994 | 3 | * @brief PPMout - PPM signal generator for K64F |
Padman | 0:04365c9c9994 | 4 | * @author Patrick Thomas |
Padman | 0:04365c9c9994 | 5 | * @version 1.0 |
Padman | 0:04365c9c9994 | 6 | * @see |
Padman | 0:04365c9c9994 | 7 | * |
Padman | 0:04365c9c9994 | 8 | * Copyright (c) 2016 |
Padman | 0:04365c9c9994 | 9 | * |
Padman | 0:04365c9c9994 | 10 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Padman | 0:04365c9c9994 | 11 | * you may not use this file except in compliance with the License. |
Padman | 0:04365c9c9994 | 12 | * You may obtain a copy of the License at |
Padman | 0:04365c9c9994 | 13 | * |
Padman | 0:04365c9c9994 | 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
Padman | 0:04365c9c9994 | 15 | * |
Padman | 0:04365c9c9994 | 16 | * Unless required by applicable law or agreed to in writing, software |
Padman | 0:04365c9c9994 | 17 | * distributed under the License is distributed on an "AS IS" BASIS, |
Padman | 0:04365c9c9994 | 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Padman | 0:04365c9c9994 | 19 | * See the License for the specific language governing permissions and |
Padman | 0:04365c9c9994 | 20 | * limitations under the License. |
Padman | 0:04365c9c9994 | 21 | */ |
Padman | 0:04365c9c9994 | 22 | |
Padman | 0:04365c9c9994 | 23 | #ifndef PPMOUT_H |
Padman | 0:04365c9c9994 | 24 | #define PPMOUT_H |
Padman | 0:04365c9c9994 | 25 | |
Padman | 0:04365c9c9994 | 26 | #include "mbed.h" |
Padman | 0:04365c9c9994 | 27 | |
Padman | 0:04365c9c9994 | 28 | #define PIN(n,min,max) ((n) > (max) ? max : ((n) < (min) ? (min) : (n))) |
Padman | 0:04365c9c9994 | 29 | |
Padman | 0:04365c9c9994 | 30 | // System clock |
Padman | 0:04365c9c9994 | 31 | #define BUS_CLOCK 60000000 |
Padman | 0:04365c9c9994 | 32 | #define CLOCK_DIVIDER 12 |
Padman | 0:04365c9c9994 | 33 | #define CMT_PPS_VAL (CLOCK_DIVIDER - 1) |
Padman | 0:04365c9c9994 | 34 | |
Padman | 0:04365c9c9994 | 35 | // Step array |
Padman | 0:04365c9c9994 | 36 | #define CHANNELS 8 |
Padman | 0:04365c9c9994 | 37 | #define ARRAY_SIZE (CHANNELS + 1) |
Padman | 0:04365c9c9994 | 38 | |
Padman | 0:04365c9c9994 | 39 | // Time parameters |
Padman | 0:04365c9c9994 | 40 | #define STEPS_PER_MS (((BUS_CLOCK/CLOCK_DIVIDER)/8)/1000) |
Padman | 2:4570e00696b9 | 41 | #define GAP_LENGTH_MS 0.3 |
Padman | 2:4570e00696b9 | 42 | #define FRAME_LENGTH_MS 22.5 |
Padman | 0:04365c9c9994 | 43 | |
Padman | 0:04365c9c9994 | 44 | // PPM parameters |
Padman | 0:04365c9c9994 | 45 | #define PPM_RANGE_MS 1 |
Padman | 2:4570e00696b9 | 46 | #define PPM_ZERO_MS 0.7 |
Padman | 0:04365c9c9994 | 47 | |
Padman | 0:04365c9c9994 | 48 | // Derived step parameters |
Padman | 0:04365c9c9994 | 49 | #define RANGE_STEPS (PPM_RANGE_MS*STEPS_PER_MS) |
Padman | 0:04365c9c9994 | 50 | #define ZERO_STEPS (PPM_ZERO_MS*STEPS_PER_MS) |
Padman | 0:04365c9c9994 | 51 | #define GAP_STEPS (int) (GAP_LENGTH_MS*STEPS_PER_MS) |
Padman | 0:04365c9c9994 | 52 | #define TOTAL_GAP_STEPS (GAP_STEPS*ARRAY_SIZE) |
Padman | 0:04365c9c9994 | 53 | #define FRAME_STEPS (FRAME_LENGTH_MS*STEPS_PER_MS) |
Padman | 0:04365c9c9994 | 54 | #define MAX_NON_GAP_STEPS (FRAME_STEPS - TOTAL_GAP_STEPS) |
Padman | 0:04365c9c9994 | 55 | |
Padman | 0:04365c9c9994 | 56 | /** A library for producing PPM output on the K64F (pin PTD7) using the CMT (Carrier/Modulator Transmitter) module |
Padman | 0:04365c9c9994 | 57 | * |
Padman | 0:04365c9c9994 | 58 | * Example: |
Padman | 0:04365c9c9994 | 59 | * @code |
Padman | 0:04365c9c9994 | 60 | * #include "mbed.h" |
Padman | 0:04365c9c9994 | 61 | * #include "PPMout.h" |
Padman | 0:04365c9c9994 | 62 | * |
Padman | 0:04365c9c9994 | 63 | * PPMout myPPM; |
Padman | 0:04365c9c9994 | 64 | * Serial pc(USBTX, USBRX); |
Padman | 0:04365c9c9994 | 65 | * |
Padman | 0:04365c9c9994 | 66 | * int main() |
Padman | 0:04365c9c9994 | 67 | * { |
Padman | 0:04365c9c9994 | 68 | * while(1) { |
Padman | 0:04365c9c9994 | 69 | * |
Padman | 0:04365c9c9994 | 70 | * char buffer[128]; |
Padman | 0:04365c9c9994 | 71 | * float f; |
Padman | 0:04365c9c9994 | 72 | * |
Padman | 0:04365c9c9994 | 73 | * pc.gets(buffer, 5); |
Padman | 0:04365c9c9994 | 74 | * f = (float) atof(buffer); |
Padman | 0:04365c9c9994 | 75 | * |
Padman | 0:04365c9c9994 | 76 | * myPPM.update_channel(0, f); |
Padman | 0:04365c9c9994 | 77 | * } |
Padman | 0:04365c9c9994 | 78 | * } |
Padman | 0:04365c9c9994 | 79 | * @endcode |
Padman | 0:04365c9c9994 | 80 | */ |
Padman | 0:04365c9c9994 | 81 | |
Padman | 0:04365c9c9994 | 82 | class PPMout { |
Padman | 0:04365c9c9994 | 83 | |
Padman | 0:04365c9c9994 | 84 | private: |
Padman | 0:04365c9c9994 | 85 | |
Padman | 0:04365c9c9994 | 86 | static int count_array[ARRAY_SIZE]; |
Padman | 0:04365c9c9994 | 87 | static int counter; |
Padman | 0:04365c9c9994 | 88 | |
Padman | 0:04365c9c9994 | 89 | static void CMT_IRQHandler(); |
Padman | 0:04365c9c9994 | 90 | void init(); |
Padman | 0:04365c9c9994 | 91 | void set_sync_length(); |
Padman | 0:04365c9c9994 | 92 | |
Padman | 0:04365c9c9994 | 93 | public: |
Padman | 0:04365c9c9994 | 94 | |
Padman | 0:04365c9c9994 | 95 | /** Create PPMout instance |
Padman | 0:04365c9c9994 | 96 | */ |
Padman | 0:04365c9c9994 | 97 | PPMout(); |
Padman | 0:04365c9c9994 | 98 | |
Padman | 0:04365c9c9994 | 99 | /** Update a PPM channel value |
Padman | 0:04365c9c9994 | 100 | * @param channel The channel to update (0-7) |
Padman | 0:04365c9c9994 | 101 | * @param value The new value for the channel (0-1) |
Padman | 0:04365c9c9994 | 102 | */ |
Padman | 0:04365c9c9994 | 103 | void update_channel(int channel, float value); |
Padman | 0:04365c9c9994 | 104 | }; |
Padman | 0:04365c9c9994 | 105 | |
Padman | 0:04365c9c9994 | 106 | #endif |