Saturday, January 1, 2011

【 Weak current College 】 MCU c language tutorial-lesson eight: the statement (4)-switch statement 】


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. SCON serial port control registers usually chip or device to monitor or control the interface status, are references to interfaces of the control register. SCON is 51 chip serial port control register. It's addressing address is 98H, is a bit addressing the role of registers, is to monitor and control 51 chip serial port working state. 51 chip serial can work in several different operating modes, its working mode setting is use SCON registers. It's the bit of specific is defined as follows: (MSB) (LSB) SM0 SM1 SM2 REN TB8 RB8 TI RI table 8-1 serial port SCON control registers SM0, SM1-serial port mode is set, this two can To correspond to the settings of the four modes. See table 8-2 serial interface mode settings. SM0 SM1 mode function baud rate 0 0 0 synchronization shift register 12 fosc/0 1 1 UART 8-bit variable 1 0 2 UART 9-bit fosc/32 or 64 fosc/1 1 3 UART 9-bit variable table 8-2 serial interface work mode settings here only the most commonly used mode 1, mode or the other one skip, interested friends can find related information in view of the hardware. Table of the fosc on behalf of the oscillator frequency is the frequency of the oscillator. UART (Universal Asynchronous Receiver) initials. SM2 in model 2, model 3 for multi-processor communications enabling bits. In mode 0 of the bit to 0. REM to allow the receiving Member, REM Reset 1 serial port allows to receive, reset 0 prohibit receive. REM is a software reset bit or cleared. If a circuit for receiving and sending pin P3.0, P3.1 and host computer connected to the serial port on the software interrupthandler, when asked in a subroutine does not allow the serial port is PC to control characters have interrupted, you can at the beginning of the subroutine to join REM = 0 to prevent the receiving of the subroutine ends joined REM = 1 open serial port receiver again. You can also use the actual source code above to join REM = 0 to experiment. TB8 send 8 data bits, in mode 2 and 3 is to send the first 9 digits. The Member can use the software according to need set or clear, usually the communication protocol in the parity bit, multi-processor communications this bit is used to indicate the address of the frame or frames. RB8 receive 8 data bits, in mode 2 and 3 is already receive 9-bit data. This bit may be parity bit, address/identity data. In mode 0, RB8 is reserved bit is not used. In mode 1, when the SM2 = 0, RB8 is already receiving data of stop bits. TI send interrupts is identified. In mode 0, sent the first 8 bits of data, the hardware reset bit. Other mode you are in the early days of sending stop bit, bit by a hardware reset. TI reset bit, the application is interrupted, CPU response after interruption, data sent to the next frame. In any mode, TI must be cleared by the software to, that is written to the data-SBUF, hardware interrupt response sent data (such as interrupts on), TI = 1, send the completed, TI will not be cleared by the hardware, so you must use the software on their zero. RI receives the interrupt identification. In mode 0, received at the end of the first 8 bits, bit by a hardware reset. Other mode you are in receipt of an intermediate stop bits, bit by a hardware reset. RI = 1, the application is interrupted, require CPU take data. But in mode 1, SM2 = 1, when you do not receive a valid stop bit, no RI reset bit. Similarly RI must rely on software removal. Common serial transmission mode 1 is the 10-digit, 1 start bit is 0, 8 data bits, 1 low priority, 1 stop bit. It's the baud rate is variable, the rate is dependent on timer 1 or 2 the value of the scheduled timer (overflow rate). AT89C51 and AT89C2051, 51 series chip only two timer, the timer timer 0 and 1, and the timer 2 is 89C52 series chips. Baudrate in serial making communication, a very important parameter is the baud rate, only the upper and lower computer baud rate can only be carried out as normal communication. Baud refers to the serial port can be transferred per second of Porter's digits. Some beginners friends think the baud rate is per second, the number of bytes transferred, such as standard 9600 be mistaken for every seconds can transfer 9600 bytes, and the fact that it refers to the transmission 9600 per second can be a binary one byte to eight binary, such as serial mode 1 to transfer so plus start and stop bits, each data byte is about to occupy 10 binary, 9600 baud with model 1 transfer per second transmission bytes is 9600 ÷ 10 = 960 bytes. 51 chip serial mode 0 baud rate is fixed for the fosc/12 to a calculation of crystal oscillator to 12M, then it's baud rate can reach 1M. Mode 2 baud rate is fixed at 64 or fosc/fosc/32, which is used depends on the PCON registers, such as SMOD SMOD bit is 0, the baud rate of focs/64, SMOD 1, baud rate of focs/32. Mode 1 and mode 3 baud rate is variable, depending on the timer 1 or 2 (52 chip) overflow rate. So how do we go to calculate the mode of the baud rate setting the value of the relevant register? you can use the following formula to calculate. Baud rate = (2SMOD÷ 32) × timer 1 spill Out rate on-such as setting a PCON registers of SMOD bit is 1 can upgrade to 2 x the baud rate. Usually use timer 1 work on the timer operating mode 2, then timing values TL1 as count, TH1 as the automatic reload value timing mode, timer overrun, TH1 value will be automatically mounted to TL1, start counting again, this can be software to intervene made more accurate timing. In the timedmode 2 down timer 1 overflow rate formula as follows: overflow rate = (count rate)/ (256-TH1) on in "count rate" and the use of crystal oscillator frequency, at 51 chip timer start will be in every machine cycle so that the timer registers values increase a TH, one machine cycle is equal to twelve oscillation cycle, so you can tell the 51 chip count rate for crystal oscillator frequency of 1/12, one of the oscillator used in 12M 51 chip, so the count rate is 51 to 1M. Usually 11.0592M crystals to get standard baud rate without error, why? calculations. If we want to get 9600 baud rate, crystal oscillator for 11.0592M and 12M, timer 1 to mode 2, SMOD is set to 1, take a look at the requirements of the value of the TH1. Substitution of a 11.0592M formula: 9600 = (2 ÷ 32) × ((11.0592M/12)/(256-TH1)) TH1 = 250//see if the above instance using the value like? 12M 9600 = (2 ÷ 32) × ((12M/12)/(256-TH1)) TH1 ≈ 249.49 above calculation can be seen when using 12M Crystal calculated TH1 is not an integer, the value of the TH1 can only take integers, so that it is error exists cannot produce accurate 9600 baud rate. Of course certain errors that can be in use is acceptable, even if the use of crystal oscillator 11.0592M will also be the result of Crystal itself so that the baud rate of error error, but the lens itself, the effect of errors on the baud rate is very small and can be ignored. This section through learning opportunities, the switch statement briefly describes some related content is a serial, but serial works by setting a good seed but also to involve other relevant registers, content is very much, this is not done examples, the following chapter will join some hardware.

No comments:

Post a Comment