#include "led.h"
#include "delay.h"
#include "key.h"
#include "sys.h"
#include "usart.h"
#include "timer.h"
#include "can.h"
int main(void)
{
u8 canbuf[8]={0,1,2,3,4,5,6,7};
u8 res;
delay_init(); //延时函数初始化
NVIC_Configuration(); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
uart_init(9600); //串口初始化为9600
LED_Init(); //初始化与LED连接的硬件接口
KEY_Init(); //按键初始化
CAN_Mode_Init(CAN_SJW_1tq,CAN_BS2_8tq,CAN_BS1_7tq,5,CAN_Mode_LoopBack);//CAN初始化环回模式,波特率450Kbps
while(1)
{
res=Can_Send_Msg(canbuf,sizeof(canbuf)); //发送8个字节
if(res)printf("\r\n Failed \r\n"); //提示发送失败
else printf("\r\n Ok \r\n"); //提示发送成功
}
此为库函数版本测试。
此时串口是一直不成功的,那我是不是可以大胆猜测原子板子的教程中can教程是存在错误的?
我的意思是是不是Can_Send_Msg(canbuf,sizeof(canbuf)); 有问题?为什么一直输出failed. 也就是 Can_Send_Msg没发送成功。
而在随后的寄存器版本测试中
#include <stm32f10x_lib.h>
#include "sys.h"
#include "usart.h"
#include "delay.h"
#include "can.h"
//ALIENTEK战舰STM32开发板实验25
//CAN 实验
//技术支持:www.openedv.com
//广州市星翼电子科技有限公司
int main(void)
{
u8 key;
u8 i=0,t=0;
u8 cnt=0;
u8 canbuf[8]="acdvs";
u8 res;
u8 mode=1;//CAN工作模式;0,普通模式;1,环回模式
Stm32_Clock_Init(9); //系统时钟设置
uart_init(72,9600); //串口初始化为9600
delay_init(72); //延时初始化
CAN_Mode_Init(1,8,7,5,mode);//CAN初始化,波特率450Kbps
while(1)
{
res=Can_Send_Msg(canbuf,8);//发送8个字节
if(res)
TransChar("Failed\r\n"); //提示发送失败,自己写的程序,不影响can的发送
else
TransChar("Send Ok\r\n"); //提示发送成功
memset(canbuf,0,8); //清空 数组 看是否收到了数据
delay_ms(300);
TransChar("Receive Data:\r\n");
key=Can_Receive_Msg(canbuf);
if(key)//接收到有数据
{
TransChar(canbuf);
TransChar("\r\n");
TransChar("Receive OK!\r\n");
}
else
TransChar("Receive Failed\r\n");
delay_ms(300);
}
此时测试是正确的。串口显示能收到Send Ok和Receive OK。
我是个初学者,并不是有意找茬,只是在学习中存在疑惑,请大家不要喷我。并没有恶意。
|