Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //=========================================================
- // src/citylight_devboard_main.c: generated by Hardware Configurator
- //
- // This file will be updated when saving a document.
- // leave the sections inside the "$[...]" comment tags alone
- // or they will be overwritten!!
- //=========================================================
- //-----------------------------------------------------------------------------
- // Includes
- //-----------------------------------------------------------------------------
- #include <SI_EFM8SB2_Register_Enums.h> // SFR declarations
- #include "InitDevice.h"
- #include "stdint.h" // data types like uint8_t
- // lets test if our data types are working
- volatile uint8_t testing_vol;
- uint16_t testing16;
- int16_t testingsigned;
- // Helper functions for bit twiddling
- #define BV(x) (1 << x)
- #define set_bit(P,B) (P |= BV(B))
- #define clear_bit(P,B) (P &= ~BV(B))
- #define toggleBit(P,B) (P ^= BV(B))
- #define battery_voltage 1
- #define solar_panel_voltage 0
- #define SOLAR_ENABLE 0
- #define SOLAR_DISABLE 1
- #define LED_OFF 1
- #define LED_ON 0
- SI_SBIT (rgb_green, SFR_P2, 0);
- SI_SBIT (rgb_blue, SFR_P2, 1);
- SI_SBIT (rgb_red, SFR_P2, 2);
- SI_SBIT (BTN0, SFR_P0, 2);
- // $[Generated Includes]
- // [Generated Includes]$
- //-----------------------------------------------------------------------------
- // SiLabs_Startup() Routine
- // ----------------------------------------------------------------------------
- // This function is called immediately after reset, before the initialization
- // code is run in SILABS_STARTUP.A51 (which runs before main() ). This is a
- // useful place to disable the watchdog timer, which is enable by default
- // and may trigger before main() in some instances.
- //-----------------------------------------------------------------------------
- void SiLabs_Startup (void)
- {
- // $[SiLabs Startup]
- // [SiLabs Startup]$
- }
- uint16_t global_battery_voltage;
- uint16_t global_solar_panel_voltage;
- uint16_t measure_voltage(uint8_t pin_select){
- // connect muxer to solar panel, wait 100uS for sampling capacitor to charge
- // do 64 readings, average the result so we get more than 10 bits precision
- // return result
- uint8_t n_conversions;
- uint16_t sum_adc_readings;
- uint16_t adc_reading;
- uint16_t mv_reading;
- if (pin_select == solar_panel_voltage){
- // connect to 1.7 which is the joystick pin
- ADC0MX = ADC0MX_ADC0MX__ADC0P17;
- }
- else if (pin_select == battery_voltage) {
- // connect to 1.7 which is the joystick pin
- ADC0MX = ADC0MX_ADC0MX__ADC0P17;
- }
- //now we wait 100us by setting timing to 0, starting the timer and waiting
- TL0 = 0x00;
- TH0 = 0x00;
- TCON |= TCON_TR0__RUN;
- // one period = 400 ns, two periods ~= 1uS, 100uS = 200 periods
- // according to allaboutcircuits with a very high impedance thing this ought to be plenty
- while (TH0 < 200) {;}; // do nothing
- // stop the timer
- clear_bit(TCON, 4);
- for (n_conversions = 0; n_conversions < 64; n_conversions++){
- // lets do an ADC conversion, muxing and tracking is already done in hwconfig
- ADC0CN0_ADBUSY = 1;
- // flag is set to 1 to indicate readiness
- // set by firmware to 0
- while ( ADC0CN0_ADINT == 0) { };
- ADC0CN0_ADINT = 0; // reset interrupt bit, as per the datasheet
- adc_reading = ADC0;
- sum_adc_readings += adc_reading;
- }
- // do the calc in a 32 bit number otherwise things get too big and overflow
- // times two because gain is 0,5x, divide by 64 samples
- mv_reading = (unsigned long) sum_adc_readings * 2 / 64 * 1680 / 1024;
- return mv_reading;
- }
- //-----------------------------------------------------------------------------
- // main() Routine
- // ----------------------------------------------------------------------------
- SI_SBIT (solar_panel_enable, SFR_P2, 0); //port 2 pin latch, bit 0, so rgb-green
- uint16_t time_on = 0; // how long are leds on? measured in interrupts from the wake up event
- void toggle_leds(){
- rgb_red = !rgb_red;
- }
- int main (void)
- {
- volatile uint16_t waste_time_timer = 0;
- // Call hardware initialization routine
- // This is where the watchdog gets disabled
- enter_default_mode_from_RESET();
- // waste some time so any mess-ups don't put the chip in an unusable state too soon
- // because then the debugger can't connect
- for (waste_time_timer=0; waste_time_timer < 5; waste_time_timer++){;}
- for (waste_time_timer=0; waste_time_timer < 20; waste_time_timer++){;}
- for (waste_time_timer=0; waste_time_timer < 120; waste_time_timer++){;}
- for (waste_time_timer=0; waste_time_timer < 250; waste_time_timer++){;}
- for (waste_time_timer=0; waste_time_timer < 300; waste_time_timer++){;}
- for (waste_time_timer=0; waste_time_timer < 10000; waste_time_timer++){;}
- for (waste_time_timer=0; waste_time_timer < 120; waste_time_timer++){;}
- while (1)
- {
- // $[Generated Run-time code]
- // [Generated Run-time code]$
- if (rgb_red == LED_ON){ // if its on
- time_on += 1;
- if (time_on > 3) { // if on too long, turn off
- rgb_red = LED_OFF;
- }
- }
- // first, disconnect solar panel
- solar_panel_enable = SOLAR_DISABLE;
- // check vbatt, vsolar
- global_battery_voltage = measure_voltage(battery_voltage );
- global_solar_panel_voltage = measure_voltage(solar_panel_voltage );
- // connect solar panel?
- if (global_solar_panel_voltage < 4100){
- if (global_solar_panel_voltage > global_battery_voltage){
- solar_panel_enable = SOLAR_ENABLE;
- // connect these two
- }
- }
- // enter sleepMode
- enter_sleep_mode_from_default_mode();
- // we can only be here from a wake event: rtc alarm / power button
- // perfect place to enter default mode
- enter_default_mode_from_sleep_mode();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment