这个是51的代码,PIC的稍后//
#define ALLOCATE_EXTERN
#include "HC89F0541.h"
#include "BIT.h"
#include "FONT0805.h"
#include "DELAY_LONG.h"
#include "MCUIIC.h"
#include "SH1106.h"
#define out P1
unsigned int i, n;
unsigned char x, a;
unsigned char test(unsigned char dat)
{
unsigned char y;
y=dat;
return y;
}
void TIMER3_ISR(void) interrupt 10
{
n++;
P2_7 =1; //LED亮
WDTC |= 0x10; //看门狗清零
OLED_gotoxy(71,3); //显示时间
OLED_show(n);
P2_7 =0;
if(a) //显示:程序在1600K个CPU时钟里运行for()语句的次数
{
a=0;
OLED_gotoxy(35,3);
OLED_show(i);
}
}
void main(void)
{
CLKSWR = 0x51; //选择内部高频RC为系统时钟,内部高频RC2分频,Fosc=16MHz
CLKDIV = 0x01; //CPU时钟Fcpu=Fosc/分频值,比如CLKDIV=0x01,则CPU=16MHz
P2M7 = 0xC2; //P27设置为推挽输出驱动LED
IIC_init();
OLED_init();
OLED_clear();
OLED_gotoxy(14,1);
OLED_string("HC8051 ==> SH1106");
OLED_gotoxy(11,5);
OLED_string("21IC.COM PANXIAOYI");
EA = 1; //打开总中断
T3CON=0;
T3CON B5H; T3CON B4H; //计数时钟=Fosc/256分频
TH3 = 0xE7; TL3 = 0x96; //消耗时钟=(65536-0xE796)*256分频=1600K,即中断时间是100ms
ET3 = 1; //打开T3中断
T3CON B2H; //启动定时器
a=1;
i=1;
n=0;
while(1)
{
for(i=0; i<65535; i++) //全部语句可以运行8988次
{
x=test(37); //注释这条可以运行9580次
out=x;
if(x>47) x=57; else x=67; //注释这条可以运行9998次
out=x;
x<<=1; //注释这条可以运行9248次
out=x;
x>>=1; //注释这条可以运行9248次
out=x;
x|=1; //注释这条可以运行9142次
out=x;
x&=1; //注释这条可以运行9142次
out=x;
x=~x; //注释这条可以运行9142次
out=x;
x=x^1; //注释这条可以运行9142次
out=x;
x+=1; //注释这条可以运行9090次
out=x;
x-=1; //注释这条可以运行9090次
out=x;
x*=3; //注释这条可以运行9523次
out=x;
x/=3; //注释这条可以运行9523次
out=x;
x%=3; //注释这条可以运行9638次
out=x;
}
while(1);
}
}
|