自己编译了一个STM32F407VGT6 的板子,在mpconfigboard.h 中设定了以下参数:
代码: 全选
#define MICROPY_HW_ENABLE_CAN (1)
// CAN1 busses
#define MICROPY_HW_CAN1_TX (pin_D1) // PB9,PD1,PA12
#define MICROPY_HW_CAN1_RX (pin_D0) // PB8,PD0,PA11
// CAN2 busses
#define MICROPY_HW_CAN2_TX (pin_B13) // PB9,PD1,PA12,PB13
#define MICROPY_HW_CAN2_RX (pin_B12) // PB8,PD0,PA11,PB12
代码: 全选
from pyb import CAN
can = CAN(2, CAN.LOOPBACK)
can.setfilter(0, CAN.LIST16, 0, (123, 124, 125, 126)) # set a filter to receive messages with id=123, 124, 125 and 126
while True:
can.send('message!', 123) # send a message with id 123
l = can.recv(0) # receive message on FIFO 0
print(l)
代码: 全选
from machine import Pin
import uasyncio as asyncio
from pyb import CAN
can1 = CAN(2, CAN.NORMAL,prescaler=2, sjw=1, bs1=14, bs2=6 )
buf = bytearray(1)
buf[0] = 0
msg_id = 123
def printmsg(msg):
buf[0] += 1
print('msg: {} count: {}'.format(msg,buf[0]))
can1.send((msg), msg_id)
async def main():
#pin = Pin(Pin.cpu.C4, Pin.IN, Pin.PULL_UP)
#pb = Pushbutton(pin)
#pb.press_func(printmsg, ('push', ))
#pb.long_func(printmsg, ('long',))
while True:
await asyncio.sleep(1) #keep the uasyncio loop alive
printmsg('message')
try:
asyncio.run(main())
except (KeyboardInterrupt, Exception) as e:
#print('Exception {} {}\n'.format(type(e).__name__, e))
pass
#finally:
# ret = asyncio.new_event_loop() # Clear retained uasyncio state