之前在网上找了一个STM32F103C8T6关于can通信的例子,直接烧录,can不能使用, 使用示波器在VP230前后端查看没有波形, 先介绍板子硬件资源: HSE时钟:8MHz; MCU : STM32F103C8T6 CAN:一路;
0 F+ {) r7 b- \8 k! e, `+ O
在软件的配置中找不到任何原因,使用USB-CAN分析仪不能识别波特率,怀疑是不是因为波特率设置不对,是不是硬件设计出现错误,查找手册 《stm32f103c8t6》 " r) b! f& g: w; q. O
, c" Z9 _6 g8 y5 r2 R1 H
can的发送接收引脚是在PA11 PA12,(注意:can和其他的复用) 我找的那个程序,我把原理找到看了一下,can的发送接收是在 PB8 PB9,怀疑是我的板子硬件出错了, 我的板子硬件原理图上是PA11 PA12,是按照手册来设计的, 既然硬件没有问题,应该是软件问题,看看GPIO的配置: - /*CAN GPIO */3 b0 X. ^1 |! W& @) d
- void CAN_GPIO_Config(void)1 c% t' w" e% o; G
- { o, M! H! j* z8 ?5 j- Q h
- GPIO_InitTypeDef GPIO_InitStructure; 9 Y4 X* ]. ^7 n. `. W
- /* GPIOB */
4 d; q& d+ @3 W! ? - RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOB, ENABLE); \ U) j* O. a, |+ w: ?( Z) |- i
! B7 g# D1 k$ B/ |/ W% g8 {# W2 r1 K- /* CAN1 */" a h* R8 G2 L u& O1 m4 A& y7 y! e& ], O
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
# `5 v% D& n" g9 \ - ' U( l9 f, `* h( {1 d8 ]
- /* Configure CAN pin: RX */ // PB8
/ j1 Z1 i; ]. l% \ - GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;/ w4 _$ N5 y2 c: E3 @7 B1 d
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; // 8 b8 H9 _8 r$ R4 l$ ^7 g f( i) P
- GPIO_Init(GPIOB, &GPIO_InitStructure);
, n0 h! Y7 {8 a" Q; ` -
, F4 k4 L" ^. E i! |) _: F - /* Configure CAN pin: TX */ // PB9
, W+ D: V0 c3 I/ P7 l - GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;7 v6 I" A4 f, c+ {/ Q
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; + j# r ^% s* A) T% [% b% r
- GPIO_Init(GPIOB, &GPIO_InitStructure);- k6 F3 [! A6 A; |+ ?8 P
- ; H6 H6 u2 V4 k1 H1 K4 @
- //#define GPIO_Remap_CAN GPIO_Remap1_CAN1
8 j/ @9 E9 ?. Y7 L7 O7 a - GPIO_PinRemapConfig(GPIO_Remap1_CAN1, ENABLE); : h6 R0 w6 G7 I) u( E8 _
-
& Y; ?1 R/ m! |$ n9 Z9 o - }
复制代码( k: {9 V6 D6 p+ R% H
明白了,原来是把can的发送接收端口映射到了PB8、9上面了, 修改后的代码: - void CAN_GPIO_Config(void)
% W( y$ }! j+ D( X, U - {. U( t1 o) Z7 o# M. v, v
- GPIO_InitTypeDef GPIO_InitStructure; / X; S7 v; d! p$ W% p
- /* GPIOA */ ; H# }1 x# ?. J2 F
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA, ENABLE);
1 Q4 V* X; {' E1 @% z/ b - 2 s+ h; }3 ~. {+ ?+ O" ~
- /* CAN1 */$ ~, ]2 O: w. _' @% d0 o$ e
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE); , L( W t2 ^# _+ o2 V
- 2 k" [" G+ g# I2 _. y
- /* Configure CAN pin: RX */ // PA11# l3 b7 c+ G3 }2 j+ W( K: @
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;5 V( ^4 ]- T# x* o5 j
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //
- _0 R3 z2 c( j2 E1 o n; A - GPIO_Init(GPIOA, &GPIO_InitStructure);
1 b) v* r7 f0 Z7 c- E* z - + w' G# d" z/ n6 L( K6 F$ L
- /* Configure CAN pin: TX */ // PA12
3 W; b7 @0 k+ l# s/ Z& r - GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
. x9 ]4 {6 [5 p( a# M- F4 s% S5 Y - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
; X5 ~: l# S- u! l7 J* G - GPIO_Init(GPIOA, &GPIO_InitStructure);" e: x4 j6 N Y% c& V {
-
8 E4 b% G# n& y9 ~) a# E8 A" J" z - //#define GPIO_Remap_CAN GPIO_Remap1_CAN1 , b; E! j1 m, w; J+ y
- //GPIO_PinRemapConfig(GPIO_Remap1_CAN1, ENABLE); //取消端口映射 craigtao 2014-4-49 a) c% H4 k. j
- 9 B; h% b) D' F1 |* P
- }
复制代码 * e$ W9 W" Q% ~' O) m3 | y7 @% j
转载自:仆人 - D1 M" J# F: T% z1 z
|