打印
[51单片机]

使用Keil C的一些经验总结

[复制链接]
1815|7
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
skw168|  楼主 | 2019-5-13 14:18 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 skw168 于 2019-6-1 10:16 编辑

总结下Keil C使用心得和遇到过的坑

1.Keil C内部延时函数直接调用库函数:
#include<intrins.h>       // 声明了void _nop_(void);
_nop_();                  // 产生一条NOP指令
NOP指令为单周期指令
//
2.keil C中定义bit变量是有上限的,
查询资料是128个,实际使用时确实遇到过这个问题,多一个变量则编译器则会报错
解决办法:将bit 定义为 char
//
3.Keil C的data,xdata区别
data:内部低128字节RAM,可高速寻址
Xdata:外部扩展RAM,寻址速度较慢
PS:优先使用data,因为速度快,代码空间少( keil中定义变量时使用 idata unsigned char)
//
4.Keil C常量数组的定义(汇编理解的表格)
共2种方式:
const unsigned char tab[] = {  };
数组为只读,存放在RAM中;
code unsigned char tab[] = {  };
数组为只读,存放在ROM中;
//



使用特权

评论回复

相关帖子

沙发
meiwen141| | 2019-6-6 00:24 | 只看该作者

使用特权

评论回复
板凳
rankey| | 2019-6-6 09:07 | 只看该作者
好!!!
   鄙人也补充一点点,嘻嘻

  1、第一部分 keil  启动代码Startup Code
   Startup code is executed immediately upon reset of the target system. The Keil startup code performs (optionally) the following operations in order:
启动代码是单片机复位后最先执行的代码,keil 中的启动代码通常按顺序 完成以下操作
•        Clears internal data memory (清空内部数据内存)
•        Clears external data memory  (清空外部数据内存)
•        Clears paged external data memory  (清空分页的外部数据内存)
•        Initializes the small model reentrant stack and pointer (初始化小模型可重入栈和指针)
•        Initializes the large model reentrant stack and pointer  
•        Initializes the compact model reentrant stack and pointer
•        Initializes the 8051 hardware stack pointer   初始化8051硬件栈指针
•        Transfers control to code that initializes global variables or to the main C function if there are no initialized global variables  
     将控制权交给全局变量初始代码段,或者如果没有全局代码初始段时交给c语言的main 函数
Following is a list of the startup files that are available. (keil 中可用的启动代码文件)
Startup File        Description
STARTUP.A51        Startup code for classic 8051 devices. (这个就是8051系列的启动代码文件了)
START_AD.A51        Startup code for Analog Devices MicroConverter devices.
START390.A51        Startup code for Dallas DS80C390/400/41x/5240/5250 contiguous mode devices.
START4XX.A51        Startup code for Dallas DS89C420/430/440/450 devices.
STARTLPC.A51        Startup code for NXP LPC700 devices series.
START900.A51        Startup code for NXP LPC90x - LPC93x devices series.
START950.A51        Startup code for NXP LPC95x - LPC99x devices series.
START_MX.A51        Startup code for NXP 80C51MX devices.
START751.A51        Startup code for NXP 80C75x devices.
START_XC.A51        Startup code for Infineon XC800 devices.
STARTUP32.A51        Startup code for ST uPSD32xx devices.
STARTUP34.A51        Startup code for ST uPSD33xx devices.
STARTUP34.A51        Startup code for ST uPSD34xx devices.
Copy the appropriate startup file from the \KEIL\C51\LIB\ folder into your project folder and make any changes to the copy.
Each startup file provides assembly constants you may change to control the operations performed at startup.
启动代码文件位于 \KEIL\C51\LIB\  , 你可以通过修改启动代码文件里的如下常量来匹配自己的程序
2、keil c51 拓展关键字
  To facilitate many of the features of the 8051, the Cx51 Compiler adds a number of new keywords to the scope of the C language:
为了帮助8051实现某些功能,CX51编译器增加了在C语言范围内的一系列关键词。
•        _at_
•        alien
•        bdata
•        bit
•        code
•        compact
•        data
•        far
•        idata
•        interrupt
•        large
•        pdata
•        _priority_
•        reentrant
•        sbit
•        sfr
•        sfr16
•        small
•        _task_
•        using
•        xdata

使用特权

评论回复
地板
千岁寒| | 2019-6-6 09:15 | 只看该作者
rankey 发表于 2019-6-6 09:07
好!!!
   鄙人也补充一点点,嘻嘻

这些都是51的隐藏基础

使用特权

评论回复
5
叶春勇| | 2019-6-6 11:08 | 只看该作者
千岁寒 发表于 2019-6-6 09:15
这些都是51的隐藏基础

你用过sdcc吗

使用特权

评论回复
6
千岁寒| | 2019-6-6 14:03 | 只看该作者

这个确实没用用过。

使用特权

评论回复
7
airwill| | 2019-6-7 19:11 | 只看该作者
本帖最后由 airwill 于 2019-6-7 19:12 编辑

用过 SDCC, 虽然这是个免费的 C51 编译器, 但编译的代码效率于 Keil C51 相去甚远哦.  
不知道现在的新版本, 情况怎么样


使用特权

评论回复
8
kk223| | 2019-7-11 14:37 | 只看该作者
1:针对STC的Startup.A51,初始化IDATA区,如果要使用STC MCU的芯片序列号,初始化长度改为0xF0,在执行完单片机初始化后,就要将序列号复制出来。
2:新版KeilC,在LX51 Misc页中的Misc Control 中填入"REMOVEUNUSED",可以链接时去除未用的函数。很方便的。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

36

主题

133

帖子

6

粉丝