威纶通can通讯怎么编程

威纶通can通讯怎么编程,第1张

连接:启动/停止,模拟量输入/输出 (2/2),可自由编程的数字量输入/输出 (8/4) 旋转变压器和通用多编码器输入 通讯:CANopen,连接:启动/停止,模拟量输入/输出 (2/2),可自由编程的数字量输入/输出 (8/4) 旋转变压器和通用多编码器输入 通讯:CANopen,连接:启动/停止,模拟量输入/输出 (2/2),可自由编程的数字量输入/输出 (8/4) 旋转变压器和通用多编码器输入 通讯:CANopen,

ain() { unsigned int aRCC_Configuration()/*初始化 GPIOC时钟*/ // RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE)USART_Configuration()Delay(10)CAN_configure()// LedInit()while(1) { //CAN_ClearFlag(CAN_FLAG_BOF)GPIO_SetBits(GPIOB,GPIO_Pin_7)Delay(10)GPIO_ResetBits(GPIOB,GPIO_Pin_7)Delay(10)GPIO_SetBits(GPIOB,GPIO_Pin_7)Delay(10)GPIO_ResetBits(GPIOB,GPIO_Pin_7)Delay(10)can_send(0x57)a=can_rev()USART1_Putc(a)} } -------------------------------------------------------------------------------------------------- #include"stm32f10x_lib.h" void CAN_configure(void) { u8 wrongCAN_InitTypeDef CAN_InitStructureCAN_FilterInitTypeDef CAN_FilterInitStructureGPIO_InitTypeDef GPIO_InitStructure// RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO , ENABLE)// RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE)// RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN, ENABLE)/* Configure CAN pin: RX */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHzGPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPUGPIO_Init(GPIOA, &GPIO_InitStructure)/* Configure CAN pin: TX */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PPGPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHzGPIO_Init(GPIOA, &GPIO_InitStructure)/*can register int*/ CAN_DeInit()CAN_StructInit(&CAN_InitStructure)/*can cell init*/ CAN_InitStructure.CAN_TTCM=DISABLECAN_InitStructure.CAN_ABOM=DISABLECAN_InitStructure.CAN_AWUM=DISABLECAN_InitStructure.CAN_NART=DISABLECAN_InitStructure.CAN_RFLM=DISABLECAN_InitStructure.CAN_TXFP=DISABLECAN_InitStructure.CAN_Mode=CAN_Mode_LoopBackCAN_InitStructure.CAN_SJW=CAN_SJW_1tqCAN_InitStructure.CAN_BS1=CAN_BS1_8tqCAN_InitStructure.CAN_BS2=CAN_BS2_7tqCAN_InitStructure.CAN_Prescaler=5wrong=CAN_Init(&CAN_InitStructure)while(wrong==CANINITFAILED){} /* CAN filter init */ CAN_FilterInitStructure.CAN_FilterNumber=0CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMaskCAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bitCAN_FilterInitStructure.CAN_FilterIdHigh=0x0000//((u32)0x0020<<21)>>16//标识符 CAN_FilterInitStructure.CAN_FilterIdLow=0x0000//(((u32)0x0020<<21)|CAN_ID_STD|CAN_RTR_DATA)// CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000CAN_FilterInitStructure.CAN_FilterFIFOAssignment=0CAN_FilterInitStructure.CAN_FilterActivation=ENABLECAN_FilterInit(&CAN_FilterInitStructure)// CAN_ITConfig(CAN_IT_FMP0, ENABLE)} void can_send(unsigned char data) { CanTxMsg TxMessageu8 TransmitMailbox = 0TxMessage.StdId=0x0020//标识符 TxMessage.RTR=CAN_RTR_DATATxMessage.IDE=CAN_ID_STDTxMessage.DLC=2TxMessage.Data[0]=dataTxMessage.Data[1]=0xFETransmitMailbox=CAN_Transmit(&TxMessage)while(CAN_TransmitStatus(TransmitMailbox) != CANTXOK){}//等待发送 } unsigned char can_rev(void) { CanRxMsg RxMessageRxMessage.StdId=0x11//不需要设置 RxMessage.IDE=CAN_ID_STDRxMessage.DLC=2RxMessage.Data[0]=0x00RxMessage.Data[1]=0x00while (CAN_MessagePending(CAN_FIFO0)==0)//等到接收 CAN_Receive(CAN_FIFO0, &RxMessage)CAN_FIFORelease(CAN_FIFO0)return RxMessage.Data[0]} ------------------------------------------------------------------------------------------------- void RCC_Configuration(void) { //*** ErrorStatus HSEStartUpStatus//*** //* system clocks configuration -----------------系统时钟配置-------------------/// //* RCC system reset(for debug purpose) // RCC_DeInit()//将外设RCC寄存器重设为缺省值 //* Enable HSE * RCC_HSEConfig(RCC_HSE_ON)//开启外部高速晶振(HSE) //* Wait till HSE is ready / HSEStartUpStatus = RCC_WaitForHSEStartUp()//等待HSE起振 if(HSEStartUpStatus == SUCCESS) //若成功起振,(下面为系统总线时钟设置) { //* Enable Prefetch Buffer / FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable)//使能FLASH预取指缓存 //* Flash 2 wait state / FLASH_SetLatency(FLASH_Latency_2)//设置FLASH存储器延时时钟周期数(根据不同的系统时钟选取不同的值) //* HCLK = SYSCLK / RCC_HCLKConfig(RCC_SYSCLK_Div1)//设置AHB时钟=72 MHz //* PCLK2 = HCLK/2 / RCC_PCLK2Config(RCC_HCLK_Div2)//设置APB1时钟=36 MHz(APB1时钟最大值) //* PCLK1 = HCLK/2 / RCC_PCLK1Config(RCC_HCLK_Div2)//设置APB2时钟=72 MHz //* Configure ADCCLK such as ADCCLK = PCLK2/2 / // RCC_ADCCLKConfig(RCC_PCLK2_Div2)//RCC_PCLK2_Div2,4,6,8 //* PLLCLK = 8MHz * 9 = 72 MHz / RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9)//PLL必须在其激活前完成配置(设置PLL时钟源及倍频系数) //* Enable PLL / RCC_PLLCmd(ENABLE)//* Wait till PLL is ready / while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) { } //* Select PLL as system clock source / RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK)//* Wait till PLL is used as system clock source / while(RCC_GetSYSCLKSource() != 0x08) { } } //***** RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE)RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN, ENABLE)//***/ }


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/7784055.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-09
下一篇 2023-04-09

发表评论

登录后才能评论

评论列表(0条)

保存