We learned a conditional statement, use multiple conditional statement can implement more direction conditional branch, but can be found using too many conditional statement for multi-directional branch will cause too many nested conditional statements, program long, this read is also very well read. Then use the switch statement can also be reached with the purpose of multiple branch to select, and can make the program structure is clear. Its syntax is the following:
Switch (expression)
{
Case constant-expression: statement 1 1; break;
Case constant-expression: statement 2; 2 break;
Case constant-expression: statement 3 3; break;
Case constant-expression: statement n n; break;
Default: statement
}
Operation switch followed by the value of the expression will be done, and the various constants later in case the value of the relative ratio, if they are equal, then the following statement, and then perform a break (stop statements), statements, jump out of the switch statement. If the case is not equal to the value and condition when the execution of statements after default. When a request does not comply with the conditions of does not do any processing, you can not write default statement.
In the above courses we have been using printf this standard C output functions do character output, use it, of course, be very convenient, but it's powerful, the storage space is naturally large, to around byte space 1K, if the input function is about to reach the scanf 2K bytes around, so if you require 2K storage chip can no longer use these two functions, for example AT89C2051. In these small projects, usually we only require simple character input and output, to the authors published in the radio magazine a simple serial application instances, for example, to learn to use the switch statement of use, simple to understand basic programming 51 chip serial port. This instance is the PC serial port via a PC program and by AT89C51 composition of slave-phase communications, using PC software control AT89C51 chip IO, so that you can then use the related circuit and device control (here is the control relay). In the author's website http://www.cdle.net can also view related articles. The use of hardware or back to our above courses do hardware to serial and PC connection, view the results of the experiment LED. The following is the source code. /*----------------------------------------
CDLE-J20_Main.cPC serial control IO port circuit
You can use character control and read IO
Simple version V2.0
More good microprocessor version and PC control software and the dynamic library DLL
Please visit the magnetic power Studio http://www.cdle.netcopyright 2003 http://www.cdle.net
All rights reserved.
Bill Rammell pnzwzw@163.com
pnzwzw@cdle.net
----------------------------------------*/#include <AT89X51.h>static unsigned char data CN[4];
static unsigned char data CT;
unsigned char TS[8] = {254,252,248,240,224,192,128,0};void main(void)
{
void InitCom(unsigned char BaudRate);
void ComOutChar(unsigned char OutData);
void CSToOut(void);
void CNToOut(void);
unsigned int a;
CT = 0; //Receive the sequence of characters
CN[0] = 0;
CN[1] = 51;
CN[2] = 51;
CN[3] = 0;
InitCom(6); //Set the baud rate to 9600 baud rate 1-8-300-57600
EA = 1;
ES = 1; //Open the serial port interrupt
do
{
for (a=0; a<30000; a++)
P3_6 = 1;
For (a = 0; a < 30000; a ++)//led flashing
P3_6 = 0;
}
while(1);
}
//Initialize crystal oscillator for serial 11.0592M mode 1 baud rate 300-57600
void InitCom(unsigned char BaudRate)
{
unsigned char THTL;
switch (BaudRate)
{
case 1: THTL = 64; break; //300 baud rate
case 2: THTL = 160; break; //600
case 3: THTL = 208; break; //1200
case 4: THTL = 232; break; //2400
case 5: THTL = 244; break; //4800
case 6: THTL = 250; break; //9600
case 7: THTL = 253;break; //19200
case 8: THTL = 255; break; //57600
default: THTL = 208;
}
SCON = 0x50; //Serial mode 1, allows to receive
TMOD = 0x20; //Timer 1 timing mode 2
TCON = 0x40; //Set timer 1-count
TH1 = THTL;
TL1 = THTL;
PCON = 0x80; //Baud rate double control, SMOD-
RI = 0; //Clear to send and receive flags
TI = 0;
TR1 = 1; //Start the timer
}//One character to the serial port output (non-disruptive)
void ComOutChar(unsigned char OutData)
{
SBUF = OutData; //Output characters
while(!TI); //Empty statement to determine whether a character is finished
TI = 0; //TI Qing
}//Serial receive interrupt
void ComInINT(void) interrupt 4 using 1
{
If (RI)//determine is not your finished characters
{
if (CT>3)
{
CT = 0; //End of a set of data collected, zero sequence pointer
CN[0] = 0;
CN[1] = 51;
CN[2] = 51;
CN[3] = 0;
}
CN[CT] = SBUF;
CT++;
RI = 0; //RI zeroing
If ([0] = CN = 0x61 & & CN [3] = = 0x61)//use the simple way to ensure aXXa receive reliability to meet the requirements of the amateur
{//A or plate of ID numbers, in the same serial port can be hung on a piece of plate
CSToOut(); //Receive data in the correct format, the call control output functions
}//To be more reliable work you want to use data validation and communication protocols
}
}//According to the global variable output control signal
void CSToOut(void)
{
unsigned char data a;
unsigned int data b;
Switch (CN [1]) is a format defined//aXXa first X is the port, 0 to P0, P1, 1 to 2 as P2, 3 = close all (at the 2 X 3, XX = 33)
{//XX = 44 5 for test purposes, for reads port status, is greater than 5 for invalid data,
Case 0://the first x is less than 3, the second x data to be output.
P0 = CN[2];
CNToOut();
break;
case 1:
P1 = CN[2];
CNToOut();
break;
case 2:
P2 = CN[2];
CNToOut();
break;
case 3:
P0 = 0xFF;
P1 = 0xFF;
P2 =0xFF;
CNToOut();
break;
case 4:
P0 = 0xFF;
P1 = 0xFF;
P2 = 0xFF;
for (a=0; a<8; a++)
{
P0 = TS[a];
for (b=0; b<50000; b++);
}
P0 = 0xFF;
for (a=0; a<8; a++)
{
P1 = TS[a];
for (b=0; b<50000; b++);
}
P1 = 0xFF;
for (a=0; a<4; a++)
{
P2 = TS[a];
for (b=0; b<50000; b++);
}
P2 = 0xFF;
CNToOut();
break;
Case 5://under CN [2] returns the value of the port to read
switch(CN[2])
{
case 0:
ComOutChar(CN[0]);
ComOutChar(CN[1]);
ComOutChar(P0);
ComOutChar(CN[3]);
break;
case 1:
ComOutChar(CN[0]);
ComOutChar(CN[1]);
ComOutChar(P1);
ComOutChar(CN[3]);
break;
case 2:
ComOutChar(CN[0]);
ComOutChar(CN[1]);
ComOutChar(P2);
ComOutChar(CN[3]);
break;
case 3:
ComOutChar(CN[0]);
ComOutChar(CN[1]);
ComOutChar(P3);
ComOutChar(CN[3]);
break;
}
break;
}
}void CNToOut(void)
{
ComOutChar(CN[0]);
ComOutChar(CN[1]);
ComOutChar(CN[2]);
ComOutChar(CN[3]);
}
There are several places in the code using the switch statement, use it on different conditions do different treatment, such as in CSToOut function under CN [1] to select the output to the IO ports, such as CN [1] = 0 is the CN [2] values sent to P0, CN [1] = 1 then sent to the P1, this way than using if (CN [1] == 0) such judgment statements of clarity. Of course, their effect is not much difference (without considering the compiled code execution efficiency).
In this code and its main role is through the serial port and PC software for communication, according to the host computer command string, the specified IO port for reading and writing. InitCom function, prototype to void InitCom (unsigned char BaudRate), its role is to initialize the serial port. Its input parameter is a byte, the program is to use this parameter as a switch statement to select parameters. Such as call InitCom (6), the function will set the baud rate to 9600. Of course, this code only uses a baud rate, you can use more efficient statement to write, this is not much discussed.
See here, you may be asked in a function, the TCON SCON, SCOM, etc. is TMOD represent? they are special function register, in a brief mention before, 51 chip's special features register notes can refer to Appendix II of ' special function register list AT89C51 ', here in short stated that serial port related hardware settings.
SBUF data buffering registers this is a direct addressing of serial port special registers. Have a friend that asks "why serial port transceiver, just use the same register SBUF? instead of sending and receiving the one register. "In fact SBUF contains two separate registers, one to send the deposit, the other is the receiving register, but they all share the same addressing address-99H. CPU in reading will refer to receive SBUF registers, in writing, when referring to send registers, and receive registers are double-buffered registers, so you can avoid receiving interrupt is not timely response, data is not removed, the next frame data has arrived, the resulting data duplication problem. Transmitter is not needed to double-buffer, typically we write send programs do not necessarily have to use to send a break to send data outside Ricoh. Operation of the SBUF registers is very simple, just put the 99H address keyword sfr is defined as a variable, you can read and write operations, such as sfr SBUF = 0x99; of course you can also use other names. Usually in a standard reg51.h or at89x51.h etc header file has been made to define, as long as you use the # include references.
No comments:
Post a Comment