4387|5

11

帖子

0

TA的资源

一粒金砂(中级)

楼主
 

f207vet6 我的can通信怎么不行 [复制链接]

/**
  ******************************************************************************
  * @file    TIM/TIM1_Synchro/main.c
  * @author  MCD Application Team
  * @version V1.0.0
  * @date    18-April-2011
  * @brief   Main program body
  ******************************************************************************
  * @attention
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  *

© COPYRIGHT 2011 STMicroelectronics


  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "stm32f2xx.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
//  #define KEY_PRESSED     0x00
//  #define KEY_NOT_PRESSED 0x01
vu32 ret;
#define  Uchar  unsigned char

#define BOX_DIS_IDENT         0x9555AA01
#define BOX_CTRL_IDENT        0x9555AAB0

typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
volatile TestStatus TestRx;
ErrorStatus HSEStartUpStatus;
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
TIM_OCInitTypeDef        TIM_OCInitStructure;
TIM_BDTRInitTypeDef      TIM_BDTRInitStructure;

GPIO_InitTypeDef         GPIO_InitStructure;

CAN_InitTypeDef         CAN_InitStructure;
CAN_FilterInitTypeDef   CAN_FilterInitStructure;
CanTxMsg                TxMessage;
CanRxMsg                RxMessage;
/* Private function prototypes -----------------------------------------------*/
void TIM1_Config(void);
void TIM1_PWM_Gpio(void);

void TIM2_Config(void);
void TIM2_Gpio(void);

void NVIC_Configuration(void);

void CAN_Config(void);

void Init_RxMes(CanRxMsg *RxMessage);

void can_send_order(Uchar order, Uchar data1, Uchar data2,Uchar data3,Uchar data4);        //发送命令参数




/* Private functions ---------------------------------------------------------*/

/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured,
       this is done through SystemInit() function which is called from startup
       file (startup_stm32f2xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f2xx.c file
     */   

    /* System Clocks Configuration */
    SystemInit();
   /* GPIOA, GPIOB and GPIOC clocks enable */
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOD, ENABLE);

    /* TIM1 clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);   
   /*TIM2 clock enablae*/
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 , ENABLE);
  /* CAN Periph clock enable */
  // RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN, ENABLE);

  /* TIM1 Configuration */
// TIM_Config();
  NVIC_Configuration();
  TIM1_PWM_Gpio();
  TIM1_Config();

  TIM2_Gpio();
  TIM2_Config();
  TIM_CtrlPWMOutputs(TIM1, ENABLE);

  u32 i=0;
  TIM_ITConfig(TIM2, TIM_IT_Update , ENABLE);//开启计数中断
  /* TIM2 enable counter */
  TIM_Cmd(TIM2, ENABLE);//开启时钟


  while (1)
  {
  can_send_order(0x1F,0xB0,7,12,0);
  for(i=0;i<=0x1ffff;i++);

  }
}

/**
  * @brief  Configures TIM1, TIM3 and TIM4 Pins.
  * @param  None
  * @retval None
  */

void NVIC_Configuration(void)
{
  NVIC_InitTypeDef  NVIC_InitStructure;
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
  /* Enable the TIM2 global Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

  NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn;  //先指定CAN1_RX0_IRQn进入中断口
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0;  //阶级0
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0;  //阶层0
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}
void TIM1_Config()
{
   /* TIM1 Peripheral Configuration ----------------------------------------*/
  /* Time Base configuration */
  TIM_TimeBaseStructure.TIM_Prescaler = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseStructure.TIM_Period = 255;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_RepetitionCounter = 4;

  TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
  /* Channel 1 Configuration in PWM mode */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
  TIM_OCInitStructure.TIM_Pulse = 127;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
  TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
  TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
  TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;

  TIM_OC1Init(TIM1, &TIM_OCInitStructure);
  //启用CCR1寄存器的影子寄存器(直到产生更新事件才更改设置)
  TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);

  //通道2 TIM_OC1PreloadConfig
   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
   TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
   TIM_OCInitStructure.TIM_Pulse = 127;
   TIM_OC2Init(TIM1, &TIM_OCInitStructure);
   //启用CCR1寄存器的影子寄存器(直到产生更新事件才更改设置)
   TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);

//通道2 TIM_OC1PreloadConfig
   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
   TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
   TIM_OCInitStructure.TIM_Pulse = 127;
   TIM_OC3Init(TIM1, &TIM_OCInitStructure);
   //启用CCR1寄存器的影子寄存器(直到产生更新事件才更改设置)
   TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);

  /* Automatic Output enable, Break, dead time and lock configuration*/
  TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
  TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
  TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_1;
  TIM_BDTRInitStructure.TIM_DeadTime = 5;
  TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable;
  TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
  TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Disable;

  TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);

  /* Master Mode selection */
  TIM_SelectOutputTrigger(TIM1, TIM_TRGOSource_Update);

  /* Select the Master Slave Mode */
  TIM_SelectMasterSlaveMode(TIM1, TIM_MasterSlaveMode_Enable);

  /* TIM1 counter enable */
  TIM_Cmd(TIM1, ENABLE);

}
void TIM1_PWM_Gpio(void)
{  
  /* GPIOA Configuration: TIM1 Channel1 as alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  /* Connect TIM pins to AF1 */
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource8 , GPIO_AF_TIM1);  
   /* Connect TIM pins to AF2 */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 ;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
  /* Connect TIM pins to AF1 */
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource13 , GPIO_AF_TIM1);

  //TIM1的channel2通道设置
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource9 , GPIO_AF_TIM1);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14 ;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
  /* Connect TIM pins to AF1 */
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource14 , GPIO_AF_TIM1);
  // GPIO_PinAFConfig(GPIOA, GPIO_PinSource14 , GPIO_AF_TIM1);

  //TIM1的channel3通道设置
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource10 , GPIO_AF_TIM1);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15 ;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
  /* Connect TIM pins to AF1 */
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource15 , GPIO_AF_TIM1);
  // GPIO_PinAFConfig(GPIOA, GPIO_PinSource14 , GPIO_AF_TIM1);
}

void TIM2_Config(void)
{
  TIM_TimeBaseStructure.TIM_Period = 6000;
  TIM_TimeBaseStructure.TIM_Prescaler = 9999;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

  TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
// TIM_ITConfig(TIM2, TIM_IT_Update , ENABLE);//开启计数中断
  /* TIM2 enable counter */
  //TIM_Cmd(TIM2, ENABLE);//开启时钟

}

void TIM2_Gpio(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
   /* 硬件连接 示意图
     PA6----->D1
     PD12---->D2
     PA5----->D3
     PA4----->D4
  */
  //  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOD, ENABLE);
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 ;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//输出模式
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推免式   还可以配置为开漏模式 可以作为双向io 比如i2c
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//扫描频率100m
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;// GPIO_PuPd_NOPULL = 0x00,GPIO_PuPd_UP     = 0x01, GPIO_PuPd_DOWN   = 0x02 上拉下拉配置
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOD, &GPIO_InitStructure);
}

void CAN_Config(void)
{
  CAN_FilterInitTypeDef  CAN_FilterInitStructure;
// CanTxMsg TxMessage;
// CanRxMsg RxMessage;
  GPIO_InitTypeDef  GPIO_InitStructure;
  u32 i = 0,j;
// u8 TransmitMailbox;

  /* CAN GPIOs configuration **************************************************/

  /* Connect CAN pins to AF9 */  //引脚的复用配置(特殊功能配置)
  GPIO_PinAFConfig(GPIOD, GPIO_PinSource0, GPIO_AF_CAN1);
  GPIO_PinAFConfig(GPIOD, GPIO_PinSource1, GPIO_AF_CAN1);

  /* Configure CAN RX pins */
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 ;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  //GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  //GPIO_InitStructure.GPIO_OType = GPIO_PuPd_UP;
  //GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
  //GPIO_Init(GPIOD, &GPIO_InitStructure);

   /* Configure CAN  TX pins */
   //GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_1;
  //GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  //GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  //GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
// GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
//  GPIO_Init(GPIOD, &GPIO_InitStructure);
   /* Configure CAN Remap   不需要重影射 */

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
  GPIO_Init(GPIOD, &GPIO_InitStructure);

    /* CAN configuration ********************************************************/  
  /* Enable CAN clock */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);

  /* CAN register init */
  CAN_DeInit(CAN1);  //can1寄存器初始化
  CAN_StructInit(&CAN_InitStructure);
  /* CAN cell init */
  CAN_InitStructure.CAN_TTCM = DISABLE;  //  禁止 时间触发通信模式
  CAN_InitStructure.CAN_ABOM = DISABLE;   //  软件对CAN_MCR寄存器的INRQ位进行置1随后清0后,一旦硬件检测
                                         //到128次11位连续的隐性位,就退出离线状态
  CAN_InitStructure.CAN_AWUM = DISABLE;  //  //睡眠模式通过清除CAN_MCR寄存器的SLEEP位,由软件唤醒
  CAN_InitStructure.CAN_NART = DISABLE;  //  按照CAN标准,DISABLE;CAN报文只被发送1次,不管发送的结果如何(成功、出错或仲裁丢失)
  CAN_InitStructure.CAN_RFLM = DISABLE;   //  在接收溢出时FIFO未被锁定,当接收FIFO的报文未被读出,下一个收到的报文会覆盖原有
                                                            //的报文
  CAN_InitStructure.CAN_TXFP = DISABLE;   //发送优先级由报文的标示符决定
  CAN_InitStructure.CAN_Mode = CAN_Mode_LoopBack;    ////CAN硬件工作在正常模式   
  CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;   // 重新同步跳跃宽度1个时间单位

  /* CAN Baudrate = 1 MBps (CAN clocked at 30 MHz) */
  CAN_InitStructure.CAN_BS1 = CAN_BS1_6tq;    //  时间段1为6个时间单位  主要是控制波特率的////////////////////////////////
  CAN_InitStructure.CAN_BS2 = CAN_BS2_8tq;    //  时间段2为8个时间单位///////////////////////////////////////////////////////////
  CAN_InitStructure.CAN_Prescaler = 4;    //  //(pclk1/((1+8+6)*4)) = 30Mhz/15/4 = 0.5Mhz设定了一个时间单位的长度2
/* 波特率计算方法 */
  /* CANbps= Fpclk/((BRP+1)*((Tseg1+1)+(Tseg2+1)+1)  此处计算为  CANbps=36000000/(45*(4+3+1))=100kHz */                                                                                                                     //此处Tseg1+1 = CAN_BS1_8tp
  /* 配置大方向: Tseg1>=Tseg2  Tseg2>=tq; Tseg2>=2TSJW */
    CAN_Init(CAN1, &CAN_InitStructure);    //

    if (CAN_Init(CAN1,&CAN_InitStructure) == CANINITFAILED)                
  {

    while(1);
    /* 初始化时先设置CAN_MCR的初始化位 */                                                                                                                 
    /* 然后查看硬件是否真的设置了CAN_MSR的初始化位来确认是否进入了初始化模式  */                                                                                                                                          
  }       
  CAN_FilterInitStructure.CAN_FilterNumber = 0; //滤波器数值  //指定了待初始化的过滤器0

CAN_FilterInitStructure.CAN_FilterNumber = 14;  //滤波器数值

  CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;  //指定了过滤器将被初始化到的模式为标识符屏蔽位模式
  CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit; //给出了过滤器位宽1个32位过滤器

  //CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0000;  //用来设定过滤器标识符(32位位宽时为其高段位,16位位宽时为第一个)
// CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000;  //用来设定过滤器标识符(32位位宽时为其低段位,16位位宽时为第二个
  CAN_FilterInitStructure.CAN_FilterIdHigh = ((BOX_CTRL_IDENT<<3)&0xFFFF0000)>>16;
  CAN_FilterInitStructure.CAN_FilterIdLow = (((BOX_CTRL_IDENT)<<3)&0x0000FFFF) | CAN_ID_EXT;


  CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000;  //用来设定过滤器屏蔽标识符或者过滤器标识符(32位位宽时为其高段位,16位位宽时为第一个
  CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000; //用来设定过滤器屏蔽标识符或者过滤器标识符(32位位宽时为其低段位,16位位宽时为第二个

  CAN_FilterInitStructure.CAN_FilterFIFOAssignment = 0;  //设定了指向过滤器的FIFO0
  CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;  //使能过滤器
  CAN_FilterInit(&CAN_FilterInitStructure);
CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);  //使能指定的CAN1的邮箱0挂起中断使能中断
/* transmit */
  TxMessage.StdId=0x00;
  TxMessage.ExtId = 0x9555AAB0;//0x9555AA01;
  TxMessage.RTR=CAN_RTR_DATA;  //确定传输消息的帧类型
  TxMessage.IDE=CAN_ID_EXT;   //设定消息标示符的类型
  TxMessage.DLC=8;

  /* can 接收设置*/

  RxMessage.StdId = 0x00;//0x00;
  RxMessage.ExtId = 0x9555AAB0;
  RxMessage.IDE=CAN_ID_EXT;  //使用扩展的ID
  RxMessage.DLC=8;
  RxMessage.FMI=0;  //过滤器匹配编号
  for (i = 0;i < 8;i++)
  {
    RxMessage.Data = 0x00;
  }
// CAN_Receive(CAN_FIFO0, &RxMessage);
     /* Enable FIFO 0 message pending Interrupt */
  //CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);  //使能指定的CAN1的邮箱0挂起中断使能中断

}
/*
void Init_RxMes(CanRxMsg *RxMessage)
{
  uint8_t i = 0;

  RxMessage->StdId = 0x00;
  RxMessage->ExtId = 0x00;
  RxMessage->IDE = CAN_ID_STD;
  RxMessage->DLC = 0;
  RxMessage->FMI = 0;
  for (i = 0;i < 8;i++)
  {
    RxMessage->Data = 0x00;
  }
}

*/

void TIM2_IRQHandler(void)
{
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
  {
    TIM_ClearITPendingBit(TIM2, TIM_IT_Update);

    /* Pin PC.06 toggling with frequency = 73.24 Hz */
   GPIO_WriteBit(GPIOA, GPIO_Pin_4, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_4)));
   GPIO_WriteBit(GPIOA, GPIO_Pin_5, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_5)));
   GPIO_WriteBit(GPIOA, GPIO_Pin_6, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_6)));
   GPIO_WriteBit(GPIOD, GPIO_Pin_12, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOD, GPIO_Pin_12)));
  }
}

void can_send_order(Uchar order, Uchar data1, Uchar data2, Uchar data3,Uchar data4)//加了一个字节????等待?超时?
{
  int num;
  // CanTxMsg TxMessage;

// TxMessage.Data[0]=0xDE;
// TxMessage.Data[1]=0xCA;
  TxMessage.Data[0] = order;   
  TxMessage.Data[1] = data1;   
  TxMessage.Data[2] = data2;   
  TxMessage.Data[3] = data3;   
  TxMessage.Data[4] = data4;   
  //TxMessage.Data[5] = CAN_DATA5;     
  //TxMessage.Data[6] = CAN_DATA6;   
  //TxMessage.Data[7] = CAN_DATA7;   

  num = CAN_Transmit(CAN1,&TxMessage);  /* 返回这个信息请求发送的邮箱号0,1,2或没有邮箱申请发送no_box */       
  while (CANTXOK != CAN_TransmitStatus(CAN1,num))  
        {

        }      
}


void CAN1_RX0_IRQHandler(void)
{
  //uint8_t CanFlag;
  //uint16_t CAN_ID;
  uint8_t CAN_DATA0,CAN_DATA1,CAN_DATA2,CAN_DATA3,CAN_DATA4,CAN_DATA5,CAN_DATA6,CAN_DATA7;
  CanRxMsg RxMessage;
  CAN_Receive(CAN1,CAN_FIFO0, &RxMessage);  /* 此函数包含释放提出报文了的,在非必要时,不需要自己释放 */
// CAN_ID=RxMessage.StdId;
  CAN_DATA0=RxMessage.Data[0];
  CAN_DATA1=RxMessage.Data[1];
  CAN_DATA2=RxMessage.Data[2];
  CAN_DATA3=RxMessage.Data[3];
  CAN_DATA4=RxMessage.Data[4];
  CAN_DATA5=RxMessage.Data[5];
  CAN_DATA6=RxMessage.Data[6];
  CAN_DATA7=RxMessage.Data[7];
  CAN_ClearITPendingBit(CAN1,CAN_IT_FMP0);  /* 清除挂起中断 */
// CanFlag = ENABLE;
}

#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  while (1)
  {}
}

#endif

/**
  * @}
  */

/**
  * @}
  */

/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
此帖出自stm32/stm8论坛

最新回复

什么话  详情 回复 发表于 2016-11-4 18:40
点赞 关注
 

回复
举报

8

帖子

0

TA的资源

一粒金砂(中级)

沙发
 
我觉得是你的波特率没有设置正确
此帖出自stm32/stm8论坛

点评

有没有例程 给我看看 QQ632115913:titter:  详情 回复 发表于 2012-9-10 13:51
波特率 能有什么问题500kHz 不就是这设置吗?不过我查了下我的寄存器,波特率的确和我设置的不吻合,且回环模式还被禁止了  详情 回复 发表于 2012-9-10 13:47
 
 

回复

11

帖子

0

TA的资源

一粒金砂(中级)

板凳
 

回复 沙发 Z.Henry 的帖子

波特率 能有什么问题500kHz  不就是这设置吗?不过我查了下我的寄存器,波特率的确和我设置的不吻合,且回环模式还被禁止了
此帖出自stm32/stm8论坛
 
 

回复

11

帖子

0

TA的资源

一粒金砂(中级)

4
 

回复 沙发 Z.Henry 的帖子

有没有例程 给我看看  QQ632115913
此帖出自stm32/stm8论坛
 
 
 

回复

11

帖子

0

TA的资源

一粒金砂(中级)

5
 
明白了  弄好了 主程序少句话
此帖出自stm32/stm8论坛

点评

什么话  详情 回复 发表于 2016-11-4 18:40
 
 
 

回复

1221

帖子

1

TA的资源

一粒金砂(高级)

6
 
weigongyuan 发表于 2012-9-10 14:04
明白了  弄好了 主程序少句话

什么话
此帖出自stm32/stm8论坛
 
个人签名我爱着爱着我的人,朋友和家人。
 
 

回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/10 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表