让串口,和按键,同时控制单片机一个io口输出高电平戓低电平,

2024-12-05 13:59:15
推荐回答(2个)
回答(1):

#include "reg52.h"

sbit key1 = P3^2;
sbit key2 = P3^3;

sbit out = P0^0;

void UartInit(void) //9600bps@11.0592MHz
{
SCON = 0x50; //8位数据,可变波特率
TMOD &= 0x0F; //设定定时器1为16位自动重装方式
TL1 = 0xE8; //设定定时初值
TH1 = 0xFF; //设定定时初值
ET1 = 0; //禁止定时器1中断
TR1 = 1; //启动定时器1
}

void main(void)
{
UartInit();
IT0 = 1; //set INT0 int type (1:Falling 0:Low level)
EX0 = 1; //enable INT0 interrupt
IT1 = 1; //set INT0 int type (1:Falling 0:Low level)
EX1 = 1; //enable INT0 interrupt
ES = 1;
EA = 1; //open global interrupt switch
while(1);

}
//External interrupt0 service routine
void exint0() interrupt 0 //(location at 0003H)
{
out = 1;
}
//External interrupt1 service routine
void exint1() interrupt 2 //(location at 0013H)
{
out = 0;
}
/*----------------------------
UART interrupt service routine
----------------------------*/
void Uart_Isr() interrupt 4 using 1
{
unsigned char temp;
if (RI)
{
RI = 0; //Clear receive interrupt flag
temp = SBUF;
if(temp == 0xaa)out = 1;
if(temp == 0x55)out = 0;
}
if (TI)
{
TI = 0; //Clear transmit interrupt flag
}
}

回答(2):

可以实现