wikara gunawan
/
DALI_RX_F446RE_NEW
DALI RECEIVE CODE
main.cpp@1:701388f40ea3, 2021-01-16 (annotated)
- Committer:
- wikarapg
- Date:
- Sat Jan 16 16:37:51 2021 +0000
- Revision:
- 1:701388f40ea3
- Parent:
- 0:d01bd4b990f0
update to mbed-os 5
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wikarapg | 0:d01bd4b990f0 | 1 | #include "mbed.h" |
wikarapg | 0:d01bd4b990f0 | 2 | DigitalOut myled(LED1); |
wikarapg | 0:d01bd4b990f0 | 3 | InterruptIn pulse(D8); //Pulse Monitoring Line |
wikarapg | 0:d01bd4b990f0 | 4 | Ticker timer; |
wikarapg | 0:d01bd4b990f0 | 5 | Timer t; |
wikarapg | 0:d01bd4b990f0 | 6 | Timer s; |
wikarapg | 0:d01bd4b990f0 | 7 | Timer bitime_low; |
wikarapg | 0:d01bd4b990f0 | 8 | Timer bitime_high; |
wikarapg | 0:d01bd4b990f0 | 9 | |
wikarapg | 0:d01bd4b990f0 | 10 | int highcount; |
wikarapg | 0:d01bd4b990f0 | 11 | int lowcount; |
wikarapg | 0:d01bd4b990f0 | 12 | int arr[17]; //Create an array of 8 se |
wikarapg | 0:d01bd4b990f0 | 13 | int bit[8]; |
wikarapg | 0:d01bd4b990f0 | 14 | int i; |
wikarapg | 0:d01bd4b990f0 | 15 | int k; |
wikarapg | 0:d01bd4b990f0 | 16 | int startbit; //Parity bit to check / header bit |
wikarapg | 0:d01bd4b990f0 | 17 | int addbit; // Int address to send bit to array |
wikarapg | 0:d01bd4b990f0 | 18 | int addbit_low; |
wikarapg | 0:d01bd4b990f0 | 19 | int addbit_high; |
wikarapg | 0:d01bd4b990f0 | 20 | int packet_fin; |
wikarapg | 0:d01bd4b990f0 | 21 | int startime; |
wikarapg | 0:d01bd4b990f0 | 22 | int firstbit; |
wikarapg | 0:d01bd4b990f0 | 23 | int fall_end; |
wikarapg | 0:d01bd4b990f0 | 24 | int rise_end; |
wikarapg | 0:d01bd4b990f0 | 25 | |
wikarapg | 0:d01bd4b990f0 | 26 | |
wikarapg | 0:d01bd4b990f0 | 27 | void risephase(){ |
wikarapg | 0:d01bd4b990f0 | 28 | fall_end = 0; |
wikarapg | 0:d01bd4b990f0 | 29 | bitime_low.stop(); |
wikarapg | 0:d01bd4b990f0 | 30 | bitime_high.start(); |
wikarapg | 0:d01bd4b990f0 | 31 | addbit_high = bitime_low.read_us(); |
wikarapg | 0:d01bd4b990f0 | 32 | |
wikarapg | 0:d01bd4b990f0 | 33 | if (addbit_high < 855 && i < 17){ |
wikarapg | 0:d01bd4b990f0 | 34 | if(addbit_high > 800) { |
wikarapg | 0:d01bd4b990f0 | 35 | arr[i] = 0; |
wikarapg | 0:d01bd4b990f0 | 36 | arr[i+1] = 0; |
wikarapg | 0:d01bd4b990f0 | 37 | i++; |
wikarapg | 0:d01bd4b990f0 | 38 | i++; |
wikarapg | 0:d01bd4b990f0 | 39 | } |
wikarapg | 0:d01bd4b990f0 | 40 | else{ |
wikarapg | 0:d01bd4b990f0 | 41 | arr[i] = 0; |
wikarapg | 0:d01bd4b990f0 | 42 | i++; |
wikarapg | 0:d01bd4b990f0 | 43 | } |
wikarapg | 0:d01bd4b990f0 | 44 | |
wikarapg | 0:d01bd4b990f0 | 45 | } |
wikarapg | 0:d01bd4b990f0 | 46 | else { |
wikarapg | 0:d01bd4b990f0 | 47 | packet_fin = 1; |
wikarapg | 0:d01bd4b990f0 | 48 | } |
wikarapg | 0:d01bd4b990f0 | 49 | bitime_low.reset(); |
wikarapg | 0:d01bd4b990f0 | 50 | rise_end = 1; //Switch states on risephase -> fallphase |
wikarapg | 0:d01bd4b990f0 | 51 | } |
wikarapg | 0:d01bd4b990f0 | 52 | |
wikarapg | 0:d01bd4b990f0 | 53 | |
wikarapg | 0:d01bd4b990f0 | 54 | void fallphase(){ |
wikarapg | 0:d01bd4b990f0 | 55 | rise_end = 0; |
wikarapg | 0:d01bd4b990f0 | 56 | bitime_high.stop(); |
wikarapg | 0:d01bd4b990f0 | 57 | bitime_low.start(); |
wikarapg | 0:d01bd4b990f0 | 58 | addbit_low = bitime_high.read_us(); |
wikarapg | 0:d01bd4b990f0 | 59 | if(i == 15 && addbit > 416){ |
wikarapg | 0:d01bd4b990f0 | 60 | arr[i+1] = 1; |
wikarapg | 0:d01bd4b990f0 | 61 | packet_fin = 1; |
wikarapg | 0:d01bd4b990f0 | 62 | } |
wikarapg | 0:d01bd4b990f0 | 63 | if (addbit_low < 855 && i < 16){ |
wikarapg | 0:d01bd4b990f0 | 64 | if(addbit_low > 800) { |
wikarapg | 0:d01bd4b990f0 | 65 | arr[i] = 1; |
wikarapg | 0:d01bd4b990f0 | 66 | arr[i+1] = 1; |
wikarapg | 0:d01bd4b990f0 | 67 | i++; |
wikarapg | 0:d01bd4b990f0 | 68 | i++; |
wikarapg | 0:d01bd4b990f0 | 69 | } |
wikarapg | 0:d01bd4b990f0 | 70 | else{ |
wikarapg | 0:d01bd4b990f0 | 71 | arr[i] = 1; |
wikarapg | 0:d01bd4b990f0 | 72 | i++; |
wikarapg | 0:d01bd4b990f0 | 73 | } |
wikarapg | 0:d01bd4b990f0 | 74 | } |
wikarapg | 0:d01bd4b990f0 | 75 | else{ |
wikarapg | 0:d01bd4b990f0 | 76 | packet_fin = 1; |
wikarapg | 0:d01bd4b990f0 | 77 | |
wikarapg | 0:d01bd4b990f0 | 78 | } |
wikarapg | 0:d01bd4b990f0 | 79 | |
wikarapg | 0:d01bd4b990f0 | 80 | bitime_high.reset(); |
wikarapg | 0:d01bd4b990f0 | 81 | fall_end = 1; |
wikarapg | 0:d01bd4b990f0 | 82 | //Switch states on fallphase -> risephase / return to previous state |
wikarapg | 0:d01bd4b990f0 | 83 | } |
wikarapg | 0:d01bd4b990f0 | 84 | |
wikarapg | 0:d01bd4b990f0 | 85 | void starthigh() { |
wikarapg | 0:d01bd4b990f0 | 86 | if (startbit == 0){ |
wikarapg | 0:d01bd4b990f0 | 87 | startbit = 1; //Activate next state |
wikarapg | 0:d01bd4b990f0 | 88 | bitime_high.reset(); //No error on bittime bcs didn't reset |
wikarapg | 0:d01bd4b990f0 | 89 | bitime_high.start(); |
wikarapg | 0:d01bd4b990f0 | 90 | s.stop(); |
wikarapg | 0:d01bd4b990f0 | 91 | startime = s.read_us(); |
wikarapg | 0:d01bd4b990f0 | 92 | |
wikarapg | 0:d01bd4b990f0 | 93 | |
wikarapg | 0:d01bd4b990f0 | 94 | //pulse.fall(&fallphase); |
wikarapg | 0:d01bd4b990f0 | 95 | ///Reset timer |
wikarapg | 0:d01bd4b990f0 | 96 | //if(startime > 416 && startime < 430){ |
wikarapg | 0:d01bd4b990f0 | 97 | // startbit = 1; |
wikarapg | 0:d01bd4b990f0 | 98 | // } |
wikarapg | 0:d01bd4b990f0 | 99 | s.reset(); |
wikarapg | 0:d01bd4b990f0 | 100 | rise_end = 1; |
wikarapg | 0:d01bd4b990f0 | 101 | } |
wikarapg | 0:d01bd4b990f0 | 102 | } |
wikarapg | 0:d01bd4b990f0 | 103 | |
wikarapg | 0:d01bd4b990f0 | 104 | void startlow() { |
wikarapg | 0:d01bd4b990f0 | 105 | //wait_us(400); |
wikarapg | 0:d01bd4b990f0 | 106 | s.start(); |
wikarapg | 0:d01bd4b990f0 | 107 | |
wikarapg | 0:d01bd4b990f0 | 108 | if (startbit == 0){ |
wikarapg | 0:d01bd4b990f0 | 109 | pulse.rise(&starthigh); |
wikarapg | 0:d01bd4b990f0 | 110 | } ///Gets starthigh |
wikarapg | 0:d01bd4b990f0 | 111 | } |
wikarapg | 0:d01bd4b990f0 | 112 | |
wikarapg | 0:d01bd4b990f0 | 113 | |
wikarapg | 0:d01bd4b990f0 | 114 | int main(){ |
wikarapg | 0:d01bd4b990f0 | 115 | while(1){ |
wikarapg | 0:d01bd4b990f0 | 116 | printf("hello world"); |
wikarapg | 0:d01bd4b990f0 | 117 | int j; |
wikarapg | 0:d01bd4b990f0 | 118 | |
wikarapg | 0:d01bd4b990f0 | 119 | pulse.fall(&startlow); // Detect 1st bit as a parity bit / 1st State |
wikarapg | 0:d01bd4b990f0 | 120 | |
wikarapg | 0:d01bd4b990f0 | 121 | if (startbit == 1){ // Detect the first fall after parity bit is confirmed, to check the length is X or 2X |
wikarapg | 0:d01bd4b990f0 | 122 | //pulse.fall(&first_fallphase); // Enter state to detect phaseshifts |
wikarapg | 0:d01bd4b990f0 | 123 | |
wikarapg | 0:d01bd4b990f0 | 124 | while(i < 17){ //Loop to process if bit index less than 16 |
wikarapg | 0:d01bd4b990f0 | 125 | if (fall_end == 1){ |
wikarapg | 0:d01bd4b990f0 | 126 | pulse.rise(&risephase); |
wikarapg | 0:d01bd4b990f0 | 127 | } |
wikarapg | 0:d01bd4b990f0 | 128 | if (rise_end == 1){ |
wikarapg | 0:d01bd4b990f0 | 129 | pulse.fall(&fallphase); |
wikarapg | 0:d01bd4b990f0 | 130 | } |
wikarapg | 0:d01bd4b990f0 | 131 | } |
wikarapg | 0:d01bd4b990f0 | 132 | |
wikarapg | 0:d01bd4b990f0 | 133 | //while(bitcount < 8){ |
wikarapg | 0:d01bd4b990f0 | 134 | // pulse.rise(&red); //Service RISING EDGE Interrupt to pointer &NAME |
wikarapg | 0:d01bd4b990f0 | 135 | // pulse.fall(&grn); //Service FALLING EDGE Interrupt to pointer &NAME |
wikarapg | 0:d01bd4b990f0 | 136 | //} |
wikarapg | 0:d01bd4b990f0 | 137 | |
wikarapg | 0:d01bd4b990f0 | 138 | if (packet_fin == 1) { //Finish coding one full packet |
wikarapg | 0:d01bd4b990f0 | 139 | |
wikarapg | 0:d01bd4b990f0 | 140 | |
wikarapg | 0:d01bd4b990f0 | 141 | |
wikarapg | 0:d01bd4b990f0 | 142 | printf("parity: %d\n", startbit); |
wikarapg | 0:d01bd4b990f0 | 143 | printf("startime: %d\n", startime); |
wikarapg | 0:d01bd4b990f0 | 144 | printf("firstime: %d\n", firstbit); |
wikarapg | 0:d01bd4b990f0 | 145 | i = 0; //Reset counter to fill a full array |
wikarapg | 0:d01bd4b990f0 | 146 | |
wikarapg | 0:d01bd4b990f0 | 147 | startbit = 0; //Reset startbit to zero |
wikarapg | 0:d01bd4b990f0 | 148 | //printf("%d\n", lowcount); |
wikarapg | 0:d01bd4b990f0 | 149 | j = 0; |
wikarapg | 0:d01bd4b990f0 | 150 | |
wikarapg | 0:d01bd4b990f0 | 151 | for (j = 1; j < 17; j++) { |
wikarapg | 0:d01bd4b990f0 | 152 | printf("%d ", arr[j]); |
wikarapg | 0:d01bd4b990f0 | 153 | } |
wikarapg | 0:d01bd4b990f0 | 154 | printf("\n"); |
wikarapg | 0:d01bd4b990f0 | 155 | j = 0; |
wikarapg | 0:d01bd4b990f0 | 156 | k = 0; |
wikarapg | 0:d01bd4b990f0 | 157 | for (j = 1; j < 17; j++) { |
wikarapg | 0:d01bd4b990f0 | 158 | if (arr[j] == 1 && arr[j+1] == 0){ |
wikarapg | 0:d01bd4b990f0 | 159 | bit[k] = 0; |
wikarapg | 0:d01bd4b990f0 | 160 | k++; |
wikarapg | 0:d01bd4b990f0 | 161 | j++; |
wikarapg | 0:d01bd4b990f0 | 162 | } |
wikarapg | 0:d01bd4b990f0 | 163 | else if (arr[j] == 0 && arr[j+1] == 1){ |
wikarapg | 0:d01bd4b990f0 | 164 | |
wikarapg | 0:d01bd4b990f0 | 165 | bit[k] = 1; |
wikarapg | 0:d01bd4b990f0 | 166 | k++; |
wikarapg | 0:d01bd4b990f0 | 167 | j++; |
wikarapg | 0:d01bd4b990f0 | 168 | |
wikarapg | 0:d01bd4b990f0 | 169 | |
wikarapg | 0:d01bd4b990f0 | 170 | } |
wikarapg | 0:d01bd4b990f0 | 171 | } |
wikarapg | 0:d01bd4b990f0 | 172 | j = 0; |
wikarapg | 0:d01bd4b990f0 | 173 | |
wikarapg | 0:d01bd4b990f0 | 174 | |
wikarapg | 0:d01bd4b990f0 | 175 | for (j = 0; j < 8; j++) { |
wikarapg | 0:d01bd4b990f0 | 176 | printf("%d ", bit[j]); |
wikarapg | 0:d01bd4b990f0 | 177 | } |
wikarapg | 0:d01bd4b990f0 | 178 | |
wikarapg | 0:d01bd4b990f0 | 179 | j = 0; //Reset Counters |
wikarapg | 0:d01bd4b990f0 | 180 | k = 0; //Reset Counters |
wikarapg | 0:d01bd4b990f0 | 181 | |
wikarapg | 0:d01bd4b990f0 | 182 | printf("\n"); |
wikarapg | 0:d01bd4b990f0 | 183 | highcount = 0; |
wikarapg | 0:d01bd4b990f0 | 184 | lowcount = 0; |
wikarapg | 0:d01bd4b990f0 | 185 | |
wikarapg | 0:d01bd4b990f0 | 186 | for (j = 0; j < 17; j++) { |
wikarapg | 0:d01bd4b990f0 | 187 | arr[j] = 0; |
wikarapg | 0:d01bd4b990f0 | 188 | } |
wikarapg | 0:d01bd4b990f0 | 189 | for (j = 0; j < 8; j++) { |
wikarapg | 0:d01bd4b990f0 | 190 | bit[j] = 0; |
wikarapg | 0:d01bd4b990f0 | 191 | } |
wikarapg | 0:d01bd4b990f0 | 192 | |
wikarapg | 0:d01bd4b990f0 | 193 | } |
wikarapg | 0:d01bd4b990f0 | 194 | |
wikarapg | 0:d01bd4b990f0 | 195 | } |
wikarapg | 0:d01bd4b990f0 | 196 | |
wikarapg | 0:d01bd4b990f0 | 197 | |
wikarapg | 0:d01bd4b990f0 | 198 | } |
wikarapg | 0:d01bd4b990f0 | 199 | } |