一、串口模块
步骤流程:
1. 初始化(使用STC初始化,选择定时器2),并且使能串口中断ES与总中断EA
2. 写中断服务函数(串口1,中断4)——对RI进行处理,负责接收数据(SBUF),然后RI清0
3. 字节发送函数(TI)
接下来我们进行实操
发送函数
1. 初始化
void UartInit(void) //[email protected]
{
SCON = 0x50; //8位数据,可变波特率
AUXR |= 0x01; //串口1选择定时器2为波特率发生器
AUXR |= 0x04; //定时器2时钟为Fosc,即1T
T2L = 0xC7; //设定定时初值
T2H = 0xFE; //设定定时初值
AUXR |= 0x10; //启动定时器2
ES=1;
EA=1;
}
2. 我们在模块里使用的为初始化与字节发送,我们接下来就是字节发送函数(电脑发送)
/* 字节发送函数 */
void SendByte(unsigned char dat)
{
SBUF = dat; //存储数组发送给day
while(TI==0); //TI为发送信号
TI==0;
}
void SendString(unsigned char *dat)
{
while(*dat!= '\0')
SendByte(*dat++);
}
3. 中断服务接受函数
void Uart1Server() interrupt 4
{
if(RI == 1) //读到数据,RI为读取标志,置1为有数据
{
Uart_Receive[Uart_Index] = SBUF; //接受数组的每一位
Uart_Index++; //发送多少位,数字就会是多少
RI = 0; //接受完置0
}
}
补充:
sprintf(需要发送的数组,”数值“,数据来源)
例:上电后发送数据
我们按下按键,然后他就会发送 温度 = 。。。。
SendString("Hello!"); //发送hello
sprintf(Uart_Send,"T = &.2f",t);//Uart_Send 为发送数组,t为我们获得的温度,存储到前面的数组里
SendString(Uart_Send);//SendString(为发送函数
处理函数
void Uart_Proc()
{
if(Uart_Index == 1)
{
dat=Uart_Receive[0]; //½ÓÊÜÊý¾Ý
Uart_Index=0; //ÖØи´Î»
}
}
经过测试后我们的到: 其实我们发送数据和接受数据,需要用同一个数组来进行,不然显示的时候以及切换的时候就会出现问题。
下面这块我们使用第三次模拟题来说明
1. uart.c
#include <STC15F2K60S2.H>
#include "uart.h"
//串口初始化,使用串口1,定时器2 然后打开ES EA
void UartInit(void) //[email protected]
{
SCON = 0x50;
AUXR |= 0x01;
AUXR |= 0x04;
T2L = 0xC7;
T2H = 0xFE;
AUXR |= 0x10;
ES =1;
EA =1;
}
//发送字节函数,把数值放入存储SBUF中,然后定义TI,循环发送
void Send_Byte(unsigned char dat)
{
SBUF = dat;
while(TI == 0);
TI=0;
}
// 发送字符串,判断字符串是否停止,未停止持续发送
void Send_String(unsigned char *dat)
{
while(*dat != '\0')
Send_Byte(*dat++);
}
2. main
#include <STC15F2K60S2.H>
#include "init.h"
#include "seg.h"
#include "led.h"
#include "key.h"
#include "uart.h"
#include "iic.h"
#include <stdio.h>
unsigned char Key_val,Key_old,Key_down;
unsigned char Key_slow_down;
unsigned char Seg_Buf[]={10,10,10,10,10,10,10,10};
unsigned char Seg_Point[]={0,0,0,0,0,0,0,0};
unsigned int Seg_slow_down;
unsigned char Seg_Pos;
unsigned char ucLed[]={0,0,0,0,0,0,0,0};
float V_dat;
float db;
unsigned char db_disp=65;
unsigned char Uart_num=0; //发送个数,放在服务函数中,接收数据
unsigned char Uart_Send[15]; //一个数组可以发送可以接受
unsigned char Seg_Mode=0;
void Key_Proc()
{
if(Key_slow_down) return;
Key_slow_down=1;
Key_val = Key_Read();
Key_down = Key_val&(Key_old^Key_val);
Key_old = Key_val;
switch(Key_down)
{
case 12:
Seg_Mode ^= 1;
break;
case 16:
if(Seg_Mode == 1)
db_disp=db_disp+5;
break;
case 17:
if(Seg_Mode == 1)
db_disp=db_disp-5;
break;
// case 4:
// sprintf(Uart_Send,"db=%.2f",db);
// Send_String(Uart_Send);
// break;
//上面是可以进行按键发送数据
}
}
void Seg_Proc()
{
if(Seg_slow_down) return;
Seg_slow_down=1;
V_dat = Ad_Read(0x03)/51.0;
db = V_dat*18;
Seg_Buf[2]=Seg_Buf[3]=Seg_Buf[4]=10;
switch(Seg_Mode)
{
case 0:
Seg_Buf[1]=1;
Seg_Buf[5]=(unsigned char)db/10;
Seg_Buf[6]=(unsigned char)db%10 ;
Seg_Buf[7]=(unsigned char)(db*100)%10;
Seg_Point[6]=1;
break;
case 1:
Seg_Buf[1]=2;
Seg_Buf[5]=10;
Seg_Buf[6]=db_disp/10;
Seg_Buf[7]=db_disp%10;
Seg_Point[6]=0;
break;
}
}
void Led_Proc()
{
}
// 串口函数,如果数据数量不是0,就判断数组里面的数,如果满足要求,就发送sprintf的数组数据
void Uart_Proc()
{
if(Uart_num!=0)
{
if((Uart_Send[0]=='R')&&(Uart_Send[1]=='e')&&(Uart_Send[2]=='t')&&(Uart_Send[3]=='u')&&(Uart_Send[4]=='r')&&(Uart_Send[5]=='n'))
{
Uart_num=0; //进入判断完成后,就需要进入把数组置0
sprintf(Uart_Send,"Noises:%.1fdB\r\n",db); //定义发送格式
Send_String(Uart_Send); //发送函数
}
}
}
void Timer0Init(void) //[email protected]
{
AUXR &= 0x7F;
TMOD &= 0xF0;
TL0 = 0x18;
TH0 = 0xFC;
TF0 = 0;
TR0 = 1;
ET0 = 1;
EA = 1;
}
void Timer0Server() interrupt 1
{
if(++Key_slow_down==10) Key_slow_down=0;
if(++Seg_slow_down==500) Seg_slow_down=0;
if(++Seg_Pos==8) Seg_Pos=0;
Seg_Disp(Seg_Pos,Seg_Buf[Seg_Pos],Seg_Point[Seg_Pos]);
Led_Disp(Seg_Pos,ucLed[Seg_Pos]);
}
void Uart1Server() interrupt 4 //串口服务函数
{
if(RI==1)//判断是否接收
{
Uart_Send[Uart_num]= SBUF; //把每一位数组字符存入SBUF,等待发送函数发送
Uart_num++; //接收到后加1,计算个数
RI = 0;//接收置0
}
}
void main()
{
Init();
Timer0Init();
UartInit();
while(1)
{
Uart_Proc();
Key_Proc();
Seg_Proc();
Led_Proc();
}
}
二、超声波函数
1. 发送波形 PWM为50的波,发送8哥,每个周期为24us
void Ut_Init()
{
unsigned char i;
for(i=0;i<8;i++)
{
Tx=1;
Delay12us();
Tx=0;
Delay12us();
}
}
接受函数
unsigned char Ut_Data()
{
unsigned int time; //这个地方必须是int 他是好几千的
TMOD &= 0x0f;
TH1=TL1=0;
Ut_Init();
TR1 =1; //¿ªÊ¼¼Æʱ
wile((Rx==1) && (TF1==0))
TR1 = 0; //Í£Ö¹¼Æʱ
if(TF==0)
{
time = (TH1<<8)|TL1;
return (time*0.017);
}
else
{ TF1=0;
return 0;
}
}