You are on page 1of 214

Transition Title

Codewarrior
使用指南

TM
内容

• 利用向导创建一个新工程项目
• 在新项目中加入或删除文件
• 编译
• 调试
• 启动程序
• PRM文件设置
文件设置
• 如何对IO及寄存器进行操作
如何对 及寄存器进行操作
• 如何写中断程序
• 如何使用汇编和C语言混合编程
如何使用汇编和 语言混合编程
• 嵌入式编程注意事项
• 如何使用Processor
如何使用 Expert

Slide 2
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
• 利用向导创建一个新工程项目
• 在新项目中加入或删除文件
• 编译
• 调试
•启动程序
• PRM文件设置
• 如何对IO及寄存器进行操作
• 如何写中断程序
• 如何使用汇编和C语言混合编程
•嵌入式编程注意事项
• 如何使用Processor Expert

TM

Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
创建新工程 1

1、创建新工程

2、选择HCS12
选择HCS12 New Project Wizard
3、输入工程名

Slide 4
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
创建新工程 2

4、选择MCU
选择MCU类型
MCU类型 5、选择语言类型

Slide 5
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
创建新工程 3

6、选择是否采用Processor
选择是否采用Processor Expert 7、选择是否使用PC
选择是否使用PC-
PC-lint

如果用Processor
如果用Processor Expert,
Expert,选择 Yes

Slide 6
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
创建新工程 4

8、选择是否用浮点运算 9、选择存储器类型

Slide 7
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
创建新工程 5

10、
10、选择调试器类型

Slide 8
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
编译 调试

是否产生调试信息
源程序文件夹
Start up文件夹
up文件夹
Prm文件夹
Prm文件夹
Map文件夹
Map文件夹
库文件夹

列出相关文件
代码长度 变量长度 是否被包含在Target
是否被包含在Target中
Target中

Slide 9
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
参考文献

www.codewarrior.com
Slide 10
Codewarrior manuals/pdf/IED_Users_Guide.pdf
TM
manuals/pdf/IED_Users_Guide.pdf
Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
• 利用向导创建一个新工程项目
• 在新项目中加入或删除文件
• 编译
• 调试
•启动程序
• PRM文件设置
• 如何对IO及寄存器进行操作
• 如何写中断程序
• 如何使用汇编和C语言混合编程
•嵌入式编程注意事项
• 如何使用Processor Expert

TM

Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
在新项目中加入或删除文件

如果编译时,
如果编译时,出现没有调试信息警告,
出现没有调试信息警告,请点击这里

新建一文件,
新建一文件,并保存到原文件夹中

点击右键,
点击右键,加入或从此工程中删除文件

Slide 12
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
• 利用向导创建一个新工程项目
• 在新项目中加入或删除文件
• 编译
• 调试
•启动程序
• PRM文件设置
• 如何对IO及寄存器进行操作
• 如何写中断程序
• 如何使用汇编和C语言混合编程
•嵌入式编程注意事项
• 如何使用Processor Expert

TM

Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
无限循环

(1) While (1);


(2) For (;;);
(3) Loop:
goto Loop;
对嵌入式系统来说:
-循环是必须要用到的.
-上述3种方式, 第2种更好一些
-为什么?

因为它不会产生警告信息
“always true warning”

Slide 14
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
编译器优化
•CodeWarrior编译器提供了几种从
编译器提供了几种从C源代码产生实际汇编代码
编译器提供了几种从 源代码产生实际汇编代码
的优化方法,
的优化方法,这些代码被下载到微控制器中

•全局优化设置面板设定编译器怎样优化目标代码。
全局优化设置面板设定编译器怎样优化目标代码。所有优化程
序重新组织目标代码,
序重新组织目标代码,不影响其逻辑执行顺序。
不影响其逻辑执行顺序。

Slide 15
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
编译器优化 – 强度减弱

•“Strength Reduction”(
(强度减弱)
强度减弱)是一种优
化,力争用开销小的操作代替开销大的操作,
力争用开销小的操作代替开销大的操作,所
要付出的代价是执行时间或代码大小。
要付出的代价是执行时间或代码大小。

•在循环内,
在循环内,用加法指令代替乘法指令。
用加法指令代替乘法指令。

•下列例子将演示编译器,
下列例子将演示编译器,根据具体应用,
根据具体应用,来决
定哪一种操作用最少代价达到同样结果。
定哪一种操作用最少代价达到同样结果。

Slide 16
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
这个变量乘以3
这个变量乘以

Slide 17
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
用乘法来实现

Slide 18
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
变量乘以4
变量乘以

Slide 19
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
用 2 次左移位来实现.
次左移位来实现 用
作为指针来指向我们想要
H:X作为指针来指向我们想要
乘的值

Slide 20
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
这句话等同于
VarA = VarA * 4;

Slide 21
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
编译器优化 –死代码消除

• 死代码消除就是编译器在优化应用程序时,
死代码消除就是编译器在优化应用程序时,对没被使
用的语句将不产生可执行代码

•去除逻辑上从未执行的语句或没有被其他语句提到的
语句

Slide 22
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Slide 23
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
在循环结束后,
在循环结束后,这个变量没有
被修改 异或指令 = ^

Slide 24
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
赋值语句被跳过
在循环结束后,
在循环结束后,变量将被修改

Slide 25
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
编译器优化–消除死赋值
编译器优化 消除死赋值

•消除死赋值就是指编译器移去变量在再次赋值之前
没有被使用的赋值

•在下面编译器优化的例子,
在下面编译器优化的例子,我们将演示通过改变编
译器的优化设置,
译器的优化设置,达到改变CodeWarrior产生代码
达到改变 产生代码
的方式。
的方式。

Slide 26
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
这个循环定义了J
这个循环定义了
的值,
的值,但是在循
环结束后,
环结束后,j的值
的值
等于1
等于

Slide 27
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Slide 28
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Codewarrior HC08 Compiler Options Settings

Slide 29
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
所有的代码将被忽略

Slide 30
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
在循环中用到的变量定
义成全局变量

Slide 31
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
这段代码并没有忽略掉,
因为它是全局变量,有可
能会被硬件(中断)用到

Slide 32
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
编译器优化 –循环解开

•在循环内复制代码,
目的是展开分支操作

•下面的例子演示一下
循环解开是如何工作 通过改变编译器设置
,我们可选择不同的
的: 优化项,
优化项,在生成代码
时将会有差异

Slide 33
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
示例(
示例(没有循环解开)
没有循环解开):

编译器产生与
次循环相关
4次循环相关
的代码。
的代码。

Slide 34
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
示例(
示例(有循环解开)
有循环解开):

相同的代码,
相同的代码,但
改变设置为
“loop
,编
unrolling”,
译器产生如下代
相当于把循环展开 码。
for ( i = 0; i < 4; j++ )
Array [ i ] = i;
--------------------
Array [ 0 ] = 0;
Array [ 1 ] = 1;
Array [ 2 ] = 2;
Array [ 3 ] = 3;

Slide 35
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
更多的优化选项:
更多的优化选项

全局寄存器分
把经常用的工作变量存在寄存器中,
把经常用的工作变量存在寄存器中,而不是RAM中
而不是 中

分支优化 合并和重组中间代码翻译的部分以减少跳转指令

用更快的功能相同的指令替代复杂运算指令,
用更快的功能相同的指令替代复杂运算指令,得到的结果也完全
算术运算
相同

表达式简化 用简单相等的表达式替换复杂的算术运算表达式

子表达式消除 用单一表达式代替冗余表达式

复制传播
用变量的单词出现替代多次出现

Slide 36
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
更多的优化选项:
更多的优化选项 :

窥孔优化 把局部的优化策略应用到代码的一个小段中

减小变量的存在时间来获得最佳分配。
减小变量的存在时间来获得最佳分配。较短的变量生存时间较
生存范围溢出
小寄存器的溢出

循环内不变的
将静态计算移到循环之外
代码转移

循环转换 将目标代码重组减小设置和完成测试

基于寄存器分 在一段特定的程序中,
在一段特定的程序中,如果没有语句同时使用这些变量的话,
如果没有语句同时使用这些变量的话,
配的生存期 使用相同的寄存器来存储不同的变量.
使用相同的寄存器来存储不同的变量

指令安排 重新排列指令顺序,
重新排列指令顺序,减少寄存器和处理器资源之间的冲突

Slide 37
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
条件编译
• 编译指示符:#if、#else、#elif、#endif
• 这些指示符均用于条件编译:
• #if <constant-expression>
• #else OR #elif <constant-expression>
• #endif
• 只有当条件表达式的值不为零时,才编译跟有#if指示符
的行。否则以后的行都被跳过直到遇到匹配的#else或
endif。
• #error定义一个用于显示的编译错误。

Slide 38
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
应定义S12DP256,
否则产生一编译错误

对于嵌入式系统:
—同一源代码支持多平台;
—源代码的适应性(在编译时设置)

Slide 39
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
• 利用向导创建一个新工程项目
• 在新项目中加入或删除文件
• 编译
• 调试
•启动程序
• PRM文件设置
• 如何对IO及寄存器进行操作
• 如何写中断程序
• 如何使用汇编和C语言混合编程
•嵌入式编程注意事项
• 如何使用Processor Expert

TM

Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
模拟调试器界面

工具条
打开文件
单步 单步
进入 跳出 停止 复位

保存当前 汇编单步
设置 运行 单步跳过

Slide 41
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
模拟调试器:
模拟调试器 特点

• 功能强大的图形界面 (拖
拖&拉操作
拉操作,
拉操作 弹出式菜单...).
弹出式菜单

• 模块化和可扩展系统(基于组件概念
模块化和可扩展系统 基于组件概念).
基于组件概念

• 不同的组件可以加入:
不同的组件可以加入
• 来自Metrowerks(Codewarrior)公司的组建
来自 公司的组建
• 来自半导体公司的组件
• 用户组件

• 对所有的目标和目标接口,
对所有的目标和目标接口,界面(GUI)完全相同
界面 完全相同

Slide 42
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
模拟调试器:
模拟调试器 界面

对不同的

simulator和

debugger,界
面完全相同
。对所有的
CPU,,界面
完全相同。
完全相同。
仅仅是子窗
口中的内容
不同。
不同。

Slide 43
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
模拟调试器:
模拟调试器 界面

断点信息

汇编指令地址

汇编指令机器码
源代码
汇编指令

覆盖信息

Profiling 信息
(代码所花时间百分比)
代码所花时间百分比)

高级语言展开/折叠命令
高级语言展开 折叠命令
Slide 44
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
模拟调试器:
模拟调试器 界面

CPU 寄存器

存储区地址

存储区中的内容

存储区中的内容(字符
存储区中的内容 字符)
字符

Slide 45
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
模拟调试器:
模拟调试器 界面

执行的函数

目标类型

目标列表

Slide 46
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
• 利用向导创建一个新工程项目
• 在新项目中加入或删除文件
• 编译
• 调试
• 启动程序
• PRM文件设置
• 如何对IO及寄存器进行操作
• 如何写中断程序
• 如何使用汇编和C语言混合编程
• 嵌入式编程注意事项
• 如何使用Processor Expert

TM

Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
启动程序

一般用汇编语言写的,
一般用汇编语言写的,并且会连接到你编译过的
Startup Code一般用汇编语言写的
可执行的模块中.
可执行的模块中 它为执行C语言写的程序做准备
它为执行 语言写的程序做准备:
语言写的程序做准备:

• 关闭中断
• 把初始化过的数据从ROM中拷贝到RAM中
• 将未初始化数据区清零
• 给堆栈分配空间并初始化
• 创建并初始化堆
• 开放中断
• 调用main()
Slide 48
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Slide 49
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
复位后第一个执行的是启动
程序,复位向量存贮了
所在位置
_startup()所在位置

Slide 50
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
• 利用向导创建一个新工程项目
• 在新项目中加入或删除文件
• 编译
• 调试
• 启动程序
• PRM文件设置
• 如何对IO及寄存器进行操作
• 如何写中断程序
• 如何使用汇编和C语言混合编程
• 嵌入式编程注意事项
• 如何使用Processor Expert

TM

Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
代码放在哪里?
代码放在哪里

• 在*.PRM文件中
文件中,
文件中,SECTIONS命令块用于定义存贮
命令块用于定义存贮
器的物理区域。
器的物理区域。
• 在SECTIONS命令块中
命令块中,
命令块中,每个单独的物理存贮器段
用一个名字、
用一个名字、一个属性和一个地址范围描述
命令块用于将代码和数据段定位到存
• PLACEMENT命令块用于将代码和数据段定位到存
贮器段
“..\CodeWarrior Manuals\pdf\Manual_SmartLinker.pdf”
C:\Program Files\Metrowerks\CW08 V3.1\Technical Notes\BuildTools\tn04.pdf

www.hiware.com/dl_hc08.htm

Slide 52
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
#pragma

#pragma 是一个编译指示器,
告诉编译器如何设置

Slide 53
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
存储区分配

Slide 54
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
代码放在哪里?
代码放在哪里

• 变量放在Default_RAM 的位置,除非另外规定一个
PRAGMA声明

• 频繁使用的变量应放在直接页RAM ($40 -$FF)

• 在直接页中访问操作数采用直接寻址模式(8位寻址模
式)比扩展寻址模式(16位寻址模式)少一个时钟周期

Slide 55
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
变量定义在段中
,它的位置由连
接器决定

数据段 (VarA) 想存放在


一个特定区域内
(0x0080-0x00A9)

定义了一个新的存
储段

Slide 56
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
VarA 在0x80

VarB 在 DEFAULT 段中

Slide 57
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
这段代码想存储在特定
的段中

定义段,
定义段,并把函数体放在该段中

Slide 58
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
funcition1 被放在
FunctionsROM代码段中 ,
其地址为 0xEF00 到 EFFF

Slide 59
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
常数段想存放在特定的位置

Slide 60
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
存放在数组里的常数并没有放在
存放在数组里的常数并没有放在
中,而是直接调用了它的内容
ROM中
。如果想要更改FLASH中变量数组
如果想要更改 中变量数组
,就会发生错误

Slide 61
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
常数替换优化必须被关掉

Slide 62
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
用的是地址,
用的是地址,而非它的值

数组存在我们期望的位置

Slide 63
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
• 利用向导创建一个新工程项目
• 在新项目中加入或删除文件
• 编译
• 调试
• 启动程序
• PRM文件设置
• 如何对IO及寄存器进行操作
• 如何写中断程序
• 如何使用汇编和C语言混合编程
• 嵌入式编程注意事项
• 如何使用Processor Expert

TM

Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
如何对IO进行操作
如何对 进行操作?
进行操作
• 利用Codewarrior头文件中已经定义好的IO寄存器

• 或者:
 #define PortA ( * ( volatile unsigned char * ) 0x0000 )

• 或者
 volatile unsigned char PortA @0x0000;

Slide 65
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
访问固定内存位置

• 嵌入式系统通常的特点是需要编程者访问一个指定的存贮器位
置。
• 练习:在某个项目中需要将绝对地址0xFFA处整型变量的值设
为0xAA55(编译器为纯粹的ANSI编译器)。完成这个任务的
代码是:

Int * ptr;
ptr = (int *)0x2FFA;
*ptr = 0xAA55;

Slide 66
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
访问CPU寄存器
访问 寄存器

• CPU中的寄存器没有对应的固定地址
• 可以用汇编指令对它们进行访问
• C语言无法直接访问这些寄存器
• C编译器允许在C代码中使用汇编指令
1)_asm AssemblyInstuction;
2)asm (AssemblyInstruction);
3)asm {
----
----
}

Slide 67
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
修改CPU中
修改 中CCR寄

存器的I位
存器的 位

Slide 68
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
利用汇编指令来修改 I 位

Slide 69
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
位域

• 位结构
 效率随编译器的不同而改变;
 不同的编译器不能移植。
• 位类型
 无法移植
 提高代码效率
• 移位和屏蔽
 可移植,合理提高效率
 经常被优化成位操作

Slide 70
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
联合体是一个变量,
联合体是一个变量,不同的时
间表示不同的类型和大小的对
象,编译器会根据要求决定变
量的大小。
量的大小。

Slide 71
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
只有PS位被修改
只有 位被修改
联合体提供一种可以对单一存
贮区不同类型数据操作的方法
,程序中没有嵌入任何依赖于
机器的信息

单指令写操作

Slide 72
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
• 利用向导创建一个新工程项目
• 在新项目中加入或删除文件
• 编译
• 调试
• 启动程序
• PRM文件设置
• 如何对IO及寄存器进行操作
• 如何写中断程序
• 如何使用汇编和C语言混合编程
• 嵌入式编程注意事项
• 如何使用Processor Expert

TM

Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
方法1
方法 :利用关键字 ‘interrupt’:
CodeWarrior 编译器提供了一种非ANSI标准的方式来实现
中断:
• 在 C 源文件中,中断服务程序前面加 “interrupt 中断向量号”

中断向量表分配 – MCU 数据手册

Vector Vector
int time_counter; Vector Address
Number Address Size
interrupt 7 INT_Timer(void) 0 0xFFFE - 0xFFFF 2
{ 1 0xFFFC – 0xFFFD 2
time_counter ++;
2 0xFFFA – 0xFFFB 2
}
... ... ...
n 0xFFFF – ( n*2 ) 2

Slide 74
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
中断向量表分配 – MCU 数据手册

Slide 75
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
方法2:
方法 :在PRM文件中声明
文件中声明

在PRM文件中声明
文件中声明

•在C代码中写中断服务程序 #pragma TRAP_PROC


void INCcount(void) {
int card1;

tcount++;
}

在 PRM 文件中,
文件中,将中断服务 VECTOR ADDRESS 0xFFF0 INCcount
程序名和中断向量地址对应

Slide 76
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
方法3:
方法 :在向量表中初始化

在C语言文件中的常量表中初始化
语言文件中的常量表中初始化:
语言文件中的常量表中初始化

•在C代码中定义 #pragma CONST_SEG VectorTable

void (*const procTable[])(void) = {


intFunc1, intFunc2, intFunc3
}
#pragma CONST_SEG DEFAULT

在PRM文件中分配
文件中分配 PLACEMENT
VectorTable INTO READ_ONLY 0xFFFA TO 0xFFFF;

END

ENTRIES
连接向量表 table
END

Slide 77
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
利用interrupt关键字声明
利用 关键字声明
中断服务程序

Slide 78
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
用 RTI 而不是 RTS

函数指针

中断向量存储了中
断服务程序的起始
地址
Slide 79
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
• 利用向导创建一个新工程项目
• 在新项目中加入或删除文件
• 编译
• 调试
• 启动程序
• PRM文件设置
• 如何对IO及寄存器进行操作
• 如何写中断程序
• 如何使用汇编和C语言混合编程
• 嵌入式编程注意事项
• 如何使用Processor Expert

TM

Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
如何使用汇编和C语言混合编程

• 汇编和C语言中使用相同的符号(变量及函数名)

• 注意参数传递规则

• 注意函数返回值原则

• 注意目标文件格式 (Elf-Dwarf or HIWARE).

• 接口模块(C/Assembly) 或 (Assembly/C)

Slide 81
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
在C中访问汇编变量
中访问汇编变量

变量声明 (ASM) 变量声明(


变量声明(C语言
语言)
语言)
XDEF ASMData, ASMConst
DataSec: SECTION extern int ASMData;
ASMData: DS.W 1
ConstSec: SECTION extern const int ASMConst;
ASMConst: DC.W $44A6

代码中使用变量
C代码中使用变量
unsigned int CData;
CData = ASMData + ASMConst + 3;

Slide 82
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
在汇编中访问C变量
在汇编中访问 变量

变量声明(
变量声明(C语言
语言)
语言) 变量声明 (ASM)
unsigned int CData;
XREF CData;
const unsigned int CConst;
XREF CConst;

在汇编代码中使用变量
LDD CConst

STD CData

Slide 83
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
在C中调用汇编函数
中调用汇编函数

•函数(
函数(汇编代码)
汇编代码) 函数原型 (C代码
代码)
代码
AddVar: void AddVar(unsigned int first,
ADDD CData unsigned int second);
ADDD 2, SP
STD ASMData
RTS

在C中调用汇编函数
中调用汇编函数
AddVar(Cdata, 10);

Slide 84
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
在汇编中调用C函数
在汇编中调用 函数

•函数(C代码中) 函数声明 (汇编


汇编)
汇编

void AddVar(unsigned int first,


unsigned int second){ XREF AddVar
ASMData = Cdata + first + second;
}

在汇编中调用C函数
在汇编中调用 函数

LDD CData ; Load value of Cdata in D


PSHD ; PUSH D on the stack
LDAB #10 ; Load 10 in D
CLRA
JSR AddVar

Slide 85
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
• 利用向导创建一个新工程项目
• 在新项目中加入或删除文件
• 编译
• 调试
• 启动程序
• PRM文件设置
• 如何对IO及寄存器进行操作
• 如何写中断程序
• 如何使用汇编和C语言混合编程
• 嵌入式编程注意事项
• 如何使用Processor Expert

TM

Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
嵌入式编程 vs. PC编程
编程

嵌入式编程环境的主要特点:
嵌入式编程环境的主要特点:

• 有限的RAM;
有限的 ;
• 有限的ROM;
有限的 ;
• 有限的栈空间;
有限的栈空间;
• 面向硬件编程;
面向硬件编程;
• 严格的定时(
严格的定时(ISR,
,任务,
任务,…);
);
• 很多不同种类的指针
(far/near/rom/uni/paged/…);
);
• 特殊关键字/标识符
特殊关键字 标识符(@,
标识符(@,interrupt,
(@, ,tiny,
,…)

Slide 87
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
数据类型
• 通过为变量选择最合适的数据类型可以最大程度地得到
最短的代码和执行时间

位微控制器内部的数据的长度是8位
位微控制器内部的数据的长度是 位(一字节),
• 8位微控制器内部的数据的长度是 一字节),然
),然
而C首选的数据类型是
首选的数据类型是‘int‘
首选的数据类型是

位机处理8位数据类型比
位机处理 位数据类型比16位数据类型效率更高
• 8位机处理 位数据类型比 位数据类型效率更高

和大数据类型只有当所描述的数据的大小需要时
• “int“和大数据类型只有当所描述的数据的大小需要时
才使用

• 当优先考虑代码效率时,
当优先考虑代码效率时,双精度和浮点操作效率低,
双精度和浮点操作效率低,应
当避免

Slide 88
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
的数据类型
HC08的数据类型
标准没有精确定义数据类型的大小,
标准没有精确定义数据类型的大小,但是 CodeWarrior 定义了...
ANSI标准没有精确定义数据类型的大小 定义了

所有的数据类型
默认为有符号(
默认为有符号
除了
0 255 char)signed
例如:
例如
‘int’ = ‘signed
int’

Slide 89
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
默认CodeWarrior
默认 的数据类型
所有的基本数据类型可以被改变…
所有的基本数据类型可以被改变
但是建议尽量使用默认值

Slide 90
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
数据类型选择
对8位MCU, 在选择数据类型时,有3条原则:
> 尽量使用最小的数据类型
>尽可能使用无符号数
> 在表达式内声明数据类型,以便将数据类型减小到最小(强制类型
转换)
 使用typedefs得到固定的大小
使用 得到固定的大小
- 根据编译器和系统而改变
- 移植到不同的机器代码不变
- 当值需要固定位时使用

Slide 91
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
定义完整的数据类
在main函数里声明了
函数里声明了3
函数里声明了 型
种不同数据类型的变量

Slide 92
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
仅最后面的位写入值;
仅最后面的位写入值 并且使用了一
个寄存器
每个变量的其余位被清零
clr ,X

变量在堆栈里有一个地址

Slide 93
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
在main函数里声明了
函数里声明了3
函数里声明了
种不同数据类型的变量

Slide 94
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
编译器仅仅为使用到的变量
保留存储空间,
保留存储空间,这个例子中
只有VarA被使用到了
只有 被使用到了

Slide 95
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
所有声明的全局变量均
被使用。
被使用。

Slide 96
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
在这种情况下,
在这种情况下,编译器
为所有变量保留了内存

Slide 97
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
根据变量大小的不同,
根据变量大小的不同,
每个加操作用不同的方
法完成

变量声明的内存区,
变量声明的内存区,每个变量有不同的大小(
每个变量有不同的大小(
Slide 98
、2和
1、 和4字节
字节)
字节)
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
修饰符

以下关键字用于声明变量,以指定特定需要或内存中变量
存贮的相关条件。

Static
volatile
const

这三个关键字,一起让我们不仅可写出好的代码,而且可
写出紧凑的代码

Slide 99
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
静态变量

使用静态变量有二个主要功能:
• 第一个最常用的用法是定义一个变量,在函数连续调用期
间,变量不会消失。
• 第二个使用静态的用法是限制变量的范围。在模块级定义
时,能被整个模块中所有函数访问,不能被其它模块中的
函数访问

这些变量不
会存贮在堆
栈中

Slide 100
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
静态变量示例

在第一次进入
MyFunction 函数之
FILE1.c 前, myVar = 0 FILE2.c

#include <FILE2.h> void MyFunction (void){


//includes functions contained in file //Definition of MyFunction in FILE2.C
//FILE2.c
static char myVar = 0;
void main (void){ //local variable declared static

MyFunction(); //included in FILE2.c myVar = myVar + 1;

MyFunction(); //included in FILE2.c }

}
静态变量会保持它的
在第二次进入 值,尽管 myVar 是
MyFunction之前: 局部变量
myVar = 1

Slide 101
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
静态函数

• 静态函数只能被其所在模块中的其它函数调用

• 使用静态函数是结构化编程的好习惯

• 静态函数能产生小而快的代码

• 编译器在编译时确切地知道什么函数能调用一个给
定的静态函数。因此,函数的相关内存区域能被调
整,以致使用调用的一个短版本或跳转指令

Slide 102
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Volatile变量
Volatile变量

变量其值在正常程序流程以外可能改变的变量
变量
Volatile变量

在嵌入式系统中,这种情况通过两种主要途径发生:
• 通过一个中断服务程序
• 硬件动作的结果

例如,通过一个串口接收到一个字符,结果串口状态寄存
器更新,这完全在程序流程之外发生。很多程序员知道编
译器不会试图优化一个volatile寄存器,而宁可每次重载它

在嵌入式设备中,将所
有外设寄存器声明为
volatile是一个好习惯

Slide 103
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
的变量从不会被编译器优化
volatile的变量从不会被编译器优化

 访问定义为volatile的变量从不会被编译器优化
访问定义为 的变量从不会被编译器优化

volatile unsigned char PORTA @0x00;


volatile unsigned char SCS1 @0x16; 不用Volatile
不用 关键字
unsigned char value;
MOV #5,PORTA
LDA #10
void main(void){ STA @value

PORTA = 0x05; /* PORTA = 00000101 用Volatile 关键字


*/
MOV #5,PORTA
PORTA = 0x05; /* PORTA = 00000101
MOV #5,PORTA
*/ LDA SCS1
SCS1; LDX #10
value = 10; STX @value
}

Slide 104
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Volatile 变量示例

/* MC68HC908GP20/32 Official Peripheral Register Names */

volatile unsigned char PORTA @0x0000; /* Ports and data direction */


volatile unsigned char PORTB @0x0001;
volatile unsigned char PORTC @0x0002;
volatile unsigned char PORTD @0x0003;
volatile unsigned char PORTE @0x0008;

volatile unsigned char DDRA @0x0004; /* Data Direction Registers */


volatile unsigned char DDRB @0x0005;
volatile unsigned char DDRC @0x0006;
volatile unsigned char DDRD @0x0007;
volatile unsigned char DDRE @0x000C;

volatile unsigned char PTAPUE @0x000D; /* Port pull-up enables */


volatile unsigned char PTCPUE @0x000E;
volatile unsigned char PTDPUE @0x000F;

Slide 105
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Const 变量

声明可用于任何变量,
声明可用于任何变量,它告诉编译器将其存贮在
• Const声明可用于任何变量
ROM区中区中

• 编译器保留了那个位置程序存贮器地址。
编译器保留了那个位置程序存贮器地址。由于位于ROM
由于位于
中,其值不能改变

变量必须初始化
• Cons变量必须初始化
比如:
比如
const double PI = 3.14159265;

Slide 106
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
修饰符
Const修饰符
Const 变量与明显的常数相对,很多文章要求用const变量代替明显的常数。
例如:
用const unsigned char channels = 8;代替#define CHANNELS 8 。
本方法的基本原理是在调试器内部,你能检查一个const变量,然而一个明
显的常数不可访问。不幸的是,在很多8位机上你将为这一好处付出极大的
代价。这两个主要代价是:
• 一些编译器在RAM中创建一个真实的变量来支持cost变量,这是一个极大
的惩罚。
• 一些编译器如CodeWarrior,知道变量为const,将把变量存贮在ROM中。
无论怎样,变量仍作为变量处理和访问,典型地用某些变址寻址(16位)的
方式。与直接寻址(8位)方式相比,这种方法通常很慢。

Slide 107
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Const volatile 变量
一个变量既能是常量,
一个变量既能是常量,又能是可变量吗?
又能是可变量吗?
答案是“能
答案是 能”。

这个修饰符应该用于能出乎意料地改变的任何存贮器位置,
这个修饰符应该用于能出乎意料地改变的任何存贮器位置,因此
需要volatile限定语
需要 限定语,
限定语,由于const该变量是只读的
由于 该变量是只读的。
该变量是只读的。
最明显的例子是硬件状态寄存器,
最明显的例子是硬件状态寄存器,象SCI状态寄存器 状态寄存器SCS1。
状态寄存器 。这
个寄存器包含信号状态标志,
个寄存器包含信号状态标志,如发送空、 如发送空、发送完成、
发送完成、接收满以及
其它。
其它。这是一个可变寄存器由于这些标志的改变依赖于串行通信
的状态,
的状态,这也是只读,
这也是只读,由于标志不能被程序直接改写,
由于标志不能被程序直接改写,它们只对
模块的状态作出响应。
模块的状态作出响应。这个状态寄存器最佳声明方法是:
这个状态寄存器最佳声明方法是:

/* SCI Status Register */


const volatile unsigned char SCS1 @0x0016

Slide 108
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
栈指针与函数参数

栈指针支持C的关键特性
栈指针支持 的关键特性:
的关键特性: typedef struct {
• 在汇编程序和C编译器中
在汇编程序和 编译器中,
编译器中,堆栈 unsigned char ID;
unsigned short Time;
通常用于给子程序传递变量;
通常用于给子程序传递变量; } ObjectType;
• 允许使用递归;
允许使用递归; void foo (unsigned char value) {
• 是自动变量的基础。
是自动变量的基础。
volatile ObjectType instance;
instance.ID = value;
典型地子程序将把需要的操作数 }

放入累加器。
放入累加器。堆栈相对寻址允许
访问堆栈上的数据,
访问堆栈上的数据,提供直接访
问操作数。
问操作数。
Assembly:
排除从堆栈压入以及弹出数值所
需要的代码和时间,
需要的代码和时间,堆栈指针指 foo:
B00B A7FB AIS #-3
令与等份的变址指令相比需要一 B00D 9EE701 STA 1,SP
B010 A707 AIS #3
个额外的字节和一个额外的执行 B012 81 RTS
周期。
周期。
Slide 109
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
堆栈指针寻址

堆栈指针相对寻址进一步增强了C代码的效率
堆栈指针相对寻址进一步增强了 代码的效率。
代码的效率。

有两种类型:
有两种类型:
位偏移的堆栈指针相对寻址
• 8位偏移的堆栈指针相对寻址
位偏移的堆栈指针相对寻址
• 16位偏移的堆栈指针相对寻址

它们和间接寻址方式工作相似,
它们和间接寻址方式工作相似,但使用堆栈指针代替H:X变址
但使用堆栈指针代替 变址
寄存器。
寄存器。

注意当中断不允许时可用堆栈指针作为额外的变址寄存器。
注意当中断不允许时可用堆栈指针作为额外的变址寄存器。

Slide 110
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
堆栈帧
帧指针
函数通常有一个包含其所有本地数据的堆栈帧。编译器并不设置一个明白的帧指针
,但堆栈上的本地数据和参数都根据SP寄存器访问。

入口代码
通常入口代码是一系列为本地变量保留空间的指令:
PSHA ;仅当有寄存器参数
PSHX ;仅当有寄存器参数
AIS #(-s) ;为本地变量保留空间
S是函数的本地数据的大小(单位:字节)

出口代码
出口代码从堆栈中移除本地变量,并返回到调用者:
AIS #(t) ;移除本地栈空间,包括最终的寄存器参数
RTS ;返回调用者

Slide 111
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
返回值
HC08返回值

除函数返回一对象大于二字节,
除函数返回一对象大于二字节,函数结果都返回到寄存器中
。依据返回类型,
依据返回类型,使用不同的寄存器。
使用不同的寄存器。如下表所示:
如下表所示:

返回类型 寄存器
Char(signed或unsigned) A
int(signed或unsigned) X:A
指针/数组 X:A
函数指针 X:A

返回大对象:
返回大对象:
函数返回大于二字节的对象均与一个附加的参数一起调用,
函数返回大于二字节的对象均与一个附加的参数一起调用,
它被传到H:
它被传到 :X。
。这个参数是对象应复制到的地址。
这个参数是对象应复制到的地址。
Slide 112
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
声明了四个不同类型的函数,每个函
声明了四个不同类型的函数 每个函
数返回一不同类型的变量

每个函数有一个不同类型的参
数(void,byte,word)

Slide 113
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
被调用的函数跳转
到其源码所在的地

给全局变量赋值

函数返回用 RTS指

Slide 114
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
参数在寄存器A中

结果存贮在变量里
 A 变量

堆栈中进行的操作和返回值存
贮在A中

Slide 115
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
A和X用作参数和返回寄
存器

Slide 116
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
A用作参数寄存器,
H:X用于返回指针 也用作直接寻址数
组中的字节

Slide 117
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
函数指针

函数指针与数据指针一样有用处:

• 当你想用一个间接访问
• 当你想用同一段代码,根据环境的不同调用
不同的函数

Slide 118
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
定义指向函数的指针
下面的代码定义了一个指向函数的指针,带了一个整型
参数并返回一整数:

int (*function)(int);

(*function) 括号是必须的,因为定义中的优先关系。
没有括号,则表示定义了一个函数返回一个整型指针

Slide 119
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Example:

•pf[ ] 是一个静态函数指针,INT是参数,返回值为void
void fna(INT); //Example prototype of a function to be called
typedef void (* const PFV_I)(INT); //Declaration using typedef
static PFV_I pf[] ={fna, foo2, foo3, foo4); //Direct declaration
static void (* const pf[])(INT) = {fna, foo2, foo3, foo4};

//Example use
INT a = 6;
pf[jump_index](a); //Calling method 1
(*pf[jump_index])(a); //Calling method 2

Slide 120
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Example:

函数指针初始化

周期性中断毁调
用函数指针指向
的函数

现在函数指针指向另一
个函数

Slide 121
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
HC08QL 示例

•SLIC 模块仅有一个中断
•用户必须读取状态向量寄存
器SLIC State Vector register
(SLCSV)来确定中断源

可能 C 代码:
代码
•Switch-case
•Nested if’s(if嵌套)
•Pointers to functions(函数
指针)

Slide 122
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
声明函数数组

每次调用不同的函数
Slide 123
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Debug (1):

1. 调用函数处,设置断点
2. 单步执行 (F11)

Slide 124
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Debug(2):

每次回执行不同的函数

Slide 125
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Debug(3)

在每个函数中,变量
VarA 被修改

Slide 126
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
When to使用指针执行时间
use pointers:
相同

#funct 3 4 5 6 7

ROM: 264 ROM: 278 ROM: 292 ROM: 306 ROM: 320
IF
RAM: 96 RAM: 96 RAM: 96 RAM: 96 RAM: 96

ROM: 307 ROM: 318 ROM: 329 ROM: 340 ROM: 351
Switch
RAM: 96 RAM: 96 RAM: 96 RAM: 96 RAM: 96

ROM: 270 ROM: 278 ROM: 286 ROM: 294 ROM: 302
Pointers
RAM: 102 RAM: 104 RAM: 106 RAM: 108 RAM: 110

• if 嵌套可读性好,当使用函数少时,代码较小
• Switch 可读性最好,代码最大
• Pointers 函数越多,产生的代码少,但是RAM使用的最多
Slide 127
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
访问数组

• 硬编码:
Array[0]=12*UNIT_VOLTS;
编译时决定地址,执行速度快
• 变址增加
Array[index++]=12*UNIT_VOLTS;
快速,比硬编码灵活
• 数组指针
*(ArrayPtr++)=12*UNIT_VOLTS;
执行速度快,可读性差,可和循环一起使用

Slide 128
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
硬编码

变址增加

数组指针

Slide 129
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
每种访问类型都有
各自的优点,使用
不同的寄存器完成
不同的操作

Slide 130
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
封装变量和函数

Slide 131
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
接口文件
(头文件)
头文件)
仅在这个文件中使用的变

仅在这个文件
中使用的函数

修饰符 extern

可以完全成为一个大工程里的经过测试的模块
,使软件模块化。
使软件模块化。

Slide 132
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
通讯”
通讯 在main.c 和 functions.c
“通讯

Slide 133
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
变量 Result 在main中被声明
中被声明

Slide 134
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
函数正在被执行

在 function.c中使用的
中使用的
变量

Slide 135
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
函数正在被执行

Slide 136
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
存储在全局变量中
Result存储在全局变量中

Slide 137
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
• 利用向导创建一个新工程项目
• 在新项目中加入或删除文件
• 编译
• 调试
• 启动程序
• PRM文件设置
• 如何对IO及寄存器进行操作
• 如何写中断程序
• 如何使用汇编和C语言混合编程
• 嵌入式编程注意事项
• 如何使用Processor Expert

TM

Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
处理器专家
Processor Expert处理器专家

• 为什么要用处理器专家

• 其步骤请转到第一页

Slide 139
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
开发人员面临的挑战

要处理的外设越来越多
外设越来越复杂
开发周期要求越来越短
越来越长的代码需要更多的时间来开发,测试
市场竞争越来越激烈,性能要求越来越高
应用中队创新的追求

这些要求往往很难同时满足,开发人员必须
做出艰难的选择。
他们需要高性能的工具来帮助他们。
CodeWarrior Development Studio及processor expert处理
器专家给工程师提供了巨大的帮助

Slide 140
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
嵌入式应用系统设计
• 工程师面临的挑战

越来越多的功能
• 通讯外设

• A/D 和 D/A 转换

• PWM,

• Timers

• I/O
Slide 141
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
嵌入式应用系统设计 (2)

• ASCI, I2C, ESPI


• CAN, BDLC, USB, Ethernet
缩短产品上市时间
• 市场竞争
• 市场需求

Slide 142
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
项目开发的主要难点

• 外设的选择和使用
需要用到很多外设
• I/O, 字节操作,
字节操作 位操作
• A/D 和 D/A 转换
• 输入捕捉,
输入捕捉 输出比较,
输出比较,PWM
• 通讯控制器

Slide 143
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
项目开发的主要难点(2)
项目开发的主要难点

设计初始化流程
设计外设的使用 (打开,读,写,关闭)
• 芯片和应用的底层开发
• 每个外设的特点
• 每个模块的特点

Slide 144
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
怎样迎接挑战
• 传统方式
 阅读数据手册(上百页的数据手册)
 理解寄存器的含义
> 外设有很多寄存器,每个位的定义,功能
> 理解各种不同的组合
> 晶振,PLL,预分频,波特率设置
 评估所有的选项
• 唤醒模式,
唤醒模式,8-9-10 位模式,
位模式 左/右对齐
右对齐,
右对齐 上升沿/下降沿
上升沿 下降沿
 注意相互影响
个8-bit模块
• 8个 模块,
模块,4个
个16-bit模块
模块,
模块 缓冲寄存器
 理解多功能复用
• 普通I/O引脚和其他模块复用
普通 引脚和其他模块复用

Slide 145
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
阅读上百页的资料

Slide 146
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
问题和风险

 需要时间来阅读和理解手册
 对资料可能存在误解
• 新功能
• 手册错误,
手册错误,或描述不清
 功能重复使用
• 比如 CAN 模块和 PWM 模块共用IO引脚
模块共用 引脚
 编程错误
• 位操作时置位和清零颠倒
 顺序错误
• 寄存器访问顺序错误,
寄存器访问顺序错误,或访问的时间有误

Slide 147
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
开发过程中遇到的困难
• 开发后期发现错误
重新入阅读数据手册
• 返回到项目的最初阶段(
返回到项目的最初阶段(浪费时间)
浪费时间)
重新理解文档
• 更加困难,
更加困难,对以前的理解需要更正
• 怀疑文档,
怀疑文档,怀疑芯片错误
假设理解正确的情况下,修改程序
• 重新修改程序,
重新修改程序,有可能会遇到以上同样的问题
• 花费大量的时间

Slide 148
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
开发过程中遇到的问题 (2)
• 累加的影响
继续重复测试
• 继续对没有任何增值的代码进行测试
• 应用程序没有测试,
应用程序没有测试,还需要软硬件集成
• 开发时间重复浪费

项目
开始 硬件开发 应用程序开发 软硬件集成 产品发布

Slide 149
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
正确迎接挑战
• 采用处理器专家
 针对数据手册的知识库
• 处理器专家包括了微处理器的功能,
处理器专家包括了微处理器的功能 特性,
特性 选项
 隐藏的寄存器
• 寄存器隐藏起来,
寄存器隐藏起来,用相应的选项来代替
 组合和交互自动计算
• 振荡器,
振荡器 PLL, 预分频,
预分频 时钟因子,
时钟因子 波特率产生器自动地被计算
• 可能的值/设置用简单的单位列出来
可能的值 设置用简单的单位列出来
• 手动输入值会被检查,
手动输入值会被检查,如果无法完成则做标记
 检测引脚复用
• 外设使用共用引脚引起的冲突会做标记,
外设使用共用引脚引起的冲突会做标记,在生成代码之前需要解决这个问

• 处理器专家会跟踪已经使用过的资源

Slide 150
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
处理器专家
Processor Expert处理器专家

面向对象的应用开发工具减少上市时间
♦ 功能通过特性,方法
和事件来定义
♦ 软件和外设Bean依
赖硬件
♦ Beans可以重复使用
,而且根据手册经过
完全测试
♦ 处理器专家产生优化
的代码

Slide 151
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
比如:
比如 CPU

Slide 152
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
比如:
比如:通用 I/O

Slide 153
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
比如:
比如 A/D converter

Slide 154
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
内嵌式Beans”
内嵌式
Processor Expert “内嵌式

•BitIO, BitsIO
•ByteIO, Byte2IO, Byte3IO, Byte4IO
•ExtInt (external interrupt)
•TimerInt
•TimerOut
•PWM, PPG
•EventCntr8, EventCntr16, EventCntr32
•FreeCntr8, FreeCntr16, FreeCntr32
•Capture
•RTIshared
•Watchdog
•AsynchroSerial
•ADfast
•InterruptVector
•IntEEPROM
•BWimage, COLimage
Slide 155
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
内嵌式Beans”
内嵌式
Processor Expert “内嵌式

 MotorolaCAN
 USB
 BDLC
 ADconverter, ADC
 ExternalFile,StringList
 AsynchroMaster, AsynchroSlave
 SynchroMaster, SynchroSlave
 Term (terminal)
 TimeDate
 Ext8IO, Ext16IO, Ext32IO

Slide 156
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
优点
 第一次设计时,保证正确性
• 代码经过测试
 测试阶段更快
• 无需阅读上百页的手册
• 不需要返回去重新对外设编程
 错误更少
• 错误只发生在Beans选项的设置上
错误只发生在 选项的设置上
• 如需修改,
如需修改,重新配置或编译,
重新配置或编译,几分钟内就可以实现
 容易移植
• 设计过程中改变CPU只需点击几个按钮
设计过程中改变 只需点击几个按钮
• 旧的设计到新的设计移植仅需点击几个按钮
• 容易移植到不同的硬件模块

Slide 157
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Processor Expert 处理器专家将会提供更多功能

• More intuitive features and options,

• Peripheral Inspector ported to HC(S)08 and HC(S)12,

• Increased performance is being investigated,

• Interface with application design tools, e.g. Mathworks

Slide 158
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
1st Scenario

Block Library
Simulink
PE Beans在模型设计时, 将会作为一个标准
的Simulink块.为MCU/DSP/PPC提
供HAL, 外设,等等

PE Beans 库
MCU/DSP/PPC 包括
Motor Control, LCD, 等,
已经被拷贝到Simulink库中

Result: Simulink model and this model matching PE project.


In PE user configures concrete mapping of beans based HAL
to selected MCU/DSP/PPC physical resources.
Application project code consists from C sources
Stateflow generated from model and its PE project

TM
Slide 159 Processor Expert
Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Double click on the BitIO bean in the Bean Selector in order to add
the bean to the project

Slide 160
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Enter name of the Bean in the Bean Inspector window - RedLED

Slide 161
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Select pin for the RedLED in the Bean Inspector window -
PTB0_ATD0

Slide 162
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Select Pin Direction in the Bean Inspector window - Output

Slide 163
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Click on the TAB Methods in the Bean Inspector window - in order
to set generated methods

Slide 164
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Set all methods "don't generate" instead of the NegVal method -
set the method "generate code"

Slide 165
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Double click on the BitIO bean in the Bean Selector in order to add
the second bean

Slide 166
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Click on the "Properties" TAB in the Bean Inspector window in
order to set the bean

Slide 167
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Enter name of the Bean in the Bean Inspector window - GreenLED

Slide 168
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Select pin for the GreenLED in the Bean Inspector window -
PTB1_ATD1

Slide 169
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Select Pin Direction in the Bean Inspector window - Output

Slide 170
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Click on the TAB Methods in the Bean Inspector window - in order
to set generated methods

Slide 171
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Set all methods "don't generate" instead of the NegVal method -
set the method "generate code"

Slide 172
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Double click on the "ExtInt" bean in the Bean Selector in order to
add the 3rd bean to the project

Slide 173
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Click on the "Properties" TAB in the Bean Inspector window in
order to set the bean

Slide 174
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Enter name of the Bean in the Bean Inspector window - Button

Slide 175
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Select pin for the Button in the Bean Inspector window -
PTG0_KBD0

Slide 176
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Select item "Generate interrupt on" to the "falling edge" in the
Bean Inspector window

Slide 177
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Click on the TAB Methods in the Bean Inspector window - in order
to set generated methods

Slide 178
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Set the following methods "generate code": Enable, Disable -
other set to "don't generate"

Slide 179
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Click in the "Code Design LED.mcp" command in the Processor
Expert menu

Slide 180
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Code design process...

Slide 181
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Double click on the "OnInterrupt" item in the folder
"Beans|Button: ExtInt"

Slide 182
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Find the place "Button_OnInterrupt" for entering the code

Slide 183
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Enter the code to invert the two LEDs:
RedLED_NegVal();
GreenLED_NegVal();

Slide 184
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
The user modules can be found in the folder "User Modules" in
the Project panel window.

Slide 185
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Click on the button "Make" in the project panel in order to compile
the project

Slide 186
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
The project building process

Slide 187
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Check the window if the process was successful

Slide 188
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Click on the button “Debug" in the project panel in order to start
the debugger

Slide 189
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
The external debugger is started now. It is configured to load the
P&E Full Chip Simulator.
Click on the button “Debug" to start program execution!

Slide 190
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Program execution stopped because inputs on port B are
undefined

Slide 191
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Open the “Simulated Port Inputs” Dialog and define values at port
B

Slide 192
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Run the program again – now it will continue running

Slide 193
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Click on the “Halt’ button to stop the program

Slide 194
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Open with the right mouse button the menu, choose “Open
Source File…” and select “Events.C”

Slide 195
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Scroll the Source windows to the function “Button_OnInterrupt” and set a breakpoint
-> click with the right mouse button on “GreenLED” and select “Set Breakpoint”

Slide 196
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
The statement is now marked with the breakpoint symbol
Run the program again

Slide 197
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Enter these commands in the
Command window to stimulate input
on port G:
inputg 1 // puts a high signal on
PTG0_KBD0
inputg 0 // puts a low signal on
PTG0_KBD0

Slide 198
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
This caused an interrupt that than hits the
breakpoint
The output port are visible in the Memory window

Slide 199
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Close and restart debugger - This will restart a new
debug session.
Create a new Visualization Component

Slide 200
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Add a “LED” instrument:
Click with the right mouse button in the Visualizationtool and select from the popup menu
the “Add New Instrument -> LED”

Slide 201
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Drag the new LED instrument into the visible area of the
window
Click with the right mouse button on the instrument to
open the context menu and select Properties

Slide 202
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Change the properties accordingly (change the view and
map to the correct I/O port and bit)

Slide 203
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Copy (Cut & Paste) the LED instrument and update the
properties of the new LED instrumant accordingly

Slide 204
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Click with the left mouse button beside the LEDs into
the Visualizationtool windows to get the properties of
the tool and select the refresh mode and time
accordingly

Slide 205
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
When toggling the port G as previously, both LED will
show the state graphically
Add an additional “Command” instrument to stimulate
port G

Slide 206
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Change the properties accordingly.
The Command field specifies to run the command file
“toggleG.cmd”.

Slide 207
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Switch back focus on the COdeWarrior IDE
Create the new file “ToggleG.cmd”

Slide 208
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Add the commands to the file:
inputg 1
inputg 0

Slide 209
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Add the file “toggleG.cmd” to the current build target:
From the “Project” menu choose “Add toggleG.cmd to
Project…
Select the target “FCSimulator-AZ60” and press OK

Slide 210
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Switch focus back to the debugger
Click the right button to get the menu and switch off
“Edit Mode” of the Visualizationtool

Slide 211
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Clicking on the “Toggle G” button will now generate an
interrupt that toggles the LEDs

Slide 212
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Save this configuration for later use in future sessions:
Select “Save Configuration” from the “File” menu

Slide 213
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Slide 214
TM Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other product
or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2005.
Freescale Semiconductor Confidential Proprietary. Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc.
All other product or service names are the property of their respective owners. © Freescale Semiconductor, Inc. 2004

You might also like