envio teste

Dependencies:   C12832 XBeeGetRssi mbed xbee_lib

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "C12832.h"
00003 
00004 Serial pc(USBTX, USBRX); // tx, rx
00005 Serial xbee1(p9,p10);
00006 DigitalOut rst1(p11); //Digital reset for the XBee, 200ns for reset
00007 Serial serial (p9,p10);
00008 C12832 lcd(p5, p7, p6, p8, p11); //C12832(PinName mosi, PinName sck, PinName reset, PinName a0, PinName ncs, const char* name = "LCD");
00009 
00010 
00011 //variaveis globais
00012 int rssi=0;
00013 int atdb=0;
00014 int recepcao;
00015 char mens[20];
00016 char mens1[20];
00017 unsigned char cont=0;
00018 unsigned char cont1=0;
00019 unsigned char tam=0;
00020 unsigned char cks=0;
00021 unsigned char estado=0;
00022 unsigned char estado1=0;
00023 //unsigned char rssi=0;
00024 unsigned char temp=0;
00025 unsigned char temp1=0;
00026 
00027 
00028 
00029 int main(void){
00030    
00031     xbee1.baud(9600);
00032     pc.baud(9600);
00033     lcd.locate(0,3);
00034     while(1){
00035          switch(estado){
00036             case 0:
00037                   INI:
00038                   while(!xbee1.readable()){
00039                       lcd.locate(0,0);
00040                       lcd.cls();
00041                       lcd.printf("Aguardando...");
00042                   }
00043                   temp = xbee1.getc();
00044                   if(temp==0x7E){
00045                        cont=0;
00046                   }
00047                   mens[cont]=temp;
00048                   //lcd.locate(0,0);
00049                   //lcd.cls();
00050                   //lcd.printf("Recebeu %x cont=%d",temp,cont);//debug - recebeu o primeiro caracter?
00051                   //wait(0.5);
00052                   temp = 0;
00053                   cont++;
00054                   if(cont==(mens[2]+4)){  //compara o tamanho enviado + 4 bytes (1 de inicio,2 de tamanho e checksum) para
00055                         estado = 1;       //  verificar se chegou ao final do frame, de maneira dinâmica
00056                         //lcd.cls();
00057                         //lcd.locate(0,10);
00058                         //lcd.printf("Frame 1 - Completo");//debug - completou o frame?
00059                         //wait(0.5);
00060                         }
00061             break;
00062             case 1:  //montou o primeiro pacote 
00063                   //lcd.locate(0,20);
00064                   //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?
00065                   //wait(7);//debug
00066                   
00067                   //lcd.locate(0,0);
00068                   //lcd.cls();//debug
00069                   //lcd.printf("Verificando...");
00070                   //wait(0.5);
00071                   REP: xbee1.printf("%c%c%c%c%c%c%c%c",0x7E,0X00,0X04,0X08,0X01,0X64,0X62,0X30);
00072                   //wait(2);
00073                   //lcd.locate(0,10);
00074                   //lcd.cls();//debug
00075                   //lcd.printf("passou.");
00076                   //wait(1);
00077                   while (atdb==0){
00078                     switch(estado1){
00079                         case 0:
00080                             while(!xbee1.readable()){
00081                                     lcd.locate(0,0);
00082                                     lcd.cls();
00083                                     lcd.printf("Aguardando ATDB...");
00084                             }
00085                             temp1 = xbee1.getc();//gets(temp);
00086                             if(temp1==0x7E){
00087                                 cont1=0;
00088                             }
00089                             mens1[cont1]=temp1;
00090                             //lcd.locate(0,0);
00091                             //lcd.cls();
00092                             //lcd.printf("Recebeu %x ATDB=%d",temp1,cont1);//debug - recebeu o primeiro caracter?
00093                             //wait(1);
00094                             temp1 = 0;
00095                             cont1++;
00096                             if(cont1==(mens1[2]+4)){
00097                                     estado1 = 1;
00098                                     //lcd.locate(0,10);
00099                                     //lcd.cls();
00100                                     //lcd.printf("Frame 2 - Completo");//debug - completou o frame?
00101                                     //wait(0.5);
00102                             }
00103                             break;
00104                         case 1: //ja montou o frame AT response
00105                                 atdb=1;
00106                                 switch(mens1[3]){
00107                                         case 0x88:
00108                                                 lcd.locate(0,10);
00109                                                 lcd.cls();
00110                                                 lcd.printf("Frame ATDB:mens1[3]=%x",mens1[3]);//debug para confirmar frame ATDB
00111                                                 //wait(1);
00112                                                 rssi=mens1[8];
00113                                                 lcd.locate(0,10);
00114                                                 lcd.cls();
00115                                                 lcd.printf("RSSI=%x",mens1[8]);//debug para confirmar frame ATDB
00116                                                 wait(0.1);
00117                                                 estado=0;
00118                                                 cont=0;
00119                                                 atdb=0;
00120                                                 goto INI;
00121                                             break;
00122                                         default:
00123                                                 estado1=0;
00124                                                 cont1=0;
00125                                                 temp1=0;
00126                                                 lcd.locate(0,10);
00127                                                 lcd.cls();
00128                                                 lcd.printf("ERRO: Nao eh ATDB:mens1[3]=%x",mens1[3]);//debug para confirmar frame ATDB
00129                                                 wait(1);
00130                                                 estado=0;
00131                                                 cont=0;
00132                                                 atdb=0;
00133                                                 goto INI;                          
00134                            }//fecha switch mensg[3]=0x88
00135                         break;
00136                     }//fecha switch estado1
00137                 }//fecha while atdb 
00138                 break;
00139             }//fecha switch estado           
00140                         
00141             
00142     }//fecha while1
00143 }//fecha int main()
00144 
00145