专家
公告
财富商城
电子网
旗下网站
首页
问题库
专栏
标签库
话题
专家
NEW
门户
发布
提问题
发文章
STM32
为什么can引脚改成pa11和pa12就没有波形了,pd0和pd1就可以,复用功能已经注释了,其它程序都不变,为什么会这样?
2019-08-08 13:31
发布
×
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮
站内问答
/
STM32/STM8
2639
2
1449
为什么can引脚改成pa11和pa12就没有波形了,pd0和pd1就可以,复用功能已经注释了,其它程序都不变,为什么会这样?
友情提示:
此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
2条回答
yobuwen
1楼-- · 2019-08-08 16:10
#include "stm32f10x.h"
#include <stdio.h>
#define CAN1_RX0_INT_ENABLE 1
void RCC_Configuration(void)
{
RCC_DeInit();//???RCC?????????
RCC_HSEConfig(RCC_HSE_ON);//????????(HSE)
if( RCC_WaitForHSEStartUp() != RESET)
{
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);//???????????--???????
FLASH_SetLatency(FLASH_Latency_2);
RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);//??PLL????????
RCC_PLLCmd(ENABLE);//??????PLL
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);//?????RCC???????
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);//??????(SYSCLK)
while(RCC_GetSYSCLKSource() == RESET);//????????????
RCC_HCLKConfig(RCC_SYSCLK_Div1);//??AHB??(HCLK)
RCC_PCLK1Config(RCC_HCLK_Div1);//????AHB??(PCLK1)
RCC_PCLK2Config(RCC_HCLK_Div1);//????AHB??(PCLK2)
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOC,ENABLE);
}
}
void USART_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_USART1,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 115200; //?????9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
//USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//??????
USART_Cmd(USART1, ENABLE);
//USART_ClearFlag(USART1, USART_FLAG_TC); /*?????????,????????,?????????????*/
}
void CAN1_Init(u8 Tsjw,u8 Tbs2,u8 Tbs1,u16 Brp,u8 Mode)
{
GPIO_InitTypeDef GPIO_InitStructure;
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
#if CAN1_RX0_INT_ENABLE
NVIC_InitTypeDef NVIC_InitStructure;
#endif
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOD,ENABLE);
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1,ENABLE);
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
// GPIO_Init(GPIOD, &GPIO_InitStructure);
//
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
// GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure CAN pin: TX */ // PA12
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
// GPIO_Init(GPIOB, &GPIO_InitStructure);
//
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
// GPIO_Init(GPIOB, &GPIO_InitStructure);
// GPIO_PinRemapConfig(GPIO_Remap2_CAN1, ENABLE);
// GPIO_PinRemapConfig(GPIO_Remap1_CAN1, ENABLE);
//-------------------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(CAN1, &CAN_InitStructure); // 3õê¼»ˉCAN1 BS1>=tBS2 tBS2>=1¸öCANÖüÆú tBS2>=2tSJW
//-------------------ÅäÖÃ1yÂËÆ÷-------------------------------------//
CAN_FilterInitStructure.CAN_FilterNumber=0; //1yÂËÆ÷0
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 CAN1_RX0_INT_ENABLE
CAN_ITConfig(CAN1,CAN_IT_FMP0,ENABLE);//FIFO0ÏûÏ¢1òoÅÖD¶ÏÔêDí.
NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; // Ö÷óÅÏ輶Îa1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // ′ÎóÅÏ輶Îa0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
#endif
}
/*********************************ÖD¶Ï·tÎñoˉêy***************************************/
#if CAN1_RX0_INT_ENABLE
void USB_LP_CAN1_RX0_IRQHandler(void)
{
int i=0;
CanRxMsg RxMessage;
CAN_Receive(CAN1, 0, &RxMessage);
for(i=0;i<8;i++)
{
printf("rxbuf[%d]:%d ",i,RxMessage.Data[i]);
}
}
#endif
/************************CAN Send Function***********************************
*@param len:data length(<=8); msg:data pointer(8bytes)
*@retval 0:success; others:failt
*******************************************************************************/
u8 CAN1_Send_Msg(u8* msg,u8 len)
{
u8 mbox;
u16 i=0;
CanTxMsg TxMessage;
TxMessage.StdId=0x12; // ±ê×¼±ê궷ûÎa0
TxMessage.ExtId=0x12; // éèÖÃà©Õ1±êê¾·û£¨29룩
TxMessage.IDE=0; // ê1óÃà©Õ1±ê궷û
TxMessage.RTR=0; // ÏûÏ¢ààDíÎaêy¾YÖ¡£¬ò»Ö¡8λ
TxMessage.DLC=len; // ·¢Ëíá½Ö¡DÅÏ¢
for(i=0;i<len;i++)
TxMessage.Data[i]=msg[i]; // μúò»Ö¡DÅÏ¢
mbox= CAN_Transmit(CAN1, &TxMessage);
i=0;
while((CAN_TransmitStatus(CAN1, mbox)==CAN_TxStatus_Failed)&&(i<0XFFF))i++; //μè′y·¢Ëí½áêø
if(i>=0XFFF)return 1;
return 0;
}
/************************CAN Receive Function***********************************
*@param buf:data buffer cache
*@retval 0:none datas; others:data length
*******************************************************************************/
u8 CAN1_Receive_Msg(u8 *buf)
{
u32 i;
CanRxMsg RxMessage;
if( CAN_MessagePending(CAN1,CAN_FIFO0)==0)return 0; //ûóD½óêÕμ½êy¾Y,Ö±½óíË3ö
CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);//¶áè¡êy¾Y
for(i=0;i<RxMessage.DLC;i++)
{
buf[i]=RxMessage.Data[i];
}
return RxMessage.DLC;
}
void delay(int num)
{
while(num--);
}
int fputc(int ch , FILE *f)
{
//USART_ClearFlag(USART1,USART_FLAG_TC);
USART_SendData(USART1, (u8)ch);
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
return (ch);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
void delay_ms(u16 nms)
{
u32 temp;
SysTick->LOAD = 9000*nms;
SysTick->VAL=0X00;//?????
SysTick->CTRL=0X01;//??,???????,???????
do
{
temp=SysTick->CTRL;//????????
}while((temp&0x01)&&(!(temp&(1<<16))));//??????
SysTick->CTRL=0x00; //?????
SysTick->VAL =0X00; //?????
}
int main()
{
u8 re;
u8 sendmsg[8] = {1,2,3,4,5,6,7,8};
RCC_Configuration();
USART_Configuration();
GPIO_Configuration();
CAN1_Init(CAN_SJW_1tq,CAN_BS2_3tq,CAN_BS1_12tq,9,CAN_Mode_Normal);//CAN_Mode_Normal CAN_Mode_LoopBack
while(1)
{
// GPIO_SetBits(GPIOC,GPIO_Pin_6);
// delay_ms(500);
// GPIO_ResetBits(GPIOC,GPIO_Pin_6);
// delay_ms(500);
printf ("ok");
re = CAN1_Send_Msg(sendmsg,8);
printf("re = %d ",re);
}
}
加载中...
目瞪狗呆
2楼-- · 2019-08-08 18:56
小白,帮顶一下
加载中...
一周热门
更多
>
相关问题
STM32F4上I2C(在PROTEUS中模拟)调试不通的问题
6 个回答
芯片供应紧张,准备换个MCU,MM32L系列替换STM32L系列的怎么样?
7 个回答
STM32同时使用两个串口进行数据收发时数据丢包的问题
5 个回答
STM32F103串口通信死机问题
4 个回答
STM32WLE5CC连接SX1268在LoRa模式下能与 SX1278互通吗?
2 个回答
STM32开发板免费用活动
7 个回答
stm32 处理 DHT11占用太多时间,大家程序是怎么设计的
8 个回答
分享一个STM32单片机做的离线编程器代码
9 个回答
相关文章
ST公司第一款无线低功耗单片机模块有效提高物联网设计生产效率
0个评论
如何实现对单片机寄存器的访问
0个评论
通过USB用STM32片内自带Bootloader下载程序及注意事项
0个评论
欲练此功必先自宫之STM32汇编启动,放慢是为了更好的前行
0个评论
×
关闭
采纳回答
向帮助了您的网友说句感谢的话吧!
非常感谢!
确 认
×
关闭
编辑标签
最多设置5个标签!
STM32
保存
关闭
×
关闭
举报内容
检举类型
检举内容
检举用户
检举原因
广告推广
恶意灌水
回答内容与提问无关
抄袭答案
其他
检举说明(必填)
提交
关闭
×
关闭
您已邀请
15
人回答
查看邀请
擅长该话题的人
回答过该话题的人
我关注的人
#include <stdio.h>
#define CAN1_RX0_INT_ENABLE 1
void RCC_Configuration(void)
{
RCC_DeInit();//???RCC?????????
RCC_HSEConfig(RCC_HSE_ON);//????????(HSE)
if( RCC_WaitForHSEStartUp() != RESET)
{
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);//???????????--???????
FLASH_SetLatency(FLASH_Latency_2);
RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);//??PLL????????
RCC_PLLCmd(ENABLE);//??????PLL
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);//?????RCC???????
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);//??????(SYSCLK)
while(RCC_GetSYSCLKSource() == RESET);//????????????
RCC_HCLKConfig(RCC_SYSCLK_Div1);//??AHB??(HCLK)
RCC_PCLK1Config(RCC_HCLK_Div1);//????AHB??(PCLK1)
RCC_PCLK2Config(RCC_HCLK_Div1);//????AHB??(PCLK2)
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOC,ENABLE);
}
}
void USART_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_USART1,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 115200; //?????9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
//USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//??????
USART_Cmd(USART1, ENABLE);
//USART_ClearFlag(USART1, USART_FLAG_TC); /*?????????,????????,?????????????*/
}
void CAN1_Init(u8 Tsjw,u8 Tbs2,u8 Tbs1,u16 Brp,u8 Mode)
{
GPIO_InitTypeDef GPIO_InitStructure;
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
#if CAN1_RX0_INT_ENABLE
NVIC_InitTypeDef NVIC_InitStructure;
#endif
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOD,ENABLE);
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1,ENABLE);
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
// GPIO_Init(GPIOD, &GPIO_InitStructure);
//
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
// GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure CAN pin: TX */ // PA12
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
// GPIO_Init(GPIOB, &GPIO_InitStructure);
//
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
// GPIO_Init(GPIOB, &GPIO_InitStructure);
// GPIO_PinRemapConfig(GPIO_Remap2_CAN1, ENABLE);
// GPIO_PinRemapConfig(GPIO_Remap1_CAN1, ENABLE);
//-------------------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(CAN1, &CAN_InitStructure); // 3õê¼»ˉCAN1 BS1>=tBS2 tBS2>=1¸öCANÖüÆú tBS2>=2tSJW
//-------------------ÅäÖÃ1yÂËÆ÷-------------------------------------//
CAN_FilterInitStructure.CAN_FilterNumber=0; //1yÂËÆ÷0
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 CAN1_RX0_INT_ENABLE
CAN_ITConfig(CAN1,CAN_IT_FMP0,ENABLE);//FIFO0ÏûÏ¢1òoÅÖD¶ÏÔêDí.
NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; // Ö÷óÅÏ輶Îa1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // ′ÎóÅÏ輶Îa0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
#endif
}
/*********************************ÖD¶Ï·tÎñoˉêy***************************************/
#if CAN1_RX0_INT_ENABLE
void USB_LP_CAN1_RX0_IRQHandler(void)
{
int i=0;
CanRxMsg RxMessage;
CAN_Receive(CAN1, 0, &RxMessage);
for(i=0;i<8;i++)
{
printf("rxbuf[%d]:%d ",i,RxMessage.Data[i]);
}
}
#endif
/************************CAN Send Function***********************************
*@param len:data length(<=8); msg:data pointer(8bytes)
*@retval 0:success; others:failt
*******************************************************************************/
u8 CAN1_Send_Msg(u8* msg,u8 len)
{
u8 mbox;
u16 i=0;
CanTxMsg TxMessage;
TxMessage.StdId=0x12; // ±ê×¼±ê궷ûÎa0
TxMessage.ExtId=0x12; // éèÖÃà©Õ1±êê¾·û£¨29룩
TxMessage.IDE=0; // ê1óÃà©Õ1±ê궷û
TxMessage.RTR=0; // ÏûÏ¢ààDíÎaêy¾YÖ¡£¬ò»Ö¡8λ
TxMessage.DLC=len; // ·¢Ëíá½Ö¡DÅÏ¢
for(i=0;i<len;i++)
TxMessage.Data[i]=msg[i]; // μúò»Ö¡DÅÏ¢
mbox= CAN_Transmit(CAN1, &TxMessage);
i=0;
while((CAN_TransmitStatus(CAN1, mbox)==CAN_TxStatus_Failed)&&(i<0XFFF))i++; //μè′y·¢Ëí½áêø
if(i>=0XFFF)return 1;
return 0;
}
/************************CAN Receive Function***********************************
*@param buf:data buffer cache
*@retval 0:none datas; others:data length
*******************************************************************************/
u8 CAN1_Receive_Msg(u8 *buf)
{
u32 i;
CanRxMsg RxMessage;
if( CAN_MessagePending(CAN1,CAN_FIFO0)==0)return 0; //ûóD½óêÕμ½êy¾Y,Ö±½óíË3ö
CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);//¶áè¡êy¾Y
for(i=0;i<RxMessage.DLC;i++)
{
buf[i]=RxMessage.Data[i];
}
return RxMessage.DLC;
}
void delay(int num)
{
while(num--);
}
int fputc(int ch , FILE *f)
{
//USART_ClearFlag(USART1,USART_FLAG_TC);
USART_SendData(USART1, (u8)ch);
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
return (ch);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
void delay_ms(u16 nms)
{
u32 temp;
SysTick->LOAD = 9000*nms;
SysTick->VAL=0X00;//?????
SysTick->CTRL=0X01;//??,???????,???????
do
{
temp=SysTick->CTRL;//????????
}while((temp&0x01)&&(!(temp&(1<<16))));//??????
SysTick->CTRL=0x00; //?????
SysTick->VAL =0X00; //?????
}
int main()
{
u8 re;
u8 sendmsg[8] = {1,2,3,4,5,6,7,8};
RCC_Configuration();
USART_Configuration();
GPIO_Configuration();
CAN1_Init(CAN_SJW_1tq,CAN_BS2_3tq,CAN_BS1_12tq,9,CAN_Mode_Normal);//CAN_Mode_Normal CAN_Mode_LoopBack
while(1)
{
// GPIO_SetBits(GPIOC,GPIO_Pin_6);
// delay_ms(500);
// GPIO_ResetBits(GPIOC,GPIO_Pin_6);
// delay_ms(500);
printf ("ok");
re = CAN1_Send_Msg(sendmsg,8);
printf("re = %d ",re);
}
}
一周热门 更多>