Chinaunix首页 | 论坛 | 博客
  • 博客访问: 436440
  • 博文数量: 122
  • 博客积分: 3010
  • 博客等级: 中校
  • 技术积分: 1538
  • 用 户 组: 普通用户
  • 注册时间: 2007-10-17 11:10
文章分类

全部博文(122)

文章存档

2011年(1)

2008年(86)

2007年(35)

我的朋友

分类:

2007-10-29 10:57:13

    早上起来测试mc8051的移位,发现在KeilC中仿真结果一样的两段程序在DE2板子上跑起来不一样。
 
    下面两段程序只有第2段跑起来和仿真结果一样:
 
程序段1:
 

#include <REG51.H>
#include <stdio.h>

void main(void)
{
    unsigned int i,j;    
    do{
        P2 = 0x80;
        for( j=0; j<7 ; j++ )
        {
            for( i=0; i<50000 ; i++ );
            {
                P2 = P2 >> 1;    
            }
        }
    }while(1);
}

程序段2:

#include <REG51.H>
#include <stdio.h>

void main(void)
{
    unsigned int i,j;
    unsigned char c;    
    do{
        c = 0x80;
        for( j=0; j<7 ; j++ )
        {
            for( i=0; i<50000 ; i++ );
            {
                c = c >> 1;    
                P2 = c;
            }
        }
    }while(1);
}

    一开始用程序段1时以为mc8051的移位指令是坏的,后来不得已用char试之,可行。但没搞明白为什么需要用char移位后再赋给P2才能移位。

    发现用 int 代替 unsigned int 也会产生不爽的结果,因为50000对int来说太大了?看来单片机里的资源是相当的紧张,一定要充分利用了。

阅读(1224) | 评论(1) | 转发(0) |
0

上一篇:Timer/Counter和TMOD

下一篇:hspice弱智笔记

给主人留下些什么吧!~~

chinaunix网友2008-05-28 16:34:33

程序段1不行是因为mc8051的P0、P1、P2、P3是单向口,语句 P2 = P2 >> 1;不能从P2口读出数据!