I'm writing a code for MX975F512H.
reads the data from the CANbus and send data to USB.
CAN and USB samples working separately. But in the same code does not work. (sending out a few times, after crashing)
Code: Select all
procedure USB1Interrupt(); iv IVT_USB_1; ilevel 7; ics ICS_SRS;
begin
USB_Interrupt_Proc();
end;
begin
CHECON := 0x32;
AD1PCFG := 0xFFFF; // Configure AN pins as digital I/O
AD1PCFGSET := 0xFFFF; // CAN2 module enabled
USBIE_bit := 0;
IPC11SET := 7 shl 10; // Interrupt priority 7
EnableInterrupts();
LATB := 0;
LATD := 0;
TRISB := 0;
TRISD := 0;
HID_Enable(@readbuff,@writebuff);
Can_Init_Flags := 0;
Can_Send_Flags := 0; // clear flags
Can_Rcv_Flags := 0;
Can_Conf_Flags := 0;
Can_Send_Flags := _CAN_TX_STD_FRAME and _CAN_TX_NO_RTR_FRAME;
Can_Conf_Flags := _CAN_CONFIG_SAMPLE_ONCE and
_CAN_CONFIG_PHSEG2_PRG_ON and
_CAN_CONFIG_STD_MSG and
_CAN_CONFIG_MATCH_MSG_TYPE and
_CAN_CONFIG_LINE_FILTER_OFF;
//CAN 1 Initialize
...
writebuff[0] := '>';
writebuff[1] := 'D';
while(TRUE) do
begin
while(HID_Read() > 0) do
;
Msg_Rcvd := CAN1Read(Rx_ID, Rx_Data, Rx_Data_Len, Can_Rcv_Flags); // receive message
if (Msg_Rcvd <> 0) then begin
writebuff[2] := 'A';
writebuff[3] := Hi(HiWord(Rx_ID));
writebuff[4] := Lo(HiWord(Rx_ID));
writebuff[5] := Hi(LoWord(Rx_ID));
writebuff[6] := Lo(LoWord(Rx_ID));
writebuff[7] := rx_stat;
writebuff[8] := Rx_Data_Len;
for i:=0 to Rx_Data_Len-1 do writebuff[9+i] := Rx_Data[i];
HID_Write(@writebuff,64);
end;
end;