![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Nucleo F303K8 framework with fast pwm and serial manager
main.cpp@0:80d3786005bd, 2017-08-09 (annotated)
- Committer:
- django
- Date:
- Wed Aug 09 03:04:28 2017 +0000
- Revision:
- 0:80d3786005bd
NUCLEO F303 SerialHandler with PWM in work
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
django | 0:80d3786005bd | 1 | //****************************************************************************************** |
django | 0:80d3786005bd | 2 | // Verison : 1.0.0.0 9.8.2017 Startversion |
django | 0:80d3786005bd | 3 | |
django | 0:80d3786005bd | 4 | //***************** INCLUDES *************************************************************** |
django | 0:80d3786005bd | 5 | #include "mbed.h" |
django | 0:80d3786005bd | 6 | #include "string.h" |
django | 0:80d3786005bd | 7 | |
django | 0:80d3786005bd | 8 | //***************** PROTOTYPES ************************************************************* |
django | 0:80d3786005bd | 9 | void pc_isr(); |
django | 0:80d3786005bd | 10 | void parse_pc(char*); |
django | 0:80d3786005bd | 11 | void MyTicker_isr(); |
django | 0:80d3786005bd | 12 | |
django | 0:80d3786005bd | 13 | //***************** HARDWARE RECOURCES ***************************************************** |
django | 0:80d3786005bd | 14 | |
django | 0:80d3786005bd | 15 | Ticker MyTicker; |
django | 0:80d3786005bd | 16 | |
django | 0:80d3786005bd | 17 | DigitalOut led1(LED1); |
django | 0:80d3786005bd | 18 | Serial pc(USBTX, USBRX); // tx, rx |
django | 0:80d3786005bd | 19 | //Serial Gps(A7,A2); // tx, rx |
django | 0:80d3786005bd | 20 | Serial Remote(D1,D0); // tx, rx |
django | 0:80d3786005bd | 21 | PwmOut Mypwm0(D3); |
django | 0:80d3786005bd | 22 | PwmOut Mypwm1(D6); |
django | 0:80d3786005bd | 23 | AnalogIn Is_1(A0); |
django | 0:80d3786005bd | 24 | AnalogIn Is_2(A1); |
django | 0:80d3786005bd | 25 | AnalogIn T_2(A3); |
django | 0:80d3786005bd | 26 | AnalogIn T_1(A4); |
django | 0:80d3786005bd | 27 | AnalogIn Poti(A5); |
django | 0:80d3786005bd | 28 | AnalogIn V_Supp(A6); |
django | 0:80d3786005bd | 29 | |
django | 0:80d3786005bd | 30 | DigitalOut Myled(LED1); |
django | 0:80d3786005bd | 31 | DigitalOut Mode0(D10); |
django | 0:80d3786005bd | 32 | DigitalOut RunStop_1(D5); |
django | 0:80d3786005bd | 33 | DigitalOut RunStop_2(D4); |
django | 0:80d3786005bd | 34 | DigitalOut Ext_Sup(D2); |
django | 0:80d3786005bd | 35 | DigitalIn Taster(D11); |
django | 0:80d3786005bd | 36 | |
django | 0:80d3786005bd | 37 | void pc_isr(); |
django | 0:80d3786005bd | 38 | void parse_pc(char*); |
django | 0:80d3786005bd | 39 | |
django | 0:80d3786005bd | 40 | //***************** GLOBAL VARIABLES ******************************************************* |
django | 0:80d3786005bd | 41 | #define RX_BUFFER_PC_SIZE 255 |
django | 0:80d3786005bd | 42 | char rx_buffer_pc[RX_BUFFER_PC_SIZE] = {' '}; |
django | 0:80d3786005bd | 43 | char stringcopy_isr_pc[RX_BUFFER_PC_SIZE] = {' '}; |
django | 0:80d3786005bd | 44 | char stringcopy_pc[RX_BUFFER_PC_SIZE] = {' '}; |
django | 0:80d3786005bd | 45 | int rx_buffer_pc_poi = 0; |
django | 0:80d3786005bd | 46 | bool stringComplete_pc = false; // whether the string is complete |
django | 0:80d3786005bd | 47 | char c; |
django | 0:80d3786005bd | 48 | |
django | 0:80d3786005bd | 49 | //***************** THE MAIN PROGRAM ******************************************************* |
django | 0:80d3786005bd | 50 | int main() { |
django | 0:80d3786005bd | 51 | //***************** INIT ******************************************************************* |
django | 0:80d3786005bd | 52 | float meas; |
django | 0:80d3786005bd | 53 | //**************** Setup *************** |
django | 0:80d3786005bd | 54 | MyTicker.attach(&MyTicker_isr, 0.2); |
django | 0:80d3786005bd | 55 | Remote.baud(115200); |
django | 0:80d3786005bd | 56 | pc.baud(115200); |
django | 0:80d3786005bd | 57 | Remote.printf("Hello World Remote\n"); |
django | 0:80d3786005bd | 58 | pc.printf("Hello World PC\n"); |
django | 0:80d3786005bd | 59 | HAL_Delay(200); |
django | 0:80d3786005bd | 60 | Mypwm0.period_us(100); |
django | 0:80d3786005bd | 61 | Mypwm0.pulsewidth_us(2); |
django | 0:80d3786005bd | 62 | Mypwm1.period_us(100); |
django | 0:80d3786005bd | 63 | Mypwm1.pulsewidth_us(1); |
django | 0:80d3786005bd | 64 | Ext_Sup = 0; |
django | 0:80d3786005bd | 65 | RunStop_1 = 1; |
django | 0:80d3786005bd | 66 | RunStop_2 = 1; |
django | 0:80d3786005bd | 67 | Mode0 = 0; |
django | 0:80d3786005bd | 68 | Myled = 0; |
django | 0:80d3786005bd | 69 | Myled = !Myled; |
django | 0:80d3786005bd | 70 | wait(0.5); |
django | 0:80d3786005bd | 71 | Myled = !Myled; |
django | 0:80d3786005bd | 72 | pc.attach(&pc_isr); |
django | 0:80d3786005bd | 73 | //**************** MAIN LOOP *************************************************************** |
django | 0:80d3786005bd | 74 | while(1) { |
django | 0:80d3786005bd | 75 | /* if (stringComplete_pc) // set in interrupt |
django | 0:80d3786005bd | 76 | { |
django | 0:80d3786005bd | 77 | strncpy(stringcopy_pc, stringcopy_isr_pc, (unsigned)strlen(stringcopy_isr_pc)); |
django | 0:80d3786005bd | 78 | parse_pc(stringcopy_pc); // handle it and respond |
django | 0:80d3786005bd | 79 | stringComplete_pc = false; // reset |
django | 0:80d3786005bd | 80 | Myled = !Myled; |
django | 0:80d3786005bd | 81 | } */ |
django | 0:80d3786005bd | 82 | meas = Poti.read(); // Converts and read the analog input value (value from 0.0 to 1.0) |
django | 0:80d3786005bd | 83 | meas = meas * 3300; // Change the value to be in the 0 to 3300 range |
django | 0:80d3786005bd | 84 | Remote.printf("Poti = %.0f mV\n", meas); |
django | 0:80d3786005bd | 85 | wait(1); |
django | 0:80d3786005bd | 86 | meas = V_Supp.read(); // Converts and read the analog input value (value from 0.0 to 1.0) |
django | 0:80d3786005bd | 87 | meas = meas * 3300; // Change the value to be in the 0 to 3300 range |
django | 0:80d3786005bd | 88 | meas = (meas * 110.0f) / (float)10.0f; |
django | 0:80d3786005bd | 89 | meas /= 1000.0f; |
django | 0:80d3786005bd | 90 | meas *= 1.02f; |
django | 0:80d3786005bd | 91 | meas -= 0.02f; |
django | 0:80d3786005bd | 92 | Remote.printf("V_Supp = %.02f V\n", meas); |
django | 0:80d3786005bd | 93 | wait(1); |
django | 0:80d3786005bd | 94 | Ext_Sup = !Ext_Sup; |
django | 0:80d3786005bd | 95 | } |
django | 0:80d3786005bd | 96 | } |
django | 0:80d3786005bd | 97 | |
django | 0:80d3786005bd | 98 | |
django | 0:80d3786005bd | 99 | //***************** PARSING PC COMMAND ****************************************************** |
django | 0:80d3786005bd | 100 | |
django | 0:80d3786005bd | 101 | void parse_pc(char* stringcopy) |
django | 0:80d3786005bd | 102 | { |
django | 0:80d3786005bd | 103 | char cmd; |
django | 0:80d3786005bd | 104 | |
django | 0:80d3786005bd | 105 | if (stringcopy_pc[0] != '#') |
django | 0:80d3786005bd | 106 | { |
django | 0:80d3786005bd | 107 | pc.printf("@Syntax Error\r\n"); |
django | 0:80d3786005bd | 108 | return; |
django | 0:80d3786005bd | 109 | } |
django | 0:80d3786005bd | 110 | cmd = stringcopy_pc[1]; |
django | 0:80d3786005bd | 111 | switch(cmd) |
django | 0:80d3786005bd | 112 | { |
django | 0:80d3786005bd | 113 | // Testing .... |
django | 0:80d3786005bd | 114 | case 'v': pc.printf("Hello world ! v ! \r\n"); |
django | 0:80d3786005bd | 115 | break; |
django | 0:80d3786005bd | 116 | case 'a': pc.printf("Hello world ! a ! \r\n"); |
django | 0:80d3786005bd | 117 | break; |
django | 0:80d3786005bd | 118 | } |
django | 0:80d3786005bd | 119 | } |
django | 0:80d3786005bd | 120 | |
django | 0:80d3786005bd | 121 | |
django | 0:80d3786005bd | 122 | //***************** PC ISR ***************************************************************** |
django | 0:80d3786005bd | 123 | void pc_isr() { |
django | 0:80d3786005bd | 124 | if (pc.readable()) { |
django | 0:80d3786005bd | 125 | do { |
django | 0:80d3786005bd | 126 | c = pc.getc(); |
django | 0:80d3786005bd | 127 | rx_buffer_pc[rx_buffer_pc_poi] = c; |
django | 0:80d3786005bd | 128 | rx_buffer_pc_poi++; |
django | 0:80d3786005bd | 129 | if (c == '\n') |
django | 0:80d3786005bd | 130 | { |
django | 0:80d3786005bd | 131 | stringComplete_pc = true; |
django | 0:80d3786005bd | 132 | strncpy(stringcopy_isr_pc, rx_buffer_pc, rx_buffer_pc_poi); |
django | 0:80d3786005bd | 133 | rx_buffer_pc_poi=0; |
django | 0:80d3786005bd | 134 | } |
django | 0:80d3786005bd | 135 | } |
django | 0:80d3786005bd | 136 | while (rx_buffer_pc_poi < RX_BUFFER_PC_SIZE && (c != '\n') && pc.readable()); |
django | 0:80d3786005bd | 137 | } |
django | 0:80d3786005bd | 138 | } |
django | 0:80d3786005bd | 139 | |
django | 0:80d3786005bd | 140 | //***************** TICKER ISR MyTICKER **************************************************** |
django | 0:80d3786005bd | 141 | void MyTicker_isr() |
django | 0:80d3786005bd | 142 | { |
django | 0:80d3786005bd | 143 | if (stringComplete_pc) // set in interrupt |
django | 0:80d3786005bd | 144 | { |
django | 0:80d3786005bd | 145 | strncpy(stringcopy_pc, stringcopy_isr_pc, (unsigned)strlen(stringcopy_isr_pc)); |
django | 0:80d3786005bd | 146 | parse_pc(stringcopy_pc); // handle it and respond |
django | 0:80d3786005bd | 147 | stringComplete_pc = false; // reset |
django | 0:80d3786005bd | 148 | } |
django | 0:80d3786005bd | 149 | Myled = !Myled; |
django | 0:80d3786005bd | 150 | } |
django | 0:80d3786005bd | 151 | //***************** THE END **************************************************************** |