{"id":197,"date":"2018-07-26T12:32:40","date_gmt":"2018-07-26T12:32:40","guid":{"rendered":"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=197"},"modified":"2018-08-13T06:02:36","modified_gmt":"2018-08-13T06:02:36","slug":"programming-embedded-systems-in-c","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/chapter\/programming-embedded-systems-in-c\/","title":{"rendered":"Programming Embedded Systems in C"},"content":{"raw":"<div>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In this module, we will discuss about the embedded C programming for microcontroller. C programs for time delay and I\/O operations will be discussed<strong>.<\/strong> Programs for handling the I\/O ports and I\/O bit manipulation will also be written and discussed.<\/p>\r\n&nbsp;\r\n\r\n<strong>1. Need for programming 8051 in embedded C<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">There are several reasons for using a high-level language such as C to program the 8051 microcontroller. It is easier and less time consuming to write programs in Embedded C than Assembly. C is easier to modify and update. We can also use code available in function libraries. Also, C code is portable to other microcontrollers with little or no modification.<\/p>\r\n&nbsp;\r\n\r\n<strong>1.1 Flow of Execution<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">We will first look at a few common structures available in Embedded C. Figure1.1 shows the flow for sequential execution , selection procedure, and looping structure.<\/p>\r\n\r\n<\/div>\r\n<img class=\"size-full wp-image-202 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-113.png\" alt=\"\" width=\"674\" height=\"400\" \/>\r\n<div>\r\n\r\n<strong>1.2 If and Switch statement(selection) structure<\/strong>\r\n\r\n&nbsp;\r\n\r\nFig1.2 shows the If -then-else and switch - case statement. Like normal C programming in embedded C also programmers will use if statements and switch case statements.\r\n\r\n<\/div>\r\n<img class=\"size-full wp-image-205 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-114.png\" alt=\"\" width=\"744\" height=\"483\" \/>\r\n\r\n<img class=\"size-full wp-image-206 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-115.png\" alt=\"\" width=\"329\" height=\"291\" \/>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>1.3 While and for looping(Repetition) structure<\/strong>\r\n\r\n&nbsp;\r\n\r\nFor looping fig 1.3 shows the structure of while loop and for looping structure .\r\n\r\n<\/div>\r\n<img class=\"size-full wp-image-207 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-116.png\" alt=\"\" width=\"735\" height=\"500\" \/>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<strong>2. Translation between C and Assembly Code<\/strong>\r\n\r\n&nbsp;\r\n\r\n<strong>Following is the example on how an assembly program loop will be written in embedded \"c\" code A loop in Assembly<\/strong>\r\n\r\n&nbsp;\r\n\r\nMOV R2,#255\r\n\r\n&nbsp;\r\n\r\nABC:\u00a0\u00a0\u00a0\u00a0\u00a0 MOV P1,R2\r\n\r\n&nbsp;\r\n\r\nDJNZ R2,ABC\r\n\r\n&nbsp;\r\n\r\n<strong>A for-loop in C<\/strong>\r\n\r\n&nbsp;\r\n\r\nfor (int z=255; z&gt;0; z--)\r\n\r\n&nbsp;\r\n\r\nP1=z;\r\n\r\n&nbsp;\r\n\r\n<strong>3. Data Type and Time Delay in 8051 C<\/strong>\r\n\r\n<\/div>\r\n<strong>\u00a0<\/strong>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">Specific C data types for 8051 can help programmers to create smaller hex files. They are<\/span>\r\n<div>\r\n\r\n&nbsp;\r\n\r\nUnsigned char\r\n\r\n&nbsp;\r\n\r\nSigned char\r\n\r\n&nbsp;\r\n\r\nUnsigned int\r\n\r\n&nbsp;\r\n\r\nSigned int\r\n\r\n&nbsp;\r\n\r\nSbit (single bit)\r\n\r\n&nbsp;\r\n\r\nBit and sfr\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Figure 1.4 shows the number of bits needed by each data type. The character data type is the most natural data choice because 8051 is an 8-bit microcontroller. The C compiler will allocate RAM space for the variables based on their data type - char, int , bit etc.<\/p>\r\n\r\n<\/div>\r\n<img class=\"size-full wp-image-208 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-117.png\" alt=\"\" width=\"684\" height=\"397\" \/>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>3.1 Unsigned and Signed Char<\/strong>\r\n\r\n&nbsp;\r\n\r\n<strong>Unsigned Char:<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The most widely used data type for the 8051 is unsigned char. It uses 8-bits. The range of values taken by unsigned char is 0-255(00-FFH).<\/p>\r\n&nbsp;\r\n\r\nIn a microcontroller setup, we use unsigned char for the following:\r\n\r\n&nbsp;\r\n\r\n\u2713\u00a0\u00a0\u00a0\u00a0\u00a0 To set counter value\r\n\r\n&nbsp;\r\n\r\n\u2713\u00a0\u00a0\u00a0\u00a0\u00a0 To handle a string of ASCII characters\r\n\r\n<\/div>\r\n<span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 \u00a0\u2713\u00a0\u00a0\u00a0\u00a0\u00a0 For toggling ports.<\/span>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>Signed Char:<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Signed char is an 8-bit data type. It uses a 2\u2019s complement representation. The range of unsigned char is -128 to +127 (00-FFH). It is used for the following purposes:<\/p>\r\n&nbsp;\r\n\r\n\u2713\u00a0\u00a0\u00a0\u00a0 To present a given quantity such as temperature which can take both positive and negative values. .\r\n\r\n&nbsp;\r\n\r\nFollowing are examples of programs using unsigned and signed char.\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Example 1 : Write an 8051 C program to send values 00 \u2013 FF to port P1.<\/p>\r\n&nbsp;\r\n\r\n<strong>Solution:<\/strong>\r\n\r\n&nbsp;\r\n\r\n#include &lt;reg51.h&gt; \/\/ library code for changing the following code to 8051 assembly code\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 z;\r\n\r\n&nbsp;\r\n\r\nfor (z=0;z&lt;=255;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\nWrite an 8051 C program to send hex values for ASCII characters of 0, 1, 2, 3, 4, 5, A, B, C, and D to port P1.\r\n\r\n&nbsp;\r\n\r\n<strong>Solution:<\/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 mynum[]=\u201c012345ABCD\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;=10;z++)\r\n\r\n&nbsp;\r\n\r\nP1=mynum[z];\r\n\r\n&nbsp;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nWrite an 8051 C program to send values of \u20134 to +4 to port P1.\r\n\r\n&nbsp;\r\n\r\n<strong>Solution:<\/strong>\r\n\r\n&nbsp;\r\n\r\n\/\/Signed numbers\r\n\r\n&nbsp;\r\n\r\n#include &lt;reg51.h&gt;\r\n\r\n<\/div>\r\n<span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 void main(void)<\/span>\r\n<div>\r\n\r\n\u00a0 {\r\n\r\n&nbsp;\r\n\r\nchar mynum[]={+1,-1,+2,-2,+3,-3,+4,-4};\r\n\r\n&nbsp;\r\n\r\nsigned char z;\r\n\r\n&nbsp;\r\n\r\nfor (z=0;z&lt;=8;z++)\r\n\r\n&nbsp;\r\n\r\nP1=mynum[z];\r\n\r\n&nbsp;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\n<strong>4. Integer<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The <strong>unsigned int<\/strong> is a 16-bit data type. It takes a value in the range of 0 to 65535 (0000 \u2013 FFFFH). It is used to<\/p>\r\n&nbsp;\r\n\r\n\u27a2\u00a0\u00a0\u00a0 Define 16-bit variables such as memory addresses.\r\n\r\n&nbsp;\r\n\r\n\u27a2\u00a0\u00a0\u00a0 Set counter values of more than 256.\r\n\r\n&nbsp;\r\n\r\nSince registers and memory accesses are in 8-bit chunks, the misuse of int variables will result in a larger hex file.\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong>Signed int <\/strong>is a 16-bit data type. It uses the most significant bit D15 to represent the sign : \u2013 or + . We have 15 bits for the magnitude of the number, giving a range from \u201332768 to +32767.<\/p>\r\n&nbsp;\r\n\r\n<strong>Following code shows examples for using unsigned bit and sbit.<\/strong>\r\n\r\n&nbsp;\r\n\r\nWrite an 8051 C program to toggle bit D0 of the port P1 (P1.0)\u00a0 50,000 times.\r\n\r\n&nbsp;\r\n\r\n<strong>Solution:<\/strong>\r\n\r\n&nbsp;\r\n\r\n#include &lt;reg51.h&gt;\r\n\r\n&nbsp;\r\n\r\nsbit MYBIT=P1^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 int z;\r\n\r\n&nbsp;\r\n\r\nfor (z=0;z&lt;=50000;z++)\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\n&nbsp;\r\n\r\nMYBIT=0;\r\n\r\n&nbsp;\r\n\r\nMYBIT=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<strong>5. Time Delay<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">There are two ways to create a time delay in 8051 C : (i) using the 8051 timer and (ii) using a simple for-loop. When using a loop to create the time delay, there are three factors that can affect the accuracy of the time delay. They are: (i) Crystal frequency of the 8051 system, (ii) 8051 machine cycle timing and (iii) Compiler used for 8051 C.<\/p>\r\n\r\n<\/div>\r\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">Following are example programs for creating time delay:<\/span><\/p>\r\n\r\n<div>\r\n<p style=\"text-align: justify\">Write an 8051 C program to toggle bits of P1 continuously forever with some delay.<\/p>\r\n&nbsp;\r\n\r\n<strong>Solution:<\/strong>\r\n\r\n&nbsp;\r\n\r\n\/\/Toggle P1 forever with some delay in between \u201con\u201d and \u201coff\u201d\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 int x;\r\n\r\n&nbsp;\r\n\r\nfor (;;) \/\/repeat forever\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\n&nbsp;\r\n\r\np1=0x55;\r\n\r\n&nbsp;\r\n\r\nfor (x=0;x&lt;40000;x++); \/\/delay size unknown\r\n\r\n&nbsp;\r\n\r\np1=0xAA;\r\n\r\n&nbsp;\r\n\r\nfor (x=0;x&lt;40000;x++);\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\nWrite an 8051 C program to toggle bits of P1 ports continuously with a 250 ms delay.\r\n\r\n&nbsp;\r\n\r\n<strong>Solution:<\/strong>\r\n\r\n&nbsp;\r\n\r\n#include &lt;reg51.h&gt;\r\n\r\n&nbsp;\r\n\r\nvoid MSDelay(unsigned int);\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) \/\/repeat forever\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\n&nbsp;\r\n\r\np1=0x55;\r\n\r\n&nbsp;\r\n\r\nMSDelay(250);\r\n\r\n&nbsp;\r\n\r\np1=0xAA;\r\n\r\n&nbsp;\r\n\r\nMSDelay(250);\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\nvoid MSDelay(unsigned int itime)\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\n&nbsp;\r\n\r\nunsigned int i,j;\r\n\r\n&nbsp;\r\n\r\nfor (i=0;i&lt;itime;i++)\r\n\r\n&nbsp;\r\n\r\nfor (j=0;j&lt;1275;j++);\r\n\r\n&nbsp;\r\n\r\n}\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The operating modes of the 8051 can be changed by manipulating the values of the 8051's Special Function Registers (SFRs). SFRs are accessed as if they were normal Internal RAM. The only difference is that Internal RAM is from address 00h through 7Fh whereas SFR registers exist in the address range of 80h through FFh.Each SFR has an address (80h through FFh) and a name.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Write an 8051 C program to toggle all the bits of P0, P1, and P2 continuously with a 250 ms delay. Use the sfr keyword to declare the port addresses.<\/p>\r\n&nbsp;\r\n\r\n<strong>Solution:<\/strong>\r\n\r\n&nbsp;\r\n\r\n\/\/Accessing Ports as SFRs using sfr data type\r\n\r\n&nbsp;\r\n\r\nsfr P0=0x80;\r\n\r\n&nbsp;\r\n\r\nsfr P1=0x90;\r\n\r\n&nbsp;\r\n\r\nsfr P2=0xA0;\r\n\r\n&nbsp;\r\n\r\nvoid MSDelay(unsigned int);\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\nP0=0x55;\r\n\r\n&nbsp;\r\n\r\nP1=0x55;\r\n\r\n&nbsp;\r\n\r\nP2=0x55;\r\n\r\n&nbsp;\r\n\r\nMSDelay(250);\r\n\r\n&nbsp;\r\n\r\nP0=0xAA;\r\n\r\n&nbsp;\r\n\r\nP1=0xAA;\r\n\r\n&nbsp;\r\n\r\nP2=0xAA;\r\n\r\n&nbsp;\r\n\r\nMSDelay(250);\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>6. I\/O programming in 8051 C<\/strong>\r\n\r\n&nbsp;\r\n\r\nThe Figure 1.5 shows the address for the SFR registers. Through which you can access individual bit of SFR also.\r\n\r\n&nbsp;\r\n\r\n<\/div>\r\n<div>\r\n\r\n<img class=\"size-full wp-image-209 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-118.png\" alt=\"\" width=\"313\" height=\"449\" \/>\r\n\r\n&nbsp;\r\n\r\nFollowing program is an example for the use of sbit and name of SFR\r\n\r\n&nbsp;\r\n\r\n#include &lt;reg51.h&gt;\r\n\r\n&nbsp;\r\n\r\nSbit MYBIT = P1^5;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/D5 of P1\r\n\r\n&nbsp;\r\n\r\nUse sbit to declare the bit of SFR and declare\r\n\r\n&nbsp;\r\n\r\nSbit MYBIT = 0x95;\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/D5 of P1\r\n\r\n&nbsp;\r\n\r\n\u2022reg51. h is not necessary.\r\n\r\n&nbsp;\r\n\r\n<strong>Following figure 1.6\u00a0 shows 8051 256 Byte RAM<\/strong>\r\n\r\n<\/div>\r\n<strong>\u00a0<img class=\"size-full wp-image-210 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-119.png\" alt=\"\" width=\"587\" height=\"454\" \/><\/strong>\r\n<div>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Write an 8051 C program to toggle all the bits of P0, P1, and P2 continuously with a 250 ms delay. Use the sfr keyword to declare the port addresses.<\/p>\r\n&nbsp;\r\n\r\n<strong>Solution:<\/strong>\r\n\r\n&nbsp;\r\n\r\n\/\/Accessing Ports as SFRs using sfr data type\r\n\r\n&nbsp;\r\n\r\nsfr P0=0x80;\r\n\r\n&nbsp;\r\n\r\nsfr P1=0x90;\r\n\r\n&nbsp;\r\n\r\nsfr P2=0xA0;\r\n\r\n&nbsp;\r\n\r\nvoid MSDelay(unsigned int);\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\nP0=0x55;\r\n\r\n&nbsp;\r\n\r\nP1=0x55;\r\n\r\n&nbsp;\r\n\r\nP2=0x55;\r\n\r\n&nbsp;\r\n\r\nMSDelay(250);\r\n\r\n&nbsp;\r\n\r\nP0=0xAA;\r\n\r\n&nbsp;\r\n\r\nP1=0xAA;\r\n\r\n&nbsp;\r\n\r\nP2=0xAA;\r\n\r\n&nbsp;\r\n\r\nMSDelay(250);\r\n\r\n<\/div>\r\n&nbsp;\r\n<div>\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\n<strong>Access Single Bit of SFR<\/strong>\r\n\r\n&nbsp;\r\n\r\nWay to access a single bit of SFR\r\n\r\n&nbsp;\r\n\r\n\u2022 Use sbit and name of SFR #include &lt;reg51.h&gt;\r\n\r\n&nbsp;\r\n\r\nSbit MYBIT = P1^5;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/D5 of P1\r\n\r\n&nbsp;\r\n\r\n\u2022 Use sbit to declare the bit of SFR and declare by yourself\r\n\r\n&nbsp;\r\n\r\nSbit MYBIT = 0x95;\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/D5 of P1\r\n\r\n&nbsp;\r\n\r\n\u2022reg51. h is not necessary.\r\n\r\n&nbsp;\r\n\r\nWrite an 8051 C program to turn bit P1.5 on and off 50,000 times.\r\n\r\n&nbsp;\r\n\r\nSolution\r\n\r\n&nbsp;\r\n\r\n<strong>Sbit MYBIT = 0x95;\/\/ P1^5<\/strong>\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 intz;\r\n\r\n&nbsp;\r\n\r\nfor (z=0;z&lt;50000;z++)\r\n\r\n&nbsp;\r\n\r\n{\r\n\r\n&nbsp;\r\n\r\nMYBIT=1;\r\n\r\n&nbsp;\r\n\r\nMYBIT=0;\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\nThis program is similar to Example for unsigned int.\r\n\r\n&nbsp;\r\n\r\n<strong>Access Bit-addressable RAM<\/strong>\r\n\r\n&nbsp;\r\n\r\nYou can use bit to access one bit of bit-addressable section of the data RAM space 20H-2FH.\r\n\r\n&nbsp;\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\nbit membit;\/\/C compiler assign a RAM\r\n\r\n&nbsp;\r\n\r\nspace for mybit\r\n\r\n&nbsp;\r\n\r\nmembit= inbit; \/\/Read P1^0 to RAM\r\n\r\n&nbsp;\r\n\r\nWrite an 8051 C program to get the status of bit P1.0, save it, and send it to P2.7 continuously.\r\n\r\n<\/div>\r\n&nbsp;\r\n\r\n<strong>Solution:<\/strong>\r\n\r\n&nbsp;\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) { \/\/repeat forever\r\n\r\n&nbsp;\r\n\r\nmembit= inbit;\r\n\r\n&nbsp;\r\n\r\noutbit= membit\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>7.Summary<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In this lecture we discussed C data type for 8051. Discussed C programs for time delay and I\/O operations. Written C code for I\/O bit and I\/O port manipulations.<\/p>\r\n&nbsp;\r\n\r\n8.<strong>References<\/strong>\r\n\r\n&nbsp;\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 module, we will discuss about the embedded C programming for microcontroller. C programs for time delay and I\/O operations will be discussed<strong>.<\/strong> Programs for handling the I\/O ports and I\/O bit manipulation will also be written and discussed.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>1. Need for programming 8051 in embedded C<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">There are several reasons for using a high-level language such as C to program the 8051 microcontroller. It is easier and less time consuming to write programs in Embedded C than Assembly. C is easier to modify and update. We can also use code available in function libraries. Also, C code is portable to other microcontrollers with little or no modification.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>1.1 Flow of Execution<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">We will first look at a few common structures available in Embedded C. Figure1.1 shows the flow for sequential execution , selection procedure, and looping structure.<\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-202 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-113.png\" alt=\"\" width=\"674\" height=\"400\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-113.png 674w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-113-300x178.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-113-65x39.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-113-225x134.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-113-350x208.png 350w\" sizes=\"auto, (max-width: 674px) 100vw, 674px\" \/><\/p>\n<div>\n<p><strong>1.2 If and Switch statement(selection) structure<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Fig1.2 shows the If -then-else and switch &#8211; case statement. Like normal C programming in embedded C also programmers will use if statements and switch case statements.<\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-205 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-114.png\" alt=\"\" width=\"744\" height=\"483\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-114.png 744w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-114-300x195.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-114-65x42.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-114-225x146.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-114-350x227.png 350w\" sizes=\"auto, (max-width: 744px) 100vw, 744px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-206 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-115.png\" alt=\"\" width=\"329\" height=\"291\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-115.png 329w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-115-300x265.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-115-65x57.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-115-225x199.png 225w\" sizes=\"auto, (max-width: 329px) 100vw, 329px\" \/><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>1.3 While and for looping(Repetition) structure<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>For looping fig 1.3 shows the structure of while loop and for looping structure .<\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-207 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-116.png\" alt=\"\" width=\"735\" height=\"500\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-116.png 735w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-116-300x204.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-116-65x44.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-116-225x153.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-116-350x238.png 350w\" sizes=\"auto, (max-width: 735px) 100vw, 735px\" \/><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>2. Translation between C and Assembly Code<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Following is the example on how an assembly program loop will be written in embedded &#8220;c&#8221; code A loop in Assembly<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>MOV R2,#255<\/p>\n<p>&nbsp;<\/p>\n<p>ABC:\u00a0\u00a0\u00a0\u00a0\u00a0 MOV P1,R2<\/p>\n<p>&nbsp;<\/p>\n<p>DJNZ R2,ABC<\/p>\n<p>&nbsp;<\/p>\n<p><strong>A for-loop in C<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>for (int z=255; z&gt;0; z&#8211;)<\/p>\n<p>&nbsp;<\/p>\n<p>P1=z;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>3. Data Type and Time Delay in 8051 C<\/strong><\/p>\n<\/div>\n<p><strong>\u00a0<\/strong><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">Specific C data types for 8051 can help programmers to create smaller hex files. They are<\/span><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p>Unsigned char<\/p>\n<p>&nbsp;<\/p>\n<p>Signed char<\/p>\n<p>&nbsp;<\/p>\n<p>Unsigned int<\/p>\n<p>&nbsp;<\/p>\n<p>Signed int<\/p>\n<p>&nbsp;<\/p>\n<p>Sbit (single bit)<\/p>\n<p>&nbsp;<\/p>\n<p>Bit and sfr<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Figure 1.4 shows the number of bits needed by each data type. The character data type is the most natural data choice because 8051 is an 8-bit microcontroller. The C compiler will allocate RAM space for the variables based on their data type &#8211; char, int , bit etc.<\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-208 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-117.png\" alt=\"\" width=\"684\" height=\"397\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-117.png 684w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-117-300x174.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-117-65x38.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-117-225x131.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-117-350x203.png 350w\" sizes=\"auto, (max-width: 684px) 100vw, 684px\" \/><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>3.1 Unsigned and Signed Char<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Unsigned Char:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The most widely used data type for the 8051 is unsigned char. It uses 8-bits. The range of values taken by unsigned char is 0-255(00-FFH).<\/p>\n<p>&nbsp;<\/p>\n<p>In a microcontroller setup, we use unsigned char for the following:<\/p>\n<p>&nbsp;<\/p>\n<p>\u2713\u00a0\u00a0\u00a0\u00a0\u00a0 To set counter value<\/p>\n<p>&nbsp;<\/p>\n<p>\u2713\u00a0\u00a0\u00a0\u00a0\u00a0 To handle a string of ASCII characters<\/p>\n<\/div>\n<p><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 \u00a0\u2713\u00a0\u00a0\u00a0\u00a0\u00a0 For toggling ports.<\/span><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>Signed Char:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Signed char is an 8-bit data type. It uses a 2\u2019s complement representation. The range of unsigned char is -128 to +127 (00-FFH). It is used for the following purposes:<\/p>\n<p>&nbsp;<\/p>\n<p>\u2713\u00a0\u00a0\u00a0\u00a0 To present a given quantity such as temperature which can take both positive and negative values. .<\/p>\n<p>&nbsp;<\/p>\n<p>Following are examples of programs using unsigned and signed char.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Example 1 : Write an 8051 C program to send values 00 \u2013 FF to port P1.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Solution:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>#include &lt;reg51.h&gt; \/\/ library code for changing the following code to 8051 assembly code<\/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 z;<\/p>\n<p>&nbsp;<\/p>\n<p>for (z=0;z&lt;=255;z++)<\/p>\n<p>&nbsp;<\/p>\n<p>P1=z;<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>Write an 8051 C program to send hex values for ASCII characters of 0, 1, 2, 3, 4, 5, A, B, C, and D to port P1.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Solution:<\/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 mynum[]=\u201c012345ABCD\u201d;<\/p>\n<p>&nbsp;<\/p>\n<p>unsigned char z;<\/p>\n<p>&nbsp;<\/p>\n<p>for (z=0;z&lt;=10;z++)<\/p>\n<p>&nbsp;<\/p>\n<p>P1=mynum[z];<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>Write an 8051 C program to send values of \u20134 to +4 to port P1.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Solution:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>\/\/Signed numbers<\/p>\n<p>&nbsp;<\/p>\n<p>#include &lt;reg51.h&gt;<\/p>\n<\/div>\n<p><span style=\"text-align: initial;font-size: 1em\">\u00a0 \u00a0 void main(void)<\/span><\/p>\n<div>\n<p>\u00a0 {<\/p>\n<p>&nbsp;<\/p>\n<p>char mynum[]={+1,-1,+2,-2,+3,-3,+4,-4};<\/p>\n<p>&nbsp;<\/p>\n<p>signed char z;<\/p>\n<p>&nbsp;<\/p>\n<p>for (z=0;z&lt;=8;z++)<\/p>\n<p>&nbsp;<\/p>\n<p>P1=mynum[z];<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p><strong>4. Integer<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The <strong>unsigned int<\/strong> is a 16-bit data type. It takes a value in the range of 0 to 65535 (0000 \u2013 FFFFH). It is used to<\/p>\n<p>&nbsp;<\/p>\n<p>\u27a2\u00a0\u00a0\u00a0 Define 16-bit variables such as memory addresses.<\/p>\n<p>&nbsp;<\/p>\n<p>\u27a2\u00a0\u00a0\u00a0 Set counter values of more than 256.<\/p>\n<p>&nbsp;<\/p>\n<p>Since registers and memory accesses are in 8-bit chunks, the misuse of int variables will result in a larger hex file.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong>Signed int <\/strong>is a 16-bit data type. It uses the most significant bit D15 to represent the sign : \u2013 or + . We have 15 bits for the magnitude of the number, giving a range from \u201332768 to +32767.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Following code shows examples for using unsigned bit and sbit.<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Write an 8051 C program to toggle bit D0 of the port P1 (P1.0)\u00a0 50,000 times.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Solution:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>#include &lt;reg51.h&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>sbit MYBIT=P1^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 int z;<\/p>\n<p>&nbsp;<\/p>\n<p>for (z=0;z&lt;=50000;z++)<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>&nbsp;<\/p>\n<p>MYBIT=0;<\/p>\n<p>&nbsp;<\/p>\n<p>MYBIT=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><strong>5. Time Delay<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">There are two ways to create a time delay in 8051 C : (i) using the 8051 timer and (ii) using a simple for-loop. When using a loop to create the time delay, there are three factors that can affect the accuracy of the time delay. They are: (i) Crystal frequency of the 8051 system, (ii) 8051 machine cycle timing and (iii) Compiler used for 8051 C.<\/p>\n<\/div>\n<p style=\"text-align: justify\"><span style=\"text-align: initial;font-size: 1em\">Following are example programs for creating time delay:<\/span><\/p>\n<div>\n<p style=\"text-align: justify\">Write an 8051 C program to toggle bits of P1 continuously forever with some delay.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Solution:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>\/\/Toggle P1 forever with some delay in between \u201con\u201d and \u201coff\u201d<\/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 int x;<\/p>\n<p>&nbsp;<\/p>\n<p>for (;;) \/\/repeat forever<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>&nbsp;<\/p>\n<p>p1=0x55;<\/p>\n<p>&nbsp;<\/p>\n<p>for (x=0;x&lt;40000;x++); \/\/delay size unknown<\/p>\n<p>&nbsp;<\/p>\n<p>p1=0xAA;<\/p>\n<p>&nbsp;<\/p>\n<p>for (x=0;x&lt;40000;x++);<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>Write an 8051 C program to toggle bits of P1 ports continuously with a 250 ms delay.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Solution:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>#include &lt;reg51.h&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>void MSDelay(unsigned int);<\/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) \/\/repeat forever<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>&nbsp;<\/p>\n<p>p1=0x55;<\/p>\n<p>&nbsp;<\/p>\n<p>MSDelay(250);<\/p>\n<p>&nbsp;<\/p>\n<p>p1=0xAA;<\/p>\n<p>&nbsp;<\/p>\n<p>MSDelay(250);<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>void MSDelay(unsigned int itime)<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>&nbsp;<\/p>\n<p>unsigned int i,j;<\/p>\n<p>&nbsp;<\/p>\n<p>for (i=0;i&lt;itime;i++)<\/p>\n<p>&nbsp;<\/p>\n<p>for (j=0;j&lt;1275;j++);<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The operating modes of the 8051 can be changed by manipulating the values of the 8051&#8217;s Special Function Registers (SFRs). SFRs are accessed as if they were normal Internal RAM. The only difference is that Internal RAM is from address 00h through 7Fh whereas SFR registers exist in the address range of 80h through FFh.Each SFR has an address (80h through FFh) and a name.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Write an 8051 C program to toggle all the bits of P0, P1, and P2 continuously with a 250 ms delay. Use the sfr keyword to declare the port addresses.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Solution:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>\/\/Accessing Ports as SFRs using sfr data type<\/p>\n<p>&nbsp;<\/p>\n<p>sfr P0=0x80;<\/p>\n<p>&nbsp;<\/p>\n<p>sfr P1=0x90;<\/p>\n<p>&nbsp;<\/p>\n<p>sfr P2=0xA0;<\/p>\n<p>&nbsp;<\/p>\n<p>void MSDelay(unsigned int);<\/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>P0=0x55;<\/p>\n<p>&nbsp;<\/p>\n<p>P1=0x55;<\/p>\n<p>&nbsp;<\/p>\n<p>P2=0x55;<\/p>\n<p>&nbsp;<\/p>\n<p>MSDelay(250);<\/p>\n<p>&nbsp;<\/p>\n<p>P0=0xAA;<\/p>\n<p>&nbsp;<\/p>\n<p>P1=0xAA;<\/p>\n<p>&nbsp;<\/p>\n<p>P2=0xAA;<\/p>\n<p>&nbsp;<\/p>\n<p>MSDelay(250);<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p><strong>6. I\/O programming in 8051 C<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The Figure 1.5 shows the address for the SFR registers. Through which you can access individual bit of SFR also.<\/p>\n<p>&nbsp;<\/p>\n<\/div>\n<div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-209 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-118.png\" alt=\"\" width=\"313\" height=\"449\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-118.png 313w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-118-209x300.png 209w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-118-65x93.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-118-225x323.png 225w\" sizes=\"auto, (max-width: 313px) 100vw, 313px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Following program is an example for the use of sbit and name of SFR<\/p>\n<p>&nbsp;<\/p>\n<p>#include &lt;reg51.h&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>Sbit MYBIT = P1^5;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/D5 of P1<\/p>\n<p>&nbsp;<\/p>\n<p>Use sbit to declare the bit of SFR and declare<\/p>\n<p>&nbsp;<\/p>\n<p>Sbit MYBIT = 0x95;\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/D5 of P1<\/p>\n<p>&nbsp;<\/p>\n<p>\u2022reg51. h is not necessary.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Following figure 1.6\u00a0 shows 8051 256 Byte RAM<\/strong><\/p>\n<\/div>\n<p><strong>\u00a0<img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-210 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-119.png\" alt=\"\" width=\"587\" height=\"454\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-119.png 587w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-119-300x232.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-119-65x50.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-119-225x174.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-119-350x271.png 350w\" sizes=\"auto, (max-width: 587px) 100vw, 587px\" \/><\/strong><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Write an 8051 C program to toggle all the bits of P0, P1, and P2 continuously with a 250 ms delay. Use the sfr keyword to declare the port addresses.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Solution:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>\/\/Accessing Ports as SFRs using sfr data type<\/p>\n<p>&nbsp;<\/p>\n<p>sfr P0=0x80;<\/p>\n<p>&nbsp;<\/p>\n<p>sfr P1=0x90;<\/p>\n<p>&nbsp;<\/p>\n<p>sfr P2=0xA0;<\/p>\n<p>&nbsp;<\/p>\n<p>void MSDelay(unsigned int);<\/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>P0=0x55;<\/p>\n<p>&nbsp;<\/p>\n<p>P1=0x55;<\/p>\n<p>&nbsp;<\/p>\n<p>P2=0x55;<\/p>\n<p>&nbsp;<\/p>\n<p>MSDelay(250);<\/p>\n<p>&nbsp;<\/p>\n<p>P0=0xAA;<\/p>\n<p>&nbsp;<\/p>\n<p>P1=0xAA;<\/p>\n<p>&nbsp;<\/p>\n<p>P2=0xAA;<\/p>\n<p>&nbsp;<\/p>\n<p>MSDelay(250);<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Access Single Bit of SFR<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Way to access a single bit of SFR<\/p>\n<p>&nbsp;<\/p>\n<p>\u2022 Use sbit and name of SFR #include &lt;reg51.h&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>Sbit MYBIT = P1^5;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/D5 of P1<\/p>\n<p>&nbsp;<\/p>\n<p>\u2022 Use sbit to declare the bit of SFR and declare by yourself<\/p>\n<p>&nbsp;<\/p>\n<p>Sbit MYBIT = 0x95;\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/D5 of P1<\/p>\n<p>&nbsp;<\/p>\n<p>\u2022reg51. h is not necessary.<\/p>\n<p>&nbsp;<\/p>\n<p>Write an 8051 C program to turn bit P1.5 on and off 50,000 times.<\/p>\n<p>&nbsp;<\/p>\n<p>Solution<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Sbit MYBIT = 0x95;\/\/ P1^5<\/strong><\/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 intz;<\/p>\n<p>&nbsp;<\/p>\n<p>for (z=0;z&lt;50000;z++)<\/p>\n<p>&nbsp;<\/p>\n<p>{<\/p>\n<p>&nbsp;<\/p>\n<p>MYBIT=1;<\/p>\n<p>&nbsp;<\/p>\n<p>MYBIT=0;<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>This program is similar to Example for unsigned int.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Access Bit-addressable RAM<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>You can use bit to access one bit of bit-addressable section of the data RAM space 20H-2FH.<\/p>\n<p>&nbsp;<\/p>\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>bit membit;\/\/C compiler assign a RAM<\/p>\n<p>&nbsp;<\/p>\n<p>space for mybit<\/p>\n<p>&nbsp;<\/p>\n<p>membit= inbit; \/\/Read P1^0 to RAM<\/p>\n<p>&nbsp;<\/p>\n<p>Write an 8051 C program to get the status of bit P1.0, save it, and send it to P2.7 continuously.<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p><strong>Solution:<\/strong><\/p>\n<p>&nbsp;<\/p>\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) { \/\/repeat forever<\/p>\n<p>&nbsp;<\/p>\n<p>membit= inbit;<\/p>\n<p>&nbsp;<\/p>\n<p>outbit= membit<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p><strong>7.Summary<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In this lecture we discussed C data type for 8051. Discussed C programs for time delay and I\/O operations. Written C code for I\/O bit and I\/O port manipulations.<\/p>\n<p>&nbsp;<\/p>\n<p>8.<strong>References<\/strong><\/p>\n<p>&nbsp;<\/p>\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":16,"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-197","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\/197","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":7,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/pressbooks\/v2\/chapters\/197\/revisions"}],"predecessor-version":[{"id":460,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/pressbooks\/v2\/chapters\/197\/revisions\/460"}],"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\/197\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/wp\/v2\/media?parent=197"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/pressbooks\/v2\/chapter-type?post=197"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/wp\/v2\/contributor?post=197"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/wp\/v2\/license?post=197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}