初级会员
- 积分
- 88
- 金钱
- 88
- 注册时间
- 2016-7-14
- 在线时间
- 12 小时
|
1金钱
电脑通过串口2发送数据给串口1,但是我的老是发的不对,发01 02 03以后,但是返回给电脑的数据就还只是00 00 00,这是怎么回事儿?
#include "reg51.h"
#include "intrins.h"
#include "main.h"
//
uchar str1[45];
char str[3]={0x03,0x02,0x01};
uchar Txden;
int count;
uint numrs;
//延时程序//
void Delay1ms(uchar msCnt)
{
uchar a,b,c;
while(msCnt--)
{
for(c=8;c>0;c--)
for(b=197;b>0;b--)
for(a=2;a>0;a--);
}
}
void main()
{
Uart_one_Init();
LED0=0;
Uart_two_Init();
LED2=0;
Time_Init();
count=0;
Txden=1;
//// SendString(str,2);
while(1)
{
if(Txden)
{
Txden=0;
SendString(str,3);
}
}
// SendString(str,2);
// Delay1ms(500);
// LED1=!LED1;
}
#include "reg51.h"
#include "intrins.h"
#include "main.h"
bit busy=0;//串口1发送标志位
#define FOSC 11059200L //System frequency
#define BAUD 9600
extern uchar Txden;
//extern uchar str[8]={0xfe};
//串口1初始化函数
void Uart_one_Init()
{
SCON = 0x50;
TMOD = 0x20;
TH1 = TL1 = -(FOSC/12/32/BAUD);
TR1 = 1;
ES = 1;
EA = 1;
}
void SendData(uchar dat)
{
int ACC;
while(busy);
ACC=dat;
LED1=0;
busy=1;
SBUF=ACC;
}
//UART发送一个字符串
void SendString(uchar *s,uint len)
{
while(len--)
{
SendData(*(s++));
}
}
//void SendString(uchar *s, uint len) //UART发送一个字符串
//{
// uchar k;
//// TxdEn=1;
// //Delay1ms(1);
// ///while(1);
//// S2BUF=0X01;
//// Delay1ms(1);
// for(k=0;k<len;k++)
// {
// S2BUF=*s;
// while(!(S2CON&S2TI));
// S2CON&=~S2TI;
// s++;
// }
//}
//串口1的接收和发送中断服务函数
void Uart_one() interrupt 4 using 2
{
if(RI)
{
RI=0;
}
if(TI)
{
TI=0;
busy=0;
LED3=0;
}
}
#include "reg51.h"
#include "intrins.h"
#include "main.h"
bit busy1=0;//串口2发送标志位
extern uchar Txden;
extern uchar str1[45];
extern uint numrs485;
//串口2的初始化
void Uart_two_Init()
{
AUXR &= 0xF7; //波特率不倍速
S2CON = 0x50; //8位数据,可变波特率
AUXR |= 0x04; //独立波特率发生器时钟为Fosc,即1T
BRT = 0xDC; //设定独立波特率发生器重装值
AUXR |= 0x10; //启动独立波特率发生器
IE2=0x01; //打开串口2的中断
EA=1; //开总中断
}
void SendData2(uchar dat)//发送一个字节的数据到串口
{
int ACC1;
while(busy1);
ACC1=dat;
busy1=1;
S2BUF=ACC1;
}
void SendString2(uchar *s, uint len) //UART发送一个字符串
{
while(len--)
{
SendData2(*(s++));
}
}
void Uart_Two() interrupt 8 using 3 //串口2的接收和发送中断服务函数
{
if(S2CON&S2RI)
{
S2CON&=~S2RI;
str1[8]=S2BUF;
}
if(S2CON&S2TI)
{
S2CON&=~S2TI;
busy1=0;
}
}
#ifndef MAIN_H
#define MAIN_H
#define uchar unsigned char
#define uint unsigned int
#define FOSC 11059200L
#define T1MS (65536-FOSC/1000)
#define S2RI 0x01
#define S2TI 0x02
sbit LED0=P0^0;
sbit LED1=P0^1;
sbit LED2=P0^2;
sbit LED3=P0^3;
sbit LED4=P0^4;
sbit LED5=P0^5;
sfr AUXR = 0x8e;
sfr S2CON = 0x9a;
sfr S2BUF = 0x9b;
sfr BRT = 0x9c;
sfr IE2 = 0xaf;
//定时器声明
void Time_Init();
void Delay1ms(uchar msCnt);
//串口1函数声明
void Uart_one_Init();
void SendData(uchar dat);
void SendString(uchar *s, uint len);
//串口2函数声明
void Uart_two_Init() ;
void SendData2(uchar dat);
void SendString2(uchar *s, uint len);
#endif
#include "reg51.h"
#include "intrins.h"
#include "main.h"
//extern char str[3]={0x03,0x02,0x01};
extern int count;
extern uchar Txden;
extern uchar str1[45];
void Time_Init()
{
AUXR |=0x80;
TMOD =0x21;
TL0 = T1MS;
TH0 = T1MS >> 8;
TR0 = 1; //开始计时
ET0 = 1; //开定时器中断
EA = 1; //开总中断
}
void time0_isr() interrupt 1 using 1
{
TL0 = T1MS;
TH0 = T1MS >> 8;
if(count-- == 0)
{
count =1000;
LED4=!LED4;
Txden=1;
// SendString(str,2);
}
// SBUF=str1;
}
|
|