void uart_recv_int_handler() { #ifdef __USE18F26J50 if (DataRdy1USART()) { uc_ptr->buffer[uc_ptr->buflen] = Read1USART(); #else if (DataRdyUSART()) { uc_ptr->buffer[uc_ptr->buflen] = ReadUSART(); #endif uc_ptr->buflen++; // check if a message should be sent if (uc_ptr->buflen == MAXUARTBUF) { ToMainLow_sendmsg(uc_ptr->buflen, MSGT_UART_DATA, (void *) uc_ptr->buffer); uc_ptr->buflen = 0; } } #ifdef __USE18F26J50 if (USART1_Status.OVERRUN_ERROR == 1) { #else if (USART_Status.OVERRUN_ERROR == 1) { #endif // we've overrun the USART and must reset // send an error message for this RCSTAbits.CREN = 0; RCSTAbits.CREN = 1; ToMainLow_sendmsg(0, MSGT_OVERRUN, (void *) 0); } } void init_uart_recv(uart_comm *uc) { uc_ptr = uc; uc_ptr->buflen = 0; }
void usartLoopback() { unsigned char data; while(!DataRdyUSART()); data = ReadUSART(); while(BusyUSART()); WriteUSART(data); }
void uart_recv_int_handler() { if (DataRdyUSART()) { char i = 0; uc_ptr->buffer[uc_ptr->buflen] = ReadUSART(); uc_ptr->buflen++; // check if a message should be sent if (uc_ptr->buflen == MAXUARTBUF) { #ifdef MASTERPIC ToMainLow_sendmsg(uc_ptr->buflen, MSGT_UART_DATA, (void *) uc_ptr->buffer); #else PORTCbits.RC1 = 1; ToMainHigh_sendmsg(uc_ptr->buflen, MSGT_UART_RFID, (void *) uc_ptr->buffer); #endif uc_ptr->buflen = 0; } } if (USART_Status.OVERRUN_ERROR == 1) { // we've overrun the USART and must reset // send an error message for this RCSTAbits.CREN = 0; RCSTAbits.CREN = 1; ToMainLow_sendmsg(0, MSGT_OVERRUN, (void *) 0); } }
//HIGH PRIORITY INTERRUPTION********************************************************************** void interrupt high_priority HIGH_P_ISR(void) { //char RX_Byte_Ascii; if(RCIE && RCIF) // USART interrupt ?************* { if (OERR)// OVERRUN ERROR CLEARING { RX_Byte = RCREG; // clear the receiver fifo while (BusyUSART()); CREN = 0; // clear the OVR flag CREN = 1; Clear_RX_CMD(); } else if (FERR)// FRAMING ERROR CLEARING { RX_Byte = RCREG; // remove the broken byte Clear_RX_CMD(); } else { // NO ERROR ** RX_Byte = ReadUSART(); while (BusyUSART()); RX_Command(RX_Byte); } return; } }
void rs232_read_line(char buffer[64]) { unsigned int index = 0; char c; // Initialiser le tampon for (index = 0; index < 64;index++) buffer[index] = '\0'; index = 0; // Lire le texte do { // Lire un caractère while (!DataRdyUSART()); c = ReadUSART(); buffer[index] = c; index++; // Afficher les caractères entrés while (BusyUSART()); WriteUSART(c); } while (c != '\r' && index < 64); // Eviter de réécrire par dessus le texte saisi while (BusyUSART()); WriteUSART('\n'); // Remove '\r' buffer[index-1] = '\0'; }
void uart_recv_int_handler() { #ifdef __USE18F46J50 if (DataRdy1USART()) { // UART state machine uart_recv_state(Read1USART()); } if (USART1_Status.OVERRUN_ERROR == 1) { // we've overrun the USART and must reset // send an error message for this RCSTAbits.CREN = 0; RCSTAbits.CREN = 1; ToMainLow_sendmsg(0, MSGT_OVERRUN, (void *) 0); } #else if (DataRdyUSART()) { // UART state machine uart_recv_state(ReadUSART()); } if (USART_Status.OVERRUN_ERROR == 1) { // we've overrun the USART and must reset // send an error message for this RCSTAbits.CREN = 0; RCSTAbits.CREN = 1; ToMainLow_sendmsg(0, MSGT_OVERRUN, (void *) 0); } #endif }
// Master Mode void master(void){ while(1){ WriteUSART(27); putrsUSART (ClrScr); putrsUSART (Menu1); while(!DataRdyUSART()); //wait for data key = ReadUSART(); //read data putcUSART(key); switch (key) { case '1': { WriteUSART(27); putrsUSART (ClrScr); putrsUSART (Menu1_1); while(!DataRdyUSART()); //wait for data key = ReadUSART(); //read data break; } case '2': { trackmode(); break; } case '3': { WriteUSART(27); putrsUSART (ClrScr); putrsUSART (Menu1_3); manmode(); break; } case '4': { blink(); } default: { putrsUSART (BadKey); break; } } } }
int ReadGPSNMEA(void) { char first = 0x00; first = ReadUSART(); if(first == '$') { getsUSART(GPS_Value, 40); return 1; } return 0; }
void __interrupt IntServe(void) { GIE = 0; //Disable interrupts while attending one of them ... if (RCIF == 1) { CharRX = ReadUSART(); //present_state = CharRX; SerialInterrupt_flag=1; //while (BusyUSART()); //WriteUSART(CharRX); RCIF = 0; } if (INT0IF == 1) { Trigger = 1; INT0IF = 0; } if (TMR3IF == 1) { TMR3H = 0xF8; // TMR3H must be written first TMR3L = 0x46; // count up from F830 but there ara a small overhead and TMR3L must be 0x46 if (postscaler < postscalermax) { postscaler++; } else { postscaler = 0; postscalerflag = 1; } if (postscaler30s < postscaler30smax) { postscaler30s++; } else { postscaler30s=0; postscalerflag30s=1; } TMR3IF = 0; } if (TMR2IF == 1) { //Trigger = 1; if (present_state == STATE_ONEWIRE) { TOUT = 1; TMR2ON = 0; // stop timer TMR2IF = 0; // Clear TMR0 interrupt flag } TMR2IF = 0; } GIE = 1; //Disable interrupts while attending one of them ... }
static void uartRxHandler (void) { unsigned char c; /* Get the character received from the USART */ c = ReadUSART(); /* Put the character received from the USART */ WriteUSART(c); /* Clear the interrupt flag */ PIR1bits.RCIF = 0; }
void YourLowPriorityISRCode() { //Check which interrupt flag caused the interrupt. //Service the interrupt //Clear the interrupt flag //Etc. char c; if ( i == 0 ) LigaTMR1 ; // se 1o byte liga tmr1 pra espera mais c = ReadUSART(); PIR1bits.RCIF = 0; /* Clear the interrupt flag */ buffer [i++] = c; //salva Reg_relogio = 0 ; //dá mais tempo pra vir mais } //This return will be a "retfie", since this is in a #pragma interruptlow section
void high_isr(void) { if(PIE1bits.RCIE && PIR1bits.RCIF) { // [ FD ] [ size | 0 | id10..8 ] [ id7..0] [ M1 ] [ M2 ] ? [ M8 ] [ BF ] incoming = ReadUSART(); if(incoming == 0xFD && Rstate == attenteFD) { Rstate = attenteSize; } else if(Rstate == attenteSize) { umessage.len = (incoming & 0xF0) >> 4; //TESTER 0 ET LEN umessage.id =0; ((char*)&umessage.id)[1] = incoming & 0b00000111; Rstate = attenteIdL; }
void Uart1_ReceiveHandler(void) { volatile UINT8 data; // Get the character received from the UART #if(defined __18F8722_H) ||(defined __18F46K22_H) data = Read1USART(); #else data = ReadUSART(); #endif uart[0].rxBuff[uart[0].rxBuffIndex++] = data; if( uart[0].rxBuffIndex >= RX_PACKET_SIZE) { uart[0].rxBuffIndex = 0; } uart[0].rxDataCount++; // Clear the interrupt flag PIR1bits.RC1IF = 0; }
void Read_str_(unsigned char *buffer) /* Reads a string data from UART of having specific length*/ { char i; unsigned char data; for(i=0;data!=0x0D;i++) { while(!DataRdyUSART()); /* Wait for data to be received */ data = ReadUSART(); if(data!=0x0D) /* Get a character from the USART */ *buffer = data; /* save data in a string */ WriteUSART(data); while(BusyUSART()); buffer++; /* Increment the string pointer */ } while(i!=16) { *buffer = ' '; buffer++; i++; } }
void Uart2_ReceiveHandler(void) { volatile UINT8 data; // Get the character received from the UART #ifdef __18F8722_H data = Read2USART(); #else data = ReadUSART(); #endif uart[1].rxBuff[uart[1].rxBuffIndex++] = data; if( uart[1].rxBuffIndex >= RX_PACKET_SIZE) { uart[1].rxBuffIndex = 0; } uart[1].rxDataCount++; // Clear the interrupt flag PIR3bits.RC2IF = 0; }
void uart_recv_int_handler() { if (DataRdyUSART()) { uc_ptr->inbuffer[uc_ptr->inbuflen] = ReadUSART(); uc_ptr->inbuflen++; // check if a message should be sent if (uc_ptr->inbuflen == 3) { ToMainLow_sendmsg(uc_ptr->inbuflen, MSGT_MOTOR_CONTROLS_FROM_ARM, (void *) uc_ptr->inbuffer); uc_ptr->inbuflen = 0; } } if (USART_Status.OVERRUN_ERROR == 1) { // we've overrun the USART and must reset // send an error message for this RCSTAbits.CREN = 0; RCSTAbits.CREN = 1; ToMainLow_sendmsg(0, MSGT_OVERRUN, (void *) 0); } #if ENABLE_GPIO MAIN_THREAD = 1; UART_RECEIVE_THREAD = 0; #endif }
void high_isr(void){ /* TIMER 0 INTERRUPT HANDLING */ // T0IF generates the interrupt for the internal clock // TMR0 expires every 125 ms if (INTCONbits.TMR0IF) { WriteTimer0(TMR0_VAL); clock_increment(); if (clock_get_seconds()==0){ check_timer_table = 1; } INTCONbits.TMR0IF = 0; } /* USART RX INTERRUPT HANDLING */ if (PIR1bits.RCIF==1){ process_uart(ReadUSART()); PIR1bits.RCIF = 0; } return; }
/** * シリアルからデータリード * @param buffer 入力バッファ * @param length サイズ */ WORD ReadSerial(BYTE *buffer, WORD length) { BYTE i; for (i=0; i<length; i++) { if (!DataRdyUSART()) break; buffer[i] = ReadUSART(); } if (RCSTAbits.OERR==1) { sprintf(uartOutBuffer, (far rom char*)"error: overrun! RCSTA:0x%02x PIR1:0x%02x i:%d\r\n", RCSTA, PIR1, i); PutsStringCPtr(uartOutBuffer); RCSTAbits.CREN = 0; // clear the OVR flag RCSTAbits.CREN = 1; sprintf(uartOutBuffer, (far rom char*)"recovery: overrun! RCSTA:0x%02x PIR1:0x%02x i:%d\r\n", RCSTA, PIR1, i); PutsStringCPtr(uartOutBuffer); return 0xFFFF; } return i; }
void uart_recv_int_handler() { #ifdef __USE18F26J50 if (DataRdy1USART()) { uc_ptr->buffer[uc_ptr->buflen] = Read1USART(); #else #ifdef __USE18F46J50 if (DataRdy1USART()) { uc_ptr->buffer[uc_ptr->buflen] = Read1USART(); #else if (DataRdyUSART()) { buffer_temp[buf_len] = ReadUSART(); #endif #endif uc_ptr->buflen++; buf_len++; parseUART(); // check if a message should be sent } #ifdef __USE18F26J50 if (USART1_Status.OVERRUN_ERROR == 1) { #else #ifdef __USE18F46J50 if (USART1_Status.OVERRUN_ERROR == 1) { #else if (USART_Status.OVERRUN_ERROR == 1) { #endif #endif // we've overrun the USART and must reset // send an error message for this RCSTAbits.CREN = 0; RCSTAbits.CREN = 1; ToMainHigh_sendmsg(0, MSGT_OVERRUN, (void *) 0); } } void init_uart_recv(uart_comm *uc) { uc_ptr = uc; uc_ptr->buflen = 0; buf_len = 0; command_length = 0; command_count = 0; State = GET_MSGID; } void parseUART() { unsigned char x = uc_ptr->buflen; // uart_write(1, &x); switch(State) { case(GET_MSGID): { command_length = buffer_temp[buf_len -1] & 0xF; command_length = command_length; if(command_length != 0) { State = GET_COMMAND; } else { State = CHECKSUM; } break; } case(GET_COMMAND): { if(command_count+1 < command_length) { command_count++; } else { State = CHECKSUM; } break; } case(CHECKSUM): { ToMainLow_sendmsg(buf_len ,MSGT_PARSE, &buffer_temp[0]); uc_ptr->buflen = 0; buf_len = 0; State = GET_MSGID; command_count = 0; command_length = 0; break; } } } void uart_write(unsigned char length, unsigned char *msg){ unsigned char i = 0; for(i = 0; i < length; i++){ #ifdef __USE18F26J50 while(Busy1USART()); Write1USART(msg[i]); #else #ifdef __USE18F46J50 while(Busy1USART()); Write1USART(msg[i]); #else while(BusyUSART()); WriteUSART(msg[i]); #endif #endif } }
void interrupt Interrupcao(void){ char buffer[4]; /* a string buffer ira guardar um numero decimal (0 a 32) traduzido pela * funcao itoa, onde ira transformar o numero inteiro em string com apontador * o tamanho poderia ser 3, mas o inteiro vai ate 3 posicoes + null * * the buffer string variable will hold a decimal number (0 to 32) that was * converted by the itoa function, changing a integer number into a string * pointer * its size could be only 3, but the interger goes up to 3 positions + null end */ // LED_VERMELHO = ~LED_VERMELHO; // muda o status do LED_VERMELHO quando a MCU entrar em interrupcao // changes the LED_VERMELHO when the MCU interrupts // BUZZ=1; Delay100TCYx(10); BUZZ=0; // toca o buzzer para cada interrupcao // plays the buzzer for each interrupt if (RCIF) // se existe interrupcao aguardando... // teoricamente este RCIF nao precisaria de verificacao // pois se a rotina de Interrupcao ja foi chamada, entao // nem seria necessario verifica-la // mas os LED1 e LED_VERMELHO servem justamente para comprovar // que o RCIF sempre ira existir, quando mudam juntos (leds) // Theoretically this RCIF "if" check should never be necessary // but just to confirm the RCIF, the two leds (LED1 and // LED_VERMELHO) will always change together. { RCIE = 0; // desabilita interrupts de RX para tratar a entrada // disables RX interrupts to treat input LED1 = ~LED1; // muda o status do primeiro LED // inverts the LED1 status chRX = ReadUSART(); // le caractere da Serial / read a character from Serial if ( chRX>31 && chRX<127 && !BOTAO) // filtra apenas os caracteres texto comuns // just filter the common readable text characters { // Se o Echo Serial esta ativo, entao imprima o caractere na Serial // If Serial Echo is on, then send the character to Serial Port if (SerialEcho) { while (BusyUSART()); // espera nao ocupado / waits non busy WriteUSART(chRX); // envia caractere na Porta Serial // sends the character in Serial Port } // Se o LcdDisplay esta ativo, entao imprima o caractere // If Lcd Display is enabled, print the character in LCD if (LcdDisplay) { while (BusyXLCD()); WriteDataXLCD(chRX); contadorDisplay++; // contador de caracteres para tabular // LCD character tabulation count if (contadorDisplay==16) // se chegar ao final da primeira linha // if reaches the end of first line { SetDDRamAddr(0x40); // comando para pular para segunda linha // command for going to second line if (SerialEcho) { while (BusyUSART()); putrsUSART("\r\n"); // imprime RETURN e NEWLINE (inicio) // prints Carriage RETURN and Newline } } if (contadorDisplay==32) // se chegar ao final da segunda linha // if reaches the end of second line { SetDDRamAddr(0x00); // pula novamente para primeira linha // go again to first line contadorDisplay=0; // ja na primeira linha, limpa contador // in first line, clear the char counter if (SerialEcho) { while (BusyUSART()); putrsUSART("\r\n"); // imprime RETURN e NEWLINE (inicio) // prints Carriage RETURN and Newline } } } } else // filtra todos demais Controls e Caracteres Especiais // this is the state of non-text characters being filtered if (BOTAO) // se o BOTAO de debug estiver pressionado, nao filtra controle //if pushBOTTON is pressed, do not filter control characters { while (BusyUSART()); WriteUSART(chRX); } else switch (chRX) { case 0x05 : // Caractere ^E SerialEcho=!SerialEcho; while(BusyUSART()); putrsUSART("\r\n[ECHO "); if (SerialEcho) putrsUSART("ON]"); else putrsUSART("OFF]"); break; /* * Control-E: * Rotina para ligar e desligar o ECHO na porta serial * Routine to turn on and off the ECHO in serial port */ case 0x10 : // Caractere ^P LcdDisplay=!LcdDisplay; while(BusyUSART()); putrsUSART("\r\n[LCD "); if (LcdDisplay) putrsUSART("ON]"); else putrsUSART("OFF]"); break; /* * Contro-P: * Rotina para ligar e desligar a amostragem de Caractere no LCD * Routine to turn on and off the display of characters in LCD */ case 0x0C : // Caractere ^L (FormFeed) putrsUSART("\r\n[LCD CLS]"); while(BusyXLCD()); WriteCmdXLCD(0x01); // Comando para limpar o LCD contadorDisplay=0; break; /* * Control-L: (LimpaTela / FormFeed) * Rotina para Limpar a Tela * Routine to CLear Screen (CLS) */ case 0x13 : // Caractere ^S putrsUSART("\r\n[Status Lcd:"); if (LcdDisplay) putrsUSART("On"); else putrsUSART("Off"); putrsUSART(" Echo:"); if (SerialEcho) putrsUSART("On"); else putrsUSART("Off"); putrsUSART(" charLCD:"); itoa ( buffer, contadorDisplay, 10); // itoa necessita da biblioteca <stdlib.h> // itoa needs the include stdlib.h putrsUSART( buffer ); putrsUSART("]\r\n"); /* * Control-S: * Mostra o Status do Echo, do LCD, e da quantidade de caracteres * no LCD * Shows the Status of Echo, LCD and characteres number in LCD */ break; default: ; } // two Peripheral Interrupt Request (Flag) registers (PIR1 and PIR2) // RCIF: EUSART Receive Interrupt Flag bit //PIR1bits.RCIF = 0; // PIR1 no registro RCIF RCIE = 1; // Re-Habilita a Interrupcao de Recepcao // Re-Enable RX Interrupts RCIF = 0; // PIR1 no registro RCIF // limpa o registrador de interrupcao, e sai da interrupcao // clear the interrupt register, and quits it } }
unsigned char ReadUART(void) { while(!(DataRdyUSART())); /* Reading a data byte */ return(ReadUSART()); }
void uart_recv_int_handler() { // Write a byte (one character) to the usart transmit buffer. // If 9-bit mode is enabled, the 9th bit is written from the field TX_NINE, // found in a union of type USART #ifdef __USE18F26J50 if (DataRdy1USART()) { uc_ptr->Rx_buffer[uc_ptr->Rx_buflen] = Read1USART(); #else #ifdef __USE18F46J50 if (DataRdy1USART()) { char data = Read1USART(); #else if (DataRdyUSART()) { char data = ReadUSART(); #endif #endif switch (msgtype_uart) { case MSGTYPE: uc_ptr->Rx_buffer[0] = data; uc_ptr->Rx_buflen++; msgtype_uart = LENGTH; break; case LENGTH: if (uc_ptr->Rx_buflen == 1) { uc_ptr->Rx_buffer[uc_ptr->Rx_buflen] = data; uc_ptr->Rx_buflen++; msgtype_uart = MESSAGE; } else { uc_ptr->Rx_buflen = 0; msgtype_uart = MSGTYPE; } break; case MESSAGE: if (uc_ptr->Rx_buflen > uc_ptr->Rx_buffer[1]) { uc_ptr->Rx_buffer[uc_ptr->Rx_buflen] = data; uc_ptr->Rx_buflen++; msgtype_uart = CHECKSUM; } else { uc_ptr->Rx_buffer[uc_ptr->Rx_buflen] = data; uc_ptr->Rx_buflen++; msgtype_uart = MESSAGE; } break; case CHECKSUM: if (uc_ptr->Rx_buflen > uc_ptr->Rx_buffer[1]) { uc_ptr->Rx_buffer[uc_ptr->Rx_buflen] = data; uc_ptr->Rx_buflen++; unsigned char checkSum = 0; unsigned char bufLength = uc_ptr->Rx_buffer[1]; // Check for correct checksum. int i = 0; for (; i < uc_ptr->Rx_buffer[1]; i++) { checkSum ^= uc_ptr->Rx_buffer[i + 2]; } if (uc_ptr->Rx_buflen != uc_ptr->Rx_buffer[1] + 3) { // Message has been scrambled. uc_ptr->Rx_buffer[0] == COMMAND_NACK; } if (checkSum != uc_ptr->Rx_buffer[uc_ptr->Rx_buflen - 1] || uc_ptr->Rx_buffer[0] == COMMAND_NACK) { // Send previous message again. ToMainHigh_sendmsg(uc_ptr->Tx_buflen, MSGT_UART_SEND, (void *) uc_ptr->Tx_buffer); } else if (uc_ptr->Rx_buffer[0] != COMMAND_ACK) { // Send sensor or motor encoder data to the ARM. ToMainHigh_sendmsg(uc_ptr->Rx_buflen, MSGT_UART_RCV, (void *) uc_ptr->Rx_buffer); // Return an ACK to the Master. ToMainLow_sendmsg(CMD_ACKLEN, MSGT_UART_SEND, (void *) uc_ptr->cmd_ack_buf); } else if (uc_ptr->Rx_buffer[0] == COMMAND_ACK) { // Allow ARM to send a new motor command. motorCommandSent = 1; } } msgtype_uart = MSGTYPE; uc_ptr->Rx_buflen = 0; break; default: break; } } #ifdef __USE18F26J50 if (USART1_Status.OVERRUN_ERROR == 1) { #else #ifdef __USE18F46J50 if (USART1_Status.OVERRUN_ERROR == 1) { #else if (USART_Status.OVERRUN_ERROR == 1) { #endif #endif // we've overrun the USART and must reset // send an error message for this RCSTAbits.CREN = 0; RCSTAbits.CREN = 1; ToMainLow_sendmsg(0, MSGT_OVERRUN, (void *) 0); } } void uart_send_int_handler() { if (uc_ptr->Tx_buflen == uc_ptr->msg_length) { PIE1bits.TX1IE = 0; // Clear TXIE to end write. uc_ptr->Tx_buflen = 0; } else { Write1USART(uc_ptr->Tx_buffer[uc_ptr->Tx_buflen++]); } } void init_uart_recv(uart_comm * uc) { uc_ptr = uc; uc_ptr->Tx_buflen = 0; uc_ptr->Rx_buflen = 0; uc_ptr->msg_length = 0; // Intialize CMD ACK response. uc_ptr->cmd_ack_buf[0] = 0x10; uc_ptr->cmd_ack_buf[1] = 0x01; uc_ptr->cmd_ack_buf[2] = 0x02; uc_ptr->cmd_ack_buf[3] = 0x02; } void uart_retrieve_buffer(int length, unsigned char* msgbuffer) { int i = 0; uc_ptr->Tx_buflen = 0; uc_ptr->msg_length = length; for (; i < length; i++) { uc_ptr->Tx_buffer[i] = msgbuffer[i]; } // Set UART TXF interrupt flag PIE1bits.TX1IE = 0x1; }
void uart_recv_int_handler() { #ifdef __USE18F26J50 if (DataRdy1USART()) { uc_ptr->buffer[uc_ptr->buflen] = Read1USART(); #else #ifdef __USE18F46J50 if (DataRdy1USART()) { uc_ptr->buffer[uc_ptr->buflen] = Read1USART(); #else if (DataRdyUSART()) { uc_ptr->buffer[uc_ptr->buflen] = ReadUSART(); #endif #endif uc_ptr->buflen++; // check if a message should be sent if (uc_ptr->buflen == MAXUARTBUF) { ToMainLow_sendmsg(uc_ptr->buflen, MSGT_UART_DATA, (void *) uc_ptr->buffer); uc_ptr->buflen = 0; } ToMainLow_sendmsg(uc_ptr->buflen, MSGT_UART_DATA, (void *) uc_ptr->buffer); } #ifdef __USE18F26J50 if (USART1_Status.OVERRUN_ERROR == 1) { #else #ifdef __USE18F46J50 if (USART1_Status.OVERRUN_ERROR == 1) { #else if (USART_Status.OVERRUN_ERROR == 1) { #endif #endif // we've overrun the USART and must reset // send an error message for this RCSTAbits.CREN = 0; RCSTAbits.CREN = 1; ToMainLow_sendmsg(0, MSGT_OVERRUN, (void *) 0); } } void uart_trans_int_handler() { unsigned char msgbuffer[MSGLEN+1]; unsigned char msgtype; signed char length; unsigned char start; length = SensorData_recvmsg(start, MSGLEN, &msgtype, (void *) msgbuffer); //unsigned char msg[6]; //msg[0] = 0x0; // i = i << 1; if (msgtest[0] == 0xFF) { msgtest[0] = 0x0; } msgtest[0]++; msgtest[1] = 0xA; msgtest[2] = 0x00; msgtest[3] = msgtest[0]; msgtest[4] = 0xFF; msgtest[5] = 0x0; // if (length == 0) { // PIE1bits.TX1IE = 0x0; // } TXREG1 = msgtest[index]; //i++; index++; //FromMainHigh_recvmsg() if (index == 6) { index = 0; msgtest[0] = msgtest[0] - 0x5; PIE1bits.TX1IE = 0x0; } } void init_uart_recv(uart_comm *uc) { uc_ptr = uc; uc_ptr->buflen = 0; } void uart_send(int length, unsigned char *msg_buffer) { int i = 0; for (i; i < length; i++) { //TXREG1 = msg_buffer[i]; uc_ptr->buffer[i] = msg_buffer[i]; } PIE1bits.TX1IE = 0x0; }
//=============================================================================================== // Main loop //=============================================================================================== void main(void) { // Creates the variable to readin the commands from the python script char reading; // Creates the controller structure and initizle the corresponding values CONTROLLER PY_GAMES_CONTROLLER; CONTROLLER_INIT(&PY_GAMES_CONTROLLER); // Set all of PORTC as outputs TRISC=0x00; // Configure UART OpenUSART(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE &USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_HIGH, 10); Delay1KTCYx(4); while(1) { // Wait for the next branch of data to come in and store it to the reading variable while(!DataRdyUSART()); reading=ReadUSART(); // Checks to see if the current reading is equal to c, which means check to see if the controller is connected if(reading=='c') { // Sends 1 to the python script sprintf(message,"1"); putsUSART(message); Delay1KTCYx(4); } // Checks to see if reading is equal to j for joystick else if(reading=='j') { // Waits for the next data to be sent and stores it to reading while(!DataRdyUSART()); reading=ReadUSART(); // Checks to see if the next reading is equal to l, or left if(reading=='l') { // Waits for the next data to come in and stores it to reading while(!DataRdyUSART()); reading=ReadUSART(); // Checks to see if x axis data is requested if(reading=='x') { // Reads in the left joystick's x axis and sends it to the python script int result=READJOYSTICKX(&PY_GAMES_CONTROLLER.LEFT_JOYSTICK); sprintf(message,"%d",result); putsUSART(message); Delay1KTCYx(4); } // Check to see if y axis data is requested else if(reading=='y') { // Reads the left joystick's y axis and sends the results to the python script int result=READJOYSTICKY(&PY_GAMES_CONTROLLER.LEFT_JOYSTICK); sprintf(message,"%d",result); putsUSART(message); Delay1KTCYx(4); } } // Checks to see if reading is equal to r for right else if(reading=='r') { // Waits for the next data to come in and stores the results to reading while(!DataRdyUSART()); reading=ReadUSART(); // Checks to see if x axis data is requested if(reading=='x') { // Reads in the right joystick's x axis and sends the results to the python script int result=READJOYSTICKX(&PY_GAMES_CONTROLLER.RIGHT_JOYSTICK); sprintf(message,"%d",result); putsUSART(message); Delay1KTCYx(4); } // Check to see if y axis data is requested else if(reading=='y') { // Checks the right joystick's y axis and sends the results to the python script int result=READJOYSTICKY(&PY_GAMES_CONTROLLER.RIGHT_JOYSTICK); sprintf(message,"%d",result); putsUSART(message); Delay1KTCYx(4); } } } // Checks to see if reading is equal to b for button else if(reading=='b') { // Waits for the next data to come in and stores the results to reading while(!DataRdyUSART()); reading=ReadUSART(); // Checks to see if reading is equal to y for yellow button if(reading=='y') { // Checks the status of the yellow button and sends it to the python script int result=ISBUTTONPRESSED(&PY_GAMES_CONTROLLER.YELLOW_BUTTON); sprintf(message,"%d",result); putsUSART(message); Delay1KTCYx(4); } // Checks to see if reading is equal to r for red button else if(reading=='r') { // Checks the status of the red button and sends the results to the python script int result=ISBUTTONPRESSED(&PY_GAMES_CONTROLLER.RED_BUTTON); sprintf(message,"%d",result); putsUSART(message); Delay1KTCYx(4); } } } }
void high_isr(void){ /* USART RX INTERRUPT HANDLING */ if (PIR1bits.RCIF==1){ xpl_fifo_push_byte(ReadUSART()); PIR1bits.RCIF = 0; return; } /* RB0 INTERRUPT HANDLING */ if (INTCONbits.INT0IF==1){ debounce_water = 2; INTCONbits.INT0IF = 0; stat0 = 0; return; } /* RB1 INTERRUPT HANDLING */ if (INTCON3bits.INT1IF==1){ debounce_gas = 2; INTCON3bits.INT1IF = 0; stat0 = 0; return; } /* RB2 INTERRUPT HANDLING */ if (INTCON3bits.INT2IF==1){ debounce_elec = 2; INTCON3bits.INT2IF = 0; stat0 = 0; return; } /* TIMER 0 INTERRUPT HANDLING */ if (INTCONbits.TMR0IF==1){ WriteTimer0(TMR0_VALUE); // Reprogram timer INTCONbits.TMR0IF=0; // Clear interrupt flag time_ticks++; time_ticks_oo++; } /* TIMER 1 INTERRUPT HANDLING */ if (PIR1bits.TMR1IF==1){ WriteTimer1(TMR1_VALUE); PIR1bits.TMR1IF=0; // Check if we need to handle a debounce if (debounce_water) { debounce_water--; stat0 = 1; if (debounce_water == 0 && PORTBbits.RB0 == 0) { xpl_trig(WATER); } } if (debounce_gas) { debounce_gas--; stat0 = 1; if (debounce_gas == 0 && PORTBbits.RB1 == 0) { xpl_trig(GAS); } } if (debounce_elec) { debounce_elec--; stat0 = 1; if (debounce_elec == 0 && PORTBbits.RB2 == 0) { xpl_trig(ELEC); } } } return; }
// Track Mode // Assume motor A and B are front left and right, motor C and D are back left and right // Num pad controls A/C and B/D void trackmode(void){ WriteUSART(27); putrsUSART (ClrScr); putrsUSART (Menu1_2); while(1){ while(!DataRdyUSART()); //wait for data key = ReadUSART(); //read data switch (key) { case 'W': { if(PWMA2+PWM_Step<=PWM_DC_Max)PWMA2=PWMA2+PWM_Step; else PWMA2=PWM_DC_Max; if(PWMB1+PWM_Step<=PWM_DC_Max)PWMB1=PWMB1+PWM_Step; else PWMB1=PWM_DC_Max; // PWMA2+=PWM_Step; // PWMB1+=PWM_Step; break; } case 'S': { if(PWMA2-PWM_Step>=PWM_DC_Min) PWMA2=PWMA2-PWM_Step; else PWMA2=PWM_DC_Min; if(PWMB1-PWM_Step>=PWM_DC_Min) PWMB1=PWMB1-PWM_Step; else PWMB1=PWM_DC_Min; // PWMA2-=PWM_Step; // PWMB1-=PWM_Step; break; } case 'A': { if(PWMA2-PWM_Step>=PWM_DC_Min) PWMA2=PWMA2-PWM_Step; else PWMA2=PWM_DC_Min; if(PWMB1+PWM_Step<=PWM_DC_Max) PWMB1=PWMB1+PWM_Step; else PWMB1=PWM_DC_Max; // PWMA2-=PWM_Step; // PWMB1+=PWM_Step; break; } case 'D': { if(PWMA2+PWM_Step<=PWM_DC_Max) PWMA2=PWMA2+PWM_Step; else PWMA2=PWM_DC_Max; if(PWMB1-PWM_Step>=PWM_DC_Min) PWMB1=PWMB1-PWM_Step; else PWMB1=PWM_DC_Min; // PWMA2+=PWM_Step; // PWMB1-=PWM_Step; break; } case 'N': { RC=0; RD=0; break; } case 'K': { RC=1; RD=1; break; } case 'O': { RC=1; RD=0; break; } case 'M': { RE=0; RF=0; break; } case 'L': { RE=1; RF=1; break; } case 'P': { RE=1; RF=0; break; } case ' ': { PWMA2=PWM_Stop; PWMB1=PWM_Stop; PWMC0=PWM_Stop; PWMD3=PWM_Stop; RC=0; RD=0; break; } case 27: { return; break; } default: { putrsUSART (BadKey); break; } } //Set Channels C and D from Channels A and B, respectivley PWMC0=PWMA2; PWMD3=PWMB1; //Upate the PWM signals Update_PWMs(); } return; }
void uart_recv_wifly_debug_handler(){ #ifdef __USE18F46J50 if (DataRdy1USART()) { unsigned char last = Read1USART(); #else if (DataRdyUSART()) { unsigned char last = ReadUSART(); #endif static unsigned char cur = 0; if (last == endmsg[cur]) { cur++; if(cur >= sizeof endmsg - 1) { //-1 for null terminated wifly_setup = 1; } } else{ cur = 0; } } } void uart_recv_int_handler() { #ifdef __USE18F26J50 if (DataRdy1USART()) { uc_ptr->buffer[uc_ptr->buflen] = Read1USART(); #else #ifdef __USE18F46J50 if (DataRdy1USART()) { unsigned char recv = Read1USART(); #else if (DataRdyUSART()) { unsigned char recv = ReadUSART(); #endif #endif int pos = uc_ptr->buflen++; uc_ptr->buffer[pos] = recv; //We recieved the last byte of data //Check the 5th byte recieved for payload length if(pos == PAYLOADLEN_POS){ payload_length = recv; } // Get checksum byte if(pos == CHECKSUM_POS){ checksum_recv_value = recv; } // Count any other byte other than checksum else{ checksum_calc_value += recv; } // check if a message should be sent if (pos == payload_length+HEADER_MEMBERS-1){ pos++; if(checksum_calc_value == checksum_recv_value){ FromUARTInt_sendmsg(pos, MSGT_UART_DATA, (void *) uc_ptr->buffer); } else{ //Invalid Checksum FromUARTInt_sendmsg(pos, MSGT_UART_RECV_FAILED, (void *) uc_ptr->buffer); } //Clean up for next packet uc_ptr->buflen = 0; payload_length = 0; checksum_recv_value = 0; checksum_calc_value = 0; #ifdef __USE18F46J50 //Read1USART(); // clears buffer and returns value to nothing #else //ReadUSART(); #endif } // portion of the bytes were received or there was a corrupt byte or there was // an overflow transmitted to the buffer else if (pos >= MAXUARTBUF) { FromUARTInt_sendmsg(pos, MSGT_OVERRUN, (void *) uc_ptr->buffer); uc_ptr->buflen = 0; payload_length = 0; checksum_recv_value = 0; checksum_calc_value = 0; } } #ifdef __USE18F26J50 if (USART1_Status.OVERRUN_ERROR == 1) { #else #ifdef __USE18F46J50 if (USART1_Status.OVERRUN_ERROR == 1) { #else if (USART_Status.OVERRUN_ERROR == 1) { #endif #endif // we've overrun the USART and must reset // send an error message for this RCSTAbits.CREN = 0; RCSTAbits.CREN = 1; FromUARTInt_sendmsg(0, MSGT_OVERRUN, (void *) 0); } #ifdef __USE18F46J50 if (USART1_Status.FRAME_ERROR) { #else if (USART_Status.FRAME_ERROR) { #endif init_uart_recv(uc_ptr); } } void init_uart_recv(uart_comm *uc) { uc_ptr = uc; uc_ptr->status = UART_IDLE; uc_ptr->buflen = 0; payload_length = 0; checksum_recv_value = 0; checksum_calc_value = 0; } void uart_send_array(char* data, char length) { #if !defined(SENSOR_PIC) && !defined(MOTOR_PIC) if(!wifly_setup) return; //just return #endif if(uc_ptr->status != UART_IDLE){ if(in_main()){ debugNum(2); if(FromMainHigh_sendmsg(length, MSGT_UART_TX_BUSY, data) == MSGQUEUE_FULL) debugNum(4); } else{ FromUARTInt_sendmsg(length, MSGT_UART_TX_BUSY, data); } return; } uc_ptr->status = UART_TX; //TODO: Create logic to prevent you from overriding the current buffer if //it has not yet been sent. uint8_t i; for(i = 0; i<length; i++) { uc_ptr->outBuff[i] = *(data + i); } uc_ptr->outLength = length; uc_ptr->outIndex = 1; #ifdef __USE18F46J50 Write1USART(uc_ptr->outBuff[0]); #else WriteUSART(uc_ptr->outBuff[0]); #endif PIR1bits.TXIF = 0; PIE1bits.TXIE = 1; } //Used to send multiple char. Will get called when uart_send_array is called //Brian says DONT DELETE! I will find you. void uart_send_int_handler() { if(uc_ptr->outLength > uc_ptr->outIndex){ uart_send(uc_ptr->outBuff[uc_ptr->outIndex++]); } else if(uc_ptr->outLength == uc_ptr->outIndex){ uc_ptr->outIndex++; //still need to increment } else //uc_ptr->outLength < uc_ptr->outIndex { // done sending message PIE1bits.TXIE = 0; PIR1bits.TXIF = 0; uc_ptr->status = UART_IDLE; } } void uart_send(char data){ //TODO possibly create logic to (without using a while) prevent writing if the buffer is not //clear #ifdef __USE18F46J50 Write1USART(data); #else WriteUSART(data); #endif }
void bucket(void){ WriteUSART(27); putrsUSART (ClrScr); putrsUSART (Menu1_2); while(1){ while(!DataRdyUSART()); //wait for data key = ReadUSART(); //read data switch (key) { case 'P': { if(PWMA2+PWM_Step<=PWM_DC_Max)PWMA2=PWMA2+PWM_Step; else PWMA2=PWM_DC_Max; // PWMA2+=PWM_Step; // PWMB1+=PWM_Step; break; } case 'L': { if(PWMA2-PWM_Step>=PWM_DC_Min) PWMA2=PWMA2-PWM_Step; else PWMA2=PWM_DC_Min; // PWMA2-=PWM_Step; // PWMB1-=PWM_Step; break; } case 'O': { if(PWMD3-PWM_Step>=PWM_DC_Min) PWMD3=PWMD3-PWM_Step; else PWMD3=PWM_DC_Min; // PWMA2-=PWM_Step; // PWMB1+=PWM_Step; break; } case 'K': { if(PWMD3+PWM_Step<=PWM_DC_Max) PWMD3=PWMD3+PWM_Step; else PWMD3=PWM_DC_Max; // PWMA2+=PWM_Step; // PWMB1-=PWM_Step; break; } case ' ': { PWMA2=PWM_Stop; PWMB1=PWM_Stop; PWMC0=PWM_Stop; PWMD3=PWM_Stop; break; } case 27: { return; break; } default: { putrsUSART (BadKey); break; } } //Upate the PWM signals Update_PWMs(); } return; }
//Manual Mode void manmode(void){ while(1){ while(!DataRdyUSART()); //wait for data key = ReadUSART(); //read data switch (key) { case 'A': { while(!DataRdyUSART()); //wait for data percent[0] = ReadUSART(); //read data while(!DataRdyUSART()); //wait for data percent[1] = ReadUSART(); //read data PWMA2=atoi(percent); //convert the string to an integer PWMA2=PWM_Conv*PWMA2; //convert to appropriate Duty Cycle value break; } case 'B': { while(!DataRdyUSART()); //wait for data percent[0] = ReadUSART(); //read data while(!DataRdyUSART()); //wait for data percent[1] = ReadUSART(); //read data PWMB1= atoi(percent); //convert the string to an integer PWMB1=PWM_Conv*PWMB1; //convert to appropriate Duty Cycle value break; } case 'C': { while(!DataRdyUSART()); //wait for data percent[0] = ReadUSART(); //read data while(!DataRdyUSART()); //wait for data percent[1] = ReadUSART(); //read data PWMC0= atoi(percent); //convert the string to an integer PWMC0=PWM_Conv*PWMC0; //convert to appropriate Duty Cycle value break; } case 'D': { while(!DataRdyUSART()); //wait for data percent[0] = ReadUSART(); //read data while(!DataRdyUSART()); //wait for data percent[1] = ReadUSART(); //read data PWMD3= atoi(percent); //convert the string to an integer PWMD3=PWM_Conv*PWMD3; //convert to appropriate Duty Cycle value break; } case ' ': { PWMA2=PWM_Stop; PWMB1=PWM_Stop; PWMC0=PWM_Stop; PWMD3=PWM_Stop; break; } case 27: { return; break; } default: { putrsUSART ("-"); break; } } //Upate the PWM signals Update_PWMs(); } return; }