void EmptySerialBuffer(void) { char recieveChar; while (UART1_Data_Ready() == 1) { recieveChar = UART1_Read(); } }
void Proceso(void){ // Lectura del Voutput del sensor LM35 // Mostrando Voutput y su conversion a °C char val[15]; float volts; float grados; char centigrados[15]; adc_value = ADC1_Get_Sample(10); grados = adc_value/cuentas; FloatToStr(grados,centigrados); UART1_Write_Text(centigrados); UART1_Write(13); UART1_Write(10); Lcd_Out(1, 1, centigrados); Lcd_Out(1, 8, "\xDFc"); volts = adc_value; FloatToStr(volts,val); Lcd_Out(2, 15, "V"); Lcd_Out(2, 1, val); delay(0xFFFFF); Lcd_Cmd(_LCD_CLEAR); if (UART1_Data_Ready()) { // If data is received uart_rd = UART1_Read(); // read the received data if (uart_rd == '1'){ LAMPARA = ~LAMPARA; } else if (uart_rd == '2') { VENTILADOR = ~VENTILADOR; } } }
void main() { int i; trisb=0; portb=0; Lcd_Init(); // Initialize LCD UART1_Init(9600); // Initialize UART module at 9600 bps Delay_ms(100); // Wait for UART module to stabilize while (1) // Endless loop { if (UART1_Data_Ready()) // If data is received, { for(i=0;i<100;i++) { uart_rd[i]= UART1_Read(); } for(i=0;i<100;i++) { Lcd_Cmd(_LCD_CLEAR); // Clear display Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off Lcd_Out(1,1,uart_rd[i]); delay_ms(10000); } } } }
char UART1_Read() { while(!UART1_Data_Ready()); //Waits for Recpetion to complete return Read1USART(); //Returns the 8 bit data /* while(!PIE1bits.RC1IE); //Waits for Recpetion to complete return RCREG1; //Returns the 8 bit data */ }
void main() { int i=0; int j=0; int iu=0; DDRB = 0xFF; UART1_Init(9600); // Initialize UART module at 9600 bps Delay_ms(100); // Wait for UART module to stabilize while (1) { if (UART1_Data_Ready() == 1) { // if data is received UART1_Read_Text(packet, " ", 10); // reads text until 'OK' is found if ((packet[0] == 0x11)&&(packet[1] == 0x01) &&(packet[2] == 0x00)&&(packet[3] == 0x55) ) { packet[0] = 0x11; packet[1] = 0x01; packet[2] = 0x04; packet[3] = 0x00; packet[4] = 0x00; packet[5] = 0x01; packet[6] = 0x23; packet[7] = 0x59; packet[8] = 0xaa; UART1_Write(packet[0]); UART1_Write(packet[1]); UART1_Write(packet[2]); UART1_Write(packet[3]); UART1_Write(packet[4]); UART1_Write(packet[5]); UART1_Write(packet[6]); UART1_Write(packet[7]); UART1_Write(packet[8]); } } } // while (UART1_Data_Ready()) { // packet[i] = UART1_Read(); // i++; // } // Delay_ms(100); // j=0; // while (i>0) { // UART1_Write(packet[j]); // j++; // i--; // } }
void WaitForRecieveChar(char delimiterChar) { char recieveChar = 0; while (1) { if (UART1_Data_Ready() > 0) { recieveChar = UART1_Read(); //UART1_Write(recieveChar); } if (recieveChar == delimiterChar) break; } }
/* Nombre: qtama Descripcion: establece el tamaño de la porcion de comida servida. Parametros: ninguno Retorna: entero sin signo */ unsigned short qtama () { unsigned short kp; qtama: do { kp = Keypad_Key_Click(); // Store key code in kp variable if (UART1_Data_Ready()){ kp = UART1_Read(); goto bluetooth1; } }while (!kp); kp = teclado(kp); bluetooth1: if(kp == 'A' ||kp== 'B' ||kp== 'C'||kp== 'D'){ Lcd_chr(2, 5, kp); do { kp = Keypad_Key_Click(); // Store key code in kp variable if (UART1_Data_Ready()){ kp = UART1_Read(); goto bluetooth2; } }while (!kp); kp = teclado(kp); bluetooth2: if(kp == '#'){ goto qtama; } } else { goto qtama; } return (kp); }
unsigned short qtama () { unsigned short kp; qtama: do { kp = Keypad_Key_Click(); if (UART1_Data_Ready()){ kp = UART1_Read(); goto bluetooth1; } }while (!kp); kp = teclado(kp); bluetooth1: if(kp == 'A' ||kp== 'B' ||kp== 'C'||kp== 'D'){ Lcd_chr(2, 4, kp); do { kp = Keypad_Key_Click(); // Store key code in kp variable if (UART1_Data_Ready()){ kp = UART1_Read(); goto bluetooth2; } }while (!kp); kp = teclado(kp); bluetooth2: if(kp == '#'){ goto qtama; } if(kp == '*'){ Lcd_Chr (2,6,'G'); Lcd_Chr (2,7,'u'); Lcd_Chr (2,8,'a'); Lcd_Chr (2,9,'r');Lcd_Chr (2,10,'d');Lcd_Chr (2,11,'a'); Lcd_Chr (2,12,'d'); Lcd_Chr (2,13,'o'); } }else { goto qtama; } return (kp); }
void interrupt(){ if(PIR1.RCIF) { PORTD.F6 = 0; while(UART1_Data_Ready() == 1){ str[count] = UART1_Read(); count++; if(count == 13){ PORTD.F6 = 0; flag = 1; }else{ PORTD.F6 = 1; } } } }
void WaitForRecieveCharAndBlink(char delimiterChar) { char recieveChar = 0; while (1) { LED_Blink(1); if (UART1_Data_Ready() > 0) { recieveChar = UART1_Read(); //UART1_Write(recieveChar); } if (recieveChar == delimiterChar) { LED_Off(); break; } } }
unsigned short obtenerNumero (unsigned short numMax) { unsigned short dig, numero, kp; volverObtenerNumero: dig = 0; numero=0; leerNum: do { kp = Keypad_Key_Click(); if (UART1_Data_Ready()){ kp = UART1_Read(); goto bluetoothRead; } }while (!kp); kp = teclado(kp); bluetoothRead: if(kp == 'A' || kp== 'B' || kp == 'C' ||kp == 'D'){ goto leerNum; }else if (kp != '#' && kp != '*'){ dig++; numero = numero*10 + kp -48; //Lcd_Chr(2, 7+dig, kp); } //}while (numero > numMax && kp != '*' && kp != '#'); if(kp == '#'){ //si presiona numeral se borra el numero goto volverObtenerNumero; }else if (kp == '*' && numero > 0){ //Lcd_Chr(2, 7, (numero+48)); Lcd_Out(2, 1, "Valor guardado"); delay(); }else if (numero > numMax){ Lcd_Cmd(_LCD_CLEAR); Lcd_Out(2, 1, "Se paso de valor maximo"); delay(); goto volverObtenerNumero; } else{ goto leerNum; } return (numero); }
void main() { TRISA = 0b00000000; // set PORTA -> salidas TRISB = 0b01000000; // set PORTB -> salida menos el pin 6. ANSEL = 0; // Configure AN2 pin as analog ANSELH = 0; C1ON_bit = 0; // Disable comparators C2ON_bit = 0; INTCON = 0b10100000; // INTERRUPCION POR TIMER0 Y GIE ACTIVADOS. /* INTCON.T0IE = 1; /\* Activado interrupcion por timer0 *\/ */ OPTION_REG = 0b10000111; // TMR0 temporizado: RBPU, TOCS= INTERNAL INSTRUCTION CLOCK= 0 PSA =0 1:256 TRISC = 0b10000000; // PUERTOC COMO SALIDA exepto pin 7 PORTC = 0; // LIMPIAR PUERTOC setupTimer1(); Delay_us(10); /* wait for acquisition time*/ Lcd_Init(); // Initialize LCD /* Programa principal */ Lcd_Out(2,0,txtDefault); // Write text in second row UART1_Init(9600); // initialize UART1 module Delay_ms(100); // Wait for UART module to stabilize moverEnAutomatico(); while (1) { waitSignal(); if (UART1_Data_Ready()) { // If data is received, uart_rd = UART1_Read(); // leer el dato recibido del celular waitSignal(); switch (uart_rd) { case 'A': { /* Modo automático */ Lcd_Out(2,6,txtAutomatic); moverEnAutomatico(); break; } case 'M': { moverManual(); break; } default: break; } } } }
unsigned short obtenerNumero (unsigned short numeroMax) { unsigned short dig,numero,kp; volverObtenerNumero: dig = 0; numero=0; Lcd_chr(2, 5, ' '); Lcd_chr(2, 4, ' '); Lcd_chr(2, 3, ' '); Lcd_chr(2, 2, ' '); Lcd_chr(2, 1, ' '); leerNumero: do { kp = Keypad_Key_Click(); // Store key code in kp variable if (UART1_Data_Ready()){ kp = UART1_Read(); goto bluetooth4; } }while (!kp); kp = teclado(kp); bluetooth4: if(kp == 'A' || kp== 'B' || kp == 'C' ||kp == 'D'){ goto leerNumero; }else if (kp>=48 && kp<=57) { dig++; Lcd_chr_Cp(kp); numero = (kp-48)+(10*numero); } if(kp == '#'){ goto volverObtenerNumero; }else if (kp == '*' ){ Lcd_Chr (2,6,'G'); Lcd_Chr (2,7,'u'); Lcd_Chr (2,8,'a'); Lcd_Chr (2,9,'r');Lcd_Chr (2,10,'d');Lcd_Chr (2,11,'a'); Lcd_Chr (2,12,'d'); Lcd_Chr (2,13,'o'); delay(); }else if (numero >= numeroMax){ //Lcd_Cmd(_LCD_CLEAR); Lcd_chr(2, 1,'M'); Lcd_chr(2, 2,'a'); Lcd_chr(2, 3,'x'); Lcd_chr(2, 5,'t'); delay(); delay(); goto volverObtenerNumero; }else{ goto leerNumero; } return (numero); }
void lerProtocolo(void) { //função que lê o protocolo e chama sua execução if(UART1_Data_Ready()) { lido = UART1_Read(); // lê o byte de inicio do protocolo if(lido == '[') { // se for '[' indica que o protocolo eh de identificação do módulo UART1_Read_Text(ID, "]", 4); executarProtocolo(); } else if(lido == '{') { // se for '{' indica que o protocolo eh de envio ao ME UART1_Read_Text(ME, "}", 11); executarProtocolo(); } else if(lido == '+') { // ser for '+' indica que o protocolo eh de resposta UART1_Read_Text(RE, "+", 4); executarProtocolo(); } } }
void main() { unsigned char i,tmp; unsigned int tout; CMCON=0x07; TRISA=0x30; TRISB=0xE7; PORTA=0; PORTB=0; lcd_init(); UART1_Init(9600); //teste serial lcd_cmd(L_CLR); lcd_cmd(L_L1); lcd_str(codetxt_to_ramtxt("Teste Serial TX")); lcd_cmd(L_L2+2); lcd_str(codetxt_to_ramtxt("9600 8N1")); UART1_Write_Text(codetxt_to_ramtxt("\r\n Picsimlab\r\n Teste Serial TX\r\n")); for(i=0;i<4;i++) { UART1_Write(i+0x30); UART1_Write_Text(codetxt_to_ramtxt(" PicsimLab\r\n")); } delay_ms(1000); lcd_cmd(L_CLR); lcd_cmd(L_L1); lcd_str(codetxt_to_ramtxt("Teste Serial RX")); UART1_Write_Text(codetxt_to_ramtxt(" Digite!\r\n")); for(i=0;i<32;i++) { if(!(i%16)) { lcd_cmd(L_L2); UART1_Write_Text(codetxt_to_ramtxt("\r\n")); } tout=0; while(!UART1_Data_Ready() && (tout < 2000)) { tout++; delay_ms(1); } if (UART1_Data_Ready() == 1) { tmp = UART1_Read(); } else { tmp='-'; } lcd_dat(tmp); UART1_Write(tmp); } delay_ms(100); lcd_cmd(L_CLR); lcd_cmd(L_L1); lcd_str(codetxt_to_ramtxt("Teste Teclado TX")); UART1_Write_Text(codetxt_to_ramtxt("\r\n Aguarde!\r\n")); for(i=0;i<32;i++) { if(!(i%16)) { lcd_cmd(L_L2); UART1_Write_Text(codetxt_to_ramtxt("\r\n")); } tmp=tc_tecla(2000)+0x30; lcd_dat(tmp); UART1_Write(tmp); } delay_ms(100); //fim teste lcd_cmd(L_CLR); lcd_cmd(L_L1+4); lcd_str(codetxt_to_ramtxt("Fim")); lcd_cmd(L_L2+1); lcd_str(codetxt_to_ramtxt("Pressione RST")); UART1_Write_Text(codetxt_to_ramtxt("\r\n FIM!\r\n")); while(1); }
//FIN CONVERSION void main() { char receive; ANSEL=ANSELH=0;///CONFIGURAMOS PINES COMO DIGITALTES PORTA=0; TRISA=0B1111; TRISD=0; PORTD=0; UART1_Init(115200); Delay_ms(100); ANSEL = 0; ANSELH = 0; i= 0; TRISD = 0x00; PORTD = 0; //VALOR INICIAL TRISC.B7 = 0x01; //RX PORTC.B7 = 0; //VALOR INICIAL 0 TRISC.B0 = 0x00; TRISC.B1 = 0x00; //PINC.B0,B1 COMO SALIDAS DIGITALTES PORTC.B0 = 0x00; PORTC.B1 = 0x00; while(1){ PORTC.B0 = CS1_DAC; PORTC.B1 = WR_DAC; if (UART1_Data_Ready() == 1) { receive = UART1_Read(); //dessincriptacion switch(receive){ case 'G' :{ if (UART1_Data_Ready() == 1) { receive = UART1_Read(); //nible bajo switch(receive){ case '0' :{ p1=0; break; } case '1' :{ p1=1; break; } case '2' :{ p1=2; break; } case '3' :{ p1=3; break; } case '4' :{ p1=4; break; } case '5' :{ p1=5; break; } case '6' :{ p1=6; break; } case '7' :{ p1=7; break; } case '8' :{ p1=8; break; } case '9' :{ p1=9; break; } case 'A' :{ p1=10; break; } case 'B' :{ p1=11; break; } case 'C' :{ p1=12; break; } case 'D' :{ p1=13; break; } case 'E' :{ p1=14; break; } case 'F' :{ p1=15; break; } } if (UART1_Data_Ready() == 1) { receive = UART1_Read(); //suma nible alto switch(receive){ case '0' :{ p1=p1+0*16; break; } case '1' :{ p1=p1+1*16; break; } case '2' :{ p1=p1+2*16; break; } case '3' :{ p1=p1+3*16; break; } case '4' :{ p1=p1+4*16; break; } case '5' :{ p1=p1+5*16; break; } case '6' :{ p1=p1+6*16; break; } case '7' :{ p1=p1+7*16; break; } case '8' :{ p1=p1+8*16; break; } case '9' :{ p1=p1+9*16; break; } case 'A' :{ p1=p1+10*16; break; } case 'B' :{ p1=p1+11*16; break; } case 'C' :{ p1=p1+12*16; break; } case 'D' :{ p1=p1+13*16; break; } case 'E' :{ p1=p1+14*16; break; } case 'F' :{ p1=p1+15*16; break; } } } } break; }//FIN CANAL 1 case 'H' :{//CANAL 2 if (UART1_Data_Ready() == 1) { receive = UART1_Read(); //nible bajo switch(receive){ case '0' :{ p2=0; break; } case '1' :{ p2=1; break; } case '2' :{ p2=2; break; } case '3' :{ p2=3; break; } case '4' :{ p2=4; break; } case '5' :{ p2=5; break; } case '6' :{ p2=6; break; } case '7' :{ p2=7; break; } case '8' :{ p2=8; break; } case '9' :{ p2=9; break; } case 'A' :{ p2=10; break; } case 'B' :{ p2=11; break; } case 'C' :{ p2=12; break; } case 'D' :{ p2=13; break; } case 'E' :{ p2=14; break; } case 'F' :{ p2=15; break; } } if (UART1_Data_Ready() == 1) { receive = UART1_Read(); //suma nible alto switch(receive){ case '0' :{ p2=p2+0*16; break; } case '1' :{ p2=p2+1*16; break; } case '2' :{ p2=p2+2*16; break; } case '3' :{ p2=p2+3*16; break; } case '4' :{ p2=p2+4*16; break; } case '5' :{ p2=p2+5*16; break; } case '6' :{ p2=p2+6*16; break; } case '7' :{ p2=p2+7*16; break; } case '8' :{ p2=p2+8*16; break; } case '9' :{ p2=p2+9*16; break; } case 'A' :{ p2=p2+10*16; break; } case 'B' :{ p2=p2+11*16; break; } case 'C' :{ p2=p2+12*16; break; } case 'D' :{ p2=p2+13*16; break; } case 'E' :{ p2=p2+14*16; break; } case 'F' :{ p2=p2+15*16; break; } } } } break; }//FIN CANAL 2 case 'I' :{//EMPIEZA CANAL 3 if (UART1_Data_Ready() == 1) { receive = UART1_Read(); //nible bajo switch(receive){ case '0' :{ p3=0; break; } case '1' :{ p3=1; break; } case '2' :{ p3=2; break; } case '3' :{ p3=3; break; } case '4' :{ p3=4; break; } case '5' :{ p3=5; break; } case '6' :{ p3=6; break; } case '7' :{ p3=7; break; } case '8' :{ p3=8; break; } case '9' :{ p3=9; break; } case 'A' :{ p3=10; break; } case 'B' :{ p3=11; break; } case 'C' :{ p3=12; break; } case 'D' :{ p3=13; break; } case 'E' :{ p3=14; break; } case 'F' :{ p3=15; break; } } if (UART1_Data_Ready() == 1) { receive = UART1_Read(); //suma nible alto switch(receive){ case '0' :{ p3=p3+0*16; break; } case '1' :{ p3=p3+1*16; break; } case '2' :{ p3=p3+2*16; break; } case '3' :{ p3=p3+3*16; break; } case '4' :{ p3=p3+4*16; break; } case '5' :{ p3=p3+5*16; break; } case '6' :{ p3=p3+6*16; break; } case '7' :{ p3=p3+7*16; break; } case '8' :{ p3=p3+8*16; break; } case '9' :{ p3=p3+9*16; break; } case 'A' :{ p3=p3+10*16; break; } case 'B' :{ p3=p3+11*16; break; } case 'C' :{ p3=p3+12*16; break; } case 'D' :{ p3=p3+13*16; break; } case 'E' :{ p3=p3+14*16; break; } case 'F' :{ p3=p3+15*16; break; } } } } break; }//FIN CANAL 3 case 'G' :{ if (UART1_Data_Ready() == 1) { receive = UART1_Read(); //nible bajo switch(receive){ case '0' :{ p4=0; break; } case '1' :{ p4=1; break; } case '2' :{ p4=2; break; } case '3' :{ p4=3; break; } case '4' :{ p4=4; break; } case '5' :{ p4=5; break; } case '6' :{ p4=6; break; } case '7' :{ p4=7; break; } case '8' :{ p4=8; break; } case '9' :{ p4=9; break; } case 'A' :{ p4=10; break; } case 'B' :{ p4=11; break; } case 'C' :{ p4=12; break; } case 'D' :{ p4=13; break; } case 'E' :{ p4=14; break; } case 'F' :{ p4=15; break; } } if (UART1_Data_Ready() == 1) { receive = UART1_Read(); //suma nible alto switch(receive){ case '0' :{ p4=p4+0*16; break; } case '1' :{ p4=p4+1*16; break; } case '2' :{ p4=p4+2*16; break; } case '3' :{ p4=p4+3*16; break; } case '4' :{ p4=p4+4*16; break; } case '5' :{ p4=p4+5*16; break; } case '6' :{ p4=p4+6*16; break; } case '7' :{ p4=p4+7*16; break; } case '8' :{ p4=p4+8*16; break; } case '9' :{ p4=p4+9*16; break; } case 'A' :{ p4=p4+10*16; break; } case 'B' :{ p4=p4+11*16; break; } case 'C' :{ p4=p4+12*16; break; } case 'D' :{ p4=p4+13*16; break; } case 'E' :{ p4=p4+14*16; break; } case 'F' :{ p4=p4+15*16; break; } } } } break; }//FIN CANAL 4 } }//FIN DE RECUPERACION DE DATOS =) // SELECCION DE CANAL A MOSTRAR if(porta.b0==1){ portd=p1; } if(porta.b1==1){ portd=p2; } if(porta.b2==1){ portd=p3; } if(porta.b3==1){ portd=p4; } //fin del programon juju =) } }
main(){ int i; trisb=0; portb=0; UART1_Init(9600); // Initialize UART module at 9600 bps Delay_ms(100); // Wait for UART module to stabilize while (1) { // Endless loop if (UART1_Data_Ready()) // If data is received, { for(i=0;i<100;i++) uart_rd[i]= UART1_Read(); UART1_Write(uart_rd); } /*if (uart_rd == ‘a’){ i=21; PWM1_Set_Duty(i); PWM1_Start(); } if (uart_rd == ‘b’){ i=42; PWM1_Set_Duty(i); PWM1_Start(); } if (uart_rd == ‘c’){ i=63; PWM1_Set_Duty(i); PWM1_Start(); } if (uart_rd == ‘d’){ portb=0b00000010; }if (uart_rd == ‘e’){ i=165; PWM1_Set_Duty(i); PWM1_Start(); } if (uart_rd == ‘f’){ i=187; PWM1_Set_Duty(i); PWM1_Start(); } if (uart_rd == ‘g’){ i=207; PWM1_Set_Duty(i); PWM1_Start(); } if (uart_rd == ‘h’){ portb=0b00000000;} if (uart_rd == ‘R’){ i=255; PWM1_Set_Duty(i); PWM1_Start(); } if (uart_rd == ‘L’){ i=0; PWM1_Set_Duty(i); PWM1_Start(); } if (uart_rd == ‘C’){ portb=0b00000001; delay_ms(10); portb=0b00000000; PWM1_Start(); } */ } }
void main() { TRISC = 0b10000000; //RC6 CONFIG. COMUNICAÇÃO SERIAL TRANSMIT PORTC = 0b10000000; //RC7 AND CONFIG. COMUNICAÇÃO SERIAL RECIEVE //RC2, 3, RD0, 1 COMANDO BOBINAS SENSOR TRISD = 0b00001100; //RD2 FDC1=OPERANDO PORTD = 0b00001100; //RD3 FDC2=DESCANSO TRISB = 0b00000000; //RB1, 2, 3 E 5 COMANDO BOBINAS DISCO PORTB = 0b00000000; //RB6 LED_SHUTTER E RB7 LED_COMUNICACAO UART1_Init(9600); // Initialize UART module at 9600 bps Delay_ms(100); // Wait for UART module to stabilize for (i=0; i<2; i++){ RB7_bit = 1; Delay_ms(100); RB7_bit = 0; Delay_ms(100); } while (1) { // Endless loop if (UART1_Data_Ready()) { // If data is received, output[0] = 0; // Clean the variable that recieves UART output[1] = 0; output[2] = 0; output[3] = 0; output[4] = 0; output[5] = 0; output[6] = 0; output[7] = 0; output[8] = 0; output[9] = 0; UART1_Read_Text(output, "OK", 7); // reads text until 'OK' is found RB7_bit = 1; Delay_ms(200); RB7_bit = 0; Delay_ms(200); if (output[0] == 'A'){ // função de abrir o shutter funcao = 0; } else if (output[0] == 'D'){ // decrementa x passos passos = (output[1]-'0')*1000+(output[2]-'0')*100+(output[3]-'0')*10+(output[4]-'0'); funcao = 1; } else if (output[0] == 'I'){ // incrementa x passos passos = (output[1]-'0')*1000+(output[2]-'0')*100+(output[3]-'0')*10+(output[4]-'0'); funcao = 2; } else if (output[0] == 'F'){ // fecha shutter funcao = 3; } else if (output[0] == 'S'){ // erro funcao = 4; } else if (output[0] == 'P'){ // posiciona sensor funcao = 5; } else if (output[0] == 'R'){ // retira sensor funcao = 6; } else{ for (i=0; i<2; i++){ // pisca indicando erro para qualquer outra letra RB7_bit = 1; delay_ms(200); RB7_bit = 0; delay_ms(200); } } //aqui chama cada uma das funções if (funcao == ABRIR_SHUTTER){ RB6_Bit = 1; //aciona ledshutter e envia sinal shutter RB7_bit = 1; //pisca ledcom 1seg 1x delay_ms(300); RB7_bit = 0; delay_ms(300); } else if (funcao == INCREMENTA_PASSOS){ RB7_bit = 1; Delay_ms(200); RB7_bit = 0; Delay_ms(200); for (j=0;j<passos;j++){ if (UART1_Data_Ready()) { UART1_Read_Text(output, "OK", 7); if (output[0] == 'S'){ // erro funcao = 4; j = passos; } } if (i==0){ i=1; PORTB = Passo1d; } else if (i==1){ i=2; PORTB = Passo2d; } else if (i==2){ i=3; PORTB = Passo3d; } else { i=0; PORTB = Passo4d; } delay_ms(20); // ATRASO ENTRE OS PASSOS } RB7_bit = 1; Delay_ms(200); RB7_bit = 0; Delay_ms(200); } else if (funcao == DECREMENTA_PASSOS){ RB7_bit = 1; Delay_ms(200); RB7_bit = 0; Delay_ms(200); for (j=0;j<passos;j++){ if (UART1_Data_Ready()) { UART1_Read_Text(output, "OK", 7); if (output[0] == 'S'){ // erro funcao = 4; j = passos; } } if (i==0){ i=1; PORTB = Passo1d; } else if (i==1){ i=2; PORTB = Passo4d; } else if (i==2){ i=3; PORTB = Passo3d; } else { i=0; PORTB = Passo2d; } delay_ms(20); // ATRASO ENTRE OS PASSOS } RB7_bit = 1; Delay_ms(200); RB7_bit = 0; Delay_ms(200); } else if (funcao == FECHA_SHUTTER){ RB6_bit = 0; for (i=0; i<2; i++){ RB7_bit = 1; Delay_ms(100); RB7_bit = 0; Delay_ms(100); } } else if (funcao == STOP){ RB6_bit = 0; passos =0; } else if (funcao == POSICIONA_SENSOR){ while (FIM_DE_CURSO1 == 1 && funcao == POSICIONA_SENSOR){ if (UART1_Data_Ready()) { UART1_Read_Text(output, "OK", 7); if (output[0] == 'S'){ // erro funcao = 4; break; } } if (i==0){ i=1; RD0_bit = 0; RD1_bit = 1; RC2_bit = 1; RC3_bit = 0; } else if (i==1){ i=2; RD0_bit = 1; RD1_bit = 1; RC2_bit = 0; RC3_bit = 0; } else if (i==2){ i=3; RD0_bit = 1; RD1_bit = 0; RC2_bit = 0; RC3_bit = 1; } else { i=0; RD0_bit = 0; RD1_bit = 0; RC2_bit = 1; RC3_bit = 1; } delay_ms(23); } if (FIM_DE_CURSO1 == 0){ RB7_bit = 1; Delay_ms(200); RB7_bit = 0; Delay_ms(200); } } else if (funcao == RETIRA_SENSOR){ while (FIM_DE_CURSO2 == 0 && funcao != 4){ if (UART1_Data_Ready()) { UART1_Read_Text(output, "OK", 7); if (output[0] == 'S'){ // erro funcao = 4; break; // Clean the variable that recieves UART } } if (i==0){ i=1; RC2_bit = 1; RC3_bit = 0; RD0_bit = 0; RD1_bit = 1; } else if (i==1){ i=2; RC2_bit = 1; RC3_bit = 1; RD0_bit = 0; RD1_bit = 0; } else if (i==2){ i=3; RC2_bit = 0; RC3_bit = 1; RD0_bit = 1; RD1_bit = 0; } else { i=0; RC2_bit = 0; RC3_bit = 0; RD0_bit = 1; RD1_bit = 1; } delay_ms(23); } if (FIM_DE_CURSO2 == 0){ RB7_bit = 1; Delay_ms(200); RB7_bit = 0; Delay_ms(200); } } } } }
void cargarDato(){ if (UART1_Data_Ready()) // If data is received, uart_rd_temp = UART1_Read(); // leer el dato recibido del celular }
//MAIN void main() { char currentByte, previousByte; char header, payload[NUMDATABYTES], checksum; char i; //Keeps track of data index unsigned int rxChecksumErrorCnt, rxInvalidHeaderCnt; enum state currentState; int direction; char openFlag; int closeCounter; // Initialize Initialize(); // Initialize Serial Comm currentByte = previousByte = 0; currentState = waitForStartBytes; rxChecksumErrorCnt = 0; rxInvalidHeaderCnt = 0; openFlag = 0; closeCounter = 0; while (1) { if (UART1_Data_Ready()) { previousByte = currentByte; currentByte = UART1_Read(); if ((previousByte == STARTBYTE1) && (currentByte == STARTBYTE2)) { currentState = getHeader; checksum = previousByte + currentByte; } else { switch (currentState) { case waitForStartBytes: //Do nothing wait for if statement above to succeed break; case getHeader: checksum += header = currentByte; i = 0; currentState = getData; break; case getData: checksum += payload[i] = currentByte; i++; if (i >= NUMDATABYTES) // was >= { currentState = processChecksum; } break; case processChecksum: if (checksum == currentByte) //Valid message received { switch (header) //Switch on header to supported messages { case ping: FPing(); break; case setMotorAddress: FSetMotorAddress(payload); break; case setServoOutputs: FSetServoOutputs(payload); break; case reportMotorStatus: FReportMotorStatus(payload); break; case reportMCUId: FReportMCUId(); break; default: rxInvalidHeaderCnt++; //Do something if count exceeds some value break; } } else //Checksum failed, throw out message { rxChecksumErrorCnt++; //Do something if error count exceeds some value } currentState = waitForStartBytes; break; default: currentState = waitForStartBytes; break; } } } else { // Nothing } } }