![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
envio teste
Dependencies: C12832 XBeeGetRssi mbed xbee_lib
main.cpp@0:f087a480051a, 2015-06-24 (annotated)
- Committer:
- mbastos
- Date:
- Wed Jun 24 19:06:55 2015 +0000
- Revision:
- 0:f087a480051a
teste comunica??o
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbastos | 0:f087a480051a | 1 | #include "mbed.h" |
mbastos | 0:f087a480051a | 2 | #include "C12832.h" |
mbastos | 0:f087a480051a | 3 | |
mbastos | 0:f087a480051a | 4 | Serial pc(USBTX, USBRX); // tx, rx |
mbastos | 0:f087a480051a | 5 | Serial xbee1(p9,p10); |
mbastos | 0:f087a480051a | 6 | DigitalOut rst1(p11); //Digital reset for the XBee, 200ns for reset |
mbastos | 0:f087a480051a | 7 | Serial serial (p9,p10); |
mbastos | 0:f087a480051a | 8 | C12832 lcd(p5, p7, p6, p8, p11); //C12832(PinName mosi, PinName sck, PinName reset, PinName a0, PinName ncs, const char* name = "LCD"); |
mbastos | 0:f087a480051a | 9 | |
mbastos | 0:f087a480051a | 10 | |
mbastos | 0:f087a480051a | 11 | //variaveis globais |
mbastos | 0:f087a480051a | 12 | int rssi=0; |
mbastos | 0:f087a480051a | 13 | int atdb=0; |
mbastos | 0:f087a480051a | 14 | int recepcao; |
mbastos | 0:f087a480051a | 15 | char mens[20]; |
mbastos | 0:f087a480051a | 16 | char mens1[20]; |
mbastos | 0:f087a480051a | 17 | unsigned char cont=0; |
mbastos | 0:f087a480051a | 18 | unsigned char cont1=0; |
mbastos | 0:f087a480051a | 19 | unsigned char tam=0; |
mbastos | 0:f087a480051a | 20 | unsigned char cks=0; |
mbastos | 0:f087a480051a | 21 | unsigned char estado=0; |
mbastos | 0:f087a480051a | 22 | unsigned char estado1=0; |
mbastos | 0:f087a480051a | 23 | //unsigned char rssi=0; |
mbastos | 0:f087a480051a | 24 | unsigned char temp=0; |
mbastos | 0:f087a480051a | 25 | unsigned char temp1=0; |
mbastos | 0:f087a480051a | 26 | |
mbastos | 0:f087a480051a | 27 | |
mbastos | 0:f087a480051a | 28 | |
mbastos | 0:f087a480051a | 29 | int main(void){ |
mbastos | 0:f087a480051a | 30 | |
mbastos | 0:f087a480051a | 31 | xbee1.baud(9600); |
mbastos | 0:f087a480051a | 32 | pc.baud(9600); |
mbastos | 0:f087a480051a | 33 | lcd.locate(0,3); |
mbastos | 0:f087a480051a | 34 | while(1){ |
mbastos | 0:f087a480051a | 35 | switch(estado){ |
mbastos | 0:f087a480051a | 36 | case 0: |
mbastos | 0:f087a480051a | 37 | INI: |
mbastos | 0:f087a480051a | 38 | while(!xbee1.readable()){ |
mbastos | 0:f087a480051a | 39 | lcd.locate(0,0); |
mbastos | 0:f087a480051a | 40 | lcd.cls(); |
mbastos | 0:f087a480051a | 41 | lcd.printf("Aguardando..."); |
mbastos | 0:f087a480051a | 42 | } |
mbastos | 0:f087a480051a | 43 | temp = xbee1.getc(); |
mbastos | 0:f087a480051a | 44 | if(temp==0x7E){ |
mbastos | 0:f087a480051a | 45 | cont=0; |
mbastos | 0:f087a480051a | 46 | } |
mbastos | 0:f087a480051a | 47 | mens[cont]=temp; |
mbastos | 0:f087a480051a | 48 | //lcd.locate(0,0); |
mbastos | 0:f087a480051a | 49 | //lcd.cls(); |
mbastos | 0:f087a480051a | 50 | //lcd.printf("Recebeu %x cont=%d",temp,cont);//debug - recebeu o primeiro caracter? |
mbastos | 0:f087a480051a | 51 | //wait(0.5); |
mbastos | 0:f087a480051a | 52 | temp = 0; |
mbastos | 0:f087a480051a | 53 | cont++; |
mbastos | 0:f087a480051a | 54 | if(cont==(mens[2]+4)){ //compara o tamanho enviado + 4 bytes (1 de inicio,2 de tamanho e checksum) para |
mbastos | 0:f087a480051a | 55 | estado = 1; // verificar se chegou ao final do frame, de maneira dinâmica |
mbastos | 0:f087a480051a | 56 | //lcd.cls(); |
mbastos | 0:f087a480051a | 57 | //lcd.locate(0,10); |
mbastos | 0:f087a480051a | 58 | //lcd.printf("Frame 1 - Completo");//debug - completou o frame? |
mbastos | 0:f087a480051a | 59 | //wait(0.5); |
mbastos | 0:f087a480051a | 60 | } |
mbastos | 0:f087a480051a | 61 | break; |
mbastos | 0:f087a480051a | 62 | case 1: //montou o primeiro pacote |
mbastos | 0:f087a480051a | 63 | //lcd.locate(0,20); |
mbastos | 0:f087a480051a | 64 | //lcd.printf("M[%x %x %x %x %x %x %x %x %x %x ]",mens[0],mens[1],mens[2],mens[3],mens[4],mens[5],mens[6],mens[7],mens[8],mens[9]);//debug - recebeu o primeiro caracter? |
mbastos | 0:f087a480051a | 65 | //wait(7);//debug |
mbastos | 0:f087a480051a | 66 | |
mbastos | 0:f087a480051a | 67 | //lcd.locate(0,0); |
mbastos | 0:f087a480051a | 68 | //lcd.cls();//debug |
mbastos | 0:f087a480051a | 69 | //lcd.printf("Verificando..."); |
mbastos | 0:f087a480051a | 70 | //wait(0.5); |
mbastos | 0:f087a480051a | 71 | REP: xbee1.printf("%c%c%c%c%c%c%c%c",0x7E,0X00,0X04,0X08,0X01,0X64,0X62,0X30); |
mbastos | 0:f087a480051a | 72 | //wait(2); |
mbastos | 0:f087a480051a | 73 | //lcd.locate(0,10); |
mbastos | 0:f087a480051a | 74 | //lcd.cls();//debug |
mbastos | 0:f087a480051a | 75 | //lcd.printf("passou."); |
mbastos | 0:f087a480051a | 76 | //wait(1); |
mbastos | 0:f087a480051a | 77 | while (atdb==0){ |
mbastos | 0:f087a480051a | 78 | switch(estado1){ |
mbastos | 0:f087a480051a | 79 | case 0: |
mbastos | 0:f087a480051a | 80 | while(!xbee1.readable()){ |
mbastos | 0:f087a480051a | 81 | lcd.locate(0,0); |
mbastos | 0:f087a480051a | 82 | lcd.cls(); |
mbastos | 0:f087a480051a | 83 | lcd.printf("Aguardando ATDB..."); |
mbastos | 0:f087a480051a | 84 | } |
mbastos | 0:f087a480051a | 85 | temp1 = xbee1.getc();//gets(temp); |
mbastos | 0:f087a480051a | 86 | if(temp1==0x7E){ |
mbastos | 0:f087a480051a | 87 | cont1=0; |
mbastos | 0:f087a480051a | 88 | } |
mbastos | 0:f087a480051a | 89 | mens1[cont1]=temp1; |
mbastos | 0:f087a480051a | 90 | //lcd.locate(0,0); |
mbastos | 0:f087a480051a | 91 | //lcd.cls(); |
mbastos | 0:f087a480051a | 92 | //lcd.printf("Recebeu %x ATDB=%d",temp1,cont1);//debug - recebeu o primeiro caracter? |
mbastos | 0:f087a480051a | 93 | //wait(1); |
mbastos | 0:f087a480051a | 94 | temp1 = 0; |
mbastos | 0:f087a480051a | 95 | cont1++; |
mbastos | 0:f087a480051a | 96 | if(cont1==(mens1[2]+4)){ |
mbastos | 0:f087a480051a | 97 | estado1 = 1; |
mbastos | 0:f087a480051a | 98 | //lcd.locate(0,10); |
mbastos | 0:f087a480051a | 99 | //lcd.cls(); |
mbastos | 0:f087a480051a | 100 | //lcd.printf("Frame 2 - Completo");//debug - completou o frame? |
mbastos | 0:f087a480051a | 101 | //wait(0.5); |
mbastos | 0:f087a480051a | 102 | } |
mbastos | 0:f087a480051a | 103 | break; |
mbastos | 0:f087a480051a | 104 | case 1: //ja montou o frame AT response |
mbastos | 0:f087a480051a | 105 | atdb=1; |
mbastos | 0:f087a480051a | 106 | switch(mens1[3]){ |
mbastos | 0:f087a480051a | 107 | case 0x88: |
mbastos | 0:f087a480051a | 108 | lcd.locate(0,10); |
mbastos | 0:f087a480051a | 109 | lcd.cls(); |
mbastos | 0:f087a480051a | 110 | lcd.printf("Frame ATDB:mens1[3]=%x",mens1[3]);//debug para confirmar frame ATDB |
mbastos | 0:f087a480051a | 111 | //wait(1); |
mbastos | 0:f087a480051a | 112 | rssi=mens1[8]; |
mbastos | 0:f087a480051a | 113 | lcd.locate(0,10); |
mbastos | 0:f087a480051a | 114 | lcd.cls(); |
mbastos | 0:f087a480051a | 115 | lcd.printf("RSSI=%x",mens1[8]);//debug para confirmar frame ATDB |
mbastos | 0:f087a480051a | 116 | wait(0.1); |
mbastos | 0:f087a480051a | 117 | estado=0; |
mbastos | 0:f087a480051a | 118 | cont=0; |
mbastos | 0:f087a480051a | 119 | atdb=0; |
mbastos | 0:f087a480051a | 120 | goto INI; |
mbastos | 0:f087a480051a | 121 | break; |
mbastos | 0:f087a480051a | 122 | default: |
mbastos | 0:f087a480051a | 123 | estado1=0; |
mbastos | 0:f087a480051a | 124 | cont1=0; |
mbastos | 0:f087a480051a | 125 | temp1=0; |
mbastos | 0:f087a480051a | 126 | lcd.locate(0,10); |
mbastos | 0:f087a480051a | 127 | lcd.cls(); |
mbastos | 0:f087a480051a | 128 | lcd.printf("ERRO: Nao eh ATDB:mens1[3]=%x",mens1[3]);//debug para confirmar frame ATDB |
mbastos | 0:f087a480051a | 129 | wait(1); |
mbastos | 0:f087a480051a | 130 | estado=0; |
mbastos | 0:f087a480051a | 131 | cont=0; |
mbastos | 0:f087a480051a | 132 | atdb=0; |
mbastos | 0:f087a480051a | 133 | goto INI; |
mbastos | 0:f087a480051a | 134 | }//fecha switch mensg[3]=0x88 |
mbastos | 0:f087a480051a | 135 | break; |
mbastos | 0:f087a480051a | 136 | }//fecha switch estado1 |
mbastos | 0:f087a480051a | 137 | }//fecha while atdb |
mbastos | 0:f087a480051a | 138 | break; |
mbastos | 0:f087a480051a | 139 | }//fecha switch estado |
mbastos | 0:f087a480051a | 140 | |
mbastos | 0:f087a480051a | 141 | |
mbastos | 0:f087a480051a | 142 | }//fecha while1 |
mbastos | 0:f087a480051a | 143 | }//fecha int main() |
mbastos | 0:f087a480051a | 144 | |
mbastos | 0:f087a480051a | 145 |