KIR Misc Functions.ino: Difference between revisions

From AnalysIR WiKi
Jump to navigation Jump to search
 
(19 intermediate revisions by the same user not shown)
Line 1: Line 1:
'''''[[KontroLIR - Arduino compatible IR remote control|> KontroLIR Home]]'''''
'''''> [[KontroLIR - Arduino compatible IR remote control|KontroLIR Home]] > [[Customizing the KontroLIR Firmware|back]]'''''


__TOC__
__TOC__




These miscelaneous functions cover:
These miscellaneous functions cover:


* Low Power Operation
* Low Power Operation
Line 10: Line 10:
* Setting MCU oscillator frequency
* Setting MCU oscillator frequency
* Supply voltage measurement
* Supply voltage measurement
* Reading average supply votage from ADC
* Reading average supply voltage from ADC
* Printing out ATmega328PB fuse settings and lockbits, in debug mode.
* Printing out ATmega328PB fuse settings and lockbits, in debug mode.


== initPins ==
== KIR_initPins ==


This function sets the pinMode for the non row/col pins and sets their HIGH/LOW level as appropriate. This essentialy covers the 2 IR receivers, The IR emitter, the 2 I2C pins and the GPIO power supply pins for the optional IR receivers and I2C EEPROM ''(Both of which are powered off by default)''.
This function sets the pinMode for the non row/col pins and sets their HIGH/LOW level as appropriate. This essentially covers the 2 IR receivers, The IR emitter, the 2 I2C pins and the GPIO power supply pins for the optional IR receivers and I2C EEPROM ''(Both of which are powered off by default)''.


== initLowPowerSleep ==
== KIR_initLowPowerSleep ==


This function configures the relevant AVR registers to obtain optimal low power current usage, when sleeping. The sleep mode is set to ''SLEEP_MODE_PWR_DOWN'', which usese the least amount of current when sleeping.
This function configures the relevant AVR registers to obtain optimal low power current usage, when sleeping. The sleep mode is set to ''SLEEP_MODE_PWR_DOWN'', which uses the least amount of current when sleeping.


== goToSleep ==
== KIR_goToSleep ==


This function is called from the main loop every time a button press is processed.
This function is called from the main loop every time a button press is processed.


Before sleeping, the LED is turned off, the UART is disabled, PCINTs are enabled, and then the sleep process is initiated.
Before sleeping, the LED is turned off, the UART is disabled, PCINTs are enabled, and then the sleep process is initiated.Similarly, pcIntCol & scanCode are both reset.


When a button is pressed, control automatically returns to the latter part of this function, where we initially disable sleep mode, and if in debug mode we re-enable the UART and insert a small delay to allow the BAUD rate generator to stabilise.
When a button is pressed, control automatically returns to the latter part of this function, where we initially disable sleep mode, and if in debug mode we re-enable the UART and insert a small delay to allow the BAUD rate generator to stabilise.


Finally we check if the battery level is too low for stable operation. If it is too low we enter a permanent power down mode to avoid potential corruption.
Finally we check if the battery level is too low for stable operation. If it is too low we enter a permanent power down mode to avoid potential corruption.'''''(See Note 1 below)'''''


== initF_CPU ==
''Note 1: This feature has been temporarily removed due to an intermitent issue we detected. We plan to activate it in future once it has been eliminted as the cause.''


This function sets the clock divider to match the MCU effective clock frequency to the one defined in the Arduino IDE (usually 8MHz for battery operation). There is a particular code sequence that must be followed to achieve this. It is highly unlikely that any user will need to change this code. DO NOt CHANGE THIS CODE, unless you fully understand what you are doing.
== KIR_initF_CPU ==
 
This function sets the clock divider to match the MCU effective clock frequency to the one defined in the Arduino IDE (usually 4MHz for battery operation). There is a particular code sequence that must be followed to achieve this. It is highly unlikely that any user will need to change this code. DO NOT CHANGE THIS CODE, unless you fully understand what you are doing.


We test the F_CPU value passed from the Arduino IDE and set the CLKPR register to reflect that clock rate.
We test the F_CPU value passed from the Arduino IDE and set the CLKPR register to reflect that clock rate.


We have designed KontroLIR with a 16MHZ crystal. This delivers optimal operation when used with AnalyIR firmware or IRremote firmware. By dividing the rate by 2 we get the optimal rate for KontroLIR operation with 2xAAA batteries.
We have designed KontroLIR with a 16MHZ crystal. This delivers optimal operation when used with AnalysIR firmware or IRremote firmware. By dividing the rate by 4 we get the optimal 4Mhz rate for KontroLIR operation with 2xAAA batteries.


== checkBattery ==
== KIR_checkBattery ==


In this function we define the upper threshold for 2xAAA batteries as 3.5 volts. vLimit is set to 10 times the voltage, because we have designed the function readAvgVCC to return the battery voltage * 10. If the detected voltage level is above vLimit we return false (not running on battery) otherwise we return true (running on battery). This function is called from the setip function once.
In this function we define the upper threshold for 2xAAA batteries as 3.5 volts. ''vLimit'' is set to 10 times the voltage (35), because we have designed the function ''readAvgVCC'' to return the battery voltage * 10. If the detected voltage level is above ''vLimit'' we return false (not running on battery) and set ''usbPowered'' variable to true otherwise we return true (running on battery). This function is called from the setup function once.


We are assuming that when powered from serialUSB the operating voltage is circa 5Volts. Do not run KontroLIR at 3V3 when powered from the serialUSB header.
We are assuming that when powered from serialUSB the operating voltage is circa 5Volts. Do not run KontroLIR at 3V3 when powered from the serialUSB header.


== checkLowBattery ==
== KIR_checkLowBattery ==


This funtion is called from the main loop.
This function is called from the ''KIR_goToSleep'' function after wake up.


We initially define lowBatteryCheckCount in KIR_Vars.h as 1 to force a batteyr check the first time. Subsequent resets of this variable use the defined value lbCheck, from file KIR_header.h (default of 2)
We initially define lowBatteryCheckCount in KIR_Vars.h as 1 to force a batteyr check the first time. Subsequent resets of this variable use the defined value lbCheck, from file KIR_header.h (default of 2)
Line 55: Line 57:
If we hit the condition to run a low battery check, we first reset the value with lbCheck. Then we read the average value of the supply voltage. If this voltage level is less than the low threshold we output debug info (debug mode only) and put the MCU to permanent sleep. If for any reason the MCU wakes up again, it is immediately put back to sleep.
If we hit the condition to run a low battery check, we first reset the value with lbCheck. Then we read the average value of the supply voltage. If this voltage level is less than the low threshold we output debug info (debug mode only) and put the MCU to permanent sleep. If for any reason the MCU wakes up again, it is immediately put back to sleep.


If the voltage is above the low threshold we simple return.
If the voltage is above the low threshold we simply return.


== readAvgVCC ==
== KIR_readAvgVCC ==


This function is called from the battery check functions above.
This function is called from the battery check functions above.


Here we use a special mechanism which involve the MCU internal voltage reference to measure the supply voltage (or the battery voltage). For additional accuracy we read this 8 times in total and ignore the first 4 reads. The result is the sum of the last 4 reads.
Here we use a special mechanism which involves the MCU internal voltage reference to measure the supply voltage (or the battery voltage). For additional accuracy we read this 8 times in total and ignore the first 4 reads. The result is the sum of the last 4 reads.


Then we get the average of the 4 reads and subsequently scale this value to get the supply voltage * 10. ''(We use 11 instead of 1.1 for the internal reference value to achieve this times ten scaling)''  
Then we get the average of the 4 reads and subsequently scale this value to get the supply voltage * 10. ''(We use 11 instead of 1.1 for the internal reference value to achieve this times ten scaling)''  


In debug mode the voltage level is printed in millivolts.  
In debug mode the voltage level is printed in milli-volts.  


Finally the final result is returned.
Finally the final result is returned.


== printFuses ==
== KIR_printFuses ==
 
 


This function is called from setup and prints out Fuse values, lock bits, signature bytes, Clock calibration value and the MCUs serial number - <u>in debug mode only</u>.


If the value of any fuse, lock bits or signature does not match the expected value, then an "ERROR !!!" text is printed out on the same line as that value.


Otherwise this function does nothing, which is also the default for normal operation.






'''''[[KontroLIR - Arduino compatible IR remote control|> KontroLIR Home]]'''''
'''''> [[KontroLIR - Arduino compatible IR remote control|KontroLIR Home]] > [[Customizing the KontroLIR Firmware|back]]'''''

Latest revision as of 06:18, 16 January 2020

> KontroLIR Home > back


These miscellaneous functions cover:

  • Low Power Operation
  • Low power sleep mode
  • Setting MCU oscillator frequency
  • Supply voltage measurement
  • Reading average supply voltage from ADC
  • Printing out ATmega328PB fuse settings and lockbits, in debug mode.

KIR_initPins

This function sets the pinMode for the non row/col pins and sets their HIGH/LOW level as appropriate. This essentially covers the 2 IR receivers, The IR emitter, the 2 I2C pins and the GPIO power supply pins for the optional IR receivers and I2C EEPROM (Both of which are powered off by default).

KIR_initLowPowerSleep

This function configures the relevant AVR registers to obtain optimal low power current usage, when sleeping. The sleep mode is set to SLEEP_MODE_PWR_DOWN, which uses the least amount of current when sleeping.

KIR_goToSleep

This function is called from the main loop every time a button press is processed.

Before sleeping, the LED is turned off, the UART is disabled, PCINTs are enabled, and then the sleep process is initiated.Similarly, pcIntCol & scanCode are both reset.

When a button is pressed, control automatically returns to the latter part of this function, where we initially disable sleep mode, and if in debug mode we re-enable the UART and insert a small delay to allow the BAUD rate generator to stabilise.

Finally we check if the battery level is too low for stable operation. If it is too low we enter a permanent power down mode to avoid potential corruption.(See Note 1 below)

Note 1: This feature has been temporarily removed due to an intermitent issue we detected. We plan to activate it in future once it has been eliminted as the cause.

KIR_initF_CPU

This function sets the clock divider to match the MCU effective clock frequency to the one defined in the Arduino IDE (usually 4MHz for battery operation). There is a particular code sequence that must be followed to achieve this. It is highly unlikely that any user will need to change this code. DO NOT CHANGE THIS CODE, unless you fully understand what you are doing.

We test the F_CPU value passed from the Arduino IDE and set the CLKPR register to reflect that clock rate.

We have designed KontroLIR with a 16MHZ crystal. This delivers optimal operation when used with AnalysIR firmware or IRremote firmware. By dividing the rate by 4 we get the optimal 4Mhz rate for KontroLIR operation with 2xAAA batteries.

KIR_checkBattery

In this function we define the upper threshold for 2xAAA batteries as 3.5 volts. vLimit is set to 10 times the voltage (35), because we have designed the function readAvgVCC to return the battery voltage * 10. If the detected voltage level is above vLimit we return false (not running on battery) and set usbPowered variable to true otherwise we return true (running on battery). This function is called from the setup function once.

We are assuming that when powered from serialUSB the operating voltage is circa 5Volts. Do not run KontroLIR at 3V3 when powered from the serialUSB header.

KIR_checkLowBattery

This function is called from the KIR_goToSleep function after wake up.

We initially define lowBatteryCheckCount in KIR_Vars.h as 1 to force a batteyr check the first time. Subsequent resets of this variable use the defined value lbCheck, from file KIR_header.h (default of 2)

We first decrement lowBatteryCheckCount and if not zero we ignore this check and return.

If we hit the condition to run a low battery check, we first reset the value with lbCheck. Then we read the average value of the supply voltage. If this voltage level is less than the low threshold we output debug info (debug mode only) and put the MCU to permanent sleep. If for any reason the MCU wakes up again, it is immediately put back to sleep.

If the voltage is above the low threshold we simply return.

KIR_readAvgVCC

This function is called from the battery check functions above.

Here we use a special mechanism which involves the MCU internal voltage reference to measure the supply voltage (or the battery voltage). For additional accuracy we read this 8 times in total and ignore the first 4 reads. The result is the sum of the last 4 reads.

Then we get the average of the 4 reads and subsequently scale this value to get the supply voltage * 10. (We use 11 instead of 1.1 for the internal reference value to achieve this times ten scaling)

In debug mode the voltage level is printed in milli-volts.

Finally the final result is returned.

KIR_printFuses

This function is called from setup and prints out Fuse values, lock bits, signature bytes, Clock calibration value and the MCUs serial number - in debug mode only.

If the value of any fuse, lock bits or signature does not match the expected value, then an "ERROR !!!" text is printed out on the same line as that value.

Otherwise this function does nothing, which is also the default for normal operation.


> KontroLIR Home > back