Понедельник, Май 25, 2020

Opel пример 1.

/*
 * File:   can.h
 *
 * Created on 3 Июнь 2014 г., 16:08
 */

#ifndef CAN_H
#define    CAN_H

#ifdef    __cplusplus
extern "C" {
#endif

typedef struct TxMSG{
        char _TXB0SIDH,
        _TXB0SIDL,
        _TXB0EIDH,
        _TXB0EIDL,
        _TXB0DLC;
        char _TXB0D[8];
}TMSG;

typedef struct RMSG{
        char _RXB0SIDH,
        _RXB0SIDL,
        _RXB0EIDH,
        _RXB0EIDL,
        _RXB0DLC,
        _RXB0D0;
        char _RXB0D[8];
}RMSG;


void  InitCAN(void);
char  RxCan(RMSG*);
void  TxCan(TMSG*);
void  InitUsart(void);
void  SetInterrupt(void);
void  SendErrorUSART(void);
void  SendCanMessage(TMSG*,char*);
#ifdef    __cplusplus
}
#endif

#endif    /* CAN_H */

 


#include <p18cxxx.h>
#include <delays.h>
#include <usart.h>
#include "can.h"
#pragma code
void  TxCan(TMSG *msg)
{
    TXB0CONbits.TXREQ=0;
    TXB0CONbits.TXABT=1;
    TXB0D0  =msg->_TXB0D[0];
    TXB0D1  =msg->_TXB0D[1];
    TXB0D2  =msg->_TXB0D[2];
    TXB0D3  =msg->_TXB0D[3];
    TXB0D4  =msg->_TXB0D[4];
    TXB0D5  =msg->_TXB0D[5];
    TXB0D6  =msg->_TXB0D[6];
    TXB0D7  =msg->_TXB0D[7];
    TXB0DLC =msg->_TXB0DLC;
    TXB0SIDH=msg->_TXB0SIDH;
    TXB0SIDL=msg->_TXB0SIDL;

    TXB0CON=0b00001000;
}
void  SendCanMessage(TMSG* msg,char* sour)
{
    msg->_TXB0SIDH=sour[1];
    msg->_TXB0SIDL=sour[2];
    msg->_TXB0EIDH=sour[3];
    msg->_TXB0EIDL=sour[4];
    msg->_TXB0DLC=sour[5];
    msg->_TXB0D[0]=sour[6];
    msg->_TXB0D[1]=sour[7];
    msg->_TXB0D[2]=sour[8];
    msg->_TXB0D[3]=sour[9];
    msg->_TXB0D[4]=sour[10];
    msg->_TXB0D[5]=sour[11];
    msg->_TXB0D[6]=sour[12];
    msg->_TXB0D[7]=sour[13];
    TxCan(msg);
}
char  RxCan(RMSG *ms)
{
    if(!RXB0CONbits.RXFUL){
        return 1;
    }
    ms->_RXB0SIDH=RXB0SIDH;
    while(!TXSTAbits.TRMT);
    putcUSART(ms->_RXB0SIDH);

    ms->_RXB0SIDL=RXB0SIDL;
    while(!TXSTAbits.TRMT);
    putcUSART(ms->_RXB0SIDL);

    ms->_RXB0EIDH=RXB0EIDH;
    while(!TXSTAbits.TRMT);
    putcUSART(ms->_RXB0EIDH);

    ms->_RXB0EIDL=RXB0EIDL;
    while(!TXSTAbits.TRMT);
    putcUSART(ms->_RXB0EIDL);

    ms->_RXB0DLC=RXB0DLC;
    while(!TXSTAbits.TRMT);
    putcUSART(ms->_RXB0DLC);

    ms->_RXB0D[0]=RXB0D0;
    while(!TXSTAbits.TRMT);
    putcUSART(ms->_RXB0D[0]);

    ms->_RXB0D[1]=RXB0D1;
    while(!TXSTAbits.TRMT);
    putcUSART(ms->_RXB0D[1]);

    ms->_RXB0D[2]=RXB0D2;
    while(!TXSTAbits.TRMT);
    putcUSART(ms->_RXB0D[2]);

    ms->_RXB0D[3]=RXB0D3;
    while(!TXSTAbits.TRMT);
    putcUSART(ms->_RXB0D[3]);

    ms->_RXB0D[4]=RXB0D4;
    while(!TXSTAbits.TRMT);
    putcUSART(ms->_RXB0D[4]);

    ms->_RXB0D[5]=RXB0D5;
    while(!TXSTAbits.TRMT);
    putcUSART(ms->_RXB0D[5]);

    ms->_RXB0D[6]=RXB0D6;
    while(!TXSTAbits.TRMT);
    putcUSART(ms->_RXB0D[6]);

    ms->_RXB0D[7]=RXB0D7;
    while(!TXSTAbits.TRMT);
    putcUSART(ms->_RXB0D[7]);

    RXB0CONbits.RXFUL=0;

    return 0;
}
//**************************************************************************************************
void  InitCAN(void){
    TRISBbits.TRISB3=1;
    TRISBbits.TRISB2=0;
    CANCON=0x80;
    while ((CANSTAT & 0xE0) != 0x80);
    RXM0SIDH=0x00;
    RXM0SIDL=0x00;
    RXM1SIDH=0x00;
    RXM1SIDL=0x00;
    RXM0EIDH=0x00;
    RXM0EIDL=0x00;
    RXM1EIDH=0x00;
    RXM1EIDL=0x00;
    RXB0CONbits.RXM0=0;
    RXB0CONbits.RXM1=0;
    RXB0CONbits.RXB0DBEN=0;

    BRGCON1=0x01;            //16MHz 500kbps
    BRGCON2=0x90;
    BRGCON3=0x02;

    CIOCONbits.ENDRHI=1;
    CIOCONbits.CANCAP=0;
    CANCON=0b00000000;
    while ((CANSTAT & 0b11100000) != 0b00000000);
}
void InitUsart(void)
{
    OSCCONbits.SCS0=0;
    OSCCONbits.SCS1=0;
    OSCCONbits.IRCF0=0;
    OSCCONbits.IRCF1=1;
    OSCCONbits.IRCF2=1;
    OSCTUNEbits.PLLEN=1;
    baudUSART(BAUD_16_BIT_RATE&BAUD_WAKEUP_OFF&BAUD_AUTO_OFF);
    OpenUSART( USART_TX_INT_OFF&USART_RX_INT_ON&USART_ASYNCH_MODE
            &USART_EIGHT_BIT &USART_CONT_RX &USART_BRGH_LOW,25);//38400//8);//HIGH 460800 кбит/с

}
void  SetInterrupt(void)
{
    RCONbits.IPEN=0;
    INTCONbits.GIE=1;
    INTCONbits.PEIE=1;
}
void  SendErrorUSART(void)
{
    int count=0;
    for(count=0;count<13;count++)
    {
        while(!TXSTAbits.TRMT);
        putcUSART(0xff);
    }
}



#include <p18cxxx.h>
#include <delays.h>
#include <usart.h>
#include <timers.h>
#include "can.h"
#pragma config OSC=IRCIO67
#pragma config FCMEN=OFF
#pragma config IESO=OFF
#pragma config PWRT=OFF
#pragma config BOREN=OFF
#pragma config BORV=3
#pragma config WDT=OFF
#pragma config WDTPS=32768
#pragma config PBADEN=OFF
#pragma config LPT1OSC=OFF
#pragma config MCLRE=OFF
#pragma config STVREN=ON
#pragma config LVP=OFF
#pragma config BBSIZ=1024
#pragma config XINST=OFF
#pragma config CP0=OFF
#pragma config CP1=OFF
#pragma config CPB=OFF
#pragma config CPD=OFF
#pragma config WRT0=OFF
#pragma config WRT1=OFF
#pragma config WRTC=OFF
#pragma config WRTB=OFF
#pragma config WRTD=OFF
#pragma config EBTR0=OFF
#pragma config EBTR1=OFF
//**********************************************************
#pragma udata
TMSG tmsg;
RMSG rmsg;
char RxIn[30]={0};
int  WaitTimer1=0;
char incRxIn=0;
int  incFor;
int  j;
//*********************************************************
#pragma code
void MyHighInt(void);
#pragma interrupt MyHighInt
#pragma code high_vector=0x08
void high_vector(void){
    _asm GOTO MyHighInt _endasm
}

void main(void)
{
    ADCON1=0;
    ADCON1=0x0f;
    RxIn[0]=0xff;
    incRxIn=0;
    InitUsart();
    SetInterrupt();
    InitCAN();
    OpenTimer1( TIMER_INT_ON &
            T1_8BIT_RW &
            T1_SOURCE_INT &
            T1_PS_1_1 &
            T1_OSC1EN_OFF &
            T1_SYNC_EXT_OFF );
    WriteTimer1(100);
    while(1)
    {
        switch(RxIn[0])
        {
            case 0x30:
                j=0;
                while(j<10)
                {
                    SendCanMessage(&tmsg,RxIn);
                    WriteTimer1(0);
                    WaitTimer1=0;
                    while(WaitTimer1!=400)
                    {
                        if(!TXB0CONbits.TXREQ)
                            goto next;
                    }
                    SendErrorUSART();
                    goto exit;

                    next:
                    for(incFor=0;incFor<=1000;incFor++)
                    {
                        RxCan(&rmsg);
                        switch(rmsg._RXB0SIDH)
                        {
                            case 0xfd:
                                switch(rmsg._RXB0D[0])
                                {
                                    case 0x01:
                                        goto exit;
                                        break;
                                    default:
                                        break;
                                }
                                break;
                            default:
                                break;
                        }
                    }
                    j++;
                }
                exit:
                RxIn[0]=0xff;
                break;
            case 0x31:
                break;
            case 0x32:
                break;
            case 0x33:
                break;
            case 0x34:
                break;
            case 0x35:
                break;
            case 0x39:
                _asm
                        reset
                _endasm
                break;
            default:
                break;
        }
    }
}
void MyHighInt(void)
{
    if(PIR1bits.RCIF)
    {
        RxIn[incRxIn++]=getcUSART();
        if(incRxIn>13)
            incRxIn=0;
        PIR1bits.RCIF=0;
    }
    else if(PIR1bits.TMR1IF)
    {
        WriteTimer1(100);
        WaitTimer1++;
        PIR1bits.TMR1IF=0;
    }
}



Back to Top