i_o programming in 8051 c

6
Subscribe what-when-how In Depth Tutorials and Information I/O PROGRAMMING IN 8051 C SECTION 7.2: I/O PROGRAMMING IN 8051 C In this section we look at C programming of the I/O ports for the 8051. We look at both byte and bit I/O programming. Byte size I/O As we stated in Chapter 4, ports PO – P3 are byte-accessible. We use the PO – P3 labels as defined in the 8051/52 C header file. See Example 7-9. Examine the next few examples to get a better understanding of how ports are accessed in 8051 C. Example 7-9 LEDs are connected to bits PI and P2. Write an 8051 C program that shows the count from 0 to FFH (0000 0000 to 1111 1111 in binary) on the LEDs. Solution: Example 7-10 Write an 8051 C program to get a byte of data from PI, wait 1/2 second, and then send it to P2. Solution: I/O PROGRAMMING IN 8051 C http://what-when-how.com/8051-microcontroller/io-programming-in-... 1 of 6 06-03-2015 18:09

Upload: naresh-ram-surisetti

Post on 17-Aug-2015

233 views

Category:

Documents


1 download

DESCRIPTION

8051c

TRANSCRIPT

Subscribewhat-when-howIn Depth Tutorials and InformationI/O PROGRAMMING IN 8051 CSECTION 7.2: I/O PROGRAMMING IN 8051 CIn this section we look at C programming of the I/O ports for the 8051. We look at both byte and bit I/Oprogramming.Byte size I/OAs we stated in Chapter 4, ports PO P3 are byte-accessible. We use the PO P3 labels as defined in the8051/52 C header file. See Example 7-9. Examine the next few examples to get a better understanding of howports are accessed in 8051 C.Example 7-9LEDs are connected to bits PI and P2. Write an 8051 C program that shows the count from 0 to FFH (0000 0000to 1111 1111 in binary) on the LEDs.Solution:Example 7-10Write an 8051 C program to get a byte of data from PI, wait 1/2 second, and then send it to P2.Solution:I/O PROGRAMMING IN 8051 C http://what-when-how.com/8051-microcontroller/io-programming-in-...1 of 6 06-03-2015 18:09Example 7-11Write an 8051 C program to get a byte of data from PO. If it is less than 100, send it toPI; otherwise, send it to P2.Solution:Bit-addressable I/O programmingThe I/O ports of PO P3 are bit-addressable. We can access a single bit without disturbing the rest of the port.We use the sbit data type to access a single bit of PO P3. One way to do that is to use the PxAy format where xis the port 0, 1, 2, or 3, and y is the bit 0 7 of that port. For example, P1A7 indicates PI.7. When using thismethod, you need to include the reg51 .h file. Study the next few examples to become familiar with the syntax.Example 7-12Write an 8051 C program to toggle only bit P2.4 continuously without disturbing the rest of the bits of P2.Solution:I/O PROGRAMMING IN 8051 C http://what-when-how.com/8051-microcontroller/io-programming-in-...2 of 6 06-03-2015 18:09Example 7-13Write an 8051 C program to monitor bit PI.5. If it is high, send 55H to PO; otherwise, sendAAHtoP2.Solution:Example 7-14A door sensor is connected to the P 1.1 pin, and a buzzer is connected to PI.7. Write an 8051 C program tomonitor the door sensor, and when it opens, sound the buzzer. You can sound the buzzer by sending a squarewave of a few hundred Hz.Solution:I/O PROGRAMMING IN 8051 C http://what-when-how.com/8051-microcontroller/io-programming-in-...3 of 6 06-03-2015 18:09Example 7-15The data pins of an LCD are connected to PI. The information is latched into the LCD whenever its Enable pingoes from high to low. Write an 8051 C program to send The Earth is but One Country to this LCD.Solution:Run the above program on your simulator to see how PI displays each character of the message. Meanwhile,monitor bit P2.0 after each character is issued.Accesssing SFR addresses 80 FFHAnother way to access the SFR RAM space 80 FFH is to use the sfr data type. This is shown in Example 7-16.We can also access a single bit of any SFR if we specify the bit address as shown in Example 7-17. Both the bitand byte addresses for the PO P3 ports are given in Table 7-2. Notice in Examples 7-16 and 7-17, that there isno ^include statement. This allows us to access any byte of the SFR RAM space 80 FFH. This is amethod widely used for the new generation of 8051 microcontrollers, and we will use it in future chapters.Table 7-2: Single Bit Addresses of PortsExample 7-16Write an 8051 C program to toggle all the bits of PO, PI, and P2 continuously with aI/O PROGRAMMING IN 8051 C http://what-when-how.com/8051-microcontroller/io-programming-in-...4 of 6 06-03-2015 18:09250 ms delay. Use the sfr keyword to declare the port addresses.Example 7-17Solution:Using bit data type for bit-addressabl e RAMThe sbit data type is used for bit-addressable SFR registers only. Sometimes we need to store some data in abit-addressable section of the data RAM space 20 2FH. To do that, we use the bit data type, as shown inExample 7-18.Example 7-18I/O PROGRAMMING IN 8051 C http://what-when-how.com/8051-microcontroller/io-programming-in-...5 of 6 06-03-2015 18:09Custom SearchWrite an 8051 C program to get the status of bit Pl.O, save it, and send it to P2.7 continuously.Solution: Next post: DATA TYPES AND TIME DELAY IN 8051 CPrevious post: LOGIC OPERATIONS IN 8051 CRelated Links8051 Microcontroller8051 MICROCONTROLLERSMICROCONTROLLERS AND EMBEDDED PROCESSORSOVERVIEW OF THE 8051 FAMILY8051 ASSEMBLY LANGUAGE PROGRAMMINGINSIDE THE 8051:: Search WWH :: Help Unprivileged ChildrenCareersPrivacy StatementCopyright Information Arduino Programming C Programming Examples AVR Programming AVR Programming 8051 Programming Code for ProgrammingI/O PROGRAMMING IN 8051 C http://what-when-how.com/8051-microcontroller/io-programming-in-...6 of 6 06-03-2015 18:09