使用ST的例程环回模式是可以的,小弟是新手,请问要改成正常模式怎么改?
我只是改了CAN_InitStructure.CAN_Mode = CAN_Mode_Normal; 发现发送不成功,发送后检查状态CAN_TransmitStatus 一直没有CANTXOK,(用示波器看TX引脚也没有波形)是不是还要改别的地方?
发送程序如下:
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_lib.h"
unsigned char TestTx=0; //发送成功标志
/* Private functions ---------------------------------------------------------*/
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
unsigned char CAN_MessageSend(void); //报文发送函数
void delay(void);
/*******************************************************************************
* Function Name : main
* Description : Main program
* Input : None
* Output : None
* Return : None
*******************************************************************************/
int main(void)
{
/* System Clocks Configuration */
RCC_Configuration();
/* NVIC Configuration */
NVIC_Configuration();
/* GPIO ports pins Configuration */
GPIO_Configuration();
TestTx = CAN_MessageSend();
while (1)
{
/* CAN transmit at 100Kb/s and receive by polling in normal mode */
if (TestTx == 1)
{
GPIO_SetBits(GPIOA,GPIO_Pin_11);
delay(); // LED1闪烁一次,发送一个字符
GPIO_ResetBits(GPIOA,GPIO_Pin_11);
delay();
}
}
}
void delay(void)
{
unsigned int a,b;
for(a=0;a<10000;a++)
{
for(b=0;b<100;b++);
}
}
/*******************************************************************************
* Function Name : RCC_Configuration
* Description : Configures the different system clocks.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void RCC_Configuration(void)
{
ErrorStatus HSEStartUpStatus;
/* RCC system reset(for debug purpose) */
RCC_DeInit();
/* Enable HSE */
RCC_HSEConfig(RCC_HSE_ON);
/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS)
{
/* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* PCLK2 = HCLK */
RCC_PCLK2Config(RCC_HCLK_Div1);
/* PCLK1 = HCLK */
RCC_PCLK1Config(RCC_HCLK_Div1);
/* Select HSE as system clock source,系统为8MHz */
RCC_SYSCLKConfig(RCC_SYSCLKSource_HSE);
/* Wait till HSE is used as system clock source */
while(RCC_GetSYSCLKSource() != 0x04)
{
}
}
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB , ENABLE);
/* CAN Periph clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN, ENABLE);
}
/*******************************************************************************
* Function Name : GPIO_Configuration
* Description : Configures the different GPIO ports.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure CAN pin: RX */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //只能工作在此速度下
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Configure CAN pin: TX */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//只能工作在此速度下
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinRemapConfig(GPIO_Remap2_CAN,ENABLE); //将PD1映射为CAN_TX,将PD0映射为CAN_RX
}
/*******************************************************************************
* Function Name : NVIC_Configuration
* Description : Configures the NVIC and Vector Table base address.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void NVIC_Configuration(void)
{
#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
}
unsigned char CAN_MessageSend(void)
{
CAN_InitTypeDef CAN_InitStructure;
CanTxMsg TxMessage;
u32 i = 0;
u8 TransmitMailbox = 0;
/* CAN register init */
CAN_DeInit();
CAN_StructInit(&CAN_InitStructure);
/* CAN cell init */
CAN_InitStructure.CAN_TTCM=DISABLE; //禁止时间触发通讯模式
CAN_InitStructure.CAN_ABOM=DISABLE; //禁止自动离线模式
CAN_InitStructure.CAN_AWUM=DISABLE; //禁止自动唤醒模式
CAN_InitStructure.CAN_NART=DISABLE; //禁止非自动重传模式
CAN_InitStructure.CAN_RFLM=DISABLE; //禁止接收FIFO锁定模式
CAN_InitStructure.CAN_TXFP=DISABLE; //禁止发送FIFO优先级
CAN_InitStructure.CAN_Mode=CAN_Mode_Normal;
CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1=CAN_BS1_8tq; //已经折算好了
CAN_InitStructure.CAN_BS2=CAN_BS2_7tq;
CAN_InitStructure.CAN_Prescaler=1;
CAN_Init(&CAN_InitStructure);
/* transmit */
TxMessage.StdId=0x11;
TxMessage.RTR=CAN_RTR_DATA;//数据帧
TxMessage.IDE=CAN_ID_STD;//标准帧格式
TxMessage.DLC=2; //帧长度,单位:字节
TxMessage.Data[0]=0xCA;//待传数据
TxMessage.Data[1]=0xFE;
/*数据发送*/
TransmitMailbox=CAN_Transmit(&TxMessage);
i = 0;
while((CAN_TransmitStatus(TransmitMailbox) != CANTXOK) && (i != 0xFF)) //等待CAN总线传输数据成功,老是卡在这边最后成发送不成功
{
i++;
}
if(i==0xff)//发送超时,但发送不成功
{
return 0;
}
return 1; /* Test Passed */
}
我用的是IAR的编程环境以及EmbededDebug v2.0调试软件,就是问题不知道在哪儿,求大神帮忙啊
|