Haberler:

Foruma Resim Yükleme ve Boyut Sınırlaması ( ! )  https://bit.ly/2GMFb8H

Ana Menü

Can Bus Ayarları

Başlatan rree, 20 Aralık 2021, 09:19:44

rree

void can_init(CAN_OP_MODE Mode=CAN_OP_NORMAL);

// Sets the CAN bit rate of the device.  'Clock' is the clock speed of the
// external crystal/oscillator connected to the MCP2515.  'Rate' is the bit
// rate to use.  The math requires that Clock be evenly divisible by Rate.
can_ec_t can_set_baud(uint32_t Clock, uint32_t Rate);


// Sets the operational mode of the CAN peripheral.  'Mode' is the operational
// mode to set.  'Update' specifies whether to update a global variable used to
// save the operation mode, which is used by some functions to return the
//operational mode back to it's previous operating mode.
void can_set_mode(CAN_OP_MODE Mode, int1 Update=TRUE);

// Sets up the specified Filter's Id, and Type.  'Filter' is the filter to
// setup (0-5), filters 0 and 1 are for RX Buffer 0 and filters 2 to 5 are for
// RX Buffer 1.  'Id' is the Id to assign to the filter and 'Type' is sets the
// type of messages filter accepts, either SID or EID, see CAN_FILTER_TYPE
// above for values to pass to function.
void can_set_filter_id(CAN_FILTER Filter, uint32_t Id, CAN_FILTER_TYPE Type);

// Sets up the specified Mask's Id and Type.  'Mask' is the mask to setup 
// (0-1), mask 0 is for filter 0 and 1 and mask 1 is for filter 2 to 5.  'Id'
// is the mask value to assign to the mask. 'Type' used to specify if the mask
// is for SID or EID messages, controls how the SID and EID bits are set for the
// mask type, see CAN_MASK_TYPE above for types it can be set to.
void can_set_mask_id(CAN_FILTER_MASK Mask, uint32_t Id, CAN_MASK_TYPE Type);

// Used to load a message to transmit into a TX buffer.  'Header' is a pointer
// to CAN_TX_HEADER type with info to send message with, Id, Data Length, etc.,
// 'Data' is pointer to the payload to send with message.  The functions loads 
// the messages into the first available TX buffer.
can_ec_t can_putd(CAN_TX_HEADER *Header, uint8_t *Data);

// Used to retrieve a received message from a RX buffer.  'Header' is a pointer
// to a CAN_RX_HEADER type variable to save the received message's header info
// to, Id, Data length, etc., 'Data' is a pointer to an array to save the
// received messages's payload to.  The function checks/retrieves the message
// from the RX buffer 0.
can_ec_t can_getd(CAN_RX_HEADER *Header, uint8_t *Data);

// Used to determine if there is a least one message to retrieve from a RX
// buffer.  Returns TRUE if there is a message to retrieve and FALSE if there
// is no message to retrieve.

Yukarıdaki fonksiyonları nasıl kullanıyoruz?
İstediğim Can Bus dan gelen tüm id lerin verilerini görmek.
Aşağıda Yazdığım program

#include <16F1939.h>
#device ADC=10
#use delay(clock=16MHz,crystal=4MHz)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,stream=PORT1)

#use spi (MASTER, SPI1, BAUD=100000, MODE=0, BITS=8, STREAM=SPI_1)

#define PIN_LED1  PIN_A1
#define PIN_LED2  PIN_A2
#define PIN_LED3  PIN_A3
 
#define MCP2515_SPI_CS_PIN    PIN_B1
#define MCP2515_SPI_SDI_PIN  PIN_C5
#define MCP2515_SPI_SDO_PIN  PIN_C4
#define MCP2515_SPI_SCK_PIN  PIN_C3

#include <can-mcp2515.c>

#define LED1_HIGH output_low(PIN_LED1)
#define LED1_LOW  output_high(PIN_LED1)
#define LED2_HIGH output_low(PIN_LED2)
#define LED2_LOW  output_high(PIN_LED2)
#define LED3_HIGH output_low(PIN_LED3)
#define LED3_LOW  output_high(PIN_LED3)

#define RESPOND_TO_ID_AD  0x00000201
#define RESPOND_TO_ID_LED  0x202

  CAN_RX_HEADER rHeader;
  CAN_TX_HEADER tHeader;
  uint8_t Buffer[8];
  uint8_t i;
  uint8_t SayacA=0;
  short BayrakA=0;
  uint16_t DegU16A=0;
void CevapVer(Void)
{
      uint8_t Data_Byte=23;            
                  tHeader.Id = RESPOND_TO_ID_AD;
                  tHeader.Length = 1;
                  tHeader.ext = TRUE;
                  tHeader.rtr = FALSE;
                  tHeader.Priority = 1;
                // i = read_adc();
                  printf("Gönderilen Data= %X\r\n\n",Data_Byte);
                  can_putd(&tHeader, &i);    //put data on transmit buffer
}

void DataGonder(Void)
{
      uint8_t Data_Byte=23;            
                  tHeader.Id = RESPOND_TO_ID_AD;
                  tHeader.Length = 1;
                  tHeader.ext = TRUE;
                  tHeader.rtr = TRUE;
                  tHeader.Priority = 1;
                // i = read_adc();
                  printf("Gönderilen Data= %u\r\n\n",Data_Byte);
                  can_putd(&tHeader, &i);    //put data on transmit buffer
}

#INT_TIMER0
void  TIMER0_isr(void) 
{
  SayacA++;
  if (SayacA > 124)
  {
      SayacA=0;
    BayrakA=1;
  }
}




void main(void)
{
  setup_adc_ports(sAN0 | sAN1 | sAN2 | sAN3, VSS_VDD);
  setup_adc(ADC_CLOCK_INTERNAL);
  setup_lcd(LCD_DISABLED);
  setup_timer_0(RTCC_INTERNAL|RTCC_DIV_128|RTCC_8_BIT);      //8,0 ms overflow


  
 
 // printf("\rCCS CAN EXAMPLE\r\n\n");
// ORJİNAL PROGRAM  
  
  
  
  set_adc_channel(0);
  delay_ms(100);
  enable_interrupts(INT_TIMER0);
  enable_interrupts(GLOBAL);
 
    for(i=0;i<8;i++)
      {
        Buffer[i]=0;
      }

  LED1_HIGH;
  LED2_HIGH;
  LED3_HIGH;
  printf("\rCCS CAN EXAMPLE\r\n\n");
  delay_ms(1000);
  LED1_LOW;
  LED2_LOW;
  LED3_LOW;
  
  can_init();
  //can_set_filter_id(0x000,0,0);

  printf("Running...\r\n\n");
///DataGonder();
//delay_ms(10);CevapVer();
  while(TRUE)
  {
      if (BayrakA==1){DataGonder();BayrakA=0; }
  
      if(can_kbhit())  //if data is waiting in buffer...
        {
            if(can_getd(&rHeader, Buffer) == CAN_EC_OK) //...then get data from buffer
            {
              DegU16A=make16(Buffer[1],Buffer[0]);
              printf("Mesajlar IdNo=%8LX Length=%X Filter=%X err_ovfl=%X  ext=%X rtr=%X Data= %4LX\r\n\n"
              , rHeader.Id,rHeader.length,rHeader.Filter,rHeader.err_ovfl,rHeader.ext,rHeader.rtr,DegU16A);
            }
        }
  }
}
Aşağıdaki  Cevap veren
#include <16F1939.h>
#device ADC=10
#use delay(clock=16MHz,crystal=4MHz)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,stream=PORT1)

#use spi (MASTER, SPI1, BAUD=100000, MODE=0, BITS=8, STREAM=SPI_1)

#define PIN_LED1  PIN_A1
#define PIN_LED2  PIN_A2
#define PIN_LED3  PIN_A3
 
#define MCP2515_SPI_CS_PIN    PIN_B1
#define MCP2515_SPI_SDI_PIN  PIN_C5
#define MCP2515_SPI_SDO_PIN  PIN_C4
#define MCP2515_SPI_SCK_PIN  PIN_C3

#include <can-mcp2515.c>

#define LED1_HIGH output_low(PIN_LED1)
#define LED1_LOW  output_high(PIN_LED1)
#define LED2_HIGH output_low(PIN_LED2)
#define LED2_LOW  output_high(PIN_LED2)
#define LED3_HIGH output_low(PIN_LED3)
#define LED3_LOW  output_high(PIN_LED3)

#define RESPOND_TO_ID_AD  0x201
#define RESPOND_TO_ID_LED  0x202


void main(void)
{
 // printf("\rCCS CAN EXAMPLE\r\n\n");
// ORJİNAL PROGRAM  
  CAN_RX_HEADER rHeader;
  CAN_TX_HEADER tHeader;
  uint8_t Buffer[8];
  uint8_t i;
  uint16_t DegU16A=0;
  
  setup_port_a(sAN0, VSS_VDD);
  setup_adc(ADC_CLOCK_INTERNAL);
  set_adc_channel(0);
  
  enable_interrupts(GLOBAL);

  for(i=0;i<8;i++) 
      {
        Buffer[i]=0;
      }

  LED1_HIGH;
  LED2_HIGH;
  LED3_HIGH;
  printf("\rCCS CAN EXAMPLE\r\n\n");
  delay_ms(1000);
  LED1_LOW;
  LED2_LOW;
  LED3_LOW;
  
  can_init();

  printf("Running...\r\n\n");

  while(TRUE)
  {
      if(can_kbhit())  //if data is waiting in buffer...
      {
        if(can_getd(&rHeader, Buffer) == CAN_EC_OK) //...then get data from buffer
        {
            printf("Buffer0= %X\r\n\n",Buffer[0]);
            if(rHeader.Id == RESPOND_TO_ID_LED)
              {
                  printf("Chaining LEDs\r\n\n");
                  if (bit_test(Buffer[0],0)) {LED1_HIGH;} else {LED1_LOW;}
                  if (bit_test(Buffer[0],1)) {LED2_HIGH;} else {LED2_LOW;}
                  if (bit_test(Buffer[0],2)) {LED3_HIGH;} else {LED3_LOW;}
              }
            if (rHeader.Id ==  RESPOND_TO_ID_AD)
              {
                  tHeader.Id = RESPOND_TO_ID_AD;
                  tHeader.Length = 2;
                  tHeader.ext = TRUE;
                  tHeader.rtr = FALSE;
                  tHeader.Priority = 1;
                  DegU16A = read_adc();
                  printf("Sending AD reading: %X\r\n\n",i);
                  can_putd(&tHeader, &DegU16A);    //put data on transmit buffer
              }
        }
      }
  }
}
Bunlar biribiri ile haberleşiyor sorun yok ama bir can bus controllu elektrik moturu sürücüsünden iletişim kuramadık.