【单片机】P3口输入,P1口输出
代码详解(方法一)
#include <reg51.h>
#include <intrins.h>
//功能:P3口输入,P1口输出
#define uchar unsigned char
unsigned char code mon51[3] _at_ 0x3b; //保留0x3B开始三个程序空间作全速中断用
sbit flag = P3 ^ 3;
uchar judge = 0;
uchar y = 1;
void delay(unsigned int i)
{
unsigned int j, k;
for (k = 0; k < i; k++)
for (j = 0; j < 1000; j++)
;
}
void main(void)
{
uchar aa;
P1 = 0xff; //P1口初始化
aa = 0xfe; //变量赋值
while (1)
{
if (!judge && flag == 1)
judge = 1;
else if (judge && flag == 0)
{
y <<= 1;
judge = 0;
}
P1 = ~y;
if (y == 128)
y = 1;
delay(10);
}
}
代码详解(方法二)
#include <reg51.h>
#include <intrins.h>
//功能:P3口输入,P1口输出
#define uchar unsigned char
unsigned char code mon51[3] _at_ 0x3b; //保留0x3B开始三个程序空间作全速中断用
sbit P3_3 = P3 ^ 3;
uchar led = 0x00;
void delay(unsigned int i)
{
unsigned int j, k;
for (k = 0; k < i; k++)
for (j = 0; j < 1000; j++)
;
}
void main(void)
{
P1 = 0xFF;
while (1)
{
if (P3_3 == 0)
{
delay(50);
if (P3_3 == 0)
{
led = led + 1;
P1 = ~led;
while (P3_3 == 0)
{
}
}
}
}
}