第一题
#include "SC95F861x_C.H"
#include <INTRINS.H>
unsigned char keydata=0;
void delay(unsigned int timer) //延时函数
{
while(timer>0)
timer--;
}
void IOinit()
{
P5CON=0x00;
P5PH=0x03;
P3CON=0xFF;
P3PH=0xFF;
}
void readkey()
{
if(P51==0)
{
delay(1000); //延时去抖
if(P51==0)
{
keydata=1;
}
}
else if(P50==0)
{
delay(1000);
if(P50==0)
{
keydata=2;
}
}
}
void main()
{
unsigned char data=0x7F;
IOinit();
while(1)
{
readkey();
if(keydata==1) //S1按下,做所有led灯循环点亮
{
P3=data;
data=_cror_(data,1); //循环右移
delay(50000);
}
else if(keydata==2)
{
P3=0xFF; //即S2按下,做熄灭所有led灯
}
}
}
第二题
#include "SC95F861x_C.H"
#include <INTRINS.H>
void delay(unsigned int timer)
{
while(timer>0)
timer--;
}
void delay1(unsigned int timer1)
{
while(timer1>0)
timer1--;
}
void delay2(unsigned int timer2)
{
while(timer2>0)
timer2--;
}
void IOinit()
{
P0CON=0x03;
P0PH=0x43;
P4CON=0x00;
P4PH=0x01;
}
void EXINT0init()
{
INT0F=0x40; //P06下降沿触发
EINT0=1; //打开分开关
EA=1; //打开总开关
}
void EXINT1init()
{
INT1F=0x01; // 外部中断1设置为P40下降沿触发
EINT1=1; //打开分开关
}
void EXINT0() interrupt 0
{
delay1(1000); //延时去抖
if(P06==0)
{
P00=0;
P01=0;
}
}
void EXINT1() interrupt 2
{
delay2(1000); //延时去抖
if(P40==0)
{
P00=1;
P01=1;
}
}
void main()
{
IOinit();
P00=1;
P01=1;
EXINT0init();
EXINT1init();
while(1)
{
}
}
第三题
#include "SC95F861x_C.H"
#include <INTRINS.H>
int timer0counter=0;
int timer0counter1=0;
bit BUZZdata=0;
bit old=0;
void delay(unsigned int timer)
{
while(timer>0)
timer--;
}
void IOinit()
{
P5CON=0x00;
P5PH=0x08;
P2CON=0x80;
P2PH=0x80;
P1CON=0x20;
}
void TIMER0init()
{
TMCON=0x00; //设置不分频
TMOD=0x21; //定时器0---方式1,定时器1-----方式2
TL0=(65536-2667)%256; //----1ms
TH0=(65536-2667)/256;
ET0=1; //打开分开关
TR0=1; //打开定时器开关
EA=1; //打开总开关
}
void TIMER0() interrupt 1
{
TL0=(65536-2667)%256; //方式1要手动置位
TH0=(65536-2667)/256;
timer0counter++;
if(timer0counter>499) //定0.5s
{
P27=!P27;
timer0counter=0;
}
if(BUZZdata==1)
{
P15=!P15;
timer0counter1++;
if(timer0counter1>4999) //蜂鸣器响5s后停止
{
timer0counter1=0;
BUZZdata=0;
}
}
}
void readkey()
{
if(P53==0)
{
delay(1000);
if(P53==0&&old==0) //让按键只识别一次P53等于0,一直按着按键也只是别一次
{
BUZZdata=1;
old=1;
}
}
else if(P53==1)
{
old=0;
}
}
void main()
{
IOinit();
P27=0;
TIMER0init();
while(1)
{
readkey();
}
}
第四题
#include "SC95F861x_C.H"
unsigned char recv=0;
void delay(int timer)
{
while(timer>0)
{
timer--;
}
}
void IOinit()
{
P3CON=0x84;
P3PH=0x80;
P2CON=0x00;
}
void URAT0init()
{
SCON = 0X50; //设置串口方式1,且允许接受
EUART = 1; //打开分开关
TMOD=0x21; //将定时器1设置为方式2
TH1 = (32000000 / 4800) / 256; //设置波特率为4800
TL1 = (32000000 / 4800) % 256;
TR1 = 0; //将定时器1开关关掉
EA=1; //打开总开关
}
void uart0() interrupt 4
{
if(RI)
{
RI=0;
recv=SBUF; //接收数据
if(recv==0x55)
{
P37=0;
SBUF=0x55;
}
else if(recv==0xAA)
{
P32=1;
SBUF=0xAA;
}
else if(recv==0x50)
{
P37=1;
SBUF=0x50;
}
else if(recv==0xA0)
{
P32=0;
SBUF=0xA0;
}
}
}
void main()
{
IOinit();
URAT0init();
while(1)
{
}
}