stm32与stc89c52进行CAN通信程序
时间: 2023-09-20 10:06:15 浏览: 133
CAN通信是一种高速、可靠的通信协议,适用于在噪声干扰和恶劣环境下进行通信。下面是stm32和stc89c52进行CAN通信的程序示例。
stm32代码:
```c
#include "stm32f10x.h"
#include "stm32f10x_can.h"
#define CAN1_RX_PIN GPIO_Pin_8
#define CAN1_TX_PIN GPIO_Pin_9
void CAN1_Configuration(void)
{
CAN_InitTypeDef CAN_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
// 使能CAN1和GPIO时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
// 配置CAN1 RX引脚
GPIO_InitStructure.GPIO_Pin = CAN1_RX_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// 配置CAN1 TX引脚
GPIO_InitStructure.GPIO_Pin = CAN1_TX_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// CAN1初始化
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;
CAN_InitStructure.CAN_TXFP = DISABLE;
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 = 6;
CAN_Init(CAN1, &CAN_InitStructure);
// CAN1接收中断使能
CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);
}
void CAN1_SendData(uint8_t data[], uint8_t len)
{
CanTxMsg TxMessage;
TxMessage.StdId = 0x123;
TxMessage.ExtId = 0x00;
TxMessage.IDE = CAN_ID_STD;
TxMessage.RTR = CAN_RTR_DATA;
TxMessage.DLC = len;
memcpy(TxMessage.Data, data, len);
CAN_Transmit(CAN1, &TxMessage);
}
void CAN1_RX0_IRQHandler(void)
{
CanRxMsg RxMessage;
CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);
// 处理接收到的数据
}
int main(void)
{
uint8_t data[8] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
CAN1_Configuration();
while (1)
{
CAN1_SendData(data, 8);
delay_ms(1000);
}
}
```
stc89c52代码:
```c
#include <reg52.h>
#define CAN_INT_PIN P3_2
#define CAN_RX_PIN P1_1
#define CAN_TX_PIN P1_2
void CAN_Configuration(void)
{
// 配置CAN RX引脚
P1M1 &= ~(1 << 1);
P1M0 |= (1 << 1);
// 配置CAN TX引脚
P1M1 &= ~(1 << 2);
P1M0 |= (1 << 2);
// 配置CAN中断引脚
P3M1 &= ~(1 << 2);
P3M0 |= (1 << 2);
// 配置CAN控制寄存器
ACC = 0xa0;
PCON |= 0x00;
TMOD &= ~0x51;
TMOD |= 0x20;
TH1 = 0xfb;
TL1 = 0xfb;
ET1 = 0;
TR1 = 1;
// 配置CAN工作模式
CANMOD = 0x00;
CANGIE = 0x00;
CANGIT = 0xff;
CANIER = 0x01;
CANBT1 = 0x00;
CANBT2 = 0x1c;
CANBT3 = 0x37;
CANCON = 0x00;
}
void CAN_SendData(uint8_t data[], uint8_t len)
{
uint8_t i;
// 等待发送缓冲器空闲
while (!(CANCON & 0x04));
// 设置发送数据长度
CANTFI = len;
// 设置发送数据
for (i = 0; i < len; i++)
{
CANTFD = data[i];
}
// 发送数据
CANCON |= 0x01;
}
void CAN_RXInterrupt(void) interrupt 0x1b
{
uint8_t i, len;
// 判断是否有新数据
if (CANRXS & 0x01)
{
// 获取数据长度
len = CANRML;
// 获取数据
for (i = 0; i < len; i++)
{
data[i] = CANRFD;
}
// 处理接收到的数据
// 清除中断标志
CANRXS &= ~0x01;
}
}
int main(void)
{
uint8_t data[8] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
CAN_Configuration();
while (1)
{
CAN_SendData(data, 8);
delay_ms(1000);
}
}
```
阅读全文