PIC Microcontrollers Programming in C - Example 2 with MikroC Pro for PIC and Protheus

preview_player
Показать описание
This is actually a sequel to the example 1, but deals with a bit more complicated problem... The idea is to make LED diodes on the port B blink slowly. It can be done by setting large value for delay parameter in the Delay function. But there is also another, more efficient manner to make this happen. You remember that this microcontroller has built-in oscillator LFINTOSC which operates at the frequency of 31kHz? Now, it’s time to ‘give it a chance’. The program starts with the do-while loop and remains there for 20 cycles. After each iteration, 100mS delay is provided, which is reflected as relatively fast LED blinking of the port B. When the program exits this loop, the microcontroller starts using the LFINTOSC oscillator as a clock signal source. The LED blinking is considerably slower now even though the program executes the same do-while loop with 10 times shorter delay. For the purpose of making some potentionally dangerous situation more obvious here, control bits are activated by assembly instructions. Simply put, when entering or exiting the assembly instruction in the program, the compiler doesn’t save data on currently active RAM bank, which means that in this program section, bank selection depends on the SFR registers in use. When switching back to the program section written in C, the control bits RP0 and RP1 must return the state they had before ‘assembly language adventure’. In this program, the problem is solved by using the saveBank auxiliary variable which saves the state of these two bits.
Рекомендации по теме
welcome to shbcf.ru