小弟买了一块ttl转can的模块,接线按照(单片机can口接上can收发器,再接上can转ttl,再接上激光测距模块),激光测距是ttl电平的,用ttl转usb用电脑检查了一下。现在有个问题:我想用单片机can口接收来自激光测距的信号,并不能如期地接受到信号,调试也不知道是软件还是硬件问题,我下面把程序贴出来,请问一下我程序有没有问题,还想找一个没问题的程序测试我硬件
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "can.h"
int main(void)
{
u8 mode=0;//CAN1¤×÷Ä£ê½;0,ÆÕí¨Ä£ê½;1,»·»ØÄ£ê½
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//éèÖÃÏμí3ÖD¶ÏóÅÏ輶·Ö×é2
delay_init(168); //3õê¼»ˉÑóê±oˉêy
uart_init(115200); //3õê¼»ˉ′®¿ú2¨ìØÂêÎa115200
CAN2_Mode_Init(CAN_SJW_1tq,CAN_BS2_6tq,CAN_BS1_7tq,6,mode); //CANÆÕí¨Ä£ê½3õê¼»ˉ,ÆÕí¨Ä£ê½,2¨ìØÂê500Kbps
while(1){}
}
can的文件
#include "can.h"
#include "delay.h"
#include "usart.h"
u8 CAN2_Mode_Init(u8 tsjw,u8 tbs2,u8 tbs1,u16 brp,u8 mode)
{
GPIO_InitTypeDef GPIO_InitStructure;
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
#if CAN2_RX0_INT_ENABLE
NVIC_InitTypeDef NVIC_InitStructure;
#endif
//ê1ÄüÏà1Øê±Öó
// RCC_APB1PeriphClockCmd(RCC_APB1Periph_GPIOB, ENABLE);//ê1ÄüPORTAê±Öó
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN2, ENABLE);//ê1ÄüCAN1ê±Öó
//3õê¼»ˉGPIO
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12| GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//¸′óÃ1|Äü
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//íÆíìêä3ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//éÏà-
GPIO_Init(GPIOB, &GPIO_InitStructure);//3õê¼»ˉPA11,PA12
//òy½Å¸′óÃó3éäÅäÖÃ
GPIO_PinAFConfig(GPIOB,GPIO_PinSource12,GPIO_AF_CAN2); //GPIOA11¸′óÃÎaCAN1
GPIO_PinAFConfig(GPIOB,GPIO_PinSource13,GPIO_AF_CAN2); //GPIOA12¸′óÃÎaCAN1
//CANμ¥ÔaéèÖÃ
CAN_InitStructure.CAN_TTCM=DISABLE; //·Çê±¼ä′¥·¢í¨DÅÄ£ê½
CAN_InitStructure.CAN_ABOM=DISABLE; //èí¼t×Ô¶ˉàëÏß1üàí
CAN_InitStructure.CAN_AWUM=DISABLE;//ËˉÃßÄ£ê½í¨1yèí¼t»½DÑ(Çå3yCAN->MCRμÄSLEEPλ)
CAN_InitStructure.CAN_NART=ENABLE; //½ûÖ1±¨ÎÄ×Ô¶ˉ′«Ëí
CAN_InitStructure.CAN_RFLM=DISABLE; //±¨ÎÄ2»Ëø¶¨,DÂμĸ2¸Ç¾éμÄ
CAN_InitStructure.CAN_TXFP=DISABLE; //óÅÏ輶ó鱨Îıê궷û¾ö¶¨
CAN_InitStructure.CAN_Mode= mode; //Ä£ê½éèÖÃ
CAN_InitStructure.CAN_SJW=tsjw; //ÖØDÂí¬2½ìøÔ¾¿í¶è(Tsjw)Îatsjw+1¸öê±¼äμ¥λ CAN_SJW_1tq~CAN_SJW_4tq
CAN_InitStructure.CAN_BS1=tbs1; //Tbs1·¶Î§CAN_BS1_1tq ~CAN_BS1_16tq
CAN_InitStructure.CAN_BS2=tbs2;//Tbs2·¶Î§CAN_BS2_1tq ~ CAN_BS2_8tq
CAN_InitStructure.CAN_Prescaler=brp; //·ÖÆμÏμêy(Fdiv)Îabrp+1
CAN_Init(CAN2, &CAN_InitStructure); // 3õê¼»ˉCAN1
//ÅäÖÃ1yÂËÆ÷
CAN_FilterInitStructure.CAN_FilterNumber=14; //1yÂËÆ÷14+
CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;
CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit; //32λ
CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000;////32λID
CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;//32λMASK
CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_Filter_FIFO0;//1yÂËÆ÷01Øáaμ½FIFO0
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE; //¼¤»î1yÂËÆ÷0
CAN_FilterInit(&CAN_FilterInitStructure);//ÂË2¨Æ÷3õê¼»ˉ
#if CAN2_RX0_INT_ENABLE
CAN_ITConfig(CAN2,CAN_IT_FMP0,ENABLE);//FIFO0ÏûÏ¢1òoÅÖD¶ÏÔêDí.
NVIC_InitStructure.NVIC_IRQChannel = CAN2_RX0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // Ö÷óÅÏ輶Îa1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // ′ÎóÅÏ輶Îa0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
#endif
return 0;
}
//ÖD¶Ï·tÎñoˉêy
void CAN2_RX0_IRQHandler(void)
{
CanRxMsg RxMessage;
int i=0;
CAN_Receive(CAN2, 0, &RxMessage);
for(i=0;i<8;i++)
printf("rxbuf[%d]:%d
",i,RxMessage.Data[i]);
}
.h文件就省略了,能编译也能下载,就是接收不到外面数据,不知道是我程序的问题还是我硬件问题,大牛们帮忙看看,里面改例程的程序的,can好难搞懂
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
z 01:传感器地址(十六进制)
z 03:状态寄存器(十六进制)
z 0100:参数地址(十六进制,数据)
z 019A:参数值(十六进制,实时距离,以毫米为单位),表示实际距离是410毫米,即0.41米
z C5CD:校验码(CRC16,低字节在前,高字节在后)
一周热门 更多>