我想改下程序,让外部按键触发,之后串口发送0x11到上位机上
我想改下程序,让外部按键触发,之后串口发送0x11到上位机上
按钮是AN,
L1 L2 L3 都是LED灯
#include "reg51.h"
//define baudrate const
//BAUD = 256 - SYSclk/3/BAUDRATE/M (1T:M=1; 12T:M=12)
//NOTE: (SYSclk/3/BAUDRATE) must be greater than 98, (RECOMMEND GREATER THAN 110)
//#define BAUD 0xF400 // 1200bps @ 11.0592MHz
//#define BAUD 0xFA00 // 2400bps @ 11.0592MHz
//#define BAUD 0xFD00 // 4800bps @ 11.0592MHz
#define BAUD 0xFE80 // 9600bps @ 11.0592MHz
//#define BAUD 0xFF40 //19200bps @ 11.0592MHz
//#define BAUD 0xFFA0 //38400bps @ 11.0592MHz
//#define BAUD 0xEC00 // 1200bps @ 18.432MHz
//#define BAUD 0xF600 // 2400bps @ 18.432MHz
//#define BAUD 0xFB00 // 4800bps @ 18.432MHz
//#define BAUD 0xFD80 // 9600bps @ 18.432MHz
//#define BAUD 0xFEC0 //19200bps @ 18.432MHz
//#define BAUD 0xFF60 //38400bps @ 18.432MHz
//#define BAUD 0xE800 // 1200bps @ 22.1184MHz
//#define BAUD 0xF400 // 2400bps @ 22.1184MHz
//#define BAUD 0xFA00 // 4800bps @ 22.1184MHz
//#define BAUD 0xFD00 // 9600bps @ 22.1184MHz
//#define BAUD 0xFE80 //19200bps @ 22.1184MHz
//#define BAUD 0xFF40 //38400bps @ 22.1184MHz
//#define BAUD 0xFF80 //57600bps @ 22.1184MHz
sfr AUXR = 0x8E;
sbit RXB = P3^0; //define UART TX/RX port
sbit TXB = P3^1;
sbit L1 = P3^2;
sbit L2 = P3^3;
sbit L3 = P3^4;
sbit AN = P3^5;
typedef bit BOOL;
typedef unsigned char BYTE;
typedef unsigned int WORD;
BYTE TBUF,RBUF;
BYTE TDAT,RDAT;
BYTE TCNT,RCNT;
BYTE TBIT,RBIT;
BOOL TING,RING;
BOOL TEND,REND;
void UART_INIT();
BYTE t, r;
BYTE buf[16];
void main()
{
TMOD = 0x00; //timer0 in 16-bit auto reload mode
AUXR = 0x80; //timer0 working at 1T mode
TL0 = BAUD;
TH0 = BAUD>>8; //initial timer0 and set reload value
TR0 = 1; //tiemr0 start running
ET0 = 1; //enable timer0 interrupt
PT0 = 1; //improve timer0 interrupt priority
EA = 1; //open global interrupt switch
UART_INIT();
while (1)
{ //user's function
if (REND)
{
REND = 0;
buf[r++ & 0x0f] = RBUF;
}
if (TEND)
{
if (t != r)
{
TEND = 0;
TBUF = buf[t++ & 0x0f];
TING = 1;
}
}
}
}
//-----------------------------------------//Timer interrupt routine for UART
void tm0() interrupt 1 using 1
{
if (RING)
{
if (--RCNT == 0)
{
RCNT = 3; //reset send baudrate counter
if (--RBIT == 0)
{
RBUF = RDAT; //save the data to RBUF
RING = 0; //stop receive
REND = 1; //set receive completed flag
}
else
{
RDAT >>= 1;
if (RXB) RDAT |= 0x80; //shift RX data to RX buffer
}
}
}
else if (!RXB)
{
RING = 1; //set start receive flag
RCNT = 4; //initial receive baudrate counter
RBIT = 9; //initial receive bit number (8 data bits + 1stop bit)
}
if (--TCNT == 0)
{
TCNT = 3; //reset send baudrate counter
if (TING) //judge whether sending
{
if (TBIT == 0) //
{
TXB = 0; //send start bit
TDAT = TBUF; //load data from TBUF to TDAT
if (TBUF==0x11)
{
L1=0;
}
if (TBUF==0x22)
{
L1=1;
}
TBIT = 9; //initial send bit number (8 data bits + 1stop bit)
}
else
{
TDAT >>= 1; //shift data to CY
if (--TBIT == 0) //
{
TXB = 1;
TING = 0; //stop send
TEND = 1; //set send completed flag
}
else
{
TXB = CY; //write CY to TX port
//TXB = 0xF2;
}//
} //
}
}
}
//-----------------------------------------//initial UART module variable
void UART_INIT()
{
TING = 0;
RING = 0;
TEND = 1;
REND = 0;
TCNT = 0;
RCNT = 0;
} STC?15?系列单片机 STC?15 stc15F104W 串口 下位机
[解决办法]
本帖最后由 wangfan027 于 2013-09-16 23:28:08 编辑 这程序肯定不是楼主写的,
先排下版,晚上回去再看
#include "reg51.h"
//define baudrate const
//BAUD = 256 - SYSclk/3/BAUDRATE/M (1T:M=1; 12T:M=12)
//NOTE: (SYSclk/3/BAUDRATE) must be greater than 98, (RECOMMEND GREATER THAN 110)
//#define BAUD 0xF400 // 1200bps @ 11.0592MHz
//#define BAUD 0xFA00 // 2400bps @ 11.0592MHz
//#define BAUD 0xFD00 // 4800bps @ 11.0592MHz
#define BAUD 0xFE80 // 9600bps @ 11.0592MHz
//#define BAUD 0xFF40 //19200bps @ 11.0592MHz
//#define BAUD 0xFFA0 //38400bps @ 11.0592MHz
//#define BAUD 0xEC00 // 1200bps @ 18.432MHz
//#define BAUD 0xF600 // 2400bps @ 18.432MHz
//#define BAUD 0xFB00 // 4800bps @ 18.432MHz
//#define BAUD 0xFD80 // 9600bps @ 18.432MHz
//#define BAUD 0xFEC0 //19200bps @ 18.432MHz
//#define BAUD 0xFF60 //38400bps @ 18.432MHz
//#define BAUD 0xE800 // 1200bps @ 22.1184MHz
//#define BAUD 0xF400 // 2400bps @ 22.1184MHz
//#define BAUD 0xFA00 // 4800bps @ 22.1184MHz
//#define BAUD 0xFD00 // 9600bps @ 22.1184MHz
//#define BAUD 0xFE80 //19200bps @ 22.1184MHz
//#define BAUD 0xFF40 //38400bps @ 22.1184MHz
//#define BAUD 0xFF80 //57600bps @ 22.1184MHz
sfr AUXR = 0x8E;
sbit RXB = P3^0; //define UART TX/RX port
sbit TXB = P3^1;
sbit L1 = P3^2;
sbit L2 = P3^3;
sbit L3 = P3^4;
sbit AN = P3^5;
typedef bit BOOL;
typedef unsigned char BYTE;
typedef unsigned int WORD;
BYTE TBUF,RBUF;
BYTE TDAT,RDAT;
BYTE TCNT,RCNT;
BYTE TBIT,RBIT;
BOOL TING,RING;
BOOL TEND,REND;
void UART_INIT();
BYTE t, r;
BYTE buf[16];
void main()
{
TMOD = 0x00; //timer0 in 16-bit auto reload mode
AUXR = 0x80; //timer0 working at 1T mode
TL0 = BAUD;
TH0 = BAUD>>8; //initial timer0 and set reload value
TR0 = 1; //tiemr0 start running
ET0 = 1; //enable timer0 interrupt
PT0 = 1; //improve timer0 interrupt priority
EA = 1; //open global interrupt switch
UART_INIT();
while (1)
{ //user's function
if (REND)
{
REND = 0;
buf[r++ & 0x0f] = RBUF;
}
if (TEND)
{
if (t != r)
{
TEND = 0;
TBUF = buf[t++ & 0x0f];
TING = 1;
}
}
}
}
//-----------------------------------------//Timer interrupt routine for UART
void tm0() interrupt 1 using 1
{
if (RING)
{
if (--RCNT == 0)
{
RCNT = 3; //reset send baudrate counter
if (--RBIT == 0)
{
RBUF = RDAT; //save the data to RBUF
RING = 0; //stop receive
REND = 1; //set receive completed flag
}
else
{
RDAT >>= 1;
if (RXB) RDAT
[解决办法]
= 0x80; //shift RX data to RX buffer
}
}
}
else if (!RXB)
{
RING = 1; //set start receive flag
RCNT = 4; //initial receive baudrate counter
RBIT = 9; //initial receive bit number (8 data bits + 1stop bit)
}
if (--TCNT == 0)
{
TCNT = 3; //reset send baudrate counter
if (TING) //judge whether sending
{
if (TBIT == 0) //
{
TXB = 0; //send start bit
TDAT = TBUF; //load data from TBUF to TDAT
if (TBUF==0x11)
{
L1=0;
}
if (TBUF==0x22)
{
L1=1;
}
TBIT = 9; //initial send bit number (8 data bits + 1stop bit)
}
else
{
TDAT >>= 1; //shift data to CY
if (--TBIT == 0) //
{
TXB = 1;
TING = 0; //stop send
TEND = 1; //set send completed flag
}
else
{
TXB = CY; //write CY to TX port
//TXB = 0xF2;
} //
} //
}
}
}
//-----------------------------------------//initial UART module variable
void UART_INIT()
{
TING = 0;
RING = 0;
TEND = 1;
REND = 0;
TCNT = 0;
RCNT = 0;
}
#include "reg51.h"
//define baudrate const
//BAUD = 256 - SYSclk/3/BAUDRATE/M (1T:M=1; 12T:M=12)
//NOTE: (SYSclk/3/BAUDRATE) must be greater than 98, (RECOMMEND GREATER THAN 110)
//#define BAUD 0xF400 // 1200bps @ 11.0592MHz
//#define BAUD 0xFA00 // 2400bps @ 11.0592MHz
//#define BAUD 0xFD00 // 4800bps @ 11.0592MHz
#define BAUD 0xFE80 // 9600bps @ 11.0592MHz
//#define BAUD 0xFF40 //19200bps @ 11.0592MHz
//#define BAUD 0xFFA0 //38400bps @ 11.0592MHz
//#define BAUD 0xEC00 // 1200bps @ 18.432MHz
//#define BAUD 0xF600 // 2400bps @ 18.432MHz
//#define BAUD 0xFB00 // 4800bps @ 18.432MHz
//#define BAUD 0xFD80 // 9600bps @ 18.432MHz
//#define BAUD 0xFEC0 //19200bps @ 18.432MHz
//#define BAUD 0xFF60 //38400bps @ 18.432MHz
//#define BAUD 0xE800 // 1200bps @ 22.1184MHz
//#define BAUD 0xF400 // 2400bps @ 22.1184MHz
//#define BAUD 0xFA00 // 4800bps @ 22.1184MHz
//#define BAUD 0xFD00 // 9600bps @ 22.1184MHz
//#define BAUD 0xFE80 //19200bps @ 22.1184MHz
//#define BAUD 0xFF40 //38400bps @ 22.1184MHz
//#define BAUD 0xFF80 //57600bps @ 22.1184MHz
sfr AUXR = 0x8E;
sbit RXB = P3^0; //define UART TX/RX port
sbit TXB = P3^1;
sbit L1 = P3^2;
sbit L2 = P3^3;
sbit L3 = P3^4;
sbit AN = P3^5;
bit NEEDTX=0; //!here
typedef bit BOOL;
typedef unsigned char BYTE;
typedef unsigned int WORD;
BYTE TBUF,RBUF;
BYTE TDAT,RDAT;
BYTE TCNT,RCNT;
BYTE TBIT,RBIT;
BOOL TING,RING;
BOOL TEND,REND;
void UART_INIT();
BYTE t, r;
BYTE buf[16];
void main()
{
TMOD = 0x00; //timer0 in 16-bit auto reload mode
AUXR = 0x80; //timer0 working at 1T mode
TL0 = BAUD;
TH0 = BAUD>>8; //initial timer0 and set reload value
TR0 = 1; //tiemr0 start running
ET0 = 1; //enable timer0 interrupt
PT0 = 1; //improve timer0 interrupt priority
EX0=1; //下降沿中断还是低电平中断?
IE
[解决办法]
=0X84;//开总中断和外部中断
EA = 1; //open global interrupt switch
UART_INIT();
while (1)
{ //user's function
if (REND)
{
REND = 0;
buf[r++ & 0x0f] = RBUF;
}
if (NEEDTX)
{
NEEDTX=0;
if (t != r)
{
TEND = 0;
TBUF = buf[t++ & 0x0f];
TING = 1;
}
}
}
}
//-----------------------------------------//Timer interrupt routine for UART
void tm0() interrupt 1 using 1
{
if (RING)
{
if (--RCNT == 0)
{
RCNT = 3; //reset send baudrate counter
if (--RBIT == 0)
{
RBUF = RDAT; //save the data to RBUF
RING = 0; //stop receive
REND = 1; //set receive completed flag
}
else
{
RDAT >>= 1;
if (RXB) RDAT
[解决办法]
= 0x80; //shift RX data to RX buffer
}
}
}
else if (!RXB)
{
RING = 1; //set start receive flag
RCNT = 4; //initial receive baudrate counter
RBIT = 9; //initial receive bit number (8 data bits + 1stop bit)
}
if (--TCNT == 0)
{
TCNT = 3; //reset send baudrate counter
if (TING) //judge whether sending
{
if (TBIT == 0) //
{
TXB = 0; //send start bit
TDAT = TBUF; //load data from TBUF to TDAT
if (TBUF==0x11)
{
L1=0;
}
if (TBUF==0x22)
{
L1=1;
}
TBIT = 9; //initial send bit number (8 data bits + 1stop bit)
}
else
{
TDAT >>= 1; //shift data to CY
if (--TBIT == 0) //
{
TXB = 1;
TING = 0; //stop send
TEND = 1; //set send completed flag
}
else
{
TXB = CY; //write CY to TX port
//TXB = 0xF2;
} //
} //
}
}
}
//-----------------------------------------//initial UART module variable
void UART_INIT()
{
TING = 0;
RING = 0;
TEND = 1;
REND = 0;
TCNT = 0;
RCNT = 0;
}
void EX0() interrupt 0 //使用了外部中断0
{
NEEDTX=1;
}