Exemple #1
0
void CANbus_setup()
{
     char SJW, BRP, Phase_Seg1, Phase_Seg2, Prop_Seg, txt[4];
     unsigned short init_flag;
     long mask;
/*
   CAN BUS Timing Parameters
*/
     SJW = 1;
     BRP = 1;
     Phase_Seg1 = 6;
     Phase_Seg2 = 7;
     Prop_Seg = 6;

     init_flag = _CAN_CONFIG_SAMPLE_THRICE  &
                 _CAN_CONFIG_PHSEG2_PRG_ON  &
                 _CAN_CONFIG_STD_MSG        &
                 _CAN_CONFIG_DBL_BUFFER_ON  &
                 _CAN_CONFIG_VALID_STD_MSG &
                 _CAN_CONFIG_LINE_FILTER_OFF;
/*
  Initialise CAN module
*/
      CANInitialize(SJW, BRP, Phase_Seg1, Phase_Seg2, Prop_Seg, init_flag);
/*
   Set CAN CONFIG mode
*/
     CANSetOperationMode(_CAN_MODE_CONFIG, 0xFF);

      mask = -1;
/* Set all MASK1 bits to 1's
*/
      CANSetMask(_CAN_MASK_B1, mask, _CAN_CONFIG_STD_MSG);
/*
   Set all MASK2 bits to 1's    */
      CANSetMask(_CAN_MASK_B2, mask, _CAN_CONFIG_STD_MSG);
      
/* Filter 0x50 (temp node) and 0x202 (rear node) only   */

      CANSetFilter(_CAN_FILTER_B1_F1,0x202,_CAN_CONFIG_STD_MSG);

      CANSetFilter(_CAN_FILTER_B1_F2,0x50,_CAN_CONFIG_STD_MSG);

/* Now set CAN module to NORMAL mode, as setup done.   */

      CANSetOperationMode(_CAN_MODE_NORMAL, 0xFF);
}/* The CANbus is now set up and ready for use  */
Exemple #2
0
/*********************************************************************
 * Function:        void CANInitialize(BYTE SJW,
 *                                      BYTE BRP,
 *                                      BYTE PHSEG1,
 *                                      BYTE PHSEG2,
 *                                      BYTE PROPSEG,
 *                                      enum CAN_CONFIG_FLAGS flags)
 *
 * PreCondition:    MCU must be in Configuration mode or else these
 *                  values will be ignored.
 *
 * Input:           SJW     - SJW value as defined in 18CXX8 datasheet
 *                              (Must be between 1 thru 4)
 *                  BRP     - BRP value as defined in 18CXX8 datasheet
 *                              (Must be between 1 thru 64)
 *                  PHSEG1  - PHSEG1 value as defined in 18CXX8
 *                            datasheet
 *                              (Must be between 1 thru 8)
 *                  PHSEG2  - PHSEG2 value as defined in 18CXX8
 *                            datasheet
 *                              (Must be between 1 thru 8)
 *                  PROPSEG - PROPSEG value as defined in 18CXX8
 *                            datasheet
 *                              (Must be between 1 thru 8)
 *                  flags   - Value of type enum CAN_CONFIG_FLAGS
 *
 * Output:          CAN bit rate is set. All masks registers are set
 *                  '0' to allow all messages.
 *                  Filter registers are set according to flag value.
 *                  If (config & CAN_CONFIG_VALID_XTD_MSG)
 *                      Set all filters to XTD_MSG
 *                  Else if (config & CONFIG_VALID_STD_MSG)
 *                      Set all filters to STD_MSG
 *                  Else
 *                      Set half of the filters to STD while rests to
 *                      XTD_MSG.
 *
 * Side Effects:    All pending transmissions are aborted.
 *
 ********************************************************************/
 void CANInitialize(BYTE SJW,
                    BYTE BRP,
                    BYTE PHSEG1,
                    BYTE PHSEG2,
                    BYTE PROPSEG,
                    enum CAN_CONFIG_FLAGS config)
 {
     BYTE FilterConfig1;
     BYTE FilterConfig2;

    // In order to setup necessary config parameters of CAN module,
    // it must be in CONFIG mode.
    CANSetOperationMode(CAN_OP_MODE_CONFIG);

    // Now set the baud rate.
    CANSetBaudRate(SJW,
                    BRP,
                    PHSEG1,
                    PHSEG2,
                    PROPSEG,
                    config);

    RXB0CON = config & CAN_CONFIG_MSG_BITS;
    if ( (config & CAN_CONFIG_DBL_BUFFER_BIT)
                == CAN_CONFIG_DBL_BUFFER_ON )
        RXB0CON_RX0DBEN = 1;



    RXB1CON = RXB0CON;

    // Set default filter and mask registers for all receive buffers.
    CANSetMask(CAN_MASK_B1, 0, CAN_CONFIG_XTD_MSG);
    CANSetMask(CAN_MASK_B2, 0, CAN_CONFIG_XTD_MSG);

    switch( (config & CAN_CONFIG_MSG_BITS) | ~CAN_CONFIG_MSG_BITS )
    {
    case CAN_CONFIG_VALID_XTD_MSG:
        FilterConfig1 = CAN_CONFIG_XTD_MSG;
        FilterConfig2 = CAN_CONFIG_XTD_MSG;
        break;

    case CAN_CONFIG_VALID_STD_MSG:
        FilterConfig1 = CAN_CONFIG_STD_MSG;
        FilterConfig2 = CAN_CONFIG_STD_MSG;
        break;
    default:
        FilterConfig1 = CAN_CONFIG_STD_MSG;
        FilterConfig2 = CAN_CONFIG_XTD_MSG;
        break;
    }

    // By default, there will be no mask on any receive filters,
    // hence filter value of '0' will be ignored.
    CANSetFilter(CAN_FILTER_B1_F1, 0, FilterConfig1);
    CANSetFilter(CAN_FILTER_B1_F2, 0, FilterConfig1);
    CANSetFilter(CAN_FILTER_B2_F1, 0, FilterConfig2);
    CANSetFilter(CAN_FILTER_B2_F2, 0, FilterConfig2);
    CANSetFilter(CAN_FILTER_B2_F3, 0, FilterConfig2);
    CANSetFilter(CAN_FILTER_B2_F4, 0, FilterConfig2);

    // Restore to Normal mode.
    CANSetOperationMode(CAN_OP_MODE_NORMAL);
}
Exemple #3
0
/////*PROGRAMME PRINCIPAL*/////
void main (void) {
    // Initialisations.
    ADCON1 = 0x0F;
    ADCON0 = 0;
    WDTCON = 0;

    // Configurations.
    TRISA = 0b11101111;
    TRISB = 0b11111111;
    TRISC = 0b00111111;

    ADCON1 = 0b00001011; // Configuration ADC de AN0 à AN3

    // Timer de rafraichissement des BP et de chronométrage des sonars
    OpenTimer0(TIMER_INT_ON & T0_16BIT & T0_SOURCE_INT & T0_PS_1_2); // 38Hz
    INTCON2bits.TMR0IP = 0; // priorité basse, car les pulses sonars prennent du temps

    // Il faut laisser 50ms (20Hz) entre deux débordements (limite des sonars).
    // Mais on alterne les salves donc 38Hz est parfait.
    // Du coup on ne vérifie pas les boutons plus souvent, mais pas grave.
    // Les sharps actualisent leur valeur toutes les 40ms, mais on va les
    // échantillonner plus vite (38Hz), tant mieux si on choppe les nouvelles
    // valeurs rapidement.

    // Interruptions sur les pins "Echo output" des sonars (AN0 et AN1).
    // OpenRBxINT faits à la main (sauf pullup, par défaut).

    /*OpenADC(ADC_FOSC_4 // Tosc < 5.7 MHz.
            & ADC_LEFT_JUST // On pourra ignorer l'octet de poid faible (2 bits non nuls).
            & ADC_20_TAD, // 15µs * 5 MHz / 4 [cf ci-dessus] = 18.75 Tosc
            ADC_CH0 // Changé par le timer de toute façon.
            & ADC_INT_ON & ADC_VREFPLUS_VDD & ADC_VREFMINUS_VSS,
            0b00001011); // Pins analogiques pour AN0 à AN3.


    // Interruption ADC.
    IPR1bits.ADIP = 0; // Priorité basse.
    PIE1bits.ADIE = 1; // Activée.
    PIR1bits.ADIF = 0;
    //*/
    // Configuration du CAN.
    CANInitialize(1, 5, 7, 6, 2, CAN_CONFIG_VALID_STD_MSG);
    // Configuration des masques et filtres.
    CANSetOperationMode(CAN_OP_MODE_CONFIG);
    // Set Buffer 1 Mask value.
    CANSetMask(CAN_MASK_B1, 0b00100000000, CAN_CONFIG_STD_MSG);
    CANSetMask(CAN_MASK_B2, 0xFFFFFF, CAN_CONFIG_STD_MSG);
    // Set Buffer 1 filter values.
    CANSetFilter(CAN_FILTER_B1_F1, 0b00100000000, CAN_CONFIG_STD_MSG);
    CANSetFilter(CAN_FILTER_B1_F2, 0b00100000000, CAN_CONFIG_STD_MSG);
    // Set CAN module into Normal mode.
    CANSetOperationMode(CAN_OP_MODE_NORMAL);

    // Interruption Buffer 0.
    IPR3bits.RXB0IP = 0; // Priorité basse.
    PIE3bits.RXB0IE = 1; // Activée.
    PIR3bits.RXB0IF = 0;

    // Signal de démarrage.
    led = 0;
    for(i = 0; i < 20; i++) {
        led = led ^ 1;
        DelayMS(50);
    }

    // Autorisation des interruptions.
    RCONbits.IPEN = 1;
    INTCONbits.GIEH = 1;
    INTCONbits.GIEL = 1;


    while(1) {
    }
}