Microcontroller 51 analog IO port UART serial communication

#include <reg52.h>
#include "main.h"
#include "smartcard.h"
#include "stdio.h"

typedef enum { false, true }bool;

0 #if
the sbit PIN_RXD = Pl ^ 0; // send the same pin receiving defined
sbit PIN_TXD = P1 ^ 0; // send the same transmission pin receiving define
the smart card clock pin defines //; sbit PIN_CLK = P3 ^ 1
sbit PIN_3v5v = P3 ^ 2; // smart card 3v_5v pin definitions
sbit PIN_RST = P3 ^ 3; // smart card reset pin definitions
sbit PIN_CMDVCC = P3 ^ 4; // smart card pin CMD defined
#else
// Test Parity
the sbit PIN_RXD = P1 ^ 0; // define pin receiving
sbit PIN_TXD = P1 ^ 2; // send pin Assignments
sbit PIN_CLK = P3 ^ 5; // definition of the smart card clock pin
sbit PIN_3v5v = P3 ^ 2; // smart card pin definitions 3v_5v
sbit PIN_RST = P3 ^ 3; // smart card reset pin definitions
sbit PIN_CMDVCC = P3 ^ 4; // smart card CMD pin definitions

sbit Test_CLK = P0 ^ 3; // define pin receiving

 

#endif

bit RxdOrTxd = 0; // indicate the current status of the transmission or reception
bit RxdEnd = 0; // receiving end flag
bit TxdEnd = 0; // transmission end flag
uint8_t RxdBuf = 0; // receive buffer
uint8_t TxdBuf = 0; / / transmission buffer
void ConfigUART (Baud unsigned int);
void StartTXD (unsigned char DAT);
void StartRXD ();
void test_IO ();

// macros by nesting, including making a number of 0 to 255 contained in each number 1, which contains an even number of 1, the ParityTable256 [i] = 0, otherwise ParityTable256 [i] = 1;


static const bool ParityTable256[256] =
{
#define P2(n) n, n^1, n^1, n
#define P4(n) P2(n), P2(n^1), P2(n^1), P2(n)
#define P6(n) P4(n), P4(n^1), P4(n^1), P4(n)
P6(0), P6(1), P6(1), P6(0)
};


mainb void () {
EA =. 1; // Open main interrupt
ConfigUART (9600); // Set the baud rate is 9600
the while (. 1) {
the while (PIN_RXD); // wait to receive pin appears low, i.e., from start bit
StartRXD (); // start receiving
while (RxdEnd!); // wait for completion of reception
StartTXD (RxdBuf + 1); // after the received data + 1, sent back
while (TxdEnd!); // wait for transmission completion
}
return;
}

void SET_3v5v()
{
PIN_3v5v=1;
}
void RESET_3v5v()
{
PIN_3v5v=0;
}

void RST_SET(BitAction ResetState)
{

PIN_RST = ResetState;
}

SET_CMVCC void ()
{
PIN_CMDVCC =. 1;
}
void RESET_CMVCC ()
{
PIN_CMDVCC = 0;
}

/ * serial configuration functions, baud- communication baud rate * /
void ConfigUART (unsigned int Baud) {
the TMOD & = 0xF0; // clear T0 is the zero control bit
TMOD | = 0x02; // T0 is configured mode 2
// TH0 is = 256 - (11059200/6) / Baud; // calculate T0 reload value
TH0 = 256 - (FOSC / 12 ) / baud; // reload value T0 calculated

}
/ * start the serial reception * /
void StartRXD () {


the while (PIN_RXD);
// the printf ( "% 02X 0x PIN_RXD2 = \ R & lt \ n-", (int) PIN_RXD);
TL0 = 256 - ((256-TH0 is) >>. 1); // time of the reception start timing of T0 a half baud period
ET0 = 1; // T0 interrupt
TR0 = 1; // start T0
RxdEnd = 0; // clear reception end flag
RxdOrTxd = 0; // sets the current state to receive a

}
/ * start the serial transmission, dat- bytes of data to be transmitted * /
void StartTXD (uint8_t DAT) {
TxdBuf = DAT; // save data to be sent to the transmission buffer
TL0 = TH0; // T0 initial count value for the reload value
ET0 = 1; // T0 interrupt
TR0 = 1; // start T0
PIN_TXD = 0; // start bit is sent
TxdEnd = 0; // clear transmission end flag
RxdOrTxd = 1; // sets the current state to transmit
}

uint16_t USART_ReceiveData(USART_TypeDef* USARTx)
{
/* Check the parameters */
// assert_param(IS_USART_ALL_PERIPH(USARTx));

/* Receive Data */
#if 0
uint16_t tt= USARTx->SR ;
USARTx->SR = tt & (uint16_t)0x3df; //wuhh add ,remove later

return (uint16_t)(USARTx->DR & (uint16_t)0x01FF);
#endif
return (uint16_t) RxdBuf;

}
void USART_SendData(USART_TypeDef* USARTx, uint8_t Data)
{
StartTXD(Data);
}
FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG)
{
FlagStatus bitstatus = RESET;
if (USART_FLAG== USART_FLAG_TC)
if (TxdEnd)
{
bitstatus = SET;
}
The else
{
bitstatus the RESET =;
}

IF (USART_FLAG == USART_FLAG_RXNE)
IF (RxdEnd)
{
bitstatus = the SET;
}
the else
{
bitstatus the RESET =;
}
return bitstatus;
}
static 'bit Polarity = 0; // smart card clock output polarity

. 1 #if

/ * T0 interrupt service routine, transmit and receive serial processing * /
void InterruptTimer0 () {interrupt. 1

static unsigned char cnt = 0; // count bits received or transmitted
! Test_CLK = Test_CLK;

IF (RxdOrTxd)
{// serial transmission processing
CNT ++;
IF (CNT <=. 8) {// LSB first 8bit data bits transmitted sequentially
PIN_TXD TxdBuf & = 0x01;
TxdBuf >> =. 1;
} the else
IF (CNT == 9)
{// stop bit
PIN_TXD =. 1;
}
the else {// transmitting end
cnt = 0; // reset the bit counter
TR0 = 0; // Close T0
TxdEnd =. 1; // set the transmission end flag
}
}
the else { // serial reception process
if (cnt == 0) {// process start bit
if (PIN_RXD!) {// start bit is 0, the reception buffer is cleared, ready to receive data bits
RxdBuf = 0;
CNT ++ ;
}
when else {// start bit is 0, abort reception
TR0 = 0; // Close T0
}
} the else IF (CNT <=. 8) {// for 8 data bits
RxdBuf >> = 1; // LSB first, the received bit to the right before the
@ 1 pin is received, the highest bit buffer,
// while still maintaining i.e. not handle the shifted 0 0
IF (PIN_RXD) {
RxdBuf | = 0x80;
}
CNT ++;
} // stop bit processing the else {
cnt = 0; // reset the bit counter
TR0 = 0; // Close T0
IF (PIN_RXD) {// when the stop bit is 1, in order that the data is valid
RxdEnd = 1; // set receiving end flag
}
}
}
}

#else
/ * T0 interrupt service function, send and receive serial processing * /

void InterruptTimer0() interrupt 1{

static unsigned char cnt = 0; // count bits received or transmitted
// test_IO ();
#if 0
PIN_TXD = PIN_TXD;!
#else

IF (RxdOrTxd) {// serial transmission processing
CNT ++;
IF (CNT <=. 8) {// LSB first 8bit data bits transmitted sequentially
PIN_TXD TxdBuf & = 0x01;
TxdBuf >> =. 1;
} the else IF (CNT ==. 9) {// parity
PIN_TXD = ParityTable256 [TxdBuf];
} the else IF (CNT = = 10) {// 1 stop bit smart card protocol of two stop bits
PIN_TXD = 1;
} the else IF (CNT ==. 11) {// stop bit 2
PIN_TXD = 1;
} // send the else {end
CNT = 0; // reset the bit counter
TR0 = 0; // Close T0
TxdEnd =. 1; // set the transmission end flag
}
} // serial reception process the else {
if (cnt == 0) {// start bit processing
if (! PIN_RXD) {// start bit is 0, the reception buffer is cleared, ready to receive data bits
RxdBuf = 0;
++ CNT;
// the printf ( "InterruptTimer 0 \ n-");
} // the else {Start bit is not 0, abort reception
TR0 = 0; // Close T0
the printf ( "InterruptTimer Close \ n-");
}
}
the else if (cnt <= 8) { // for 8 data bits
RxdBuf >> = 1; // LSB first, the received bit to the right before the
@ 1 pin is received, the highest bit buffer,
// 0 and is not processed remain shifted i.e. 0
IF (PIN_RXD) {
RxdBuf | = 0x80;
}
the printf ( "% 02X 0x RxdBuf = \ R & lt \ n-", (int) RxdBuf);
CNT ++;
} else {// stop bit processing
cnt = 0; // reset the bit counter
TR0 = 0; // Close T0
IF (PIN_RXD) {// stop bit is 1, in order that the data is valid
RxdEnd = 1; // set receiving end flag
}
}
}
#endif
}
#endif

Guess you like

Origin www.cnblogs.com/wuhh123/p/11367595.html