Exemple #1
0
void main( void ) {
    unsigned char i = 0;
    // Pins C6 and C7 are used for UART TX and RX respectively
    InitEcoCar();
    ReadAnalog(0);
    J1939_Initialization( TRUE );

    LATB = 0;
    TRISB = 0;
    TRISC = 0;

    ShowBootupAnimation();

    while (J1939_Flags.WaitingForAddressClaimContention)
        J1939_Poll(5);

    // CANbus is now initialized and we can now loop while we check
    // our message receive buffer for new CANbus messages (where all received messages are put).
    while (1) {
        //Receive Messages
        J1939_Poll(10);
        while (RXQueueCount > 0) {
            J1939_DequeueMessage( &Msg );
            LATCbits.LATC5 = 1;

            if ( J1939_Flags.ReceivedMessagesDropped )
                J1939_Flags.ReceivedMessagesDropped = 0;
        }
    }
}
Exemple #2
0
void main( void ) {
    // Pins C6 and C7 are used for UART TX and RX respectively
    InitEcoCar();
    TRISCbits.RC4 = 0;

    // Open USART:
    OpenUSART( USART_TX_INT_OFF &
        USART_RX_INT_OFF &
        USART_ASYNCH_MODE &
        USART_EIGHT_BIT &
        USART_CONT_RX &
        USART_BRGH_HIGH, 207 );     // 9600 bps baud rate

    RCSTAbits.SPEN = 1;		// Enable USART on pins C6, C7
    BAUDCONbits.BRG16 = 0;	// 8-bit BR Generator
    
    J1939_Initialization( TRUE );

    while (J1939_Flags.WaitingForAddressClaimContention)
        J1939_Poll(5);
    
    // CANbus is now initialized and we can now loop while we check
    // our message receive buffer for new CANbus messages (where all received messages are put).
    while (1) {
        //Receive Messages
        J1939_Poll(10);
        while (RXQueueCount > 0) {

            J1939_DequeueMessage( &Msg );
            if ( Msg.GroupExtension >= 0xA0 && Msg.GroupExtension <= 0xA7 )
                LATCbits.LATC4 = 1;
            // Currently, only broadcast messages are repeated
//            if( Msg.PDUFormat == PDU_BROADCAST )
                putSerialData(Msg.GroupExtension, Msg.Data[0], Msg.Data[1]);
            
            if ( J1939_Flags.ReceivedMessagesDropped )
                J1939_Flags.ReceivedMessagesDropped = 0;

            LATCbits.LATC4 = 0;
        }
    }
}
Exemple #3
0
void main( void )
{
    int i;
    unsigned int counter;
    
    InitEcoCar();

    TRISCbits.TRISC1 = 1;   // Listen on this pin for LCD
    
    J1939_Initialization( TRUE );

    // Open USART:
    OpenUSART( USART_TX_INT_OFF &
    USART_RX_INT_OFF &
    USART_ASYNCH_MODE &
    USART_EIGHT_BIT &
    USART_CONT_RX &
    USART_BRGH_HIGH, 34 );
    
    // 34 = 57,600 bps at 32 MHz
    BAUDCONbits.BRG16 = 0;  // Enable 16-bit SPBRG (high speed!)

    RCSTAbits.SPEN = 1; // Enable USART pin.

    // Check for address collisions:
    while (J1939_Flags.WaitingForAddressClaimContention)
            J1939_Poll(5);

    LATCbits.LATC0 = 1;     // PICIN to LCD

    while (1)
    {
       
        // Listen in for a signal from the master wanting our data.
        J1939_Poll(10);
        while (RXQueueCount > 0)
        {
            J1939_DequeueMessage( &Msg );
            if (Msg.PDUFormat == PDU_BROADCAST && Msg.GroupExtension != CYCLE_COMPLETE && NEW_CYCLE == 1)
            {
                // Store data broadcasted by slaves
                // MSB = Msg.Data[0], DataList[i][1]
                // LSB = Msg.Data[1], DataList[i][2]
                for(i=0;i<(sizeof(DataList)/sizeof(DataList[0]));i++)
                {
                    if(DataList[i][0] == ERR_TIMEOUT && TimeoutLog == 0x00)
                    {
                        // Clear the timeout info that is currently stored in memory.
                        DataList[i][1] = 0x00;
                        DataList[i][2] = 0x00;
                    }
                           
                    if (Msg.GroupExtension == DataList[i][0])
                    {
                        // Handle timeout logging:
                        if(Msg.GroupExtension == ERR_TIMEOUT)
                        {
                            TimeoutLog = Msg.Data[0];   // Set timeout log to current failing node.
                        }
                        else if(Msg.GroupExtension == TimeoutLog)
                        {
                            TimeoutLog = 0x00;  // Obviously, the node is responding now.
                        }

                        DataList[i][1] = Msg.Data[0];
                        DataList[i][2] = Msg.Data[1];
                        
                    }
                }
            }
            else if (Msg.PDUFormat == PDU_BROADCAST && Msg.GroupExtension == CYCLE_COMPLETE && NEW_CYCLE == 1)
            {
                // Master is finished asking for data, send data now
		// Transmit slave data

                NEW_CYCLE = 0;                  // Reset cycle flag
                LATCbits.LATC0 = 1;     // Signal to LCD to start listening.

                for(i=0;i<sizeof(DataList)/sizeof(DataList[0]);i++)
                {
                    while(PORTCbits.RC1 == 0){ Nop(); };  // Wait until LCD is ready to receive serial data.
                    putSerialData(DataList[i][0], DataList[i][1], DataList[i][2]);

                    // Wait while LCD turns listening off and checks for screen events.
                    counter = 0;
                    while(PORTCbits.RC1 == 1)
                    {
                        counter++;
                        if(counter > 50000)
                        { 
                            LATCbits.LATC4 = 1;     // Turn timeout error LED on.
                            counter = 0;
                            break;
                        }
                    }

                }

                LATCbits.LATC0 = 0;     // Signal to LCD to stop listening.
            }

            else if (Msg.PDUFormat == PDU_BROADCAST && Msg.GroupExtension == CYCLE_COMPLETE)
            {
                NEW_CYCLE = 1;   // Receiving data from beginning of cycle
            }

        }
    }
}