STC串口中断问题,请各位老师帮帮我!
#include "STC_NEW_8051.H"
#include"intrins.h"
#include<string.h>
#include<stdio.h>
#define Uint unsigned int
#define Uchar unsigned char
#define uchar8 unsigned char
#define uint16 unsigned int
//sbit P03= P0^0;
sbit SpkerBit=P0^0;
sbit LedBit = P1^6;//led 控制位
sbit P43= P4^3;
Uchar point;
Uchar rbuf[100]=NULL;
Uchar flag;
//#define FOSC 12000000L
#define FOSC 11059200L
//#define FOSC 12450000L
#define BAUD 2400
#define BAUD2 19200
#define MODE1T //Timer clock mode,comment this line is 12T mode,uncomment is 1T mode
#ifdef MODE1T
#define T1MS (65536-FOSC/1000) //1ms timer calculation method in 1T mode
#else
#define T1MS (65536-FOSC/12/1000) //1ms timer calculation method in 12T mode
#endif
#define NONE_PARITY 0 //无校验位
#define ODD_PARITY 1 //奇校验
#define EVEN_PARITY 2 //偶校验
#define MARK_PARITY 3 //标记校验
#define SPACE_PARITY 4 //空校验
#define PARITYBIT NONE_PARITY
#define S2RI 0x01
#define S2TI 0x02
#define S2RB8 0x04
#define S2TB8 0x08
#define N 50
//sfr AUXR = 0x8e;
//sfr AUXR1 = 0xa2;
//sfr S2CON = 0x9a;
//sfr S2BUF = 0x9b;
//sfr BRT = 0x9c;
//sfr IE2 = 0xaf;
bit busy1,busy;
Uchar UART1Count,UART2Count=0;//uart1 收发计数器
Uchar xdata UART1Buf[5]={0,0,0,0,0};//uart1 收发缓存器
Uchar UART2Buf[15]={0,0,0,0,0,0,0,0,0,0,0};//uart2 收发缓存器
void Delay(Uint n);
void InitAll();//所有初始化 函数
void InitUart1();
void InitUart2();
void SendHex7(char *s);
void Send2Hex7(Uchar *s);
void paixu(Uint b[],int n);
void select(Uint b[],int n);
void qiuhe(Uint b[],int n);
void fun(m);
void init_Uart();
Uchar flag;//接收数据标识,0未接收数据1接收数据
void main()
{
Uint bat[120]={2100,2102,2424,2143,2144,2245,2146,2147,2348,2349,2350,2351,2330,2331,2332,2333,2334,2335,2336,2337,2338,2339,2230,2231,2232,2233,2234,2235,
2236,2237,2238,2239,2240,2241,2242,2243,2245,2246,2247,2248,2249,2250,2251,2252,2253,2254,2255,2256,2257,2258,2259,2260,2261,2262,2263,2264,2265,2266,
2267,2268,2269,2270,2271,2272,2273,2274,2275,2276,2277,2278,2279,2280,2281,2282,2283,2284,2240,2286,2287,2288,2289,2290,2291,2292,2293,2294,2295,2296,
2256,2298,2299,2352,2353,2354,2355,2356,2357,2358,2359,2360,2361,2362,2363,2364,2365,2366,2367,2368,2369,2370,2371,2372,2173,2174,2375,2376,2377,2378,
2379,2388};
Uchar zlp[7]={0xAA,0xBB,0x04,0x80,0x03,0x0,0x04};
Uchar Command[8]={0xAA,0xBB,0x05,0x82,0x00,0x00,0x00,0x10};
Uchar zlp1[7]={0xAA,0xBB,0x04,0x80,0x03,0x00,0x05};
Uchar zlp2[7]={0xAA,0xBB,0x04,0x80,0x03,0x00,0x17};
Uint Umax1=1;
InitAll();
busy1=0;
busy=0;
Delay(50000);Delay(50000);Delay(50000);Delay(50000);Delay(50000);Delay(50000);
Delay(50000);Delay(50000);Delay(50000);Delay(50000);Delay(50000);Delay(50000);
Delay(50000);Delay(50000);Delay(50000);Delay(50000);Delay(50000);Delay(50000);
while(1)
{
//LedBit =0;
Delay(50000);Delay(50000);
//LedBit =1;
Delay(50000);Delay(50000);
Send2Hex7(UART2Buf);
}
}
void InitAll()//所有初始化 函数
{
InitUart1();
InitUart2();
EA = 1; //开总中断
}
void InitUart1()
{
#if (PARITYBIT == NONE_PARITY)
SCON = 0x50; //8-bit variable UART
#elif (PARITYBIT == ODD_PARITY) || (PARITYBIT == EVEN_PARITY) || (PARITYBIT == MARK_PARITY)
SCON = 0xda; //9-bit variable UART, parity bit initial to 1
#elif (PARITYBIT == SPACE_PARITY)
SCON = 0xd5; //9-bit variable UART, parity bit initial to 0
#endif
TMOD |= 0x20; //Set Timer1 as 8-bit auto reload mode
TH1=(Uchar)(256-(FOSC/(384L*BAUD)));//波特率为9600设置定时器T1的初值
TL1=(Uchar)(256-(FOSC/(384L*BAUD)));
TR1 = 1; //Timer1 start run
ES = 1; //Enable UART interrupt
//EA = 1; //Open master interrupt switch
}
void InitUart2()
{
#if (PARITYBIT == NONE_PARITY)
S2CON = 0x5a; //8位可变波特率 (无校验位)
#elif (PARITYBIT == ODD_PARITY) || (PARITYBIT == EVEN_PARITY) || (PARITYBIT == MARK_PARITY)
S2CON = 0xda; //9位可变波特率,校验位初始为1
#elif (PARITYBIT == SPACE_PARITY)
S2CON = 0xd5; //9位可变波特率,校验位初始为0
#endif
BRT = -(FOSC/32/BAUD2); //设置独立波特率发生器的重载初值
AUXR = 0x14; //独立波特率发生器工作在1T模式
IE2 = 0x01; //使能串口2中断
P43=1;
AUXR1 |= 0x10;//串口2切换到 P4.3 P4.2
}
/*----------------------------
Send a byte data to UART
Input: dat (data to be sent)
Output:None
----------------------------*/
void SendData1(uchar8 dat)
{
while (busy1); //Wait for the completion of the previous data is sent
ACC = dat; //Calculate the even parity bit P (PSW.0)
if (P) //Set the parity bit according to P
{
#if (PARITYBIT == ODD_PARITY)
TB8 = 0; //Set parity bit to 0
#elif (PARITYBIT == EVEN_PARITY)
TB8 = 1; //Set parity bit to 1
#endif
}
else
{
#if (PARITYBIT == ODD_PARITY)
TB8 = 1; //Set parity bit to 1
#elif (PARITYBIT == EVEN_PARITY)
TB8 = 0; //Set parity bit to 0
#endif
}
busy1 = 1;
SBUF = ACC; //Send data to UART buffer
}
void SendHex7(Uchar *s)//送7字节16进制数
{
char i;
for(i=0;i<7;i++)
{
SendData1(*s++);
}
}
void SendData2(char dat)
{
while (busy); //等待上个数据发送完成
ACC = dat; //取得偶校验位P
if (P)//根据P来设置串口数据的校验位
{
#if (PARITYBIT == ODD_PARITY)
S2CON &= ~S2TB8; //置校验位为0
#elif (PARITYBIT == EVEN_PARITY)
S2CON |= S2TB8; //置校验位为1
#endif
}
else
{
#if (PARITYBIT == ODD_PARITY)
S2CON |= S2TB8; //置校验位为1
#elif (PARITYBIT == EVEN_PARITY)
S2CON &= ~S2TB8; //置校验位为0
#endif
}
busy = 1;
S2BUF = ACC; //发送数据
}
void Send2Hex7(Uchar *s)//送7字节16进制数
{
char i;
for(i=0;i<8;i++)
{
SendData2(*s++);
}
}
/*----------------------------
UART1 interrupt service routine
----------------------------*/
void Uart1_Isr() interrupt 4 using 1
{
Uchar i;
if (RI)
{
RI = 0; //Clear receive interrupt flag
i=SBUF;
if(UART1Count==0)
{
if(i==0xaa)//收到有效数据包
{
UART1Buf[UART1Count]=0xaa;
UART1Count+=1;
}
LedBit =0;
}
else if(UART1Count<5)
{
UART1Buf[UART1Count]=i;
UART1Count+=1;
}
//P0 = SBUF; //P0 show UART data
//bit9 = RB8; //P2.2 show parity bit
}
if (TI)
{
TI = 0; //Clear transmit interrupt flag
busy1 = 0; //Clear transmit busy flag
}
}
void Uart2() interrupt 8 using 1
{
Uchar a;
LedBit =0;
if (S2CON & S2TI)
{
S2CON &= ~S2TI;//清除发送完成标志
a=S2BUF;
//if(UART2Count==0)
//{
if(a==0xAA)//收到有效数据包
{
UART2Buf[0]=0xAA;
UART2Count=1;
}
//}
else
{
UART2Buf[UART2Count]=a;
UART2Count+=1;
}
}
if (S2CON & S2TI)
{
S2CON &= ~S2TI; //清除发送完成标志
busy = 0;
}
}
//LedBit =0;
//zlp add
void Delay(Uint n)
{
Uint a;
for(a=0;a<n;a++)
{ _nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
/*_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();*/
}
}
这是我的程序代码,请大家帮我看看,为什么程序烧到单片机里,发送接收没反应呢?我的单片机用的是STC12C5A32S2.紧急,非常紧急!求大家帮帮我,不胜感激!!!! 单片机 数据 缓存
[解决办法]
#define BAUD 2400
#define BAUD2 19200
检查波特率
[解决办法]
示波器有没有?看看Tx脚上有没有信号输出
[解决办法]
检查波特率是指你单片机和上位机设置是否一致。