Question: microcontrollers question: osc.h file osc.c file please, I need your help with writing the code for this part! yes, this is microcontrollers Part C: Altering
microcontrollers question:
osc.h file
osc.c file
please, I need your help with writing the code for this part!
yes, this is microcontrollers
Part C: Altering the Clock Speed of the Processor Unlike the CPU in a desktop computer, the PIC MCU can work at various speeds. Low speeds reduce the power used by the MCU. Often, the PIC MCU runs off batteries and is run at low speeds to prolong battery life. Laptop computers often vary their speed for the same reason. The speed of the chip is controlled by two registers, register 2.2 OSCCON, the Oscillator Control Register, and register 2.1 OSCTUNE, the Oscillator Tuning Register. Registers are special reserved portions of the PIC MCU's memory whose settings determine how the particular feature will operate. Registers only have 8 setting bits, which can be either on (1) or off (O), and thus are only 8 bits wide. All the bits of the OSCCON and OSCTUN registers can be read or written i.e. changed by you) but some registers have bits that are read-only. The notation in the datasheet for each individual bit is registername so OSCCON refers to the 6th bit of the Oscillator Control Register and OSCCON refers to bits 6, 5, and 4. For extra convenience, the individual bits of a register have names. For instance, OSCCON is called IRCF2 in the datasheet under Register 2-2 on page 30. In the XC8 compiler, you set (turn on the value of a register bit by the code registername+bits.bitname = 1. For example, to set OSCCON, type OSCCONbits.IRCF2 = 1. If you want to clear turn off) the register bit, you type OSCCONbits. IRCF2 = 0. The header file xc.h tells the processor how to interpret your command to set the proper area of memory to the proper state. The header file osch contains a prototype for the function set_OSC_32MHz () to set the processor speed to 8 MHz by setting the appropriate bits of the OSCCON register. The function itself is in the osc.c source file. The function also sets PLLEN, bit 6 of the OSCTUNE register, which further increases the oscillator speed by a factor of four. As a result, the PIC MCU will now operate at 32 MHz. Note that the PLLEN only increases the frequency if the oscillator speed is 4 MHz or 8 MHz. At lower processor speeds, it has no effect. Since osch is not a XC8 header file, you must tell the compiler to use it. In your code you need a #include directive. Use quotes rather than brackets, i.e. "ose.h" rather than cosc.h>. Chevron- brackets indicate that the header file is in the XC8 compiler directory. Also, in the project window, you must add the file to header files. Add functions to make the processor operate at 8 Mhz, 1 Mhz, and 500 kHz. They should be named consistently with set_osc_32MHz (). Run the processor at these different speeds and use the oscilloscope to confirm that the chip frequency changes. Note: You must make sure that you are communicating with the LCD at the correct speed for each clock frequency. The supplied configureUSARTO function should make this easy to do. Note: You should run your programs at the highest speed of the processor for the rest of the course. /* file osc.h */ /* */ /* adapted from July 2006 osc.h by Mike Coombes */ /* function moved to osc.c Nov. 7, 2006 (by Dan Peirce) */ /* Set clock speed to 32 MHZ void set_osc_32MHz (void); file osc.c //*** * * * * * * * ****** * * * ******* 11 // set_osc_32MHZ( 11 sets the oscillator from the default 1 MHz to 32 MHZ // 11 Note TOSC = 1/(32 MHz) = 31.25 ns and TCY = 4*TOSC = 125 ns / /* * * * * * ** * ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * #include #include "osc.h" void set_osc_32MHz (void) int i; // Set the OSCILLATOR OSCCONbits. IRCF2 = 1; Control Register to 8 MHZ OSCCONbits.IRCF1 = 1; OSCCONbits. IRCF0 = 1; OSCTUNEbits.PLLEN = 1; 4 -> 32 MHZ // Enable PLL, boost by for(i=0;i so OSCCON refers to the 6th bit of the Oscillator Control Register and OSCCON refers to bits 6, 5, and 4. For extra convenience, the individual bits of a register have names. For instance, OSCCON is called IRCF2 in the datasheet under Register 2-2 on page 30. In the XC8 compiler, you set (turn on the value of a register bit by the code registername+bits.bitname = 1. For example, to set OSCCON, type OSCCONbits.IRCF2 = 1. If you want to clear turn off) the register bit, you type OSCCONbits. IRCF2 = 0. The header file xc.h tells the processor how to interpret your command to set the proper area of memory to the proper state. The header file osch contains a prototype for the function set_OSC_32MHz () to set the processor speed to 8 MHz by setting the appropriate bits of the OSCCON register. The function itself is in the osc.c source file. The function also sets PLLEN, bit 6 of the OSCTUNE register, which further increases the oscillator speed by a factor of four. As a result, the PIC MCU will now operate at 32 MHz. Note that the PLLEN only increases the frequency if the oscillator speed is 4 MHz or 8 MHz. At lower processor speeds, it has no effect. Since osch is not a XC8 header file, you must tell the compiler to use it. In your code you need a #include directive. Use quotes rather than brackets, i.e. "ose.h" rather than cosc.h>. Chevron- brackets indicate that the header file is in the XC8 compiler directory. Also, in the project window, you must add the file to header files. Add functions to make the processor operate at 8 Mhz, 1 Mhz, and 500 kHz. They should be named consistently with set_osc_32MHz (). Run the processor at these different speeds and use the oscilloscope to confirm that the chip frequency changes. Note: You must make sure that you are communicating with the LCD at the correct speed for each clock frequency. The supplied configureUSARTO function should make this easy to do. Note: You should run your programs at the highest speed of the processor for the rest of the course. /* file osc.h */ /* */ /* adapted from July 2006 osc.h by Mike Coombes */ /* function moved to osc.c Nov. 7, 2006 (by Dan Peirce) */ /* Set clock speed to 32 MHZ void set_osc_32MHz (void); file osc.c //*** * * * * * * * ****** * * * ******* 11 // set_osc_32MHZ( 11 sets the oscillator from the default 1 MHz to 32 MHZ // 11 Note TOSC = 1/(32 MHz) = 31.25 ns and TCY = 4*TOSC = 125 ns / /* * * * * * ** * ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * #include #include "osc.h" void set_osc_32MHz (void) int i; // Set the OSCILLATOR OSCCONbits. IRCF2 = 1; Control Register to 8 MHZ OSCCONbits.IRCF1 = 1; OSCCONbits. IRCF0 = 1; OSCTUNEbits.PLLEN = 1; 4 -> 32 MHZ // Enable PLL, boost by for(i=0;i