Guojun Jiang
/
StewartPlatform
stewartplatform
Fork of PwmOut_HelloWorld by
src/usart.cpp@3:c5ea5b6a7460, 2016-08-20 (annotated)
- Committer:
- wheels
- Date:
- Sat Aug 20 12:06:14 2016 +0000
- Revision:
- 3:c5ea5b6a7460
- Parent:
- 2:50062ac8646d
update
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wheels | 2:50062ac8646d | 1 | #include "usart.h" |
wheels | 2:50062ac8646d | 2 | |
wheels | 3:c5ea5b6a7460 | 3 | Serial usart(PA_9, PA_10); |
wheels | 2:50062ac8646d | 4 | |
wheels | 2:50062ac8646d | 5 | char positionTemp[7]; |
wheels | 2:50062ac8646d | 6 | char position[6]; |
wheels | 2:50062ac8646d | 7 | bool instructionReceived = false; |
wheels | 2:50062ac8646d | 8 | |
wheels | 2:50062ac8646d | 9 | //serial interupt service function |
wheels | 2:50062ac8646d | 10 | //ͨÐÅÐÒé£bpositionTemp = {0xff, ID, angle, 0xEE} |
wheels | 2:50062ac8646d | 11 | // angle·¶Î§£º[0,180]£¬¶ÔÓ¦[-90, 90] |
wheels | 2:50062ac8646d | 12 | typedef enum |
wheels | 2:50062ac8646d | 13 | { |
wheels | 2:50062ac8646d | 14 | CheckS,CheckW,Checka,Checkb,Checkc,Checkd,Checke,Checkf,CheckSum |
wheels | 2:50062ac8646d | 15 | }STATE; |
wheels | 2:50062ac8646d | 16 | |
wheels | 2:50062ac8646d | 17 | void usart_ISR() { |
wheels | 2:50062ac8646d | 18 | //if (usart.readable()) |
wheels | 2:50062ac8646d | 19 | { |
wheels | 2:50062ac8646d | 20 | char data = usart.getc(); |
wheels | 2:50062ac8646d | 21 | //usart.printf("%d \n", data); |
wheels | 2:50062ac8646d | 22 | static STATE State = CheckS; |
wheels | 2:50062ac8646d | 23 | switch(State) |
wheels | 2:50062ac8646d | 24 | { |
wheels | 2:50062ac8646d | 25 | case CheckS: |
wheels | 2:50062ac8646d | 26 | if(data==0XFF) { |
wheels | 2:50062ac8646d | 27 | State=CheckW; |
wheels | 2:50062ac8646d | 28 | //usart.printf("receive S\n"); |
wheels | 2:50062ac8646d | 29 | //instructionReceived = false; |
wheels | 2:50062ac8646d | 30 | } |
wheels | 2:50062ac8646d | 31 | else |
wheels | 2:50062ac8646d | 32 | State=CheckS; |
wheels | 2:50062ac8646d | 33 | break; |
wheels | 2:50062ac8646d | 34 | case CheckW: |
wheels | 2:50062ac8646d | 35 | if(data==0XFE){ |
wheels | 2:50062ac8646d | 36 | State=Checka; |
wheels | 2:50062ac8646d | 37 | //usart.printf("receive W\n"); |
wheels | 2:50062ac8646d | 38 | } |
wheels | 2:50062ac8646d | 39 | else if(data==0XFF) |
wheels | 2:50062ac8646d | 40 | State=CheckW; |
wheels | 2:50062ac8646d | 41 | else |
wheels | 2:50062ac8646d | 42 | State=CheckS; |
wheels | 2:50062ac8646d | 43 | break; |
wheels | 2:50062ac8646d | 44 | case Checka: |
wheels | 2:50062ac8646d | 45 | positionTemp[0]=data; |
wheels | 2:50062ac8646d | 46 | State=Checkb; |
wheels | 2:50062ac8646d | 47 | //usart.printf("receive pos 0 \n"); |
wheels | 2:50062ac8646d | 48 | break; |
wheels | 2:50062ac8646d | 49 | case Checkb: |
wheels | 2:50062ac8646d | 50 | positionTemp[1]=data; |
wheels | 2:50062ac8646d | 51 | State=Checkc; |
wheels | 2:50062ac8646d | 52 | //usart.printf("receive pos 1 \n"); |
wheels | 2:50062ac8646d | 53 | break; |
wheels | 2:50062ac8646d | 54 | case Checkc: |
wheels | 2:50062ac8646d | 55 | positionTemp[2]=data; |
wheels | 2:50062ac8646d | 56 | //usart.printf("receive pos 2 \n"); |
wheels | 2:50062ac8646d | 57 | State=Checkd; |
wheels | 2:50062ac8646d | 58 | break; |
wheels | 2:50062ac8646d | 59 | case Checkd: |
wheels | 2:50062ac8646d | 60 | positionTemp[3]=data; |
wheels | 2:50062ac8646d | 61 | //usart.printf("receive pos 3 \n"); |
wheels | 2:50062ac8646d | 62 | State=Checke; |
wheels | 2:50062ac8646d | 63 | break; |
wheels | 2:50062ac8646d | 64 | case Checke: |
wheels | 2:50062ac8646d | 65 | positionTemp[4]=data; |
wheels | 2:50062ac8646d | 66 | //usart.printf("receive pos 4 \n"); |
wheels | 2:50062ac8646d | 67 | State=Checkf; |
wheels | 2:50062ac8646d | 68 | break; |
wheels | 2:50062ac8646d | 69 | case Checkf: |
wheels | 2:50062ac8646d | 70 | positionTemp[5]=data; |
wheels | 2:50062ac8646d | 71 | //usart.printf("receive pos 5 \n"); |
wheels | 2:50062ac8646d | 72 | State=CheckSum; |
wheels | 2:50062ac8646d | 73 | break; |
wheels | 2:50062ac8646d | 74 | case CheckSum: |
wheels | 2:50062ac8646d | 75 | positionTemp[6]=data; |
wheels | 2:50062ac8646d | 76 | //usart.printf("receive sum \n"); |
wheels | 2:50062ac8646d | 77 | //if(positionTemp[6]==(char)(positionTemp[0]+positionTemp[1]+positionTemp[2]+positionTemp[3]+positionTemp[4]+positionTemp[5])) |
wheels | 2:50062ac8646d | 78 | { |
wheels | 2:50062ac8646d | 79 | position[0] = positionTemp[0]; |
wheels | 2:50062ac8646d | 80 | position[1] = positionTemp[1]; |
wheels | 2:50062ac8646d | 81 | position[2] = positionTemp[2]; |
wheels | 2:50062ac8646d | 82 | position[3] = positionTemp[3]; |
wheels | 2:50062ac8646d | 83 | position[4] = positionTemp[4]; |
wheels | 2:50062ac8646d | 84 | position[5] = positionTemp[5]; |
wheels | 2:50062ac8646d | 85 | instructionReceived = true; |
wheels | 2:50062ac8646d | 86 | } |
wheels | 2:50062ac8646d | 87 | State=CheckS; |
wheels | 2:50062ac8646d | 88 | break; |
wheels | 2:50062ac8646d | 89 | default: |
wheels | 2:50062ac8646d | 90 | State=CheckS; |
wheels | 2:50062ac8646d | 91 | //instructionReceived = false; |
wheels | 2:50062ac8646d | 92 | break; |
wheels | 2:50062ac8646d | 93 | } |
wheels | 2:50062ac8646d | 94 | |
wheels | 2:50062ac8646d | 95 | } |
wheels | 2:50062ac8646d | 96 | } |
wheels | 2:50062ac8646d | 97 | |
wheels | 2:50062ac8646d | 98 | |
wheels | 2:50062ac8646d | 99 | void usart_init(int baud) { |
wheels | 3:c5ea5b6a7460 | 100 | usart.baud(baud); |
wheels | 2:50062ac8646d | 101 | usart.attach(&usart_ISR); // attach serial interupt service function |
wheels | 2:50062ac8646d | 102 | wait(1); |
wheels | 2:50062ac8646d | 103 | //usart.printf("this is a testing code \n"); |
wheels | 2:50062ac8646d | 104 | } |