当前位置:我的异常网» C语言 » STC串口中断有关问题,请各位老师帮帮小弟我
STC串口中断有关问题,请各位老师帮帮小弟我
www.myexceptions.net 网友分享于:2013-10-23 浏览:91次
STC串口中断问题,请各位老师帮帮我!
#include "STC_NEW_8051.H"
#include"intrins.h"
#include
#include
#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
文章评论