以下是我程序一段,我本想用P0口前四位和后四位做个4*4的键盘
就在while的后面四名哈,读P0具然出现了0xFF,莫名其妙,难道又是手册没看好?
还是芯片。。。。。。?
PCR0=0x0F; //就是这里开始的
P0 = 0x0F;
PUCR0 = 0x0F;
delay(20);
middle1 = P0 ; //middle1具然出现了0XFF
以下是我的程序:
#include "REG8051.h"
#include <stdio.h>
#include "INTRINS.h"
sbit LED1=P1^0;
unsigned char i;
unsigned char middle1;
unsigned char middle2;
unsigned char KEYDATA;
void delay(int delaytimes);
void soundpwm();
main()
{
PCON|=0x02; /* 使能外部时钟 */
PCR0=0xFF;
PUCR0 = 0xFF;
P0=0x00;
PCR1=0xFF;
PUCR1 = 0xFF; //P1带上拉
P1 = 0x00;
for(i=0;i<3;i++)
{
LED1 = 1;
delay(30000);
LED1 = 0;
delay(30000);
}
while(1)
{
//PCR0=(1<<3)|(1<<2)|(1<<1)|(1<<0);
PCR0=0x0F;
P0 = 0x0F;
PUCR0 = 0x0F;
delay(20);
middle1 = P0 ;
if((middle1&0xF0) != 0)
{
//LED1 =1; //LED1亮了
PCR0 = 0xFF;
P0 = 0xF0;
PUCR0 = 0xF0;
delay(20);
middle2 = P0;
if((middle2 & 0x0f) != 0)
{
KEYDATA = middle1 |middle2;
LED1 = 1;
soundpwm();
}
else
{
LED1 = 0;
}
}
}
}
void delay(int delaytimes)
{
int i;
for(i=0;i<delaytimes;i++)
{
_nop_();
}
}
void soundpwm()
{
PWMCKS=0xFC; /* PWM TIMER period register;conversion freq =system clock/2/512=5kHz;
PWM pulse freq =11.0592MHz/1024=10.8KHz */
PWM0L=0x00; /* 25% */
PWM0H=0xFD;
PWM1L=0x00; /* 50% */
PWM1H=0xFE;
PWM2L=0x00; /* 75% */
PWM2H=0x00;
}