东南大学电子系统设计实验代码

实验1:led灯闪烁
#include
sbit Led1=P1^0;
sbit Led2=P1^1;
void Delay1s(void)
{
unsigned char h,i,j,k;
for(h=10;h>0;h--)
for(i=4;i>0;i--)
for(j=116;j>0;j--)
for(k=214;k>0;k--);
}
void main()
{
while(1)
{
Led1=1
Led2=0;
Delay1s();
Led1=0;
Led2=1;
Delay1s();
}
}


实验2:串口调试实验(中断方式)
#include //包含51 头文件
#include //访问外部地址空间头文件
#define uint unsigned int
sbit Led1=P1^0; //LED 位声明
sbit Led2=P1^1;
unsigned char flag, sdata;
void delay_s(uint x);
void Led1Invert(void) //led1 闪烁(状态反转)
{
if(Led1==0)
{
Led1=1;
}
else
{
Led1=0;
}
}

void InitSerial()//串口初始化程序
{
SCON=0x50;//将串口设置为方式1,8 位数据
PCON=0x00;//波特率不加倍
TMOD=0x20;//定时器1设为模式2,
TL1=0xfa;//设置波特率为9600=22118400/(32*12*(256-0xfa))
TH1=0xfa;//设置波特率
TR1=1;//启动定时器1
ES=1;//允许串口中断
}


void Sen(unsigned char ch) //串口发送数据
{
ES=0;
TI=0;
SBUF=ch;
while(!TI){}//等待发送结束信号
TI=0;
ES=1;
}


void Ser_ISR() interrupt 4 //串口中断服务程序,串口中断号为4,外部有数据传送时。
{
ES=0;
if(RI)
{
flag=1;
sdata=SBUF;
RI=0;
}
ES=1;
}

void main()//主程序
{
unsigned char temp,state;
InitSerial();//初始化串口
P0=0xff;
EA=1; //允许系统中断
Led1=1; //点亮Led1
temp=XBYTE[0x8000];//读输入状态
Sen(temp); //将状态号码通过串口发送
while(1)
{
if(flag)
{
Led1Invert();
if(sdata==0x23)
{
Led2=0;
delay_s(1);
Led2=1;
}
Sen(sdata+1); //将收到的数据加一后回送
flag=0;
}
state=XBYTE[0x8000]; //访问按键状态
if(state!=temp) //按键和拨码状态改变
{
temp=state;
Sen(temp); //将状态字输出
}
}
}

void delay_s(uint x) //误差 0us
{
uint a,b,c,d;
for(d=x;d>0;d--)
for(c=155;c>0;c--)
for(b=154;b>0;b--)
for(a=4;a>0;a--);
}





实验2:串口调试实验(查询方式)
#include
#include
#define uint unsigned int
#define uchar unsigned char
uchar sdata,temp,state;
sbit Led1=P1^0;
sbit Led2=P1^1;
void InitSerial();
void delay_s(uint x);//延时函数晶振为24M时
void Sent(uchar ch);//串口发送数据
void main()
{
InitSerial();
Led1=1;
Led2=1;
temp=XBYTE[0x8000];//读输入状态
Sent(temp); //将状态号码通过串口发送
while(1)
{
if(RI==1)
{
RI=0;
sdata=SBUF;
Led1=~Led1;
if(sdata==0x23)
{
Led2=0;
delay_s(1);
Led2=1;
}
Sent(sdata+1); //将收到的数据加一后回送
}
state=XBYTE[0x8000]; //访问按键状态

if(state!=temp) //按键和拨码状态改变
{
temp=state;
Sent(temp); //将状态字输出
}
}
}

void InitSerial()
{
SCON=0x50;
PCON=0x00;
TMOD=0x20;
TH1=0xFA;
TL1=0xFA;
TR1=1;
}

void delay_s(uint x)
{
uint a,b,c,d;
for(d=x;d>0;d--)
for(c=155;c>0;c--)
for(b=154;b>0;b--)
for(a=4;a>0;a--);
}

void Sent(uchar ch) //串口发送数据
{
TI=0;
SBUF=ch;
while(!TI){}//等待发送结束信号
TI=0;
}






实验三:数码管的显示(未用定时器)
#include
#define uint unsigned int
#define uchar unsigned char
sbit SER=P1^5;
sbit RCK=P1^6;
sbit SCK=P1^7;
uchar code table[]={0x82,0x9f,0xc1,0x85,0x9c,0xa4,0xa0,0x8f,
0x80,0x84,0x88,0xb0,0xe2,0x91,0xe0,0xe8};
uchar aa;
void display();
void hc595_write(uchar);
void hc595_read();
void delay_us(uint);
void delay_ms(uint y);

void main()
{
while(1)
{
display();
}

}


void display()
{
uchar i,num=0;
for(i=0;i<16;i++)
{
aa=0;
num=table[i];
hc595_write(num);
hc595_read();
delay_ms(500);

}
}
}

void hc595_write(uchar temp)
{
uchar i;
for(i=0;i<8;i++)
{
SCK=0;
SER=temp&0x80;
SCK=1;
temp=temp<<1;
}
}

void hc595_read()
{
RCK=0;
delay_us(1);
RCK=1;
delay_us(1);
RCK=0;
}

void delay_us(uint x)
{
uint i;
for(i=x;i>0;i--);
}

void delay_ms(uint y)
{
uint a,b,c;
for(a=y;a>0;a--)
for(b=2;b>0;b--)
for(c=100;c>0;c--);
}


实验三:数码管的显示(采用定时器)
#include
#define uint unsigned int
#define uchar unsigned char
sbit SER=P1^5;
sbit RCK=P1^6;
sbit SCK=P1^7;
uchar code table[]={0x82,0x9f,0xc1,0x85,0x9c,0xa4,0xa0,0x8f,
0x80,0x84,0x88,0xb0,0xe2,0x91,0xe0,0xe8};
uchar aa;
void display();
void hc595_write(uchar);
void hc595_read();
void delay_us(uint);
void delay_ms(uint y);
void initial();

void main()
{
initial();
while(1)
{
display();
}

}

void initial()
{
aa=0;
TMOD=0x01;
TH0=(65536-46296)/256;
TL0=(65536-46296)%256;
EA=1;
ET0=1;
TR0=1;
}

void display()
{
uchar i,num=0;
if(aa==20)
{
aa=0;
num=table[i];
hc595_write(num);
hc595_read();
i++;
if(i==16)
i=0;
}
}

void hc595_write(uchar temp)
{
uchar i;
for(i=0;i<8;i++)
{
SCK=0;
SER=temp&0x80;
SCK=1;
temp=temp<<1;
}
}

void hc595_read()
{
RCK=0;
delay_us(1);
RCK=1;
delay_us(1);
RCK=0;
}

void delay_us(uint x)
{
uint i;
for(i=x;i>0;i--);
}

/*void delay_ms(uint y)
{
uint a,b,c;
for(a=y;a>0;a--)
for(b=2;b>0;b--)
for(c=100;c>0;c--);
}*/

void timer0() interrupt 1
{
TH0=(65536-46296)/256;
TL0=(65536-46296)%256;
aa++;
/*if(aa==20)
{
aa=0;
temp++;
if(temp==100)
temp=0;
display(temp);
}*/
}



实验三:数码管的显示(显示16进制数,通过串口输入进行改变)
#

include
#define uint unsigned int
#define uchar unsigned char
sbit SER=P1^5;//串行数据输入口
sbit RCK=P1^6;//存储寄存器时钟输入
sbit SCK=P1^7;//移位寄存器时钟输入
uchar code table[]={0x82,0x9f,0xc1,0x85,0x9c,0xa4,0xa0,0x8f,
0x80,0x84,0x88,0xb0,0xe2,0x91,0xe0,0xe8,0xff};
uchar sdata,ge,shi;
void hc595_write(uchar);
void hc595_read();
void delay_us(uint);
void display2(uchar a,uchar b,uchar c,uchar d);
void InitSerial();//串口初始化程序
void Sent(uchar ch); //向串口助手发送数据
void main()
{
InitSerial();
while(1)
{
if(RI==1)
{
RI=0;
sdata=SBUF;
Sent(sdata+1);
shi=sdata/16;
ge=sdata%16;
display2(16,16,ge,shi);
}
}
}


void InitSerial()//串口初始化程序
{
SCON=0x50;//将串口设置为方式1,8 位数据
PCON=0x00;//波特率不加倍
TMOD=0x20;//定时器1设为模式2,
TL1=0xfa;//设置波特率为9600=22118400/(32*12*(256-0xfa))
TH1=0xfa;//设置波特率
TR1=1;//启动定时器1
display2(16,16,16,16);
}

void Sent(uchar ch) //串口发送数据
{
TI=0;
SBUF=ch;
while(!TI){}//等待发送结束信号
TI=0;
}


void display2(uchar a,uchar b,uchar c,uchar d)
{
uchar x,y,z,k;
x=table[a];
hc595_write(x);
hc595_read();
delay_us(500);

y=table[b];
hc595_write(y);
hc595_read();
delay_us(500);

z=table[c];
hc595_write(z);
hc595_read();
delay_us(500);

k=table[d];
hc595_write(k);
hc595_read();
delay_us(500);


}

void hc595_write(uchar temp)//从串行输入口写数据,写一个字节(8位)
{
uchar i;
for(i=0;i<8;i++)
{
SCK=0;//移位寄存器上升沿写数据
SER=temp&0x80;
SCK=1;
temp=temp<<1;
}
}

void hc595_read()
{
RCK=0;//存储寄存器上升沿从并行口输出数据,没有上升沿时是三态输出。
delay_us(1);
RCK=1;
delay_us(1);
RCK=0;
}
void delay_us(uint x)
{
uint i;
for(i=x;i>0;i--);
}



实验5 I2C总线控制
#include
#define uchar unsigned char
#define uint unsigned int
sbit led1=P1^0;
sbit led2=P1^1;
sbit scl=P1^3;
sbit sda=P1^4;
sbit SER=P1^5;//串行数据输入口
sbit RCK=P1^6;//存储寄存器时钟输入
sbit SCK=P1^7;//移位寄存器时钟输入
uchar code table[]={0x82,0x9f,0xc1,0x85,0x9c,0xa4,0xa0,0x8f,
0x80,0x84,0x88,0xb0,0xe2,0x91,0xe0,0xe8,0xff};
uchar flag,sdata;
void start(); //开始信号
void stop(); //停止信号
void respons(); //应答信号
void init(); //初始化串口和I2C总线
void sent(uchar ch); //串口发送数据(发往串口助手)
void write_byte(uchar date);//I2C写数据或地址(一个无符号字符型数据,一个字节)
void write_2byte(uint date);//I2C写数据或地址(一个无符号整形数据,两个字节)
uchar read_byte();//I2C读取数据,返回一个uchar型数据
void delay1(uchar x);//延时函数
void write_add(uchar add1,u

char add2,uchar date);//根据地址写数据,两个地址分开写(共15位)
//void write_2add(uint addr,uchar date);//根据地址写数据,两个地址一起写(15位,两个字节)
uchar read_add(uchar add1,uchar add2);//根据地址读取数据,两个地址分开写
//void init_EEPROM ( );//初始化EEPROM,各存储单元都清零
void delay_us(uint x);//延时微秒函数
void hc595_write(uchar temp);//数码管写数据,从串行输入口写数据,写一个字节(8位)
void hc595_read();//数码管并行口输出数据,输出一个字节(8位)
void display2(uchar a,uchar b,uchar c,uchar d);//显示到数码管
void delay();//进行微秒延时函数
void delay_1s();
void main()
{
uchar temp,temp2,addr1,addr2,ge,shi;
init();
//init_EEPROM();
EA=1;
addr1=0x00;
led1=0;
led2=0;
display2(16,16,16,16);
/*while(1)
{
if(flag==1)
{
sdata=SBUF;
write_add(0x01,addr1,sdata);
delay1(100);
temp2=read_add(0x01,addr1);
sent(temp2+1);
shi=temp2/16;
ge=temp2%16;
display2(16,16,ge,shi);
delay1(100);
flag=0;
}
}*/

while(1)
{
if(flag==1)
{
temp=SBUF;
sent(temp);
write_add(0x01,addr1,temp);
delay1(100);
addr1++;
if(addr1==11)
addr1=0;
flag=0;
}
if(addr1==10)
{
for(addr2=0;addr2<10;addr2++)
{
temp2=read_add(0x01,addr2);
delay1(100);
sent(temp2);
shi=temp2/16;
ge=temp2%16;
display2(16,16,ge,shi);
delay_1s();
}
display2(16,16,16,16);
delay_1s();
}
}
}


void start() //开始信号
{
sda=1;
delay();
scl=1;
delay();
sda=0;
delay();
}

void stop() //停止
{
sda=0;
delay();
scl=1;
delay();
sda=1;
delay();
}

void respons() //应答
{
uchar i;
scl=1;
delay();
while((sda==1)&&(i<250))i++;//应答信号为低电平,或者等待时间过长,表示应答
scl=0;
delay();
}

void init()
{
sda=1;
delay();
scl=1;
delay();
SCON=0x50;//将串口设置为方式1,8 位数据
PCON=0x00;//波特率不加倍
TMOD=0x20;//定时器1设为模式2,
TL1=0xfa;//设置波特率为9600=22118400/(32*12*(256-0xfa))
TH1=0xfa;//设置波特率
TR1=1;//启动定时器1
ES=1;//允许串口中断
}

void sent(uchar ch) //串口发送数据
{
ES=0;
TI=0;
SBUF=ch;
while(!TI){}//等待发送结束信号
TI=0;
ES=1;
}


void Ser_ISR() interrupt 4 //串口中断服务程序,串口中断号为4,外部有数据传送时。
{
ES=0;
if(RI)
{
flag=1;
sdata=SBUF;
RI=0;
}
ES=1;
}


void write_byte(uchar date)
{
uchar i,temp;
temp=date;
for(i=0;i<8;i++)
{
temp=temp<<1;
scl=0;
delay();
sda=CY;
delay();
scl=1;
delay();
}
scl=0;
delay();
sda=1;
delay();
}

/*void write_2byte(uint date)
{
uint i,temp;
temp=date;
for(i=0;i<16;i++)
{
temp=temp<<1;
scl=0;

delay();
sda=CY;
delay();
scl=1;
delay();
}
scl=0;
delay();
sda=1;
delay();
}*/

uchar read_byte()
{
uchar i,k;
scl=0;
delay();
sda=1;
delay();
for(i=0;i<8;i++)
{
scl=1;
delay();
k=(k<<1)|sda;//读取数据,位或
scl=0;
delay();
}
/*scl=0;
delay();
sda=1;
delay();*/
return k;
}

void delay1(uchar x)
{
uchar a,b;
for(a=x;a>0;a--)
for(b=100;b>0;b--);
}

void write_add(uchar add1,uchar add2,uchar date)
{
uchar address1,address2;
address1=add1;
address2=add2;
start();//起始信号
write_byte(0xa2);//从机地址+0(写)
respons();
write_byte(address1);//写入地址的第一个字节(总7位,最高位是无效位,任意填写)(AT24c256任一单元的地址为15 位)
respons();
write_byte(address2);//写入地址的第二个字节(8位)
respons();
write_byte(date);//写入数据
respons();
stop();
}

/*void write_2add(uint addr,uchar date)
{
start();//起始信号
write_byte(0xa2);//从机地址+0(写)
respons();
write_2byte(addr);//写入15位的地址
respons();
write_byte(date);//写入数据
respons();
stop();
}*/

uchar read_add(uchar add1,uchar add2)
{
uchar date,address1,address2;
address1=add1;
address2=add2;
start();//起始信号
write_byte(0xa2);//写入从机地址+0(写)
respons();
write_byte(address1);//写入首地址第一个字节
respons();
write_byte(address2);//写入首地址第二个字节
respons();
start();//起始信号
write_byte(0xa3);//读入从机地址+1(读)
respons();
date=read_byte();//根据从机设备首地址读取数据
stop();
return date;
}

/*-------------------------------------------------------------
功能:初始化EEPROM子程序内容为00
------------------------------------------------------------*/
/*void init_EEPROM ( ) // 64 Byte/1Page init 0x00
{
uint addr;
for(addr=0;addr<65536;addr++)
{
start();
write_byte(0xa2);
respons();
write_2byte(addr);
respons();
write_byte(0x00);
respons();
stop();
}
}*/

void delay_us(uint x)
{
uint i;
for(i=x;i>0;i--);
}

void hc595_write(uchar temp)//从串行输入口写数据,写一个字节(8位)
{
uchar i;
for(i=0;i<8;i++)
{
SCK=0;//移位寄存器上升沿写数据
SER=temp&0x80;//上升沿读一位
SCK=1;
temp=temp<<1;
}
}

void hc595_read()
{
RCK=0;//存储寄存器上升沿从并行口输出数据,没有上升沿时是三态输出。
delay_us(1);
RCK=1;
delay_us(1);
RCK=0;
}

void display2(uchar a,uchar b,uchar c,uchar d)
{
uchar x,y,z,k;
x=table[a];
hc595_write(x);
hc595_read();
delay_us(1);

y=table[b];
hc595_write(y);
hc595_read();
delay_us(1);

z=table[c];
hc595_write(z);
hc595_read();
delay_us(1);

k=table[d];
hc595_write(k);
hc595_read();
delay_us(1);
}

void delay()
{ ;; }

void delay_1s(void)

//误差 -0.000000000227us
{
unsigned char a,b,c;
for(c=23;c>0;c--)
for(b=216;b>0;b--)
for(a=184;a>0;a--);
}

相关文档
最新文档