0% found this document useful (0 votes)
93 views

Microcontroller

This is small book about micro controller.

Uploaded by

shruti bakare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF or read online on Scribd
0% found this document useful (0 votes)
93 views

Microcontroller

This is small book about micro controller.

Uploaded by

shruti bakare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF or read online on Scribd
You are on page 1/ 22
PIC MICROCONTROLLER AND EMBEDDED SYSTEMS Using Assembly and C for PIC18 Muhammad Ali Mazidi Rolin D. McKinlay Danny Causey Pearson Education International BRIEF CONTENTS CHAPTERS Introduction to Computing, The PIC Microcontrollers. History and Features PIC Architecture & Assembly Language Programming Branch, Call, and Time Delay Loop PIC VO Port Programming Arithmetic, Logie Instructions, and Programs Bank Switching, Table Processing, Macros, and Modules PIC Programming in © PICISF Hardware Connection and ROM Loaders PICTS Timer Programming in Assembly and C PICTS Serial Port Programming in Assembly and C Interrupt Programming in Assembly and C LCD and Keyboard Interfacing ADC, DAC, and Sensor Interfacing Using Flash and EEPROM Memories for Data Storage CCP and ECP Programming SPT Protocol and DS1306 RTC Interfucing ‘Motor Control: Relay, PWM, DC, and Stepper Motors APPENDICES PICI8 Instructions: Format and Deseription Basics of Wire Wrapping IC Technology and System Desig sues Floweharts and Pseudocode ICTS Primer for x86 and 8081 Programmers ASCU Codes Assemblers, Development Resources, and Suppliers Data Sheets =a eoR> 2B » 29 155 193 21 299 335 387 23 473 9 529 5369 603 635 03 ma ns mS 750 192 184 156 CONTENTS CHAPTER 0: INTRODUCTION TO COMPUTING SECTION 0.1: NUMBERING AND CODING SYSTEMS SECTION 0.2: DIGITAL PRIMER SECTION 0.3: INSIDE THE COMPUTER CHAPTER 1: THE PIC MICROCONTROLLERS: HISTORY AND FEATURES. SECTION 1.1: MICROCONTROLLERS AND EMBEDDED PROCESSORS: SECTION 1.2: OVERVIEW OF THE PICI8 FAMILY: CHAPTER 2: PIC ARCHITECTURE & ASSEMBLY LANGUAGE, PROGRAMMING SECTION 2.1: THE WREG REGISTER IN THE PIC SECTION 2.2: THE PIC FILE REGISTER SECTION 2.3: USING INSTRUCTIONS WITH THE DEFAULT ACCESS BANK SECTION 2.4: PIC STATUS REGISTER SECTION 2.5: PIC DATA FORMAT AND DIRECTIVES SECTION 2.6: INTRODUCTION TO PIC ASSEMBLY PROGRAMMING SECTION 2.7: ASSEMBLING AND LINKING A PIC PROGRAM SECTION 2.8: THE PROGRAM COUNTER AND PROGRAM ROM SPACE IN THE PIC SECTION 2.9: RISC ARCHITECTURE IN THE PIC ‘SECTION 2.10: VIEWING REGISTER AND MEMORY. WITH MPLAB SIMULATOR (CHAPTER 3: BRANCH, CALL, AND TIME DELAY LOOP. SECTION 3.1: BRANCH INSTRUCTIONS AND LOOPING SECTION 3.2: CALL INSTRUCTIONS AND STACK, ‘SECTION 3.3: PICI8 TIME DELAY AND INSTRUCTION PIPELINE. CHAPTER 4: PIC VO PORT PROGRAMMING SECTION 4.1: VO PORT PROGRAMMING IN PICI8 SECTION 4.2: /O BIT MANIPULATION PROGRAMMING 4 28 39 40 4B 48 37 6 6 7 B 84 87 ” 98 0 u7 129 130 3 CHAPTER 5: ARITHMETIC, LOGIC INSTRUCTIONS, AND. PROGRAMS SECTION 5.1: ARITHMETIC INSTRUCTIONS SECTION 5.2: SIGNED NUMBER CONCEPTS AND, ARITHMETIC OPERATIONS. SECTION 5.3: LOGIC AND COMPARE INSTRUCTIONS, SECTION 5.4: ROTATE INSTRUCTION AND DATA SERIALIZATION SECTION 5.5: BCD AND ASCII CONVERSION CHAPTER 6: BANK SWITCHING, TABLE PROCESSING, MACROS, AND MODULES SECTION 6.1: IMMEDIATE AND DIRECT ADDRESSING MODES SECTION 6.2: REGISTER INDIRECT ADDRESSING MODE SECTION 6.3: LOOK-UP TABLE AND TABLE PROCESSING SECTION 6.4: BIT-ADDRESSABILITY OF DATA RAM, SECTION 6.5: BANK SWITCHING IN THE PICI8 SECTION 6.6; CHECK SUM AND ASCII SUBROUTINES SECTION 6.7; MACROS AND MODULES (CHAPTER 7: PIC PROGRAMMING IN C SECTION 7.1: DATA TYPES AND TIME DELAYS IN SECTION 7.2: 0 PROGRAMMING TN C SECTION 7.3: LOGIC OPERATIONS INC SECTION 7.5: DATA SERIALIZATION IN C SECTION 7.6: PROGRAM ROM ALLOCATION IN C18 SECTION 7.7; DATA RAM ALLOCATION IN C18 CHAPTER 8: PICI8F HARDWARE CONNECTION AND ROM LOADERS SECTION 8.1: PICI8F458/452 PIN CONNECTION SECTION 8.2: PICI8 CONFIGURATION REGISTERS, SECTION 8.3: EXPLAINING THE INTEL HEX FILE FOR PICL8 SECTION 8.4: PICI8 TRAINER DESIGN AND LOADING CHAPTER 9: PICI8 TIMER PROGRAMMING IN ASSEMBLY AND C SECTION 9.1: PROGRAMMING TIMERS 0 AND 1 SECTION 9.2: COUNTER PROGRAMMING SECTION 9.3: PROGRAMMING TIMERS 0 AND 1 TNC SECTION 9.4: PROGRAMMING TIMERS 2 AND 3 158 156 166 im 179 184 193 194 199) 205 214 219 227 234 251 252 259 267 21 280 286 299 300 308 316 323 335 336 355, 362 373 vil CHAPTER 10: PICI8 SERIAL PORT PROGRAMMING IN ASSEMBLY AND C ‘SECTION 10.1: BASICS OF SERIAL COMMUNICATION SECTION 10.2: PICI8 CONNECTION TO RS232 SECTION 10.3: PICI8 SERIAL PORT PROGRAMMING IN ASSEMBLY SECTION 10.4: PICI8 SERIAL PORT PROGRAMMING INC CHAPTER LI: INTERRUPT PROGRAMMING IN ASSEMBLY AND C ‘SECTION 11.1: PICI8 INTERRUPTS SECTION 11.2: PROGRAMMING TIMER INTERRUPTS SECTION 11.3: PROGRAMMING EXTERNAL HARDWARE INTERRUPTS SECTION 11.4: PROGRAMMING THE SERIAL (COMMUNICATION INTERRUPTS, SECTION 11.5: PORTB-CHANGE INTERRUPT SECTION 11.6: INTERRUPT PRIORITY IN THE PICI8 (CHAPTER 12: LCD AND KEYBOARD INTERFACING SECTION 12.1: LCD INTERFACING SECTION 12.2: KEYBOARD INTERFACING CHAPTER 13: ADC, DAC, AND SENSOR INTERFACING SECTION 13.1: ADC CHARACTERISTICS SECTION 13.2: ADC PROGRAMMING IN THE PICI8 SECTION 13.3: DAC INTERFACING SECTION 13.4: SENSOR INTERFACING AND SIGNAL, CONDITIONING CHAPTER 14: USING FLASH AND EEPROM MEMORIES, FOR DATA STORAGE SECTION 14.1: SEMICONDUCTOR MEMORY SECTION 14.2: ERASING AND WRITING TO FLASH. INTHE PICISF SECTION 14.3: READING AND WRITING TO DATA EEPROM IN THE PICI8. CHAPTER 15: CCP AND ECCP PROGRAMMING SECTION 15.1: STANDARD AND ENHANCED CCP MODULES SECTION 15.2: COMPARE MODE PROGRAMMING: SECTION 15.3: CAPTURE MODE PROGRAMMING, SECTION 15.4: PWM PROGRAMMING SECTION 15.5: ECCP PROGRAMMING 387 388 395 397 ala 43 424 429 439 445 449 454 473 474 487 499 500 50s S16 S21 s29 $30 539 535 569 370 573 579 586 992 CHAPTER 16: SPI PROTOCOL AND DS1306 RTC INTERFACING SECTION 16.1: SPI BUS PROTOCOL, SECTION 16.2: DS1306 RTC INTERFACING AND. PROGRAMMING SECTION 16.3: DS1306 RTC PROGRAMMING IN C SECTION 16.4: ALARM AND INTERRUPT FEATURES OF THE DS1306 CHAPTER 17: MOTOR CONTROL: RELAY, PWM, DC, AND STEPPER MOTORS SECTION 17.1: RELAYS AND OPTOISOLATORS, SECTION 17.2: STEPPER MOTOR INTERFACING: SECTION 173: DC MOTOR INTERFACING AND PWM, SECTION 17.4: PWM MOTOR CONTROL WITH CCP SECTION 17.5: DC MOTOR CONTROL WITH ECCP APPENDIX A: PICI8 INSTRUCTIONS: FORMAT AND DESCRIPTION APPENDIX B: BASICS OF WIRE WRAPPING APPENDIX C: IC TECHNOLOGY AND SYS EM DESIGN ISSUES APPENDIX D; FLOWCHARTS AND PSEUDOCODE APPENDIX E: PICI8 PRIMER FOR x86 AND 8051 PROGRAMMERS APPENDIX F: ASCII CODES APPENDIX G: ASSEMBLERS, DEVELOPMENT RESOURCES, AND SUPPLIERS APPENDIX H: DATA SHEETS INDEX 608 619 622 635 636 642 651 663 665 673 m2 ms 745 750 792 754 756 807 CHAPTER 7 PIC PROGRAMMING INC OBJECTIVES Upon completion of this chapter, you will be able to: >> > > > > > > >> Examine C data types for the PICI8 Code C18 programs for time delay and 1/0 operations Code €18 programs for /O bit manipulation Code C18 programs for logic and arithmetic operations Code C18 programs for ASCII and BCD data conversion Code C18 programs for binary (hex) to decimal conversion Code C18 programs for data serialization Understand C18 C compiler RAM and ROM allocation 251 Why program the PIC18 in C? Compilers produce hex files that we download into the ROM of the micro- controller. The size of the hex file produced by the compiler is one of the main ‘concems of microcontroller programmers for two reasons: 1, Microcontrollers have limited on-chip ROM. 2. The code space for the PICIS is limited to 2M. How does the choice of programming language affect the compiled pro- gram size? While Assembly language produces a hex file that is much smaller than €, programming in Assembly language is often tedious and time consuming. On the other hand, C programming is less time consuming and much easier to write, but the hex file size produced is much larger than if we used Assembly language. The following are some of the major reasons for writing programs in € instead of Assembly: 1. It is easier and fess time consuming to write in C than in Assembly. 2. Cis easier to modify and update 3. You can use code available in function libraries. 4. C code is portable to ather microcontrollers with little or no modification. Several third-party companies develop C compilers for the PIC microcon- troller, Our goal is not to recommend one over another, but to provide you with the fundamentals of € programming for the PICI8, You can use the compiler of your choice for the chapter examples and programs. For this book we have chosen Microchip's C18 C compiler to integrate with MPLAB IDE. Microchip has a stu- dent version of the C18 C compiler available for download from their web site. See hp:/iwww. MiesoDigitalEd.com for tutorials on the C18 C compiler and MPLAB simulator. C programming for the PICLS is the main topic of this chapter. In Scction 7.1, we discuss data types, and time delays. 1/O programming is shown in Section 7.2. The logic operations AND, OR, XOR, inverter, and shift are discussed in Section 7.3, Section 7.4 describes ASCII and BCD conversions and checksums. In Section 7.5, data serialization for the PICS is shown. In Section 7.6, we show how the C18 C compiler uses program ROM for data storage. We will examine C18 data RAM allocation in Section 7.7 SECTION 7.1: DATA TYPES AND TIME DELAYS IN C In this section we first discuss C data types for the PICI8 and then provide code for time delay functions. C data types for the PIC18 One of the goals of C18 programmers is to create smaller hex files, s0 it is worthwhile to re-examine C data types for C18. In other words, a good under- standing of C data types for the C18 can help programmers to create smaller hex 252 files, In this section we focus on the specific C data types that ere most useful and widely used for the PICI8 microcontroller. Table 7-1 shows data types and sizes. ‘Table 7-1: Some Data Types Widely Used by C18, Data Ty Size in Bits_Data Range/Usage unsigned char B-bit (010 255 ‘char B-bit 128 to 4127 ‘unsigned int Té-bit 0 to 65,535, int 16-bit =32,768 (0 132.167 unsigned short Té-bit to 65,535, short 1é-bit =32,768 to $32,167 ‘unsigned short lon; abit O10 16,777.215 short long 24-bit —8,388,608 to $8,388,607 unsigned lon, 32-bit D to 4,294,967.295. Tong ‘Bo-bit_—2,147,483,648 to +2,147,483,648 Unsigned char Because the PIC18 is an 8-bit microcontroller, the character data type is the most natural choice for many applications, The unsigned chat is an 8-bit data type that takes a value in the range of 0-255 (00-FFH), It is one of the most widely used data types for the PICTS. In many situations, such as setting a counter value, ‘where there is no need for signed data, we should use the unsigned char instead of the signed char. Remember that C compilers use the signed char as the default unless we put the keyword unsigned in front of the char (see Example 7-1). We can also use the unsigned char data type for a string of ASCII characters, including extended ASCII characters. Example 7-2 shows @ string of ASCII characters. See Example 7-3 for toggling ports. In declaring variables, we must pay careful attention to the size of the data, and try to use unsigned char instead of int if possible. Because the PICI8 micro- controller has a limited number of registers and dat RAM locations, using int in place of cher can lead to a larger-size hex file. Such misuse of data types in com- pilers such as Microsoft Visual C--+ for x86 IBM PCs is not a significant issue. jixample Write a C18 program to send values 00-FF to Port B. Solution: include //fox TRISB and FORTE declarations cia aL uneigned char 2: TRIEB = 0; Hwake Port B an output for (z=0;2¢0255;2++) PORTE = 2; while (1): /INBEDED TP RUNNING IN HARDWARE x Run the above program on yout simulator to see how Port B displays values 00-FFH in binary. Notice that “while(1)” is needed if this program is running in hardware. ——————— CHAPTER 7: PIC PROGRAMMING IN C 253 [Example 72 Write a C18 program to send hex values for ASCII characters of 0, 1, 2,3, 4, 5, A, B, C, and D to Port B. Solutio include void main|(void) : unsigned char mynum[]= "012345ABCD";//data is stored in RAM unsigned char 2: TRISB = 0; //wake Port 8 an output for(20;2<10;204) PORTE = mynum(z) ; while(2): Lstay here forever } ‘Run the above program on your simulator to see how Port B displays values 30H, 31H, 32H, 33H, 34H, 35H, 41H, 42H, 43H, and 44H (the hex values for ASCII 0, 1, 2, ete.). Notice that the last statement “while(1)” is needed only if we run the program in hardware. This is like “GOTO $” or “BRA $” in Assembly language. [Example 7-3 Write a C18 program to toggle all the bits of Port B continuously. Solutios // Toggle PB forever #include void main (void) ( ‘RISB = 0; J/wake Port B an cutput for (1) /frepeat. forever { PORTB = 0x55; //0x indicates the data is in hex (binary) PORTE = OxAA; } } Run the above program on your simulator to sce how Port B toggles continuously. ‘90000000 Examining the Special Function Registers Using MPLAB Signed char ‘The signed char is an 8-bit data type that uses the most significant bit (D7 of D7-D0) to represent the - or + value, AS a result, we have only 7 bits for the magnitude of the signed number, giving us values from ~128 to +127. In situations ‘where + and — are needed to represent a given quantity such as temperature, the use of the signed char data type is necessary. Again, notice that if we do not use the keyword unsigned, the default is the signed value. For that reason we should stick with the unsigned char unless the data needs to be represented as signed numbers. [Example 7-4 ‘Write a C18 program to send values of -4 to +4 to Port B. Solution: [/sign nunbers #inciude void nainivoid) { hax mymomt)= {42,-1,42.-2,43,-3004,-4}) nwighed char TRISB = 07 Jake vort & an outpot for(za07208)2++) PORTS = myn 2]; waile (2); Hatay bere forever ) Run the above program on your simulator to sce how PORTE displays values of 1, FFH, 2, FEH, 3, FDH, 4, and FCH (the hex values for +1,~1, +2, -2, ete.). See Chapter 5 for discussion of signed numbers. Unsigned int “The unsigned int is a 16-bit data type that takes a value in the range of 0 to 65,535 (0000-FFFFH). In the PIC18, unsigned int is used to define 16-bit vari- ables such as memory addresses. It is also used to set counter values of more than 256. Because the PIC® is an 8-bit microcontroller and the int data type takes two bytes of RAM, we must not use the int data type unless we have to. Because rex~ isters and memory accesses are in 8-bit chunks, the rtisuse of int variables will result in a larger hex file. Such misuse is not a problem in PCs with $12 megabytes of memory, the 32-bit Pentium’s registers and memory accesses, and a bus speed of 133 MHz, For PICI8 programming, however, do not use signed int in places where unsigned char will do the job. Of course, the compiler will not generate an crror for this misuse, but the ovethead in hex file size will be noticeable, Also, in situations where there is no need for signed data (such as setting counter values), ‘we should use unsigned int instead of signed int. This gives a much wider range for data declaration. Again, remember that the C compiler uses signed int as the default unless we specify the keyword unsigned. CHAPTER 7: PIC PROGRAMMING IN C 255 Signed int Signed int is a 16-bit data type that uses the most significant bit (DIS of DI5-DO) to represent the — or + value. As a result, we have only 15 bits for the magnitude of the number, or values from ~32,768 to +32,767. Other data types ‘The unsigned int is limited to values 065,535 (0000-FFFFH). The C18 C compiler supports both short long and long data types, if we want values greater than 16-bit, See Table 7-1. The short long value is 24 bits wide, while the long value is 32 bits wide, Example Write a C18 program to toggle all bits of Port B 50,000 times. Solution: #include void main(void} { unsigned int 2: ‘RISB = 0; //wake Port 8 an output for (z=0;2<=50000;2++) { PORTE = 0x55; PORTB = OxAR; } white (a): /istay here forever } Run the above program on your simulator to see how Port B toggles continuously, Notice that the maximum value for unsigned int is 65,535. Example 7-6 Write a C18 program to toggle all bits of Port B 100,000 times. Solution: /{eoggie PB 100,00 tines include void main(void) { unsigned short long 2; unsigned int x; TRISB = 0; //wake Port B an output fox (240; 2<-100000;z++) { PORTE = 0x85; PORT = OxAA; } while); //otay here forever } 256 Time delay ‘There are two ways to create a time delay in C18: 1. Using a simple for loop 2. Using the PICIS timers Incither case, when we write a time delay we must use the oscilloscope to ‘measure the duration of our time delay. Next, we use the for loop to create time delays. The use of the PICI8 timer fo create time delays is postponed until Chapter 9 In creating a time detay using a for loop, we must be mindful of two fac~ tors that can affect the accuracy of the delay: 1. The erystal frequency connected to the OSC1-OSC2 input pins is the most important factor in the time delay calculation. The duration of the clock peri- ‘od for the instmction cycle is a function of this erystal frequency. 2, The second factor that affects the time delay is the compiler used to compile the C program. When we program in Assembly language, we can control the ‘exact instructions and their sequences used in the delay subroutine. Ih the case ‘of C programs, itis the C compiler that converts the C statements and func- tions to Assembly language instructions. As a result, different compilers pro- duce different code. In other words, if we compile a given C program with dif- ferent compilers, each compiler produces different-size hex code. For the above reasons, when we write time delays for C, we must use the oscilloscope to measure the exact duration. Look at Examples 7-7 and 7-8. [Example 7-7 ‘Write a C18 program to toggle all the bits of Port B ports continuously with a 250 ms delay. Assume that the system is PICI8F458 with XTAL = 10 MHz. Solution: Winolude void Mspelay (unsigned int) ; void main(voia) RISE = 0; U/wake Port 8 an ouvput while(1) Jizepeat forever PORTS = 0x55; MsDeLay (250) ; PORTE > OXBA; mpelay (250) ; } void MEDelay{ungigned int itina) i wnaigned int i; unsigned char j; for (1e0;icitimesi++) For (9=0;4<155;544) 7 — CHAPTER 7; PIC PROGRAMMING IN C 287 [Example 7-8 Write a C18 program to toggle all the bits of Port C and Port D continuously with a 250 ms delay. Solution: J/chis program is tested for the PICLaF4se with XTAL = 10 Miz include void MsDelay (unsigned int); void main(void) { TRISC = 0; TRISD = 0; Jfwake Yorte C and D output while (2) V/another way to do it forever FORTC = 0x55; PORTD = 0x55; MsDelay (250); PORTC = OxAA; PORTD = OxAA; Mspelay (250) ; } t void Msbelay (unsigned int itine} { unsigned int i; unsigned char 3; for (i=G)icitime;ie+) for(=0)j<165;5++); MPLAB's simulator has a stop- watch function that allows us to view the time delay before we. rogram the microcontroller. Figure 7-2. Time Delay Measurement for Example 7-8 Using MPLAB Review Questions 1. Give the magnitude of the unsigned char and signed char data types. 2. Give the magnitude of the unsigned int and signed int data types. 3. If'we are declaring a variable for a person's age, we should use the data type. 4. True or false. Using a for loop to create a time delay is not recommended if ‘you want your code be portable to other PICI8-based systems. 5. Give two factors that can affect the delay size. =) 258 SECTION 7.2: /0 PROGRAMMING IN C In this section we look at C programming of the 1/0 ports for the PICI8. We look at both byte and bit /O programming. Byte size 0 ‘As we stated in Chapter 4, ports PORTA-PORTD are byte accessible. We use the PORTA-PORTD labels as defined in the C18 header file. See Example 7-9. Examine the next few examples to get a better understanding of how ports are accessed in C18. Example 7-9 LEDs are connected to bits in Port B and Port C. Write a C18 program that shows the ‘count. from 0 to FFH (0000 0000 to 1111 1111 in binary) on the LEDs. Solution: include fidefine LED PORTC J/nctice how we can define Port ¢ void main(voia) { ‘RISE = Jfoake Port B an output TRISC = V/oake Port C an output PORTE = Y/cleax Port B LED = oy Vfelear Port C for (33) /frepeat forever { PORTB ++; S/imerenent Port 8 ‘LED++; (Ancrement Port ¢ } Siena ERT eee TENTED [Tasseess eT sre tensa [peste T oameer [eee Tal 9 00 co 60 00 60 00 00 = oo 09 00 FF 00 00 00 1€ 00 05 00 Figure 7-3. Example 7-9 Results After 24 Terations CHAPTER 7: IC PROGRAMMING IN C 259 [Example 7-10 ‘Write « C18 program to get a byte of data from Port itt Por, , wait 1/2 second, and then send Solution: include void MsDelay (unsigned int) ; void main(void) uneigned char aybyte, RISE = OxFF; (Port Bae input RISC = 0; “(Port © as output while(2) { nybyte = PORTE; /iget a byte from Port B meDelay (500) ; FORTC = mybyte; //send it to Port © } , void Msbelay (unsigned int itime) { uneigned int i; unsigned char j; for (i=0;icitime; i++) for (30 ;5<16535+41 (Example 7-11 Write a C18 program to get a byte of dats from Port C. If it is less than 100, send it to Port B; otherwise, send it to Port D. Solution: Hinclude fidefine mybit PORTBbits.aB¢ //declare single bit void main(void) { ‘TRIGBbite.TRISB4-0; //wake RBA an output while (1) - mybit = 1) f/turn on RE mybit = 07 (Journ off RB4 ie [Example 7-13, Write a C18 program to monitor bit PCS. Ifit is HIGH, send $$H to Port B; otherwise, send AAH to Port D. Solution: include fdefine mybit PORTCbits.RcS //notice single-bit declaration void main(void) { ‘TRISCDits.TRISCS = 1; 1/xcs as input RISD = 0; //eorts © and D output while (1) it(mybit == 1) PORTD = 0x55; else PORTD = OxAA; 0000000 Figure 7-5. Example 7-13 Results on MPLAB Simulator 262 [Example 7-14 "A door sensor is connected to the RBI pin, and a buzzer is connected to RC7. Write a C18 program to monitor the door sensor, and when it opens, sound the buzzer. You can sound the buzzer by sending a square wave of a few hundred Hz frequency to it. Solution: #include void MSDelay (unsigned int) define Dsensor PORTSbits.RBI define buzzer PORTCDits.RC7 void main(voia) { ‘qaissbits.TRIsB1 = ‘TRISCbite.TRISC? = //PORTB.1 as an input //make PORTC.7 an output while (Dsensor == 1) { buzzer = 0; MsDelay (200) ; buzzer = 1; MsDelay (200) 7 while (2); //stay here forever } void Mspelay(unsigned int itime) { unsigned int i; unsigned char j; for (i=0;icitimesi++) for (j=075<16! Figure 7-6, MPLAB Logic Analyzer for Example 7-14 CHAPTER 7: PIC PROGRAMMING IN C 263 [Example 7-16 Write a C18 program to toggle all the bits of Port B, Port C, and Port D continuously with a 250 ms delay. Solution: include void MSDelay (unsigned int); void main (void) { RISB = 0; TRIEC = 07 TRIED © 0; while() { {40 it forever { PORTE = 0x55; FORTC = 0x55; PORTD = 0255; MsDelay (2503; (11250 ms delay PORT = OXAA; FORTC = OxAA; FORTD = OxAA; MsDelay (250) ; t ) void MsDelay(unsigned int itime) { vnsigned int i: ungigned char J; for (in0/deitime; i++) for (J=0:4<165;3+4) ¢ [Example 7-17 Write a C18 program to turn bit $ of Port B on and off 50,000 times, Solution: #inclade define MYBTT PORTBbits.RBS void main (void) { unsigned int z; TRISBbite.TRISES = 0; Jfeake PORTE.5 an output for (2=0;2¢50000;2++) { MBIT = 1; MyBIT = 0; ) wattle (1) /fstay here forever } CHAPTER 7: PIC PROGRAMMING IN C 265, [Example 7-18 _ OT ] Write a C18 program to get the status of bit REO, and send it to RC7 continuously. Solution: #include define inbit PORTBbits. REO Hdefine outbit PORTCbits.RC7 void main (void) ‘TRISBbite-7RISBO = 1 //make 820 an input ‘TRISCbite-TRISC7 = 0; /foake RC7 an output waite (1) { outbit = inbit, Jfget 2 bit from RRO Y/and gend it to RC? 1 } 2 Finelude 2 fdefine inbit PORTBbits .RS0 35 Hdefine outbit PORTCbits.RC7 4 void main(void) 5 { 6 ‘TRIspbite TRIS = 1; ffoake FBO an input ag00R2 992 BSF Oxf93, 0, ACCESS 1 ‘TRISeite.TRISCT = 0; Afwoke RET an output oon0Es 9594 RCF 0xE94, 0x7, ACCESS 8: wate (1) ODOR? DTFS BRA Oxe6 9 { 10 cutbit - inbity Jiget bit fron RBO ogg0B6 508, MOVF Oxf82, W, ACCESS 000988 9501, ANDLN Oxi 0oaDEA E002 52 oxfo cooozc eBe2 REF Cxf82, 0x7, ACCESS 000ZE Dd01 BRA Oxf2 coooFo ps2 OP cxée2, ox7, accass ua: /Jand send it to RCT } } 00004 0012 RETURN 0 Figure 7-8, Disassembly of Example 7-18 Review Questions The address of PORT is Write a short program that toggles all bits of PORTC. Write a short program that toggles only bit 0 of PORTB. True or false. All bits of PORTB are bit addressable. Truc or false. All bits of TRISB are bit addressable, 266

You might also like