我刚开始学040单片机,对片内资源还不是很了解,今天做了一个很简单的程序,可是有点小问题,请教高手指教
#include<C8051F040.H>
sbit P2_7=P2^7;
sbit P0_0=P0^0;
sbit P0_1=P0^1;
sbit P0_2=P0^2;
sbit P0_3=P0^3;
void config (void)//配置函数
{
WDTCN = 0x07;// 设置WDT超时间隔
WDTCN = 0xDE; // 禁止WDT
WDTCN = 0xAD;
SFRPAGE = 0x0F; //选择特殊功能寄存器所在页
XBR0 = 0x00;// XBAR0: Initial Reset Value
XBR1 = 0x00;// XBAR1: Initial Reset Value
XBR2 = 0x40; // XBAR2: Initial Reset Value
XBR3 = 0x00; // XBAR3: Initial Reset Value
SFRPAGE = 0x0F;
P0MDOUT = 0xff; // Output configuration for P0
P1MDOUT = 0x00; // Output configuration for P1
P2MDOUT = 0x00; // Output configuration for P2
P3MDOUT = 0x00; // Output configuration for P3
P4MDOUT = 0xff; // Output configuration for P4
P5MDOUT = 0x07; // Output configuration for P5
P6MDOUT = 0x00; // Output configuration for P6
P7MDOUT = 0x00; // Output configuration for P7
P1MDIN = 0xFF; // Input configuration for P1
P2MDIN = 0xFF; // Input configuration for P2
P3MDIN = 0xFF; // Input configuration for P3
SFRPAGE = 0x0F;
CLKSEL = 0x00; // Oscillator Clock Selector
OSCXCN = 0x00; // EXTERNAL Oscillator Control Register
OSCICN = 0x80; // Internal Oscillator Control Register
}
void main()
{
unsigned long int i;
P2_7=0; //这个口接led的负极
config();
while(1)
{
//下面的P0_0到P0_3口都接led的正极,可是出现的结果是,P1_1口特别亮,而其他3个口的led
//一般亮,后来我改了这个P0MDOUT = 0xFF;,所有的led都一样亮了
//这是为什么呢
for(i=0;i<45000;i++)
P0_0=1;
for(i=0;i<45000;i++)
P0_1=1;
for(i=0;i<45000;i++)
P0_2=1;
for(i=0;i<45000;i++)
P0_3=1;
}
} |