Interfacing Input-Output Peripherals
Interfacing Input-Output Peripherals
Interfacing Input-Output Peripherals
Write instructions to initialize and access ports of 8251 microcontroller. Initialize and access ports at the bit level. Use seven-segment LEDs for display. 3.1.1 Problem Statement: Write instructions in C to turn on and off LED connected to Bit7 of port P1 at the interval of one second. Compile the program using Keil compiler. Run the program using the simulator and watch the output of port P1 bit 7. .
#include <reg51.h> // Provides hardware information
void MSDelay (unsigned int Count) ; sbit bitFlip = P1^7; void main (void) { P1 = 00 ; while (1) // Continue execution of following { bitFlip = ~bitFlip; MSDelay(18000); // Delay @ 12 MHz } } void MSDelay ( unsigned int count) { unsigned int a; for (a=0; a<count; a++); return ; }
Part 2: Display Count at Seven-Segment LED 3.2.1 Program /* The following program counts from 0 to 9 at the interval of one second and display the count at a common cathode seven-segment LED connected to port P2 */ #include <reg51.h> // Header file for 8051 char Get7Seg (int BCD); // Prototype for Get7Seg() function char SegCode [] = {0x60, 0xda, 0xf2, 0x66, 0xb6, 0xbe, 0xe0, 0xfe, 0xf6, 0xfc}; void Second(); void main (void) { unsigned int c1; // Declare c1 as char variables P1 = 0; // Initialize port P2 for (c1=0; c1<10; c1++) // 1's digit from 0 to 9 { P1 = Get7Seg(c1); // Display seven-segment code at PORTB Second(); //One second delay if (c1 == 9) c1 = -1; } while (1); // Stay here } // function to select and return a character from the array SegCode char Get7Seg (int BCD) { if (BCD <=9) // check for end of array return SegCode[BCD]; // get proper entry from array else; } void Second() { unsigned int i; unsigned int count =18000000; for(i =0; i<count; i++); }
Extra Credit: Write a program to display '2014' at four common cathode LEDs interfaced as time multiplexed circuit (to be discussed in the class).
#include <reg51.h> // Header file for 8051 char Get7Seg (int BCD); // Prototype for Get7Seg() function //char SegCode [] = {0x60, 0xda, 0xf2, 0x66, 0xb6, 0xbe, 0xe0, 0xfe, 0xf6, 0xfc}; char SegCode [] = {0xda, 0xfc, 0x60, 0x66, 0x00}; char switchPort [] = {0x80, 0x40, 0x20, 0x10}; int i=0; void Second(unsigned int); //sbit multiplex = P3; void main (void) { //unsigned int c1; // Declare c1 as char variables P1 = 0; // Initialize port P2 while(1) // 1's digit from 0 to 9 { P3 = switchPort[0]; P1 = Get7Seg(0); Second(300); P3 = switchPort[1]; P1 = Get7Seg(1); Second(300); P3 = switchPort[2]; P1 = Get7Seg(2); Second(300); P3 = switchPort[3]; P1 = Get7Seg(3); Second(300); } } // function to select and return a character from the array SegCode char Get7Seg (int BCD) { if (BCD <=9) // check for end of array return SegCode[BCD]; // get proper entry from array 3
else; } void Second(unsigned int count) { unsigned int i; for(i =0; i<count; i++); }