串口编程实验报告

/**************************************************

实验名称:串口实验一

实验目的:在上位机上用串口调试助手发送一个字符X,单

 片机在收到字符后返回给上位机,显示"I got 

 X",串口波特率设定为9600bps。

实验备注:SMOD处在TCON第一位,TCON在单片机复位上电

 后清零。所以不设置TCON的话,串口方式在1、

 2、3时,波特率处于正常不加倍状态,也不用

 特意去设置TCON。

**************************************************/

#include<reg52.h>

//#include<stdio.h>


#define uchar unsigned char

#define uint unsigned int


uchar flag, temp, i;


uchar code table[] = "I got ";


void init();


void main()

{

flag = 0;

temp = 0;

i = 0;

init();

while(1)

{

if(flag == 1)

{

ES = 0;

for(i = 0; i < 6; i++)

{

// TI = 1;

// printf("I got the ");

SBUF = table[i];

while(!TI);

TI = 0;


}

SBUF = temp;

while(!TI);

TI = 0;

flag = 0;

ES = 1;

}

}

}


void init()

{

TMOD = 0X20;//在定时器工作模式2时,

TH1 = 0xfd;//处在自动填装八位寄存器状态

TL1 = 0xfd;//波特率为9600两寄存器各填装0xfd

EA = 1;

ES = 1;

SM0 = 0;//10位异步收发,8位数据,

SM1 = 1;//定时器1溢出控制,波特率可控制

REN = 1;//串口接受允许位

TR1 = 1;

}


void ser() interrupt 4

{

RI = 0;

temp = SBUF;

flag = 1;

}


/**************************************************

实验名称:串口实验二

实验目的:单片机上电后等待从上位机串口发送来的命令,

 同时在数码管的前三位上以十进制方式显示A/D

 采集的数值,在未收到上位机发送来的启动A/D

 转换命令前数码管始终显示000。

 当收到上位机以十六进制发送来的01后,向上

 位机发送字符串"Turn on ad!",同时间隔1秒

 读取一次A/D的值,然后把A/D采样回来的8位二

 进制数转换成十进制数表示的实际电压浮点数,

 并发送给上位机,形式如:"The voltage is 

 3.398438V",发送周期间隔1秒,同时在数码管

 上显示十进制数。

 当收到上位机发来的十六进制02后,向上位机

 发送字符串"Turn off ad!",然后停止发送电

 压值,数码管上显示上次结束时保持的值。

 当收到上位机发来的其他数时显示"Error"

实验备注:

**************************************************/

#include<reg52.h>

#include<intrins.h>

#include<stdio.h>


#define uchar unsigned char

#define uint unsigned int


sbit DUAN = P2^6;

sbit WEI = P2^7;

sbit ADRD = P3^7;

sbit ADWR = P3^6;


uchar code duan_table[]=

{0x3f,0x06,0x5b,0x4f,

 0x66,0x6d,0x7d,0x07,

 0x7f,0x6f,0x77,0x7c,

 0x39,0x5e,0x79,0x71};


uchar code wei_table_ad[]=

{0x7e,0x7d,0x7b,

 0x77,0x6f,0x5f};


uchar s_flag, t_flag, s_var, vol_led;

float vol_ser_t;


void init();

uchar read_ad();

void delay1ms(uchar);

void display(uchar);


void main()

{

init();

while(1)

{

if(s_flag == 1)

{

s_flag = 0;

ES = 0;

TI = 1;

switch (s_var)

{

case 1:

printf("Turn on the ad!\n");

TR0 = 1;

break;

case 2:

printf("Turn off the ad!\n");

TR0 = 0;

break;

default:

printf("Error!\n");

break;

}

while(!TI);

TI = 0;

ES = 1;

}

if(t_flag == 20)

{

vol_led = read_ad();

t_flag = 0;

vol_ser_t = (float)vol_led/256*5;

ES = 0;

TI = 1;

printf("The voltage is %fV\n",vol_ser_t);

while(!TI);

TI = 0;

ES = 1;

}

display(vol_led);


}

}


void init()

{

WEI = 1;

P0 = 0x7f;

WEI =1;

s_flag = 0;

t_flag = 0;

vol_led = 0;

vol_ser_t = 0;

TMOD = 0x21;

TH0 = (65536-46080)/256;

TL0 = (65536-46080)%256;

TH1 = (0xfd);

TL1 = (0xfd);

EA = 1;

ET0 = 1;

ES = 1;

// TR0 = 1;

TR1 = 1;

REN = 1;

SM0 = 0;

SM1 = 1;

}


uchar read_ad()

{

uchar vol;

ADWR = 1;

_nop_();

ADWR = 0;

_nop_();

ADWR = 1;

P1 = 0xff;

ADRD = 1;

_nop_();

ADRD = 0;

_nop_();

vol = P1;

_nop_();

ADRD = 1;

return vol;

}


void delay1ms(uchar x)

{

uchar i, j;

for(i = 0; i < x; i++)

for(j = 0; j < 110; j++);

}


void display(uchar num)

{

DUAN = 1;

P0 = duan_table[num/100];

DUAN = 0;

P0 = 0xff;


WEI = 1;

P0 = wei_table_ad[0];

WEI = 0;

delay1ms(1);


DUAN = 1;

P0 = duan_table[num%100/10];

DUAN = 0;

P0 = 0xff;


WEI = 1;

P0 = wei_table_ad[1];

WEI = 0;

delay1ms(1);


DUAN = 1;

P0 = duan_table[num%10];

DUAN = 0;

P0 = 0xff;


WEI = 1;

P0 = wei_table_ad[2];

WEI = 0;

delay1ms(1);

}


void Timer0() interrupt 1

{

TH0 = (65536-46080)/256;

TL0 = (65536-46080)%256;

t_flag++;

}


void Ser() interrupt 4

{

RI = 0;

s_var = SBUF;

s_flag = 1;

}


/**************************************************

实验名称:串口实验三

实验目的:在上位机上用串口调试助手发送一个字符X,单

 片机在收到字符后返回给上位机,显示"I got 

 X",串口波特率设定为9600bps。使用printf。

实验备注:

**************************************************/

#include<reg52.h>

#include<stdio.h>


#define uchar unsigned char

#define uint unsigned int


uchar temp, s_flag;


//void delay1ms(uchar);


void main()

{

temp = 0;

s_flag = 0;

TMOD = 0X20;

TH1 = 0XFD;

TL1 = 0XFD;

EA = 1;

ES = 1;

SM0 = 0;

SM1 = 1;

REN = 1;

TR1 = 1;

while(1)

{

if(s_flag ==1)

{

ES = 0;

TI = 1;//在printf之前必须把TI置1;

printf("I got the %c\n",temp);

while(!TI);

TI = 0;

s_flag = 0;

ES = 1;

}

}

}


/*void delay1ms(uchar x)

{

uchar i, j;

for(i = 0; i < x; i++)

for(j = 0; j < 110; j++);

}*/


void Ser() interrupt 4

{

RI = 0;

temp = SBUF;

s_flag = 1;

}


你可能感兴趣的:(单片机,串口,波特率)