早上起来测试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来说太大了?看来单片机里的资源是相当的紧张,一定要充分利用了。
阅读(1287) | 评论(1) | 转发(0) |