write a C program that first initializes the clock speed at 4 MHz. You should th
ID: 3668161 • Letter: W
Question
write a C program that first initializes the clock speed at 4 MHz. You should then have LED1 and LED2 alternate blinking on and off at a rate of 1 Hz (0.5 seconds on, 0.5 seconds off; if LED1 is on, LED2 is off and vice versa). While SW1 is pressed, you must double the blinking frequency by changing the clock speed. While SW2 is pressed, you must half the blinking frequency by changing the clock speed. As soon as the switch is released, the clock speed should be reset to 4 MHz. You must use an interrupt to interface the switches.
Explanation / Answer
#include /* */ void main (void) { unsigned char Port1_value; // Must set up P1 for reading P1 = OxFF; while(l) { // Read the value of P1 Port1_val ue = P1 ; // Copy the value to P2 P2 = Port1_value; } } /* - --- END OF FILE -- -- --* */ Listing 1 0.1 A simple Super Loop application which copies the values from PI to P2 Example: Reading and writing bits Listing 10.1 demonstrated how to read from or write to an entire port. Consider another common problem: reading and writing individual pins on a port. This prob- lem arises because often the various parts of a port will be serving different purposes. Suppose, for example, that we have a switch connected to Port 1 (pin 3) and an LED connected to Port 1 (pin 4) and also have other input and output devices con- nected to the other pins on this port. How do we read from pin 3 and write to pin 4 without disrupting anything else? We can do this by making use of the bitwise AND, OR and 'complement' operators. These, and other, bitwise operators are not widely used by desktop developers. The various bitwise operators allow a number of data manipulations that are invaluable in embedded applications. Some examples of the use of these operators are given in Listing 10.2. Note that this file is written in ISO 'C ('Desktop C): it cannot be run on the Keil compiler. /* - Main.C Copyright © 2001-2008 TTE Systems Ltd. All rights reserved. For further information about time-triggered systems, please visit: www.tte-systems.com 180 SOFTWARE FOUNDATIONS Test program for pattern PORT 1-0 Illustrating the use of bitwise operators #inc1ude void Di sp1ay_Byte(const unsigned char) /* i nt main() { unsigned char x = OxFE; unsigned int y = OxOAOB; printf ("%-35s","X") ; Di sp1ay_Byte(x) ; printf ("%-35s" , "1s complement [~x]"); Di sp1ay_Byte(~x) ; printf ("%- 35s", "Bitwise AND [x & OxOf]"); Disp1ay_Byte(x & OxOf ) ; printf ("%-35s", "Bitwise OR [x | OxOf]"); Disp1ay_Byte(x | OxOf); printf ("%-35s" , "Bitwise XOR [x A OxOf]"); Disp1ay_Byte(x A OxOf); printf ("%-35s" , "Left shift, 1 place [x «= 1]"); Disp1ay_Byte(x «= 1); x = OxFE; /* Return x to original value */ printf ("%-35s", "Right shift, 4 places [x »= 4]"); Disp1ay_Byte(x »= 4); printf(" "); pri ntf ( "%-35s" , "Displ ay MS byte of unsigned int y") Di sp1ay_Byte( (unsigned char) (y » 8)); pri ntf ( "%-35s" , "Displ ay LS byte of unsigned int y") Di sp1ay_Byte(unsigned char) (y & OxFF) ) ; return 0: } Copyright © 2001-2008 TTE Systems Ltd. All rights reserved. This file may be freely redistributed provided only that this footer remains intact. PORT I/O 181 /* */ void Display_Byte(const unsigned char Ch) { unsigned char i, c = Ch; unsigned char Mask = 1 « 7; for (i = 1 : iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.