Docs

Micro-controllers, wireless transmission and database

Head

MCP2515 High-Speed CAN Controller with Blue Pill using STM32CubeIDE

Prerequisites

This project assumes you have already installed STM32CubeIDE. You need to have previously done a basic blink sketch with blue-pill using STM32CubeIDE. I have made a complete video from installing STM32CubeIDE to LED blink program. You can watch it by clicking this link. https://www.youtube.com/watch?v=kXg467nVd_A

Wiring Diagram

Diagram

STM32CubeIDE Settings

Click RCC → High Speed Clock (HSE) to Crystal/Ceramic Resonator

Click Clock Configuration tab → HCLK (MHz) to 72

Click Pinout and Configuration tab

Click connectivity → Click SPI1

For Mode select Full Duplex Master

Set Prescalor to 8

Set PA4 to GPIO_Output and User Label to CAN_CS

Click connectivity --> Click I2C1

For I2C select I2C

Configuration --> Parameter Settings

For I2C speed select Fast Mode

ADC1 - IN9 (tick)

Parameter Settings --> ADC Settings --> Continuous Conversion Mode (Enabled)

Click Clock Configuration tab → Set ADC Prescalor to /6

Libraries

Taken from https://blog.naver.com/eziya76/221188525127

Inside Core/Inc Folder

fonts.h ssd1306.h CANSPI.h MCP2515.h

Inside Core/Src Folder

fonts.c ssd1306.c CANSPI.c MCP2515.c

Additional code on top of STM32CubeIDE generated code for main.c

/* USER CODE BEGIN Includes */
#include "fonts.h"
#include "ssd1306.h"
#include "CANSPI.h"
/* USER CODE END Includes */

  /* USER CODE BEGIN 2 */
  uint16_t readValue;
  uint16_t rxValue;
  char sstr[5];
  char snum[5];
  uCAN_MSG txMessage;
  uCAN_MSG rxMessage;
  HAL_ADC_Start(&hadc1);
  CANSPI_Initialize();
  SSD1306_Init();
  SSD1306_GotoXY (0,0);
  SSD1306_Puts ("OLED Test", &Font_11x18, 1);
  SSD1306_UpdateScreen();
  HAL_Delay(1000);
  /* USER CODE END 2 */

  /* USER CODE BEGIN WHILE */
  while (1)
  {
    HAL_ADC_PollForConversion(&hadc1,1000);
    readValue = HAL_ADC_GetValue(&hadc1);
    txMessage.frame.idType = dSTANDARD_CAN_MSG_ID_2_0B;
    txMessage.frame.id = 0x127; // ID can be between Hex1 and Hex7FF (1-2047 decimal)
    txMessage.frame.dlc = 8;
    txMessage.frame.data0 = 'S';
    txMessage.frame.data1 = 'T';
    txMessage.frame.data2 = 'M';
    txMessage.frame.data3 = '3';
    txMessage.frame.data4 = '2';
    txMessage.frame.data5 = '-';
    txMessage.frame.data6 = readValue & 0xff;
    txMessage.frame.data7 = readValue >> 8;
    CANSPI_Transmit(&txMessage);

    if(CANSPI_Receive(&rxMessage))
    {
        sstr[0] = rxMessage.frame.data0;
        sstr[1] = rxMessage.frame.data1;
        sstr[2] = rxMessage.frame.data2;
        sstr[3] = rxMessage.frame.data3;
        sstr[4] = rxMessage.frame.data4;
        sstr[5] = ' ';
        rxValue = ((uint16_t)rxMessage.frame.data7 << 8) | rxMessage.frame.data6;
        itoa(rxValue, snum, 10);
        SSD1306_Clear();
        SSD1306_GotoXY (0, 0);
        SSD1306_Puts (sstr, &Font_11x18, 1);
        SSD1306_GotoXY (0, 30);
        SSD1306_Puts (snum, &Font_11x18, 1);
        SSD1306_UpdateScreen();
    }
    HAL_Delay(100);
    /* USER CODE END WHILE */

CAN Baud rate settings

8MHz 1000kBPS CNF1 -  0x00
8MHz 1000kBPS CNF2 -  0x80
8MHz 1000kBPS CNF3 -  0x80

8MHz 500kBPS CNF1 -  0x00
8MHz 500kBPS CNF2 -  0x90
8MHz 500kBPS CNF3 -  0x82

8MHz 250kBPS CNF1 -  0x00
8MHz 250kBPS CNF2 -  0xB1
8MHz 250kBPS CNF3 -  0x85

8MHz 200kBPS CNF1 -  0x00
8MHz 200kBPS CNF2 -  0xB4
8MHz 200kBPS CNF3 -  0x86

8MHz 125kBPS CNF1 -  0x01
8MHz 125kBPS CNF2 -  0xB1
8MHz 125kBPS CNF3 -  0x85

8MHz 100kBPS CNF1 -  0x01
8MHz 100kBPS CNF2 -  0xB4
8MHz 100kBPS CNF3 -  0x86

8MHz 80kBPS CNF1 -  0x01
8MHz 80kBPS CNF2 -  0xBF
8MHz 80kBPS CNF3 -  0x87

8MHz 50kBPS CNF1 -  0x03
8MHz 50kBPS CNF2 -  0xB4
8MHz 50kBPS CNF3 -  0x86

8MHz 40kBPS CNF1 -  0x03
8MHz 40kBPS CNF2 -  0xBF
8MHz 40kBPS CNF3 -  0x87

8MHz 33k3BPS CNF1 -  0x47
8MHz 33k3BPS CNF2 -  0xE2
8MHz 33k3BPS CNF3 -  0x85

8MHz 31k25BPS CNF1 -  0x07
8MHz 31k25BPS CNF2 -  0xA4
8MHz 31k25BPS CNF3 -  0x84

8MHz 20kBPS CNF1 -  0x07
8MHz 20kBPS CNF2 -  0xBF
8MHz 20kBPS CNF3 -  0x87

8MHz 10kBPS CNF1 -  0x0F
8MHz 10kBPS CNF2 -  0xBF
8MHz 10kBPS CNF3 -  0x87

8MHz 5kBPS CNF1 -  0x1F
8MHz 5kBPS CNF2 -  0xBF
8MHz 5kBPS CNF3 -  0x87

16MHz 1000kBPS CNF1 -  0x00
16MHz 1000kBPS CNF2 -  0xD0
16MHz 1000kBPS CNF3 -  0x82

16MHz 500kBPS CNF1 -  0x00
16MHz 500kBPS CNF2 -  0xF0
16MHz 500kBPS CNF3 -  0x86

16MHz 250kBPS CNF1 -  0x41
16MHz 250kBPS CNF2 -  0xF1
16MHz 250kBPS CNF3 -  0x85

16MHz 200kBPS CNF1 -  0x01
16MHz 200kBPS CNF2 -  0xFA
16MHz 200kBPS CNF3 -  0x87

16MHz 125kBPS CNF1 -  0x03
16MHz 125kBPS CNF2 -  0xF0
16MHz 125kBPS CNF3 -  0x86

16MHz 100kBPS CNF1 -  0x03
16MHz 100kBPS CNF2 -  0xFA
16MHz 100kBPS CNF3 -  0x87

16MHz 95kBPS CNF1 -  0x03
16MHz 95kBPS CNF2 -  0xAD
16MHz 95kBPS CNF3 -  0x07

16MHz 83k3BPS CNF1 -  0x03
16MHz 83k3BPS CNF2 -  0xBE
16MHz 83k3BPS CNF3 -  0x07

16MHz 80kBPS CNF1 -  0x03
16MHz 80kBPS CNF2 -  0xFF
16MHz 80kBPS CNF3 -  0x87

16MHz 50kBPS CNF1 -  0x07
16MHz 50kBPS CNF2 -  0xFA
16MHz 50kBPS CNF3 -  0x87

16MHz 40kBPS CNF1 -  0x07
16MHz 40kBPS CNF2 -  0xFF
16MHz 40kBPS CNF3 -  0x87

16MHz 33k3BPS CNF1 -  0x4E
16MHz 33k3BPS CNF2 -  0xF1
16MHz 33k3BPS CNF3 -  0x85

16MHz 20kBPS CNF1 -  0x0F
16MHz 20kBPS CNF2 -  0xFF
16MHz 20kBPS CNF3 -  0x87

16MHz 10kBPS CNF1 -  0x1F
16MHz 10kBPS CNF2 -  0xFF
16MHz 10kBPS CNF3 -  0x87

16MHz 5kBPS CNF1 -  0x3F
16MHz 5kBPS CNF2 -  0xFF
16MHz 5kBPS CNF3 -  0x87

20MHz 1000kBPS CNF1 -  0x00
20MHz 1000kBPS CNF2 -  0xD9
20MHz 1000kBPS CNF3 -  0x82

20MHz 500kBPS CNF1 -  0x00
20MHz 500kBPS CNF2 -  0xFA
20MHz 500kBPS CNF3 -  0x87

20MHz 250kBPS CNF1 -  0x41
20MHz 250kBPS CNF2 -  0xFB
20MHz 250kBPS CNF3 -  0x86

20MHz 200kBPS CNF1 -  0x01
20MHz 200kBPS CNF2 -  0xFF
20MHz 200kBPS CNF3 -  0x87

20MHz 125kBPS CNF1 -  0x03
20MHz 125kBPS CNF2 -  0xFA
20MHz 125kBPS CNF3 -  0x87

20MHz 100kBPS CNF1 -  0x04
20MHz 100kBPS CNF2 -  0xFA
20MHz 100kBPS CNF3 -  0x87

20MHz 83k3BPS CNF1 -  0x04
20MHz 83k3BPS CNF2 -  0xFE
20MHz 83k3BPS CNF3 -  0x87

20MHz 80kBPS CNF1 -  0x04
20MHz 80kBPS CNF2 -  0xFF
20MHz 80kBPS CNF3 -  0x87

20MHz 50kBPS CNF1 -  0x09
20MHz 50kBPS CNF2 -  0xFA
20MHz 50kBPS CNF3 -  0x87

20MHz 40kBPS CNF1 -  0x09
20MHz 40kBPS CNF2 -  0xFF
20MHz 40kBPS CNF3 -  0x87

20MHz 33k3BPS CNF1 -  0x0B
20MHz 33k3BPS CNF2 -  0xFF
20MHz 33k3BPS CNF3 -  0x87