中级会员
- 积分
- 332
- 威望
- 244
- 贡献
- 70
- 兑换币
- 48
- 注册时间
- 2009-2-16
- 在线时间
- 9 小时
|
本帖最后由 chenrunshe_007 于 2009-11-16 17:13 编辑
////我这是给初学者的,感觉SCI挺简单的,就两条语句,查询有则接受或发送,如sciReceive,sciPut
////也是当初刚接触时所写的,用串口调试精灵助手的,芯片用的是s12dg128,步骤是在精灵助手里输入如11则将其发送到芯片,
////芯片接受后在通过串口转发到精灵助手上显示,基本可以了解串口的工作方式,供初学者学习之用.初到贵宝地,还请高手赐教.
- ////sci.h 文件//////////////////////////
- #define ST_ID_100 0x00000000
- #define SC0BDL (*((volatile unsigned char*)(0x00C1)))
- #define SC0CR1 (*((volatile unsigned char*)(0x00C2)))
- #define SC0CR2 (*((volatile unsigned char*)(0x00C3)))
- #define SC0DRL (*((volatile unsigned char*)(0x00C7)))
- #define SC0SR1 (*((volatile unsigned char*)(0x00C4)))
- //////sci.c文件////////////////////////////////
- #include "mc9s12dg128.h"
- #include "sci.h"
- ///初始化
- void sciInit(int baud){
- SCI0BDL=8000000/16/baud ;
- SCI0CR1=0x00;
- SCI0CR2=0x0c;
- }
- ///接受
- void sciReceive(unsigned char *Re){
- while((SCI0SR1&0x20)!=0)
- *Re=SCI0DRL;
- }
- ///发送
- void sciPut(unsigned char data){
- while((SCI0SR1&0xc0)!=0)
- SCI0DRL=data;
- }
- ////////main.c文件/////////////////////////////
- #include <hidef.h> /* common defines and macros */
- #include <mc9s12dg128.h> /* derivative information */
- unsigned char Re;
- void main(void) {
- sciInit(9600);
- DDRB=0x2f;
- sciReceive(&Re);
- sciPut(Re);
- }
复制代码 |
|