Exemple #1
0
void sendTest() {
    Write1USART(Test[0]);
    Delay1KTCYx(4);
    Write1USART(Test[1]);
    Delay1KTCYx(4);
    Write1USART(Test[2]);
    Delay1KTCYx(4);
    Write1USART(Test[3]);
    Delay1KTCYx(4);
    Write1USART('\n');
}
Exemple #2
0
void sendAccel()
{
    Write1USART(accArr[0]);
    Delay1KTCYx(4);
    Write1USART('.');
    Delay1KTCYx(4);
    Write1USART(accArr[1]);
    Delay1KTCYx(4);
    Write1USART(accArr[2]);
    Delay1KTCYx(4);
    Write1USART('\n');
}
Exemple #3
0
void UART_init(){
    // PIN A0 is  temp[0]In, A1 is bp1In, A2 is bp2In, A3 is pulseIn
    
    //C6 is USART/RS232 Tx, C7 is USART/RS232 Rx
    
    TRISCbits.RC6 = 0; //TX pin set as output
    TRISCbits.RC7 = 1; //RX pin set as input
    
    // Enable digital for all c pins
    ANSELC=0x00;
    //LATC  =0x00;
    //TRISC =0x00;
    
    RCONbits.IPEN = 1; // turn on priorities
    IPR1bits.RCIP = 1; // make Rx interrupt high priority
    INTCONbits.GIEH = 1; // turn on high priority interrupts
    
    Open1USART(USART_TX_INT_OFF  &
            USART_RX_INT_OFF   &
            USART_ASYNCH_MODE &
            USART_EIGHT_BIT   &
            //USART_CONT_RX     &
            USART_BRGH_HIGH, 
            BAUD_9600_4MHz);   //Set Baud Rate here  Fosc / (64 * (spbrg + 1))

    Write1USART(0x0c);   // clear hyperterminal
}
Exemple #4
0
/************************************************************************
SetDate

Reads a date entered from UART and sets the RTCC to the new date.

Parameters:     None
Return:         None
Side Effects:   RTCC date modified.
 ************************************************************************/
void SetDate(void) {
    unsigned char year, month, day;

    printf("\r\nEnter Date (yy/mm/dd): ");

    year = ReadBCD();
    Write1USART('/');

    month = ReadBCD();
    Write1USART('/');

    day = ReadBCD();
    printf("\r\n");

    SetRTCCDate(year, month, day);
}
Exemple #5
0
/************************************************************************
SetTime

Reads time entered from UART and sets the RTCC to the new time.

Parameters:     None
Return:         None
Side Effects:   RTCC time modified.
 ************************************************************************/
void SetTime(void) {
    unsigned char hour, min, sec;

    printf("\r\nEnter Time (hh:mm:ss): ");

    hour = ReadBCD();
    Write1USART(':');

    min = ReadBCD();
    Write1USART(':');

    sec = ReadBCD();
    printf("\r\n");

    SetRTCCTime(hour, min, sec);
}
void main() {
    char keyNum = 0;
    keypadSetup();
    rs232Setup1(); // setup the serial port

    while (1) { // just loop for test
        keyNum = (char) checkForInput() + '`';
        Write1USART(keyNum);

        Delay1KTCYx(1);
    }

}
Exemple #7
0
/************************************************************************
ReadBCD

Reads a two digit number from the serial port and stores it in
Binary Coded Decimal (BCD) formatted byte return value.

Parameters:     None
Return:         Two digit number entered from UART, stored in BCD form.
Side Effects:   None
 ************************************************************************/
unsigned char ReadBCD(void) {
    unsigned char byte = 0;
    unsigned char bcd;

    while (byte < '0' || byte > '9') {
        while (DataRdy1USART() == 0);
        byte = Read1USART();
    }
    Write1USART(byte);
    byte -= '0';
    bcd = byte << 4;

    while (byte < '0' || byte > '9') {
        while (DataRdy1USART() == 0);
        byte = Read1USART();
    }
    Write1USART(byte);
    byte -= '0';
    bcd |= byte;

    return bcd;
}
Exemple #8
0
// http://microchip.wikidot.com/faq:29
void putch(char c)
{
#ifdef DEBUG
#ifdef WITH_HOS
    while (Busy2USART())
        CLRWDT();
    Write2USART(c);
    while (Busy2USART())
        CLRWDT();
#else
    while (Busy1USART())
        ;
    Write1USART(c);
    while (Busy1USART())
        ;
#endif
#endif
}
Exemple #9
0
void uart_send_int_handler() {
    //LATBbits.LATB7 = 0;
    // Make sure we still have data to send
    if(uc_ptr->sendSize > 0) {
#ifdef __USE18F46J50
        Write1USART(uc_ptr->sendBuffer[uc_ptr->sendCurrent]);
#else
        WriteUSART(uc_ptr->sendBuffer[uc_ptr->sendCurrent]);
#endif
        


        // Flag interrupt for next byte
        PIE1bits.TXIE = 1;

        // Increment/decrement size and index
        uc_ptr->sendCurrent++;
        uc_ptr->sendSize--;
    }
}
Exemple #10
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
}
Exemple #11
0
Fichier : hax.c Projet : ellbur/hax
/*
 * STREAM IO
 */
void putc(char data) {
	/* From the Microchip C Library Docs */
	while(Busy1USART());
	Write1USART(data);
}
Exemple #12
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;
}
Exemple #13
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
    }
}