工作項目用到了can總線,是在樹莓派的板子上運行的。用的can芯片是mcp2515。效果還不錯,波形很好。
0.png (44.94 KB, 下載次數(shù): 88)
下載附件
2018-4-21 02:04 上傳
單片機源程序如下:
- #define CAN_10Kbps 0x31
- #define CAN_25Kbps 0x13
- #define CAN_50Kbps 0x09
- #define CAN_100Kbps 0x04
- #define CAN_125Kbps 0x03
- #define CAN_250Kbps 0x01
- #define CAN_500Kbps 0x00
- #define SCK 14
- #define SI 12
- #define SO 13
- #define CS 10 //CE0
- #define INTIN 3
- #define RST 4
- #define TXREQ_M 0b11110111
- #define CH 0
- #include "MCP2515.h"
- #include "bcm2835.h"
- #include <wiringPi.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- //#include <wiringPiSPI.h>
- //采用bcm2835spi
- void MCP2515_WriteByte(unsigned char addr,unsigned char dat)
- {
- unsigned char buf = CAN_WRITE;
- unsigned char spiDat[] = {CAN_WRITE,addr,dat};
- digitalWrite(CS,LOW); //置MCP2515的CS為低電平
- bcm2835_spi_transfern(spiDat,3);
- digitalWrite(CS,HIGH); //置MCP2515的CS為高電平
- }
- unsigned char MCP2515_ReadByte(unsigned char addr)
- {
- unsigned char rByte = 0;
- unsigned char spiDat[] = {CAN_READ,addr,0xff};
- digitalWrite(CS,LOW); //置MCP2515的CS為低電平
- bcm2835_spi_transfern(spiDat,3);
- digitalWrite(CS,HIGH); //置MCP2515的CS為高電平
- rByte = spiDat[2];
- return rByte; //返回讀到的一個字節(jié)數(shù)據(jù)
- }
- void MCP2515_Reset(void)
- {
- digitalWrite(RST,LOW);
- delay(100);
- digitalWrite(RST,HIGH);
- delay(100);
- }
- //void IntHandle()
- //{
- // char temp=0;
- // //中斷處理函數(shù),MCU清除中斷FLAG,重新使能INTE,并且輸出RXB
- // //digitalWrite(28,HIGH);
- // printf("into the interrupt mode\nINT-IN is: %d\n",digitalRead(INTIN));
- //// temp = MCP2515_ReadByte(CANINTF);
- // //MCP2515_WriteByte(CANINTF,0x00);
- // printf("into the interrupt mode\nINT-IN is: %d\n",digitalRead(INTIN));
- // printf("RXB0SIDH : %02x\n",MCP2515_ReadByte(RXB0SIDH));
- // printf("RXB0SIDL : %02x\n",MCP2515_ReadByte(RXB0SIDL));
- // printf("RXB0D0 : %02x\n",MCP2515_ReadByte(RXB0D0));
- // printf("RXB0D1 : %02x\n",MCP2515_ReadByte(RXB0D1));
- // printf("RXB0D2 : %02x\n",MCP2515_ReadByte(RXB0D2));
- // printf("RXB0D3 : %02x\n",MCP2515_ReadByte(RXB0D3));
- // printf("RXB0D4 : %02x\n",MCP2515_ReadByte(RXB0D4));
- // printf("RXB0D5 : %02x\n",MCP2515_ReadByte(RXB0D5));
- // printf("RXB0D6 : %02x\n",MCP2515_ReadByte(RXB0D6));
- // printf("RXB0D7 : %02x\n",MCP2515_ReadByte(RXB0D7));
- // printf("\n\n");
- // exit(1);
- //}
- void MCP2515_Init(void)
- {
- unsigned char temp=1;
- MCP2515_Reset(); //發(fā)送復位指令軟件復位MCP2515
- printf("Reset OK \n");
- // delay(1); //Delay_Nms(1); //通過軟件延時約nms(不準確)
- //設置波特率為125Kbps
- //set CNF1,SJW=00,長度為1TQ,BRP=49,TQ=[2*(BRP+1)]/Fsoc=2*50/8M=12.5us
- MCP2515_WriteByte(CNF1,CAN_125Kbps);
- //set CNF2,SAM=0,在采樣點對總線進行一次采樣,PHSEG1=(2+1)TQ=3TQ,PRSEG=(0+1)TQ=1TQ
- MCP2515_WriteByte(CNF2,0x80|PHSEG1_3TQ|PRSEG_1TQ);
- //set CNF3,PHSEG2=(2+1)TQ=3TQ,同時當CANCTRL.CLKEN=1時設定CLKOUT引腳為時間輸出使能位
- MCP2515_WriteByte(CNF3,PHSEG2_3TQ);
-
- MCP2515_WriteByte(RXB0CTRL,0x20);//接收 過濾器信息
- MCP2515_WriteByte(RXB0DLC,DLC_8);//設置接收數(shù)據(jù)的長度為8個字節(jié)
- MCP2515_WriteByte(RXF0SIDH,0x12);//配置驗收濾波寄存器n標準標識符高位
- MCP2515_WriteByte(RXF0SIDL,0x11);//配置驗收濾波寄存器n標準標識符低位
- MCP2515_WriteByte(RXM0SIDH,0xFF);//配置驗收屏蔽寄存器n標準標識符高位
- MCP2515_WriteByte(RXM0SIDL,0xE0);//配置驗收屏蔽寄存器n標準標識符低位
-
- // MCP2515_WriteByte(RXB0SIDH,0x00);//清空接收緩沖器0的標準標識符高位
- // MCP2515_WriteByte(RXB0SIDL,0x00);//清空接收緩沖器0的標準標識符低位
-
- MCP2515_WriteByte(CANINTE,0x01);//配置CAN中斷使能寄存器的接收緩沖器0滿中斷使能,其它位禁止中斷
- MCP2515_WriteByte(CANINTF,0x00);//清空CAN中斷標志寄存器的所有位(必須由MCU清空)
-
- MCP2515_WriteByte(CANCTRL,REQOP_LOOPBACK|CLKOUT_ENABLED);//將MCP2515設置為環(huán)回模式,退出配置模式
-
- temp=MCP2515_ReadByte(CANSTAT);//讀取CAN狀態(tài)寄存器的值
-
- while(OPMODE_LOOPBACK!=(temp & 0xE0))//判斷MCP2515是否已經(jīng)進入環(huán)回模式
- {
- //MCP2515_WriteByte(CANCTRL,REQOP_LOOPBACK|CLKOUT_ENABLED);//再次將MCP2515設置為環(huán)回模式,退出配置模式
- temp=MCP2515_ReadByte(CANSTAT);
-
- }
- printf("MCP2515 loopback mode OK\n");
- }
- void LOOPTest()
- {
- printf("LoopTest begin\n");
- MCP2515_WriteByte(TXB0DLC,DLC_8);//設置發(fā)送數(shù)據(jù)長度為8
- MCP2515_WriteByte(TXB0SIDH,0x12);//發(fā)送緩沖器0標準標識符高位
- MCP2515_WriteByte(TXB0SIDL,0x11);//發(fā)送緩沖器0標準標識符低位
- MCP2515_WriteByte(TXB0CTRL,0x02); //高優(yōu)先級
- MCP2515_WriteByte(TXB0D0,0x83); //數(shù)據(jù)
- MCP2515_WriteByte(TXB0D1,0x83);
- MCP2515_WriteByte(TXB0D2,0x83);
- MCP2515_WriteByte(TXB0D3,0x87);
- MCP2515_WriteByte(TXB0D4,0x87);
- MCP2515_WriteByte(TXB0D5,0x87);
- MCP2515_WriteByte(TXB0D6,0x84);
- MCP2515_WriteByte(TXB0D7,0x84);
- printf("TXB0D0 : %02x\n",MCP2515_ReadByte(TXB0D0));
- printf("TXB0D1 : %02x\n",MCP2515_ReadByte(TXB0D1));
- printf("TXB0D2 : %02x\n",MCP2515_ReadByte(TXB0D2));
- printf("TXB0D3 : %02x\n",MCP2515_ReadByte(TXB0D3));
- printf("TXB0D4 : %02x\n",MCP2515_ReadByte(TXB0D4));
- printf("TXB0D5 : %02x\n",MCP2515_ReadByte(TXB0D5));
- printf("TXB0D6 : %02x\n",MCP2515_ReadByte(TXB0D6));
- printf("TXB0D7 : %02x\n",MCP2515_ReadByte(TXB0D7));
-
-
- printf("RXB0SIDH : %02x\n",MCP2515_ReadByte(RXB0SIDH));
- printf("RXB0SIDL : %02x\n",MCP2515_ReadByte(RXB0SIDL));
- printf("RXB0D0 : %02x\n",MCP2515_ReadByte(RXB0D0));
- printf("RXB0D1 : %02x\n",MCP2515_ReadByte(RXB0D1));
- printf("RXB0D2 : %02x\n",MCP2515_ReadByte(RXB0D2));
- printf("RXB0D3 : %02x\n",MCP2515_ReadByte(RXB0D3));
- printf("RXB0D4 : %02x\n",MCP2515_ReadByte(RXB0D4));
- printf("RXB0D5 : %02x\n",MCP2515_ReadByte(RXB0D5));
- printf("RXB0D6 : %02x\n",MCP2515_ReadByte(RXB0D6));
- printf("RXB0D7 : %02x\n",MCP2515_ReadByte(RXB0D7));
- printf("\n\n");
- char temp = 0;
-
- temp = MCP2515_ReadByte(TXB0CTRL);
- MCP2515_WriteByte(TXB0CTRL,(temp| (~TXREQ_M ))); //請求發(fā)送
-
- int cnt = 0;
- while(MCP2515_ReadByte(TXB0CTRL)&(0b00001000) != 0x00)
- {
- cnt++;
- if(cnt>=100000)
- {
- printf("trasmission failed\n");
- exit(1);
- }
- }
- delay(100);
- printf("RXB0SIDH : %02x\n",MCP2515_ReadByte(RXB0SIDH));
- printf("RXB0SIDL : %02x\n",MCP2515_ReadByte(RXB0SIDL));
- printf("RXB0D0 : %02x\n",MCP2515_ReadByte(RXB0D0));
- printf("RXB0D1 : %02x\n",MCP2515_ReadByte(RXB0D1));
- printf("RXB0D2 : %02x\n",MCP2515_ReadByte(RXB0D2));
- printf("RXB0D3 : %02x\n",MCP2515_ReadByte(RXB0D3));
- printf("RXB0D4 : %02x\n",MCP2515_ReadByte(RXB0D4));
- printf("RXB0D5 : %02x\n",MCP2515_ReadByte(RXB0D5));
- printf("RXB0D6 : %02x\n",MCP2515_ReadByte(RXB0D6));
- printf("RXB0D7 : %02x\n",MCP2515_ReadByte(RXB0D7));
- printf("\n\n");
-
- }
- void bcm2835_SPISetup(void)
- {
- if (!bcm2835_init())
- {
- printf("bcm2835_init failed. Are you running as root??\n");
- exit(1);
- }
- if (!bcm2835_spi_begin())
- {
- printf("bcm2835_spi_begin failed. Are you running as root??\n");
- exit(1);
- }
- bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); // The default
- bcm2835_spi_setDataMode(BCM2835_SPI_MODE0); // The default
- bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_65536); // The default
- bcm2835_spi_chipSelect(BCM2835_SPI_CS0); // The default
- bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW); // the default
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
所有資料51hei提供下載:
MCP2515.rar
(25.34 KB, 下載次數(shù): 116)
2018-4-21 01:50 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|