Exemplo n.º 1
0
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';
}	
Exemplo n.º 2
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

}
Exemplo n.º 3
0
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;
}
Exemplo n.º 4
0
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);
    }
}
Exemplo n.º 5
0
void usartLoopback() {
  unsigned char data;
  while(!DataRdyUSART());
    data = ReadUSART();
  while(BusyUSART());
    WriteUSART(data);
}
Exemplo n.º 6
0
// 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;
      }
    }
  }
}
Exemplo n.º 7
0
void terminalTask() {
        if (DataRdyUSART()) {\
            while(BusyUSART());
            cmd = getcUSART();
            while(BusyUSART());
            putcUSART(cmd);
        }
    
        switch (cmd) {
        case '?':
            sendTerminalCommandList();
            break;
        case '1':
            terminalSendPString(TERMINAL_RETURN);
            terminalSendPString("Measuring carbon...");
            measureCarbon();
            terminalSendPString(TERMINAL_RETURN);
            updateTerminal();
            break;
        case '2':
            terminalSendPString(TERMINAL_RETURN);
            terminalSendPString("Measuring salinity...");
            measureSalinity();
            terminalSendPString(TERMINAL_RETURN);
            updateTerminal();
            break;
        case '3':
            terminalSendPString(TERMINAL_RETURN);
            terminalSendPString("Measuring flow rate...");
            measureFlowRate();
            terminalSendPString(TERMINAL_RETURN);
            updateTerminal();
            break;
        case '4':
            terminalSendPString(TERMINAL_RETURN);
            terminalSendPString("Measuring temperature...");
            measureTemperature();
            terminalSendPString(TERMINAL_RETURN);
            updateTerminal();
            break;
        case 'c':
            updateTerminal();
            break;
        case 'd':
            terminalSendPString(TERMINAL_RETURN);
            terminalSendPString("Revealing display");
            sendTerminalCommandLine();
            break;
        case 0xff:
            break;
        default:
            terminalSendPString(TERMINAL_RETURN);
            terminalSendPString("Invalid command");
            sendTerminalCommandLine();
        }
        cmd = 0xff;
}
Exemplo n.º 8
0
void Rx_Mensagem(void)
{
	/*while(PIR1bits.RCIF==0); // Wait until RCIF gets low
	data[j++] = RCREG;*/
	
     if(PIE1bits.RCIE && PIR1bits.RCIF)
     {
		timer = 0;
        while(!DataRdyUSART());
		data[j++] = getcUSART();
		
		PIR1bits.RCIF = 0;
    }
}
Exemplo n.º 9
0
void interrupt SerialRxPinInterrupt(void)
{
    unsigned char data;
    //check if the interrupt is caused by RX pin
    if(PIR1bits.RCIF == 1)
    {
    do{    
    while(!DataRdyUSART());// Wait for data to be received

    data = getcUSART();    // Get a character from the USART
    reception[i] = data;
    i++;              
    }while(PIR1bits.RCIF);
    }
}
Exemplo n.º 10
0
void getsUSART(char *buffer, unsigned char len)
{
  char i;    // Length counter
  unsigned char data;

  for(i=0;i<len;i++)  // Only retrieve len characters
  {
    while(!DataRdyUSART());// Wait for data to be received

    data = getcUSART();    // Get a character from the USART
                           // and save in the string
    *buffer = data;
    buffer++;              // Increment the string pointer
  }
}
Exemplo n.º 11
0
void Read_string(unsigned char *buffer, unsigned char len)      /* Reads a string data from UART of having specific length*/
  {
  char i;
  unsigned char data;
      Write(0x0D);
      Write('\n');
  for(i=0;i<len;i++)
    {
    while(!DataRdyUSART());                                          /* Wait for data to be received                           */
    data = getcUSART();                                              /* Get a character from the USART                         */
    *buffer = data;                                                  /* save data in a string                                  */
    WriteUSART(data);
    while(BusyUSART());
    buffer++;                                                        /* Increment the string pointer                           */
    }
  }
Exemplo n.º 12
0
void uartLoopbackSM(LoopbackData *data){

  switch(data->state){
    case WAIT_DATA:
      if(DataRdyUSART() == 0)
        break;
      data->dataByte = getcUSART();
      data->state = WAIT_TO_TX;
      break;

    case WAIT_TO_TX:
      if(BusyUSART() == 1)
        break;
      putcUSART(data->dataByte);
      data->state = WAIT_DATA;
      break;
      
    default:
      break;
  }
}
Exemplo n.º 13
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++;
  }
  }
Exemplo n.º 14
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
}
Exemplo n.º 15
0
/**
 * シリアルからデータリード
 * @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;
}
Exemplo n.º 16
0
// 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;
}
Exemplo n.º 17
0
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;
}
Exemplo n.º 18
0
void main()
{
    int i;

    TRISAbits.TRISA0 = 1;   //Direciona o pino 0 da porta A como entrada
    TRISAbits.TRISA3 = 1;   //Direciona o pino 3 da porta A como entrada
    TRISCbits.TRISC2 = 0;	//Direciona o pino 2 da porta C como saída (Aquecimento)
    TRISAbits.TRISA4 = 1;   //Direciona o pino 4 da porta A como entrada (B2)

    OpenUSART(USART_TX_INT_OFF		//desabilita interrupção de transmissão
              & USART_RX_INT_OFF	//desabilita interrupção de recepção
              & USART_ASYNCH_MODE	//modo assíncrono
              & USART_EIGHT_BIT		//transmissão e recepção em 8 bits
              & USART_BRGH_HIGH,	//Baud Rate em alta velocidade
              25);					//SPBRG p/ 19200 bps

    ADCON2 = 0b10100001;     /*ADFM1 = 1 -> Resultado da conversão AD
                                    ... justificado à direita
                                    -
                                    ****Velocidade de aquisição em 8TAD
                                    ACQT2 = 1
                                    ACQT1 = 0
                                    ACQT0 = 0
                                    ****Fonte de clock em Fosc/8
                                    ADCS2 = 0
                                    ADCS1 = 0
                                    ADCS0 = 1*/


    ADCON1 = 0b00001011;      /* -
                           	 -
                             VCFG1 = 0 -> Vref- = terra
                             VCFG0 = 1 -> Vref+ = VDD
                             *****Seleciona os canais AN0 e AN3 como analógicos
                             PCFG3 = 1
                             PCFG2 = 1
                             PCFG1 = 0
                             PCFG0 = 1*/

    msg_inicial();


    while(1)
    {
        while(!DataRdyUSART());  //Aguarda a chegada de um caractere no buffer de recepção

        rec[i] = getcUSART();	    //recebe o caractere e armazena no índice n_dado da
        //matriz n_dado

        putcUSART(rec[i]);
        Delay10KTCYx(1);   	//Gera um delay de 5ms
        i++;

        if(rec[i-1]==0x0D)
        {
            trata_serial();
            i=0;
            msg_inicial();
        }



    }
}
Exemplo n.º 19
0
void SerialProc(void)
{
    WORD numBytesRead;
    BYTE i;
    char buff[10];
    char *buffPtr;
    BYTE *readPtr;
    char state; // 0:readed ',' 1:readed number
    BYTE exit;
    WORD byteOrWord; // 0:未設定
    WORD pos;
    WORD v;
    WORD bpos;

    // check whether data is recieved from uart
    if (!DataRdyUSART())
        return;

    bpos = 0;
    // read data from uart
    numBytesRead = ReadSerial(uartInBuffer, sizeof(uartInBuffer));
    if (numBytesRead == 0 || numBytesRead == 0xFFFF)
        return;

    state = 0;
    buffPtr = buff;
    readPtr = uartInBuffer;
    pos = 0;
    byteOrWord = 0;
    exit = 0;
loop:
    for(i=0;i<numBytesRead;i++) {
        if (readPtr[i]=='\r' || readPtr[i]=='\n'){
            if (pos>0) {
                if (buffPtr==buff) {
                    sprintf(uartOutBuffer,
                            (far rom char*)"parse error. need number. [%d,%c]\r\n",
                            bpos+i,readPtr[i]);
                    PutsStringCPtr(uartOutBuffer);
                    return;
                }
                *buffPtr++ = '\0';
                v = atoi(buff);
                exit += WriteBuffer(v,&pos,&byteOrWord);
                buffPtr = buff;

                WriteEOF(pos);
                SendIRImpl();
                PrintIRBuffer("echo,");
            }
            return;
        } else if (state==0) {
            if(readPtr[i] != 'H' && readPtr[i] != 'L') {
                sprintf(uartOutBuffer,
                        (far rom char*)"parse error. need 'H' or 'L'. [%d,%c]\r\n",
                        bpos+i,readPtr[i]);
                PutsStringCPtr(uartOutBuffer);

                WriteEOF(pos);
                PrintIRBuffer("echo,");
                return;
            }
            state = 1;
        } else if (state==1) {
            if (readPtr[i] == ',') {
                state = 0;
                if (buffPtr==buff) {
                    sprintf(uartOutBuffer,
                            (far rom char*)"parse error. need number. [%d,%c]\r\n",
                            bpos+i,readPtr[i]);
                    PutsStringCPtr(uartOutBuffer);
                    WriteEOF(pos);
                    PrintIRBuffer("echo,");
                    return;
                }
                *buffPtr++ = '\0';
                v = atoi(buff);
                exit += WriteBuffer(v,&pos,&byteOrWord);
                buffPtr = buff;
            } else if ('0' <= readPtr[i] && readPtr[i] <= '9') {
                *buffPtr++ = readPtr[i];
            } else {
                sprintf(uartOutBuffer,
                        (far rom char*)"parse error. need ',' or number. [%d,%c]\r\n",
                        bpos+i,readPtr[i]);
                PutsStringCPtr(uartOutBuffer);
                WriteEOF(pos);
                PrintIRBuffer("echo,");
                return;
            }
        }
    }
    bpos += numBytesRead;
    numBytesRead = ReadSerial(uartInBuffer, sizeof(uartInBuffer));
    if (numBytesRead == 0xFFFF)
        return;
    goto loop;
}
Exemplo n.º 20
0
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;
}
Exemplo n.º 21
0
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
}
Exemplo n.º 22
0
//===============================================================================================
//  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);
            }
        }







    }


}
Exemplo n.º 23
0
unsigned char ReadUART(void)
{
while(!(DataRdyUSART()));                                       /* Reading a data byte                                        */
     return(ReadUSART());
}
Exemplo n.º 24
0
void Read_str(unsigned char* str, unsigned char n)
{
while(!(DataRdyUSART()));                                       /* configure external LCD                                       */
getsUSART((char*)str, n);                                       /* Reading a string from UART                                   */
}
Exemplo n.º 25
0
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;
}
Exemplo n.º 26
0
//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;
}
Exemplo n.º 27
0
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
    }
}