{"id":212,"date":"2018-07-27T04:47:12","date_gmt":"2018-07-27T04:47:12","guid":{"rendered":"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=212"},"modified":"2018-07-27T04:53:35","modified_gmt":"2018-07-27T04:53:35","slug":"212","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/chapter\/212\/","title":{"rendered":"Arithmetic and logical processing- Embedded C"},"content":{"raw":"<div>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In this lecture, we will discuss about the Logical operators in Embedded C. Data conversion programs in embedded C will also be written and discussed. At the end Data Serialization in 8051 using embedded C will be discussed.<\/p>\r\n&nbsp;\r\n\r\n<strong>1.1 Bitwise Operations in C<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">One of the most important and powerful features of the C language is its ability to perform bit wise manipulation. This section describes the action of bitwise logic operators. Figure1.1 shows the bitwise logical operators.<\/p>\r\n\r\n<\/div>\r\n<img class=\"size-full wp-image-215 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-120.png\" alt=\"\" width=\"604\" height=\"236\" \/>\r\n<div>\r\n<p style=\"text-align: justify\">Using these bit wise operators Embedded c will perform the logical operations bit wise on binary numbers. Following are some examples which show how they are used.<\/p>\r\n\r\n<\/div>\r\n<img class=\"size-full wp-image-217 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-121.png\" alt=\"\" width=\"590\" height=\"309\" \/>\r\n<div>\r\n\r\n<strong>1.2 Bit-wise Shift Operation in C<\/strong>\r\n\r\n&nbsp;\r\n\r\nThere are two bit-wise shift operators in C:\r\n\r\n&nbsp;\r\n\r\n(1) shift right ( \u00bb), and\r\n\r\n&nbsp;\r\n\r\n(2)\u00a0 shift left (\u00ab).\r\n\r\n&nbsp;\r\n\r\nTheir format in C is as follows:\r\n\r\n&nbsp;\r\n\r\ndata \u00bb number of bits to be shifted right\r\n\r\n&nbsp;\r\n\r\ndata \u00ab number of bits to be shifted left\r\n\r\n&nbsp;\r\n\r\nSome examples are shown below:\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-218 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-122.png\" alt=\"\" width=\"634\" height=\"312\" \/>\r\n<p style=\"text-align: justify\">Following program will show the demo of the logical operations. Run the following program on your simulator and examine the results.<\/p>\r\n&nbsp;\r\n\r\n#include &lt;reg51.h&gt;\r\n\r\n&nbsp;\r\n\r\nvoid main(void)\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\n&nbsp;\r\n\r\nP0=0x35 &amp; 0x0F; \/\/ANDing\r\n\r\n&nbsp;\r\n\r\nP1=0x04 | 0x68; \/\/ORing\r\n\r\n&nbsp;\r\n\r\nP2=0x54 ^ 0x78; \/\/XORing\r\n\r\n&nbsp;\r\n\r\nP0=~0x55; \/\/inverting\r\n\r\n&nbsp;\r\n\r\nP1=0x9A &gt;&gt; 3; \/\/shifting right 3\r\n\r\n&nbsp;\r\n\r\nP2=0x77 &gt;&gt; 4; \/\/shifting right 4\r\n\r\n&nbsp;\r\n\r\nP0=0x6 &lt;&lt; 4; \/\/shifting left 4\r\n\r\n&nbsp;\r\n\r\n}\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Next program will show the operation on port pins to access data bit-wise and manipulate through logical operator.<\/p>\r\n&nbsp;\r\n\r\n<em>1.2.1 Write an 8051 C program to get bit P1.0 and send it to P2.7 after inverting it.<\/em>\r\n\r\n&nbsp;\r\n\r\nSolution:\r\n\r\n<\/div>\r\n<div>\r\n\r\n#include &lt;reg51.h&gt;\r\n\r\n&nbsp;\r\n\r\nsbit inbit=P1^0;\r\n\r\n&nbsp;\r\n\r\nsbit outbit=P2^7;\r\n\r\n&nbsp;\r\n\r\nbit membit;\r\n\r\n&nbsp;\r\n\r\nvoid main(void)\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\n&nbsp;\r\n\r\nwhile (1)\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\n&nbsp;\r\n\r\nmembit=inbit; \/\/get a bit from P1.0\r\n\r\n&nbsp;\r\n\r\noutbit=~membit; \/\/invert it and send\r\n\r\n&nbsp;\r\n\r\n\/\/it to P2.7\r\n\r\n&nbsp;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\n<strong>1.3 Data Conversion<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">We have seen BCD numbers in previous modules. As stated there, many newer microcontrollers have a real-time clock (RTC) where the time and date are kept, even when the power is off. Very often the RTC provides the time and date in packed BCD. However, to display them they must be converted to ASCII. Like this example, we need to convert the data from one form to another form. In this section we show some example programs to demo how Embedded C helps for data conversions. Some data conversions needed are listed below.<\/p>\r\n&nbsp;\r\n\r\n\u2022\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Packed BCD to ASCII conversion\r\n\r\n&nbsp;\r\n\r\n\u2022\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ASCII to packed BCD conversion\r\n\r\n&nbsp;\r\n\r\n\u2022\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Checksum byte in ROM\r\n\r\n&nbsp;\r\n\r\n\u2022\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Binary to decimal and ASCII conversion in C\r\n\r\n&nbsp;\r\n\r\nFigure.2 shows the ASCII, binary and BCD codes for the digits 0 to 9.\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-219 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-123.png\" alt=\"\" width=\"707\" height=\"262\" \/>\r\n\r\nFollowing program\u00a0 shows Packed BCD to ASCII conversion<strong>.<\/strong>\r\n\r\n&nbsp;\r\n\r\n<em>1.3.1 Example.1<\/em>\r\n\r\n<\/div>\r\n<div>\r\n\r\nWrite an 8051 C program to convert packed BCD 0x29 to ASCII and display the bytes on P1 and P2.\r\n\r\n&nbsp;\r\n\r\nSolution:\r\n\r\n&nbsp;\r\n\r\n#include &lt;reg51.h&gt;\r\n\r\n&nbsp;\r\n\r\nvoid main(void)\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\n&nbsp;\r\n\r\nunsigned char x,y,z;\r\n\r\n&nbsp;\r\n\r\nunsigned char mybyte=0x29;\r\n\r\n&nbsp;\r\n\r\nx=mybyte&amp;0x0F;\r\n\r\n&nbsp;\r\n\r\nP1=x|0x30;\r\n\r\n&nbsp;\r\n\r\ny=mybyte&amp;0xF0;\r\n\r\n&nbsp;\r\n\r\ny=y&gt;&gt;4;\r\n\r\n&nbsp;\r\n\r\nP2=y|0x30;\r\n\r\n&nbsp;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nFollowing program\u00a0 shows the ASCII to packed BCD conversion.\r\n\r\n&nbsp;\r\n\r\n<em>1.3.2 Example.2<\/em>\r\n\r\n&nbsp;\r\n\r\nWrite an 8051 C program to convert ASCII digits of \u20184\u2019 and \u20187\u2019 to packed BCD and display them on P1.\r\n\r\n&nbsp;\r\n\r\nSolution:\r\n\r\n&nbsp;\r\n\r\n#include &lt;reg51.h&gt;\r\n\r\n&nbsp;\r\n\r\nvoid main(void)\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\n&nbsp;\r\n\r\nunsigned char bcdbyte;\r\n\r\n&nbsp;\r\n\r\nunsigned char w=\u20184\u2019;\r\n\r\n&nbsp;\r\n\r\nunsigned char z=\u20187\u2019;\r\n\r\n&nbsp;\r\n\r\nw=w&amp;0x0F;\r\n\r\n&nbsp;\r\n\r\nw=w&lt;&lt;4;\r\n\r\n&nbsp;\r\n\r\nz=z&amp;0x0F;\r\n\r\n&nbsp;\r\n\r\nbcdbyte=w|z;\r\n\r\n&nbsp;\r\n\r\nP1=bcdbyte;\r\n\r\n&nbsp;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nFollowing program\u00a0 is for finding the Checksum byte in ROM.\r\n\r\n&nbsp;\r\n\r\n<em>1.3.3 Example.3<\/em>\r\n\r\n&nbsp;\r\n\r\nWrite an 8051 C program to calculate the checksum byte for the data 25H, 62H, 3FH, and 52H.\r\n\r\n&nbsp;\r\n\r\nSteps\u00a0 to calculate Checksum byte in ROM are:\r\n\r\n&nbsp;\r\n\r\n1.\u00a0\u00a0\u00a0 Add the bytes together and drop carries.\r\n\r\n&nbsp;\r\n\r\n2.\u00a0\u00a0\u00a0 Take the 2\u2019s complement (invert and then add 1) of the total sum. This is the\r\n\r\n&nbsp;\r\n\r\nChecksum byte, which becomes the last byte of the series.\r\n\r\n&nbsp;\r\n\r\nSolution:\r\n\r\n&nbsp;\r\n\r\n#include &lt;reg51.h&gt;\r\n\r\n&nbsp;\r\n\r\nvoid main(void)\r\n\r\n<\/div>\r\n&nbsp;\r\n<div>\r\n\r\n{\r\n\r\n&nbsp;\r\n\r\nunsigned char mydata[]={0x25,0x62,0x3F,0x52};\r\n\r\n&nbsp;\r\n\r\nunsigned char sum=0;\r\n\r\n&nbsp;\r\n\r\nunsigned char x;\r\n\r\n&nbsp;\r\n\r\nunsigned char chksumbyte;\r\n\r\n&nbsp;\r\n\r\nfor (x=0;x&lt;4;x++)\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\n&nbsp;\r\n\r\nP2=mydata[x];\r\n\r\n&nbsp;\r\n\r\nsum=sum+mydata[x];\r\n\r\n&nbsp;\r\n\r\nP1=sum;\r\n\r\n&nbsp;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nchksumbyte=~sum+1;\u00a0 \/\/logical operator used here\r\n\r\n&nbsp;\r\n\r\nP1=chksumbyte;\r\n\r\n&nbsp;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nFollowing program\u00a0 will demo Binary to Decimal and ASCII Conversion in 8051 C\r\n\r\n&nbsp;\r\n\r\n<em>1.3.4 Example.4<\/em>\r\n\r\n&nbsp;\r\n\r\nWrite an 8051 C program to convert 11111101 (FD hex) to decimal and display the digits on P0, P1 and P2.\r\n\r\n&nbsp;\r\n\r\nSolution:\r\n\r\n&nbsp;\r\n\r\n#include &lt;reg51.h&gt;\r\n\r\n&nbsp;\r\n\r\nvoid main(void)\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\n&nbsp;\r\n\r\nunsigned char x,binbyte,d1,d2,d3;\r\n\r\n&nbsp;\r\n\r\nbinbyte=0xFD;\r\n\r\n&nbsp;\r\n\r\nx=binbyte\/10;\r\n\r\n&nbsp;\r\n\r\nd1=binbyte%10;\r\n\r\n&nbsp;\r\n\r\nd2=x%10;\r\n\r\n&nbsp;\r\n\r\nd3=x\/10;\r\n\r\n&nbsp;\r\n\r\nP0=d1;\r\n\r\n&nbsp;\r\n\r\nP1=d2;\r\n\r\n&nbsp;\r\n\r\nP2=d3;\r\n\r\n&nbsp;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\n<strong>1.4 Accessing Code ROM space in 8051 C<\/strong>\r\n\r\n&nbsp;\r\n\r\nUsing the code (program) space for predefined data is a widely used option in 8051. We saw how to use the Assembly language instruction MOVC to access the data stored in the 8051 code space. Here, we see the same concept with 8051 C.\r\n\r\n&nbsp;\r\n\r\nIn 8051, we have three spaces to store data:\r\n\r\n&nbsp;\r\n\r\n1.\u00a0 The 128 bytes RAM space with address range 00-7FH\r\n\r\n&nbsp;\r\n\r\n\u2713\u00a0\u00a0\u00a0\u00a0 If you declare variables (eg.: char) to store data, C compiler will allocate a RAM space for these variable.\r\n\r\n&nbsp;\r\n\r\n2.\u00a0 User code space\r\n\r\n&nbsp;\r\n\r\n\u2713\u00a0\u00a0\u00a0\u00a0 External code memory (64K) + on-chip ROM (64K)\r\n\r\n<\/div>\r\n&nbsp;\r\n<div>\r\n\r\n\u2713\u00a0\u00a0\u00a0\u00a0 Data is embedded to code or is separated as a data section.\r\n\r\n&nbsp;\r\n\r\n3.\u00a0 External data memory for data\r\n\r\n&nbsp;\r\n\r\n\u2713\u00a0\u00a0\u00a0\u00a0 RAM or ROM is used.\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<strong>2.1 RAM data space usage<\/strong>\r\n\r\n&nbsp;\r\n\r\nThe 8051 C compiler allocates RAM locations as follows:\r\n\r\n&nbsp;\r\n\r\n1.\u00a0 Bank 0 \u2013 addresses 0 \u2013 7\r\n\r\n&nbsp;\r\n\r\n2.\u00a0 Individual variables \u2013 addresses 08 and beyond\r\n\r\n&nbsp;\r\n\r\n3.\u00a0 Array elements \u2013 addresses right after variables\r\n\r\n&nbsp;\r\n\r\n\u2713\u00a0\u00a0\u00a0\u00a0 Array elements need contiguous RAM locations and that limits the size of the array due to the fact that we have only 128 bytes of RAM for everything.\r\n\r\n&nbsp;\r\n\r\n4.\u00a0 Stack \u2013 addresses right after array elements.\r\n\r\n&nbsp;\r\n\r\nFollowing is an example program to access data from memory.\r\n\r\n&nbsp;\r\n\r\n<em>2.1.1 Write, compile and single-step the following program on your 8051 simulator. Examine the contents of the code space to locate the values.<\/em>\r\n\r\n&nbsp;\r\n\r\n#include &lt;reg51.h&gt;\r\n\r\n&nbsp;\r\n\r\nvoid main(void)\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\n&nbsp;\r\n\r\nunsigned char mydata[100]; \/\/RAM space\r\n\r\n&nbsp;\r\n\r\nunsigned char x,z=0;\r\n\r\n&nbsp;\r\n\r\nfor (x=0;x&lt;100;x++)\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\n&nbsp;\r\n\r\nz--;\r\n\r\n&nbsp;\r\n\r\nmydata[x]=z;\r\n\r\n&nbsp;\r\n\r\nP1=z;\r\n\r\n&nbsp;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nWhile running this program you can see how the data will be stored in an array and displayed in a port.\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<strong>2.2 8052 RAM Space<\/strong>\r\n\r\n&nbsp;\r\n\r\nOne of the new features of the 8052 was an extra 128 bytes of RAM space.\r\n\r\n&nbsp;\r\n\r\n\u2713 The extra 128 bytes of RAM helps the 8051\/52 C compiler to manage its registers and resources much more effectively.\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">\u2713 Based on 8052 architecture, you should use the reg52.h header file. Choose the 8052 option when compiling the program.<\/p>\r\n&nbsp;\r\n\r\n<em>2.2.1 Using ROM to Store Data<\/em>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">To make C compiler use the code space (on-chip ROM) instead of RAM space, we can put the keyword \u201ccode\u201d in front of the variable declaration.<\/p>\r\n\r\n<\/div>\r\n&nbsp;\r\n<div>\r\n\r\nunsigned char mydata[] = \u201cHELLO\u201d\r\n\r\n&nbsp;\r\n\r\n\u2713 HELLO is saved in RAM.\r\n\r\n&nbsp;\r\n\r\ncode unsigned char mydata[] = \u201cHELLO\u201d\r\n\r\n&nbsp;\r\n\r\n\u2713 HELLO is saved in ROM.\r\n\r\n&nbsp;\r\n\r\nThis is discussed in the following examples.\r\n\r\n&nbsp;\r\n\r\n<em>2.2.2 Examples 5<\/em>\r\n\r\n&nbsp;\r\n\r\nLet us compare and contrast the following programs and discuss the advantages and disadvantages of each one.\r\n\r\n&nbsp;\r\n\r\n<strong>Example 5(a):<\/strong>\r\n\r\n&nbsp;\r\n\r\n#include &lt;reg51.h&gt;\r\n\r\nvoid main(void)\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\n&nbsp;\r\n\r\nP1=\u201cH\u201d;\r\n\r\n&nbsp;\r\n\r\nP1=\u201cE\u201d;\r\n\r\n&nbsp;\r\n\r\nP1=\u201cL\u201d;\r\n\r\n&nbsp;\r\n\r\nP1=\u201cL\u201d;\r\n\r\n&nbsp;\r\n\r\nP1=\u201cO\u201d;\r\n\r\n&nbsp;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nData is embedded into code. Simple, short, not flexible.\r\n\r\n&nbsp;\r\n\r\n<strong>Example 5(b):<\/strong>\r\n\r\n&nbsp;\r\n\r\n#include &lt;reg51.h&gt;\r\n\r\n&nbsp;\r\n\r\nvoid main(void)\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\n&nbsp;\r\n\r\nunsigned char mydata[]=\u201cHELLO\u201d;\r\n\r\n&nbsp;\r\n\r\nunsigned char z;\r\n\r\n&nbsp;\r\n\r\nfor (z=0; z&lt;5; z++)\r\n\r\n&nbsp;\r\n\r\nP1 = mydata[z];\r\n\r\n&nbsp;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nData is stored in RAM and does not occupy ROM.\r\n\r\n&nbsp;\r\n\r\n<strong>Example 5(c):<\/strong>\r\n\r\n&nbsp;\r\n\r\n#include &lt;reg51.h&gt;\r\n\r\n&nbsp;\r\n\r\nvoid main(void)\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\n&nbsp;\r\n\r\nCode unsigned char mydata[]=\u201cHELLO\u201d;\r\n\r\n&nbsp;\r\n\r\nunsigned char z;\r\n\r\n&nbsp;\r\n\r\nfor (z=0; z&lt;5; z++)\r\n\r\n&nbsp;\r\n\r\nP1 = mydata[z];\r\n\r\n&nbsp;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nData is stored in ROM. However, data and code are separate.\r\n\r\n&nbsp;\r\n\r\n<strong>2.3 Data serialization using 8051 C<\/strong>\r\n\r\n<\/div>\r\n<strong>\u00a0<\/strong>\r\n<div>\r\n\r\n&nbsp;\r\n\r\nSerializing data is a way of sending a byte of data one bit at a time through a single pin of microcontroller.\r\n\r\n&nbsp;\r\n\r\n\u2713 Using the serial port (discussed in Serial Communication module)\r\n\r\n&nbsp;\r\n\r\n\u2713 Transfer data one bit a time and control the sequence of data and spaces in between them.\r\n\r\n&nbsp;\r\n\r\nIn many new generations of devices such as LCD, ADC, and ROM, the serial versions are becoming popular since they take less space on a PCB.\r\n\r\n&nbsp;\r\n\r\nThe following are examples that demonstrate data serialization using embedded C. <strong>Example. 6<\/strong>\r\n\r\n&nbsp;\r\n\r\nWrite a C program to send out the value 44H serially one bit at a time via P1.0. The LSB should go out first.\r\n\r\n&nbsp;\r\n\r\nSolution :\r\n\r\n&nbsp;\r\n\r\n#include &lt;reg51.h&gt;\r\n\r\n&nbsp;\r\n\r\nsbit P1b0=P1^0;\r\n\r\n&nbsp;\r\n\r\nsbit regALSB=ACC^0;\r\n\r\n&nbsp;\r\n\r\nvoid main(void)\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\n&nbsp;\r\n\r\nunsigned char conbyte=0x44;\r\n\r\n&nbsp;\r\n\r\nunsigned char x;\r\n\r\n&nbsp;\r\n\r\nACC=conbyte;\r\n\r\n&nbsp;\r\n\r\nfor (x=0;x&lt;8;x++)\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\n&nbsp;\r\n\r\nP1b0=regALSB;\r\n\r\n&nbsp;\r\n\r\nACC=ACC&gt;&gt;1;\r\n\r\n&nbsp;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<em>2.3.1 Write a C program to bring in a byte of data serially one bit at a time via P1.0. The MSB should come in first.<\/em>\r\n\r\n&nbsp;\r\n\r\n<strong>Example.7<\/strong>\r\n\r\n&nbsp;\r\n\r\n#include &lt;reg51.h&gt;\r\n\r\n&nbsp;\r\n\r\nsbit P1b0=P1^0;\r\n\r\n&nbsp;\r\n\r\nsbit regALSB=ACC^0;\r\n\r\n&nbsp;\r\n\r\nbit membit;\r\n\r\n&nbsp;\r\n\r\nvoid main(void)\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\n&nbsp;\r\n\r\nunsigned char x;\r\n\r\n&nbsp;\r\n\r\nfor (x=0;x&lt;8;x++)\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\n&nbsp;\r\n\r\nmembit=P1b0;\r\n\r\n&nbsp;\r\n\r\nACC=ACC&lt;&lt;1;\r\n\r\n&nbsp;\r\n\r\nregALSB=membit;\r\n\r\n&nbsp;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nP2=ACC;\r\n\r\n<\/div>\r\n&nbsp;\r\n\r\n}\r\n\r\n&nbsp;\r\n<ol start=\"3\">\r\n \t<li><strong> Summary<\/strong><\/li>\r\n<\/ol>\r\n<p style=\"text-align: justify\">In this module we discussed about the Logical Operators in 8051 C. C code for Data conversion was written. Also, the C code to access the 8051 code space was written. At the end we showed the demo of C code for data serialization.<\/p>\r\n&nbsp;\r\n<ol start=\"4\">\r\n \t<li><strong>References<\/strong><strong style=\"text-align: initial;font-size: 1em\">\u00a0<\/strong><\/li>\r\n<\/ol>\r\n<ol>\r\n \t<li>Muhammad Ali Mazidi, Janice Gillispie Mazidi, Rolin D. McKinlay, \u201cThe 8051 Microcontroller and Embedded Systems Using Assembly and C -Second Edition\u201d, NEwdelhi(2000).<\/li>\r\n<\/ol>","rendered":"<div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In this lecture, we will discuss about the Logical operators in Embedded C. Data conversion programs in embedded C will also be written and discussed. At the end Data Serialization in 8051 using embedded C will be discussed.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>1.1 Bitwise Operations in C<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">One of the most important and powerful features of the C language is its ability to perform bit wise manipulation. This section describes the action of bitwise logic operators. Figure1.1 shows the bitwise logical operators.<\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-215 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-120.png\" alt=\"\" width=\"604\" height=\"236\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-120.png 604w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-120-300x117.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-120-65x25.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-120-225x88.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-120-350x137.png 350w\" sizes=\"auto, (max-width: 604px) 100vw, 604px\" \/><\/p>\n<div>\n<p style=\"text-align: justify\">Using these bit wise operators Embedded c will perform the logical operations bit wise on binary numbers. Following are some examples which show how they are used.<\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-217 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-121.png\" alt=\"\" width=\"590\" height=\"309\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-121.png 590w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-121-300x157.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-121-65x34.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-121-225x118.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-121-350x183.png 350w\" sizes=\"auto, (max-width: 590px) 100vw, 590px\" \/><\/p>\n<div>\n<p><strong>1.2 Bit-wise Shift Operation in C<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>There are two bit-wise shift operators in C:<\/p>\n<p>&nbsp;<\/p>\n<p>(1) shift right ( \u00bb), and<\/p>\n<p>&nbsp;<\/p>\n<p>(2)\u00a0 shift left (\u00ab).<\/p>\n<p>&nbsp;<\/p>\n<p>Their format in C is as follows:<\/p>\n<p>&nbsp;<\/p>\n<p>data \u00bb number of bits to be shifted right<\/p>\n<p>&nbsp;<\/p>\n<p>data \u00ab number of bits to be shifted left<\/p>\n<p>&nbsp;<\/p>\n<p>Some examples are shown below:<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-218 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-122.png\" alt=\"\" width=\"634\" height=\"312\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-122.png 634w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-122-300x148.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-122-65x32.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-122-225x111.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-122-350x172.png 350w\" sizes=\"auto, (max-width: 634px) 100vw, 634px\" \/><\/p>\n<p style=\"text-align: justify\">Following program will show the demo of the logical operations. Run the following program on your simulator and examine the results.<\/p>\n<p>&nbsp;<\/p>\n<p>#include &lt;reg51.h&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>void main(void)<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>&nbsp;<\/p>\n<p>P0=0x35 &amp; 0x0F; \/\/ANDing<\/p>\n<p>&nbsp;<\/p>\n<p>P1=0x04 | 0x68; \/\/ORing<\/p>\n<p>&nbsp;<\/p>\n<p>P2=0x54 ^ 0x78; \/\/XORing<\/p>\n<p>&nbsp;<\/p>\n<p>P0=~0x55; \/\/inverting<\/p>\n<p>&nbsp;<\/p>\n<p>P1=0x9A &gt;&gt; 3; \/\/shifting right 3<\/p>\n<p>&nbsp;<\/p>\n<p>P2=0x77 &gt;&gt; 4; \/\/shifting right 4<\/p>\n<p>&nbsp;<\/p>\n<p>P0=0x6 &lt;&lt; 4; \/\/shifting left 4<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Next program will show the operation on port pins to access data bit-wise and manipulate through logical operator.<\/p>\n<p>&nbsp;<\/p>\n<p><em>1.2.1 Write an 8051 C program to get bit P1.0 and send it to P2.7 after inverting it.<\/em><\/p>\n<p>&nbsp;<\/p>\n<p>Solution:<\/p>\n<\/div>\n<div>\n<p>#include &lt;reg51.h&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>sbit inbit=P1^0;<\/p>\n<p>&nbsp;<\/p>\n<p>sbit outbit=P2^7;<\/p>\n<p>&nbsp;<\/p>\n<p>bit membit;<\/p>\n<p>&nbsp;<\/p>\n<p>void main(void)<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>&nbsp;<\/p>\n<p>while (1)<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>&nbsp;<\/p>\n<p>membit=inbit; \/\/get a bit from P1.0<\/p>\n<p>&nbsp;<\/p>\n<p>outbit=~membit; \/\/invert it and send<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/it to P2.7<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p><strong>1.3 Data Conversion<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">We have seen BCD numbers in previous modules. As stated there, many newer microcontrollers have a real-time clock (RTC) where the time and date are kept, even when the power is off. Very often the RTC provides the time and date in packed BCD. However, to display them they must be converted to ASCII. Like this example, we need to convert the data from one form to another form. In this section we show some example programs to demo how Embedded C helps for data conversions. Some data conversions needed are listed below.<\/p>\n<p>&nbsp;<\/p>\n<p>\u2022\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Packed BCD to ASCII conversion<\/p>\n<p>&nbsp;<\/p>\n<p>\u2022\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ASCII to packed BCD conversion<\/p>\n<p>&nbsp;<\/p>\n<p>\u2022\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Checksum byte in ROM<\/p>\n<p>&nbsp;<\/p>\n<p>\u2022\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Binary to decimal and ASCII conversion in C<\/p>\n<p>&nbsp;<\/p>\n<p>Figure.2 shows the ASCII, binary and BCD codes for the digits 0 to 9.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-219 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-123.png\" alt=\"\" width=\"707\" height=\"262\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-123.png 707w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-123-300x111.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-123-65x24.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-123-225x83.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-123-350x130.png 350w\" sizes=\"auto, (max-width: 707px) 100vw, 707px\" \/><\/p>\n<p>Following program\u00a0 shows Packed BCD to ASCII conversion<strong>.<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><em>1.3.1 Example.1<\/em><\/p>\n<\/div>\n<div>\n<p>Write an 8051 C program to convert packed BCD 0x29 to ASCII and display the bytes on P1 and P2.<\/p>\n<p>&nbsp;<\/p>\n<p>Solution:<\/p>\n<p>&nbsp;<\/p>\n<p>#include &lt;reg51.h&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>void main(void)<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>&nbsp;<\/p>\n<p>unsigned char x,y,z;<\/p>\n<p>&nbsp;<\/p>\n<p>unsigned char mybyte=0x29;<\/p>\n<p>&nbsp;<\/p>\n<p>x=mybyte&amp;0x0F;<\/p>\n<p>&nbsp;<\/p>\n<p>P1=x|0x30;<\/p>\n<p>&nbsp;<\/p>\n<p>y=mybyte&amp;0xF0;<\/p>\n<p>&nbsp;<\/p>\n<p>y=y&gt;&gt;4;<\/p>\n<p>&nbsp;<\/p>\n<p>P2=y|0x30;<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>Following program\u00a0 shows the ASCII to packed BCD conversion.<\/p>\n<p>&nbsp;<\/p>\n<p><em>1.3.2 Example.2<\/em><\/p>\n<p>&nbsp;<\/p>\n<p>Write an 8051 C program to convert ASCII digits of \u20184\u2019 and \u20187\u2019 to packed BCD and display them on P1.<\/p>\n<p>&nbsp;<\/p>\n<p>Solution:<\/p>\n<p>&nbsp;<\/p>\n<p>#include &lt;reg51.h&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>void main(void)<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>&nbsp;<\/p>\n<p>unsigned char bcdbyte;<\/p>\n<p>&nbsp;<\/p>\n<p>unsigned char w=\u20184\u2019;<\/p>\n<p>&nbsp;<\/p>\n<p>unsigned char z=\u20187\u2019;<\/p>\n<p>&nbsp;<\/p>\n<p>w=w&amp;0x0F;<\/p>\n<p>&nbsp;<\/p>\n<p>w=w&lt;&lt;4;<\/p>\n<p>&nbsp;<\/p>\n<p>z=z&amp;0x0F;<\/p>\n<p>&nbsp;<\/p>\n<p>bcdbyte=w|z;<\/p>\n<p>&nbsp;<\/p>\n<p>P1=bcdbyte;<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>Following program\u00a0 is for finding the Checksum byte in ROM.<\/p>\n<p>&nbsp;<\/p>\n<p><em>1.3.3 Example.3<\/em><\/p>\n<p>&nbsp;<\/p>\n<p>Write an 8051 C program to calculate the checksum byte for the data 25H, 62H, 3FH, and 52H.<\/p>\n<p>&nbsp;<\/p>\n<p>Steps\u00a0 to calculate Checksum byte in ROM are:<\/p>\n<p>&nbsp;<\/p>\n<p>1.\u00a0\u00a0\u00a0 Add the bytes together and drop carries.<\/p>\n<p>&nbsp;<\/p>\n<p>2.\u00a0\u00a0\u00a0 Take the 2\u2019s complement (invert and then add 1) of the total sum. This is the<\/p>\n<p>&nbsp;<\/p>\n<p>Checksum byte, which becomes the last byte of the series.<\/p>\n<p>&nbsp;<\/p>\n<p>Solution:<\/p>\n<p>&nbsp;<\/p>\n<p>#include &lt;reg51.h&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>void main(void)<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div>\n<p>{<\/p>\n<p>&nbsp;<\/p>\n<p>unsigned char mydata[]={0x25,0x62,0x3F,0x52};<\/p>\n<p>&nbsp;<\/p>\n<p>unsigned char sum=0;<\/p>\n<p>&nbsp;<\/p>\n<p>unsigned char x;<\/p>\n<p>&nbsp;<\/p>\n<p>unsigned char chksumbyte;<\/p>\n<p>&nbsp;<\/p>\n<p>for (x=0;x&lt;4;x++)<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>&nbsp;<\/p>\n<p>P2=mydata[x];<\/p>\n<p>&nbsp;<\/p>\n<p>sum=sum+mydata[x];<\/p>\n<p>&nbsp;<\/p>\n<p>P1=sum;<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>chksumbyte=~sum+1;\u00a0 \/\/logical operator used here<\/p>\n<p>&nbsp;<\/p>\n<p>P1=chksumbyte;<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>Following program\u00a0 will demo Binary to Decimal and ASCII Conversion in 8051 C<\/p>\n<p>&nbsp;<\/p>\n<p><em>1.3.4 Example.4<\/em><\/p>\n<p>&nbsp;<\/p>\n<p>Write an 8051 C program to convert 11111101 (FD hex) to decimal and display the digits on P0, P1 and P2.<\/p>\n<p>&nbsp;<\/p>\n<p>Solution:<\/p>\n<p>&nbsp;<\/p>\n<p>#include &lt;reg51.h&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>void main(void)<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>&nbsp;<\/p>\n<p>unsigned char x,binbyte,d1,d2,d3;<\/p>\n<p>&nbsp;<\/p>\n<p>binbyte=0xFD;<\/p>\n<p>&nbsp;<\/p>\n<p>x=binbyte\/10;<\/p>\n<p>&nbsp;<\/p>\n<p>d1=binbyte%10;<\/p>\n<p>&nbsp;<\/p>\n<p>d2=x%10;<\/p>\n<p>&nbsp;<\/p>\n<p>d3=x\/10;<\/p>\n<p>&nbsp;<\/p>\n<p>P0=d1;<\/p>\n<p>&nbsp;<\/p>\n<p>P1=d2;<\/p>\n<p>&nbsp;<\/p>\n<p>P2=d3;<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p><strong>1.4 Accessing Code ROM space in 8051 C<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Using the code (program) space for predefined data is a widely used option in 8051. We saw how to use the Assembly language instruction MOVC to access the data stored in the 8051 code space. Here, we see the same concept with 8051 C.<\/p>\n<p>&nbsp;<\/p>\n<p>In 8051, we have three spaces to store data:<\/p>\n<p>&nbsp;<\/p>\n<p>1.\u00a0 The 128 bytes RAM space with address range 00-7FH<\/p>\n<p>&nbsp;<\/p>\n<p>\u2713\u00a0\u00a0\u00a0\u00a0 If you declare variables (eg.: char) to store data, C compiler will allocate a RAM space for these variable.<\/p>\n<p>&nbsp;<\/p>\n<p>2.\u00a0 User code space<\/p>\n<p>&nbsp;<\/p>\n<p>\u2713\u00a0\u00a0\u00a0\u00a0 External code memory (64K) + on-chip ROM (64K)<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div>\n<p>\u2713\u00a0\u00a0\u00a0\u00a0 Data is embedded to code or is separated as a data section.<\/p>\n<p>&nbsp;<\/p>\n<p>3.\u00a0 External data memory for data<\/p>\n<p>&nbsp;<\/p>\n<p>\u2713\u00a0\u00a0\u00a0\u00a0 RAM or ROM is used.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>2.1 RAM data space usage<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The 8051 C compiler allocates RAM locations as follows:<\/p>\n<p>&nbsp;<\/p>\n<p>1.\u00a0 Bank 0 \u2013 addresses 0 \u2013 7<\/p>\n<p>&nbsp;<\/p>\n<p>2.\u00a0 Individual variables \u2013 addresses 08 and beyond<\/p>\n<p>&nbsp;<\/p>\n<p>3.\u00a0 Array elements \u2013 addresses right after variables<\/p>\n<p>&nbsp;<\/p>\n<p>\u2713\u00a0\u00a0\u00a0\u00a0 Array elements need contiguous RAM locations and that limits the size of the array due to the fact that we have only 128 bytes of RAM for everything.<\/p>\n<p>&nbsp;<\/p>\n<p>4.\u00a0 Stack \u2013 addresses right after array elements.<\/p>\n<p>&nbsp;<\/p>\n<p>Following is an example program to access data from memory.<\/p>\n<p>&nbsp;<\/p>\n<p><em>2.1.1 Write, compile and single-step the following program on your 8051 simulator. Examine the contents of the code space to locate the values.<\/em><\/p>\n<p>&nbsp;<\/p>\n<p>#include &lt;reg51.h&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>void main(void)<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>&nbsp;<\/p>\n<p>unsigned char mydata[100]; \/\/RAM space<\/p>\n<p>&nbsp;<\/p>\n<p>unsigned char x,z=0;<\/p>\n<p>&nbsp;<\/p>\n<p>for (x=0;x&lt;100;x++)<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>&nbsp;<\/p>\n<p>z&#8211;;<\/p>\n<p>&nbsp;<\/p>\n<p>mydata[x]=z;<\/p>\n<p>&nbsp;<\/p>\n<p>P1=z;<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>While running this program you can see how the data will be stored in an array and displayed in a port.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>2.2 8052 RAM Space<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>One of the new features of the 8052 was an extra 128 bytes of RAM space.<\/p>\n<p>&nbsp;<\/p>\n<p>\u2713 The extra 128 bytes of RAM helps the 8051\/52 C compiler to manage its registers and resources much more effectively.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">\u2713 Based on 8052 architecture, you should use the reg52.h header file. Choose the 8052 option when compiling the program.<\/p>\n<p>&nbsp;<\/p>\n<p><em>2.2.1 Using ROM to Store Data<\/em><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">To make C compiler use the code space (on-chip ROM) instead of RAM space, we can put the keyword \u201ccode\u201d in front of the variable declaration.<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div>\n<p>unsigned char mydata[] = \u201cHELLO\u201d<\/p>\n<p>&nbsp;<\/p>\n<p>\u2713 HELLO is saved in RAM.<\/p>\n<p>&nbsp;<\/p>\n<p>code unsigned char mydata[] = \u201cHELLO\u201d<\/p>\n<p>&nbsp;<\/p>\n<p>\u2713 HELLO is saved in ROM.<\/p>\n<p>&nbsp;<\/p>\n<p>This is discussed in the following examples.<\/p>\n<p>&nbsp;<\/p>\n<p><em>2.2.2 Examples 5<\/em><\/p>\n<p>&nbsp;<\/p>\n<p>Let us compare and contrast the following programs and discuss the advantages and disadvantages of each one.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example 5(a):<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>#include &lt;reg51.h&gt;<\/p>\n<p>void main(void)<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>&nbsp;<\/p>\n<p>P1=\u201cH\u201d;<\/p>\n<p>&nbsp;<\/p>\n<p>P1=\u201cE\u201d;<\/p>\n<p>&nbsp;<\/p>\n<p>P1=\u201cL\u201d;<\/p>\n<p>&nbsp;<\/p>\n<p>P1=\u201cL\u201d;<\/p>\n<p>&nbsp;<\/p>\n<p>P1=\u201cO\u201d;<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>Data is embedded into code. Simple, short, not flexible.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example 5(b):<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>#include &lt;reg51.h&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>void main(void)<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>&nbsp;<\/p>\n<p>unsigned char mydata[]=\u201cHELLO\u201d;<\/p>\n<p>&nbsp;<\/p>\n<p>unsigned char z;<\/p>\n<p>&nbsp;<\/p>\n<p>for (z=0; z&lt;5; z++)<\/p>\n<p>&nbsp;<\/p>\n<p>P1 = mydata[z];<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>Data is stored in RAM and does not occupy ROM.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example 5(c):<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>#include &lt;reg51.h&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>void main(void)<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>&nbsp;<\/p>\n<p>Code unsigned char mydata[]=\u201cHELLO\u201d;<\/p>\n<p>&nbsp;<\/p>\n<p>unsigned char z;<\/p>\n<p>&nbsp;<\/p>\n<p>for (z=0; z&lt;5; z++)<\/p>\n<p>&nbsp;<\/p>\n<p>P1 = mydata[z];<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>Data is stored in ROM. However, data and code are separate.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>2.3 Data serialization using 8051 C<\/strong><\/p>\n<\/div>\n<p><strong>\u00a0<\/strong><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p>Serializing data is a way of sending a byte of data one bit at a time through a single pin of microcontroller.<\/p>\n<p>&nbsp;<\/p>\n<p>\u2713 Using the serial port (discussed in Serial Communication module)<\/p>\n<p>&nbsp;<\/p>\n<p>\u2713 Transfer data one bit a time and control the sequence of data and spaces in between them.<\/p>\n<p>&nbsp;<\/p>\n<p>In many new generations of devices such as LCD, ADC, and ROM, the serial versions are becoming popular since they take less space on a PCB.<\/p>\n<p>&nbsp;<\/p>\n<p>The following are examples that demonstrate data serialization using embedded C. <strong>Example. 6<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Write a C program to send out the value 44H serially one bit at a time via P1.0. The LSB should go out first.<\/p>\n<p>&nbsp;<\/p>\n<p>Solution :<\/p>\n<p>&nbsp;<\/p>\n<p>#include &lt;reg51.h&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>sbit P1b0=P1^0;<\/p>\n<p>&nbsp;<\/p>\n<p>sbit regALSB=ACC^0;<\/p>\n<p>&nbsp;<\/p>\n<p>void main(void)<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>&nbsp;<\/p>\n<p>unsigned char conbyte=0x44;<\/p>\n<p>&nbsp;<\/p>\n<p>unsigned char x;<\/p>\n<p>&nbsp;<\/p>\n<p>ACC=conbyte;<\/p>\n<p>&nbsp;<\/p>\n<p>for (x=0;x&lt;8;x++)<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>&nbsp;<\/p>\n<p>P1b0=regALSB;<\/p>\n<p>&nbsp;<\/p>\n<p>ACC=ACC&gt;&gt;1;<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><em>2.3.1 Write a C program to bring in a byte of data serially one bit at a time via P1.0. The MSB should come in first.<\/em><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example.7<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>#include &lt;reg51.h&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>sbit P1b0=P1^0;<\/p>\n<p>&nbsp;<\/p>\n<p>sbit regALSB=ACC^0;<\/p>\n<p>&nbsp;<\/p>\n<p>bit membit;<\/p>\n<p>&nbsp;<\/p>\n<p>void main(void)<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>&nbsp;<\/p>\n<p>unsigned char x;<\/p>\n<p>&nbsp;<\/p>\n<p>for (x=0;x&lt;8;x++)<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>&nbsp;<\/p>\n<p>membit=P1b0;<\/p>\n<p>&nbsp;<\/p>\n<p>ACC=ACC&lt;&lt;1;<\/p>\n<p>&nbsp;<\/p>\n<p>regALSB=membit;<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>P2=ACC;<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<ol start=\"3\">\n<li><strong> Summary<\/strong><\/li>\n<\/ol>\n<p style=\"text-align: justify\">In this module we discussed about the Logical Operators in 8051 C. C code for Data conversion was written. Also, the C code to access the 8051 code space was written. At the end we showed the demo of C code for data serialization.<\/p>\n<p>&nbsp;<\/p>\n<ol start=\"4\">\n<li><strong>References<\/strong><strong style=\"text-align: initial;font-size: 1em\">\u00a0<\/strong><\/li>\n<\/ol>\n<ol>\n<li>Muhammad Ali Mazidi, Janice Gillispie Mazidi, Rolin D. McKinlay, \u201cThe 8051 Microcontroller and Embedded Systems Using Assembly and C -Second Edition\u201d, NEwdelhi(2000).<\/li>\n<\/ol>\n","protected":false},"author":2,"menu_order":17,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["dr-k-vani"],"pb_section_license":""},"chapter-type":[],"contributor":[58],"license":[],"class_list":["post-212","chapter","type-chapter","status-publish","hentry","contributor-dr-k-vani"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/pressbooks\/v2\/chapters\/212","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/wp\/v2\/users\/2"}],"version-history":[{"count":4,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/pressbooks\/v2\/chapters\/212\/revisions"}],"predecessor-version":[{"id":220,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/pressbooks\/v2\/chapters\/212\/revisions\/220"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/pressbooks\/v2\/chapters\/212\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/wp\/v2\/media?parent=212"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/pressbooks\/v2\/chapter-type?post=212"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/wp\/v2\/contributor?post=212"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/wp\/v2\/license?post=212"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}