5

【笔记】Mac上搭建嵌入式开发环境

 1 year ago
source link: https://feiju12138.github.io/2022/10/19/Mac%E4%B8%8A%E6%90%AD%E5%BB%BA%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91%E7%8E%AF%E5%A2%83/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

在Mac上编译针对于51单片机的C语言源代码,并将编译后的代码下载到单片机,本篇文章针对于Intel和Apple Silicon版本的Mac均有效

安装编译器

brew install sdcc

安装成功后,当电脑与单片机相连时,可以通过以下方法验证
末位数字需要根据自己查询到的为准,前缀相同

ls /dev/tty.*

/dev/tty.wchusbserial110
/dev/tty.usbserial-110
ls /dev/cu.*

/dev/cu.wchusbserial110
/dev/cu.usbserial-110

安装下载器

  • 下载器grigorig/stcgal项目是用Python写的模块,所以需要有Python环境,使用Pip安装
pip3 install stcgal
  • 代码为C语言,C语言代码编辑器此处省略
  • Mac中编写的C51代码使用sdcc编译器编译,与Windows中的Keil编译器中的部分引用有区别,如下表
位置 Windows Keil Mac sdcc
头文件 reg51.hreg52.h 8051.h8052.h
IO端口 P2^0 P2_0
IO口定义 sbit LED = P2^0 #define LED P2_0
中断函数 void INT0_ISR() interrupt 0 void INT0_ISR() __interrupt 0
  • 通过编译得到.ihx文件,.ihx可以进一步转换为.hex文件和.bin文件

直接完成编译

sdcc <library>.c

先编译成链接库,再完成编译

  • 通常用于多模块的大项目,先将所有模块编译成链接库文件,再将所有链接库编译完成
  • 当部分模块被修改时,只需要先将修改的模块编译成新的链接库,再将其他模块的链接库与修改后模块的新链接库组合完成编译

<source>:C语言源代码文件
<library>:链接库文件

sdcc -c <source>.c
sdcc <library>.rel

ihx文件转换为hex文件(可选)

<file>:文件名

packihx <file>.ihx > <file>.hex

ihx文件转换为bin文件(可选)

sdobjcopy -I ihex -O binary <file>.ihx <file>.bin

编译后生成的文件

<file>:编译后的文件名

- <file>.asm
- <file>.ihx
- <file>.lk
- <file>.lst
- <file>.map
- <file>.mem
- <file>.rel
- <file>.rst
- <file>.sym
  • 确保已经安装完驱动,电脑与单片机已经连接好并测试没有问题的情况下

-P auto:指定单片机类型

auto:自动判定

-p /dev/cu.wchusbserial110:指定连接的端口

/dev/cu.wchusbserial110:末尾数字根据自己的端口决定

<file>.ihx:编译后的文件

stcgal -P auto -p /dev/cu.wchusbserial110 <file>.ihx

# 以下是输出内容
Waiting for MCU, please cycle power: # 当出现这句话时将单片机复位

# 以下是完成复位后的输出内容
Waiting for MCU, please cycle power: done
Protocol detected: stc12
Target model:
Name: STC12C5A60S2
Magic: D17E
Code flash: 60.0 KB
EEPROM flash: 2.0 KB
Target frequency: 11.952 MHz
Target BSL version: 7.1I
Target options:
reset_pin_enabled=True
low_voltage_reset=False
oscillator_stable_delay=32768
por_reset_delay=long
clock_gain=high
clock_source=external
watchdog_por_enabled=False
watchdog_stop_idle=True
watchdog_prescale=256
eeprom_erase_enabled=False
bsl_pindetect_enabled=False
Loading flash: 106 bytes (Intel HEX)
Switching to 19200 baud: testing setting done
Erasing 2 blocks: done
Writing flash: 640 Bytes [00:00, 1904.60 Bytes/s]
Finishing write: done
Setting options: done
Target UID: D17EC5F0102FD9
Disconnected!

博客园——俺踏月色而来
SDCC官网
简书——SyunSiu
CSDN——下一站2381


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK