美文网首页
MAC 开发C51

MAC 开发C51

作者: 云醉倚清风 | 来源:发表于2022-12-06 14:57 被阅读0次

一:工具准备

1:ch340串口驱动

下载地址

①:下载后安装,dmg和pkg均可

安装完成后一直有个安装的弹窗,不用理会


image.png

②:插上开发板,查看设备位置

如果有很多设备,多次插拔就能确认,注意需要重新打开系统配置,我试过不是即时更新的


image.png

③:命令查询usbID地址

ls /dev/tty*

会出来很多结果,我们只需要找到我们需要的
/dev/tty            /dev/ttyse
/dev/tty.serial0        /dev/ttysf
/dev/tty.usbserial-14720    /dev/ttyt0
/dev/ttyp0          /dev/ttyt1
/dev/ttyp1          /dev/ttyt2
/dev/ttyp2          /dev/ttyt3

/dev/tty.usbserial-14720 就是我们要找到驱动成功得位置,记录

2:stcgal烧录工具

安装方式1:

pip3 install stcgal 

安装方式2:github 地址

//***下载代码后cd到代码目录下***//
 //构建
./setup.py build
  //安装
./setup.py install  
  //确认
stcgal -V    
  //结果                                                  
stcgal 1.6

3:sdcc编译工具

 //安装
brew install sdcc
 //确认
sdcc -v
//结果
SDCC : mcs51/z80/z180/r2k/r2ka/r3ka/sm83/tlcs90/ez80_z80/z80n/ds390/pic16/pic14/TININative/ds400/hc08/s08/stm8/pdk13/pdk14/pdk15/mos6502 4.2.0 #13081 (Mac OS X x86_64)
published under GNU General Public License (GPL)

Homebrew安装可参

4:VSCode 开发IDE

根据个人喜好选择合适的C编辑器

二:点亮Led测试

新建c文件,led.c

#include <8052.h>     

void Delay10ms(unsigned int);   
void main()
{ 
    while(1)
    {
        P2  = 0x00;    
        Delay10ms(50); 
        P2  = 0xff;    
        Delay10ms(50);
    }
}
void Delay10ms(unsigned int c)   
{
    unsigned char a,b;
    for(;c>0;c--)
        for(b=38;b>0;b--)
            for(a=130;a>0;a--);
}

1:编译

//执行
sdcc led.c
//生成
led.ihx

2:烧录

//执行
 stcgal -P stc89 -p /dev/tty.usbserial-14720 led.ihx
//提示
Waiting for MCU, please cycle power: 
//断点重启
Waiting for MCU, please cycle power: done
Waiting for MCU, please cycle power: done
Target model:
  Name: STC89C52RC/LE52R
  Magic: F002
  Code flash: 8.0 KB
  EEPROM flash: 6.0 KB
Target frequency: 11.952 MHz
Target BSL version: 4.3C
Target options:
  cpu_6t_enabled=False
  bsl_pindetect_enabled=False
  eeprom_erase_enabled=False
  clock_gain=high
  ale_enabled=True
  xram_enabled=True
  watchdog_por_enabled=False
Loading flash: 146 bytes (Intel HEX)
Switching to 19200 baud: checking setting testing done
Erasing 2 blocks: done
Writing flash: 640 Bytes [00:00, 1749.15 Bytes/s]                                                      
Setting options: done
Disconnected!

三:注意事项及遇到的问题

mac sdcc与 win keil c的区别

sdcc keil c
头文件 8051.h/8052.h reg51.h/reg52/h
端口 P2_0 P2^0
端口定义 #define LED1 P2_0 sbit LED1=P2^0
中断声明 void time1() __interrupt 3 __using 2 void time1() interrupt 3 using 2

问题1:fatal error: intrins.h: No such file or directory

原因:intrins.h 是 keil c51提供的方法
解决:新建intrins.h文件

#ifndef _INTRINS_H_
#define _INTRINS_H_

/* warning: __push __pop 使用堆栈临时保存 sfr 数据,必需成对使用!  
     __push(x);
         ...  // 保护代码块
         __pop(x);  //缺少无该语句编译不会出错,但运行错误!
*/
        #define __push(x)      __asm push _##x  __endasm  /* void _push_ (unsigned char _sfr); */
        #define __pop(x)       __asm pop  _##x  __endasm  /* void _pop_  (unsigned char _sfr); */

        #define _push_        __push        /*兼容 keil c51*/
        #define _pop_        __pop         /*兼容 keil c51*/
        
/*   安全使用保护宏: 
     pushSfr(x);
         ...                 // 受保护代码块
         popSfr(x);         // 缺少无该语句编译出错,确保生成正确代码。
*/
    #define pushSfr(x)  do{\
                                  __push(x)

        #define  popSfr(x)     __pop(x);\
                              }while(0)

#endif //_INTRINS_H_

参考文档

相关文章

网友评论

      本文标题:MAC 开发C51

      本文链接:https://www.haomeiwen.com/subject/yqgwfdtx.html