源代码如下,在模拟环境上,是可以正常运行的,可是烧到板子上,就开不了机了!
#include
unsigned long data1;
int bitcnt;
bit Power_flag,Key_flag;
sbit P12=P1^2;
sbit LED =P1^1;
sbit Power=P0^0;
sbit Reset=P1^4;
//定时器初始化
void clock_initial()
{
TMOD=0x10; //采用定时器1,模式1
TR1=0;
TH1=0x00; //初始化定时器1
TL1=0x00;
}
//串口发送
/*void putchar(unsigned char rsbuf1)
{
TI=0;
SBUF = rsbuf1;
while (TI==0) ;
TI=0;
}
//ASCII码转换
char ASCIITurn(unsigned char c)
{
if (c>=0&&c<=9)
{
c=c+0x30;
}
else if (c>=0xA&& c<=0xF)
{
c=c+0x57;
}
return c;
}
// 字符串输出
void putstring(char *rsbuf,int b)
{
char i=0;
char b1,b2;
while(rsbuf)
{
if (rsbuf=='\n')
{
putchar('\r');
putchar('\n');
}
else if (rsbuf=='%'&&rsbuf[i+1]=='d')
{
b1=ASCIITurn(b&(0x0f)); //另一种方式表达
b2=ASCIITurn((b>>4)&(0x0f));
putchar(b2);
putchar(b1);
i++;
}
else
{
putchar(rsbuf);
}
i ++;
}
}*/
//时间延迟
delay(unsigned int DelayTime)
{
while (DelayTime)
{
DelayTime--;
}
}
//输出波形(整个编码波形)
void SentKey(unsigned long data1)
{
char BitKey;
EA=0;
P12=0;
delay(2070);
P12=1;
delay(1034); //输出9ms的脉宽
while(bitcnt)
{
BitKey=(data1>>(bitcnt-1))&(0x01); //取位
bitcnt--;
if (BitKey==0)
{
P12=0;
delay(128);
P12=1;
delay(128); //输出0信号脉宽
}
else
{
P12=0;
delay(128);
P12=1;
delay(388); //输出1信号脉宽
}
}
Key_flag=0;
delay(65000);
EA=1;
}
//外部中断服务程序
void exint0_isr(void) interrupt 0 using 0
{ /*unsigned char rsbuf1;*/
unsigned int timer;
/*char b1,b2;*/
bit temp;
EA=0;
TR1=0;
timer=(TH1*256+TL1); //读定时器1的值
TH1=0x00;
TL1=0x00;
TR1=1;
if (timer>1704 && timer<2442) // "0"信号
{
temp=0;
}
else if (timer>3769 &&timer<4506) //"1"信号
{
temp=1;
}
else if (timer>19906 && timer<29859) //header信号
{
bitcnt=-1;
data1=0;
Key_flag=0;
}
bitcnt++;
if (bitcnt>=1&&bitcnt<=32) //开始接收前32位
{
data1=data1|temp;
}
if (bitcnt==24)
{
/*rsbuf1=data1>>(bitcnt-24)&0xff;
b1=ASCIITurn(rsbuf1&(0x0f)); //另一种方式表达
b2=ASCIITurn((rsbuf1>>4)&(0x0f));
putchar(b2);
putchar(b1);
putstring("\n");*/
Key_flag=1;
}
if (bitcnt<32&&bitcnt>0) //左移
{
data1=data1<<1;
}
EA=1;
}
//复位
void ReSet(void)
{
Reset=1;
delay(4608);
Reset=0;
delay(2308);
Reset=1;
}
//电源控制
void Power_Set(void)
{
if (((data1>>8)&0xff)==0x30)
{
if (Power_flag==0)
{
Power=1;
delay(4600);
ReSet();
LED=0;
/*delay(5000);
LED=1;*/
Power_flag=1; //开电源并且复位
}
else
{
LED=1;
delay(5600);
LED=0;
Power=0;
Power_flag=0;
}
}
Key_flag=0;
}
main (void)
{
DIVM=1;
EX0=1;
EA=1; //开中断
TMOD=0x10; //采用定时器1,模式1
SCON=0x50; //串口方式1,允许接收
IT0=1; //下降沿触发
ET1=1;
BRGCON =0;
BRGR1=0x02;
BRGR0=0xF0; //波特率为4800
BRGCON =3;
P0M1=0x00;
P0M2=0x01; //P0.0口为推挽
P1M1=0x00; //P1.1,P1.4为推挽
P1M2=0x12;
clock_initial();
Power=0;
Reset=0;
Power_flag=0;
/* Reset=0;
Power=0;
LED=0;
delay(46100); //延时200ms
LED=1;
ReSet(); */
while(1)
{
/*if (flag==1)
{
delay(46100);
LED=1;
SentKey(data1);
flag==0;
}*/
if(Key_flag==1&&((data1>>8)&0xff)==0x30)
{
Power_Set();
}
delay(6500);
if (((data1>>8)&0xff)==0x00&&Key_flag==1)
{
SentKey(data1);
LED=1;
}
delay(6500);
}
}
开不了机应该 跟程度没关系,看看你的复位电路有没有问题,,应该 是在P1.5上上拉一个电阻,其它都不用接的。
还有就是跟复位电路有关的寄存器设置看看有没有写错,,
如果还是不行的话,,就把程序分开功能,一块一块烧进去,,,看看具体是哪块出问题了吧。