标题:
关于STC12C5A60S2单片机双串口程序问题
[打印本页]
作者:
楠仔高
时间:
2019-3-11 14:32
标题:
关于STC12C5A60S2单片机双串口程序问题
为什么我的程序编译成功了,但是串口2却不会发送数据呢?谢谢!
#include<STC12C5A60S2.h>
#include <stdio.h>
#include <string.h>
#include<intrins.h>
#include "UART2.h"
#define uchar unsigned char
#define uint unsigned int
uchar code dis_tab1[]={" 识别结果 "};
uchar code dis_tab4[]={"------------------------"};
uchar RecDate[14]={0}; //串口接收 数据
uint count_ms=0; //计时
uchar recnum2=0; //串口接收长度
uchar disflag=0;//显示模式
bit recfinish=0; //串口接收完成为1
void Timer0Init(void) //10m秒@11.0592MHz
{
TMOD &= 0xF0; //设置定时器模式
TMOD |= 0x01; //设置定时器模式
TL0 = 0x00; //设置定时初值
TH0 = 0xDC; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 0; //定时器0关闭计时
ET0=1;
EA=1;
}
/* 串行通信初始化*/
void UartInit(void) //9600bps@11.0592MHz
{
PCON &= 0x7F; //波特率不倍速
SCON = 0x50; //8位数据,可变波特率
TMOD &= 0x0F; //清除定时器1模式位
TMOD |= 0x20; //设定定时器1为8位自动重装方式
TL1 = 0xfd; //设定定时初值
TH1 = 0xfd; //设定定时器重装值
ET1 = 0; //禁止定时器1中断
TR1 = 1; //启动定时器1
ES = 1; //Enable UART1 interrupt
EA=1; //开总中断
}
/************************************************/
void Delay_1ms(unsigned int xms)
{
unsigned int i,j;
for(i=xms;i>0;i--)
for(j=122;j>0;j--);
}
复制代码
#include"12864.h"
void display()///液晶显示函数
{
uchar i=0;
uchar temp[14]={0};
LCD_pos(3,0);
for(i=0;i<16;i++)
write_dat(' ');
LCD_pos(3,0);
sprintf((char*)temp,"%13s",RecDate);
i=0;
while(temp[i] != '\0')
{
write_dat(temp[i]);
i++;
}
LCD_pos(1,3);
for(i=0;i<10;i++)
write_dat(' ');
LCD_pos(1,3);
}
void send_char_com(unsigned char ch) //串口1发送字节
{
SBUF=ch;
while(TI==0); //若果TI=0,循环等待
TI=0; //发送完,清零TI
}
void main() //主函数
{
uint count=0;
uchar i=0;
Timer0Init();//初始化T0
LCD_init();//初始化led
UartInit(); //初始化串口1
Uart2Init(); //初始化串口2
while(1)
{
count++;
if(count>10)
{
count=0;
}
if(recfinish==1)//串口接收到数据,得到识别码
{
recfinish=0;
TR0=0;//关闭定时器0
count_ms=0;
display();
Delay_1ms(30);
recnum2=0;// 接收数据长度清零
for(i=0;i<14;i++)
RecDate[i]=0;
}
Delay_1ms(30);
}
}
/***********************************************
函数名称:Timer0_ISR
功 能:定时器0中断处理函数
入口参数:无
返 回 值:无
备 注:无
************************************************/
void Timer0_ISR() interrupt 1 //10ms
{
TL0 = 0x00; //设置定时初值
TH0 = 0xDC; //设置定时初值
count_ms++;
if(count_ms>100)//超过1s
{
count_ms=0;
recfinish=1; //串口接收完成
}
}
/*----------------------------
UART interrupt service routine 串口接收条形码数据
----------------------------*/
void Uart_Isr() interrupt 4 using 1
{
if (RI)
{
RI = 0; //Clear receive interrupt flag
TR0=1;//打开定时器0
if(recnum2<13)
{
RecDate[recnum2]=SBUF;
recnum2++;
}
}
}
#ifndef _UART2_H_
#define _UART2_H_
void Uart2Init(void);
void Uart2_send_byte(unsigned char date);
void Uart2_send_string(unsigned char *p);
void Uart2Init(void) //9600bps@11.0592MHz
{
AUXR |= 0x08; //使能波特率倍速位S2SMOD:(波特率加倍)
S2CON = 0x50; //8位数据,可变波特率
AUXR &= 0xFB; //独立波特率发生器时钟为Fosc/12,即12T
BRT = 0xFA; //设定独立波特率发生器重装值
AUXR |= 0x10; //启动独立波特率发生器
IE2 =0x01; //开串口2中断 ES2=1
}
void Uart2_send_byte(unsigned char date)//串口2发送一个字节
{
IE=0 ;
S2BUF=date;
while((S2CON&0X02)==0);
{
S2CON&=~0X02; //清除发送标志位
IE=1;
}
}
void Uart2_send_string(unsigned char *p) //串口2发送一个字符串
{
while(*p!='\0')
{
Uart2_send_byte(*p);
p++;
}
}
#endif
复制代码
作者:
saya0769
时间:
2019-4-6 08:35
我是串口2无法接受数据
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1