WouterV

Untitled

Nov 13th, 2018
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.77 KB | None | 0 0
  1. //=========================================================
  2. // src/citylight_devboard_main.c: generated by Hardware Configurator
  3. //
  4. // This file will be updated when saving a document.
  5. // leave the sections inside the "$[...]" comment tags alone
  6. // or they will be overwritten!!
  7. //=========================================================
  8.  
  9. //-----------------------------------------------------------------------------
  10. // Includes
  11. //-----------------------------------------------------------------------------
  12. #include <SI_EFM8SB2_Register_Enums.h>                  // SFR declarations
  13. #include "InitDevice.h"
  14. #include "stdint.h"  // data types like uint8_t
  15.  
  16. // lets test if our data types are working
  17. volatile uint8_t testing_vol;
  18. uint16_t testing16;
  19. int16_t testingsigned;
  20.  
  21. // Helper functions for bit twiddling
  22. #define BV(x)           (1 << x)
  23. #define set_bit(P,B)    (P |= BV(B))
  24. #define clear_bit(P,B)  (P &= ~BV(B))
  25. #define toggleBit(P,B)  (P ^= BV(B))
  26.  
  27. #define battery_voltage     1
  28. #define solar_panel_voltage 0
  29.  
  30. #define SOLAR_ENABLE 0
  31. #define SOLAR_DISABLE 1
  32.  
  33. #define LED_OFF 1
  34. #define LED_ON 0
  35.  
  36.  
  37.  
  38. SI_SBIT (rgb_green, SFR_P2, 0);
  39. SI_SBIT (rgb_blue, SFR_P2, 1);
  40. SI_SBIT (rgb_red, SFR_P2, 2);
  41. SI_SBIT (BTN0, SFR_P0, 2);
  42.  
  43. // $[Generated Includes]
  44. // [Generated Includes]$
  45.  
  46. //-----------------------------------------------------------------------------
  47. // SiLabs_Startup() Routine
  48. // ----------------------------------------------------------------------------
  49. // This function is called immediately after reset, before the initialization
  50. // code is run in SILABS_STARTUP.A51 (which runs before main() ). This is a
  51. // useful place to disable the watchdog timer, which is enable by default
  52. // and may trigger before main() in some instances.
  53. //-----------------------------------------------------------------------------
  54. void SiLabs_Startup (void)
  55. {
  56.   // $[SiLabs Startup]
  57.   // [SiLabs Startup]$
  58. }
  59.  
  60.  
  61. uint16_t global_battery_voltage;
  62. uint16_t global_solar_panel_voltage;
  63.  
  64.  
  65.  
  66. uint16_t measure_voltage(uint8_t pin_select){
  67.     // connect muxer to solar panel, wait 100uS for sampling capacitor to charge
  68.     // do 64 readings, average the result so we get more than 10 bits precision
  69.     // return result
  70.  
  71.     uint8_t n_conversions;
  72.     uint16_t sum_adc_readings;
  73.     uint16_t adc_reading;
  74.  
  75.     uint16_t mv_reading;
  76.  
  77.     if (pin_select == solar_panel_voltage){
  78.         // connect to 1.7 which is the joystick pin
  79.         ADC0MX = ADC0MX_ADC0MX__ADC0P17;
  80.     }
  81.     else if (pin_select == battery_voltage) {
  82.         // connect to 1.7 which is the joystick pin
  83.         ADC0MX = ADC0MX_ADC0MX__ADC0P17;
  84.     }
  85.  
  86.  
  87.  
  88.     //now we wait 100us by setting timing to 0, starting the timer and waiting
  89.     TL0 = 0x00;
  90.     TH0 = 0x00;
  91.     TCON |= TCON_TR0__RUN;
  92.  
  93.     // one period = 400 ns, two periods ~= 1uS, 100uS = 200 periods
  94.     // according to allaboutcircuits with a very high impedance thing this ought to be plenty
  95.     while (TH0 < 200) {;}; // do nothing
  96.  
  97.     // stop the timer
  98.     clear_bit(TCON, 4);
  99.  
  100.  
  101.     for (n_conversions = 0; n_conversions < 64; n_conversions++){
  102.  
  103.         // lets do an ADC conversion, muxing and tracking is already done in hwconfig
  104.         ADC0CN0_ADBUSY = 1;
  105.  
  106.         // flag is set to 1 to indicate readiness
  107.         // set by firmware to 0
  108.         while ( ADC0CN0_ADINT == 0) { };
  109.         ADC0CN0_ADINT = 0; // reset interrupt bit, as per  the datasheet
  110.  
  111.         adc_reading = ADC0;
  112.  
  113.         sum_adc_readings += adc_reading;
  114.     }
  115.  
  116.     // do the calc in a 32 bit number otherwise things get too big and overflow
  117.     // times two because gain is 0,5x, divide by 64 samples
  118.     mv_reading = (unsigned long) sum_adc_readings * 2 / 64 * 1680 / 1024;
  119.  
  120.     return mv_reading;
  121.  
  122. }
  123.  
  124.  
  125.  
  126. //-----------------------------------------------------------------------------
  127. // main() Routine
  128. // ----------------------------------------------------------------------------
  129.  
  130.  
  131. SI_SBIT (solar_panel_enable, SFR_P2, 0); //port 2 pin latch, bit 0, so rgb-green
  132.  
  133. uint16_t time_on = 0; // how long are leds on? measured in interrupts from the wake up event
  134.  
  135. void toggle_leds(){
  136.     rgb_red = !rgb_red;
  137. }
  138.  
  139.  
  140. int main (void)
  141. {
  142.   volatile uint16_t waste_time_timer = 0;
  143.  
  144.   // Call hardware initialization routine
  145.   // This is where the watchdog gets disabled
  146.   enter_default_mode_from_RESET();
  147.  
  148.  
  149.   // waste some time so any mess-ups don't put the chip in an unusable state too soon
  150.   // because then the debugger can't connect
  151.   for (waste_time_timer=0; waste_time_timer < 5; waste_time_timer++){;}
  152.   for (waste_time_timer=0; waste_time_timer < 20; waste_time_timer++){;}
  153.   for (waste_time_timer=0; waste_time_timer < 120; waste_time_timer++){;}
  154.   for (waste_time_timer=0; waste_time_timer < 250; waste_time_timer++){;}
  155.   for (waste_time_timer=0; waste_time_timer < 300; waste_time_timer++){;}
  156.   for (waste_time_timer=0; waste_time_timer < 10000; waste_time_timer++){;}
  157.   for (waste_time_timer=0; waste_time_timer < 120; waste_time_timer++){;}
  158.  
  159.  
  160.  
  161.   while (1)
  162.   {
  163.     // $[Generated Run-time code]
  164.     // [Generated Run-time code]$
  165.  
  166.       if (rgb_red == LED_ON){ // if its on
  167.           time_on += 1;
  168.           if (time_on > 3) { // if on too long, turn off
  169.               rgb_red = LED_OFF;
  170.           }
  171.       }
  172.  
  173.       // first, disconnect solar panel
  174.       solar_panel_enable = SOLAR_DISABLE;
  175.  
  176.  
  177.       // check vbatt, vsolar
  178.       global_battery_voltage     = measure_voltage(battery_voltage     );
  179.       global_solar_panel_voltage = measure_voltage(solar_panel_voltage );
  180.  
  181.       // connect solar panel?
  182.       if (global_solar_panel_voltage < 4100){
  183.           if (global_solar_panel_voltage > global_battery_voltage){
  184.               solar_panel_enable = SOLAR_ENABLE;
  185.             // connect these two
  186.           }
  187.       }
  188.  
  189.  
  190.       // enter sleepMode
  191.       enter_sleep_mode_from_default_mode();
  192.  
  193.       // we can only be here from a wake event: rtc alarm / power button
  194.       // perfect place to enter default mode
  195.       enter_default_mode_from_sleep_mode();
  196.  
  197.  
  198.   }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment