Skip to content

Commit

Permalink
- Changed Battery Measurement to use it's own state in statemachine
Browse files Browse the repository at this point in the history
- Changed the VCC measurement to used 2.048V instead of 1.024V as 1.024 is not enough to be used for ADC reference
- Changed ifdef in spi.h file (had wrong flag which was copied from another file)
- Now show battery measurement when booted
- States is again being shown in Click 7Seg Display
- Battery Voltage is also written to 7Seg Display when measure (one decimal)
- Daily numbers of 256s counts changed to be more accurate to 24 hours
  • Loading branch information
henrikkoch committed Jun 27, 2021
1 parent 1eb6986 commit 3263f1f
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 205 deletions.
1 change: 0 additions & 1 deletion Click_7Seg.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ void int2bcd(int number, char comma) {
// display a number between 0-99 in the two 7-segment diplays on Click module "7SegClick"
// if comma == 1 then the first number has a comma turned on


if (number < 100)
if (comma == 1)
spi1_send((C7SEG_CHAR_TABLE[(number/10)+3] | C7SEG_CHAR_TABLE[1]) ,C7SEG_CHAR_TABLE[(number%10)+3]);
Expand Down
177 changes: 84 additions & 93 deletions Click_7Seg.h
Original file line number Diff line number Diff line change
@@ -1,93 +1,84 @@
/* Microchip Technology Inc. and its subsidiaries. You may use this software
* and any derivatives exclusively with Microchip products.
*
* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
* EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED
* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A
* PARTICULAR PURPOSE, OR ITS INTERACTION WITH MICROCHIP PRODUCTS, COMBINATION
* WITH ANY OTHER PRODUCTS, OR USE IN ANY APPLICATION.
*
* IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
* INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
* WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS
* BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE
* FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS
* IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF
* ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
*
* MICROCHIP PROVIDES THIS SOFTWARE CONDITIONALLY UPON YOUR ACCEPTANCE OF THESE
* TERMS.
*/

/*
* File:
* Author:
* Comments:
* Revision history:
*/

// This is a guard condition so that contents of this file are not included
// more than once.
#ifndef XC_CLICK_7SEG_H
#define XC_CLICK_7SEG_H

#include <xc.h> // include processor files - each processor file is guarded.

// TODO Insert appropriate #include <>

// TODO Insert C++ class definitions if appropriate

// TODO Insert declarations

// Comment a function and leverage automatic documentation with slash star star
/**
<p><b>Function prototype:</b></p>
<p><b>Summary:</b></p>
<p><b>Description:</b></p>
<p><b>Precondition:</b></p>
<p><b>Parameters:</b></p>
<p><b>Returns:</b></p>
<p><b>Example:</b></p>
<code>
</code>
<p><b>Remarks:</b></p>
*/
// TODO Insert declarations or function prototypes (right here) to leverage
// live documentation


extern const unsigned char C7SEG_CHAR_TABLE[51];

extern void int2bcd(int number, char comma);
extern void Click_reset(void);
extern void Click_latch_data(void);
extern void watch_dog_incrementing_timer(void);

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
// TODO If C++ is being used, regular C code needs function names to have C
// linkage so the functions can be used by the c code.

#ifdef __cplusplus
}
#endif /* __cplusplus */






#endif /* XC_HEADER_TEMPLATE_H */



/* Microchip Technology Inc. and its subsidiaries. You may use this software
* and any derivatives exclusively with Microchip products.
*
* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
* EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED
* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A
* PARTICULAR PURPOSE, OR ITS INTERACTION WITH MICROCHIP PRODUCTS, COMBINATION
* WITH ANY OTHER PRODUCTS, OR USE IN ANY APPLICATION.
*
* IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
* INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
* WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS
* BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE
* FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS
* IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF
* ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
*
* MICROCHIP PROVIDES THIS SOFTWARE CONDITIONALLY UPON YOUR ACCEPTANCE OF THESE
* TERMS.
*/

/*
* File:
* Author:
* Comments:
* Revision history:
*/

// This is a guard condition so that contents of this file are not included
// more than once.
#ifndef XC_CLICK_7SEG_H
#define XC_CLICK_7SEG_H

#include <xc.h> // include processor files - each processor file is guarded.

// TODO Insert appropriate #include <>

// TODO Insert C++ class definitions if appropriate

// TODO Insert declarations

// Comment a function and leverage automatic documentation with slash star star
/**
<p><b>Function prototype:</b></p>
<p><b>Summary:</b></p>
<p><b>Description:</b></p>
<p><b>Precondition:</b></p>
<p><b>Parameters:</b></p>
<p><b>Returns:</b></p>
<p><b>Example:</b></p>
<code>
</code>
<p><b>Remarks:</b></p>
*/
// TODO Insert declarations or function prototypes (right here) to leverage
// live documentation

extern const unsigned char C7SEG_CHAR_TABLE[51];

extern void int2bcd(int number, char comma);
extern void Click_reset(void);
extern void Click_latch_data(void);
extern void watch_dog_incrementing_timer(void);

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
// TODO If C++ is being used, regular C code needs function names to have C
// linkage so the functions can be used by the c code.

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* XC_CLICK_7SEG_H */
59 changes: 26 additions & 33 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
* PIC12(L)F1822 (@ Tiny iCP07A modified PCB to fit correct on PCB modified pins)
* +-------_-------+
* VDD -> : 1 VDD VSS 8 : <- VSS
* RA5 <> : 2 PGD 7 : <> RA0 DOOR OPEN OUTPUT (reserved for ICSP data)
* BATTERY LOW OUTPUT RA4 <> : 3 PGC 6 : <> RA1 DOOR CLOSED OUTPUT (reserved for ICSP clock)
* RA3-> : 4 *MRCL INT 5 : <> RA2 DOOR INPUT
* RA5 <> : 2 PGD 7 : <> RA0 DOOR OPEN OUTPUT (reserved for ICSP data)
* BATTERY LOW OUTPUT RA4 <> : 3 PGC 6 : <> RA1 DOOR CLOSED OUTPUT (reserved for ICSP clock)
* RA3-> : 4 *MRCL INT 5 : <> RA2 DOOR INPUT
* +---------------+
* DIP-8
*
Expand Down Expand Up @@ -219,8 +219,8 @@ void main(void) {
timer2_init();

#if defined(_16LF1829)
//PWM_init(); // Init of PWM to dim display
//PWM_set(50);
PWM_init(); // Init of PWM to dim display
PWM_set(50);
output_polarity = 0; // used to toogle port pins on every while round
pwm_direction = 1; // set direction of PWM count
Click_latch_data(); // Set Click 7Seg module latch signal correct
Expand All @@ -234,7 +234,7 @@ void main(void) {
disable_watchdog_timer();

#if defined(_16LF1829)
int2bcd(11, 1); // will write 1.1 in 7-seg display
int2bcd(11, 1); // will write 1.1 in 7-seg display [number, comma[YES|NO]]
printf("Welcome to KOCH Engineering GardenGate\r\n");
printf("Microchip 16LF1829 starting up...\r\n");
#endif
Expand All @@ -243,19 +243,7 @@ void main(void) {

// initiate state machine
state_door = DOOR_INPUT; // read door state
state_machine = STATE_JUST_AWAKED;


// check battery voltage at power up by measuring a fixed 1.024volt reference and back calculation the VDD from that
BatteryVoltage = getBatteryVoltage();
millivolts = (8192 / BatteryVoltage) * 1024;
millivolts = millivolts / 8;
if (BatteryVoltage <= VOLTAGE_LOW) { // this if voltage measured is lower than 2.4 volt
// turn on IHC pulse to indicate low voltage
OUTPUT_BATTERY_LOW = PULSE_ON; // turn on battery voltage signal
__delay_ms(IHC_PULSE_WIDTH * 8); // Timer2 takes 8ms therefore 5 * 8ms = ~40 ms for IHC pulse width
OUTPUT_BATTERY_LOW = !PULSE_ON; // turn on battery voltage signal
}
state_machine = STATE_JUST_AWAKED;

DoorStateBeforePrel = DOOR_INPUT;
DoorStateAfterPrel = DOOR_INPUT;
Expand Down Expand Up @@ -298,7 +286,7 @@ void main(void) {
#if defined(_16LF1829)
if (state_machine != write2display_copy) {
if (state_machine != 12) {
int2bcd('0', state_machine); // show current state number in Click 7Seg display
int2bcd(state_machine, 0);
// __delay_ms(10); // by adding a delay here it helps seing what sequence of state the statemachine is in
write2display_copy = state_machine; // update to prevent running update to 7Seg display if not different data to show
}
Expand Down Expand Up @@ -403,7 +391,7 @@ void main(void) {
#endif

void timer2_init(void) {
// Timer2 setup
// Timer2 setup
T2CONbits.T2OUTPS = 0b1111;
T2CONbits.TMR2ON = 1;
//while (PIR1bits.TMR2IF==0); // 6) Enable PWM output pin
Expand All @@ -414,25 +402,30 @@ void timer2_init(void) {
int getBatteryVoltage(void) { // <editor-fold defaultstate="collapsed" desc="getBatteryVoltage function">
// https://edeca.net/pages/measuring-pic-vdd-with-no-external-components-using-the-fvr/
// returns raw maasurement on 1.024V ref with full scale on Vdd.
// this can be used to back calculate the VDD voltage when we know how much 1.024Volt takes on a 10 bit ADC
// this can be used to calculate back the VDD voltage when we know how much 1.024Volt takes on a 10 bit ADC

#if defined(_16LF1829)
FVRCONbits.FVREN = 1; // Fixed Voltage Reference is enabled
while (!FVRCONbits.FVRRDY); // Wait for FVR to be stable (bit is always 1 when using 12xF1823)

ADCON1bits.ADPREF = 0b00; // VREF+ is connected to VDD (works for both 16xL1829 and 12xF1822)
__delay_us(50); // wait minimum 5 usec to stabilize

#if defined(_16LF1829)
// set negative reference. Only possible on 16xF1829
ADCON1bits.ADNREF = 0; // 0 = VREF- is connected to VSS
#endif

FVRCONbits.ADFVR = 0b01; // ADC Fixed Voltage Reference Peripheral output is 1x (1.024V)

FVRCONbits.FVREN = 1; // Fixed Voltage Reference is enabled
while (!FVRCONbits.FVRRDY); // Wait for FVR to be stable

ADCON1bits.ADFM = 1; // Right justify result
//FVRCONbits.ADFVR = 0b01; // ADC Fixed Voltage Reference Peripheral output is 1x (1.024V)
FVRCONbits.ADFVR = 0b10; // ADC Fixed Voltage Reference Peripheral output is 2x (2.048V)
__delay_us(50); // wait minimum 5 usec to stabilize

ADCON1bits.ADFM = 1; // Right justify result. Six Most Significant bits of ADRESH are set to ?0? when the conversion result is loaded.
ADCON0bits.CHS = 0b11111; // FVR (Fixed Voltage Reference) Buffer 1 Output
__delay_us(200); // wait minimum 200 usec

ADCON1bits.ADPREF = 0b00; // VREF+ is connected to VDD
__delay_us(50); // wait minimum 5 usec to stabilize

ADCON0bits.ADON = 1; // Turn on ADC module

__delay_us(50); // wait minimum 5 usec to stabilize

// now make measurement ------------
int adc_val = 0;

Expand Down
13 changes: 7 additions & 6 deletions nbproject/configurations.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@
<conf name="default" type="2">
<toolsSet>
<developmentServer>localhost</developmentServer>
<targetDevice>PIC16LF1829</targetDevice>
<targetDevice>PIC12LF1822</targetDevice>
<targetHeader></targetHeader>
<targetPluginBoard></targetPluginBoard>
<platformTool>pkob4hybrid</platformTool>
<platformTool>ICD4Tool</platformTool>
<languageToolchain>XC8</languageToolchain>
<languageToolchainVersion>2.32</languageToolchainVersion>
<platform>3</platform>
Expand Down Expand Up @@ -216,7 +216,7 @@
<property key="ToolFirmwareFilePath"
value="Press to browse for a specific firmware version"/>
<property key="ToolFirmwareOption.UseLatestFirmware" value="true"/>
<property key="debugoptions.useswbreakpoints" value="false"/>
<property key="debugoptions.useswbreakpoints" value="true"/>
<property key="hwtoolclock.frcindebug" value="false"/>
<property key="memories.aux" value="false"/>
<property key="memories.bootflash" value="true"/>
Expand All @@ -230,15 +230,15 @@
<property key="memories.instruction.ram.ranges"
value="${memories.instruction.ram.ranges}"/>
<property key="memories.programmemory" value="true"/>
<property key="memories.programmemory.ranges" value="0-7ff"/>
<property key="memories.programmemory.ranges" value="0-1fff"/>
<property key="poweroptions.powerenable" value="false"/>
<property key="programmertogo.imagename" value=""/>
<property key="programoptions.donoteraseauxmem" value="false"/>
<property key="programoptions.eraseb4program" value="true"/>
<property key="programoptions.pgmspeed" value="2"/>
<property key="programoptions.pgmspeed" value="Med"/>
<property key="programoptions.preservedataflash" value="false"/>
<property key="programoptions.preservedataflash.ranges"
value="${programoptions.preservedataflash.ranges}"/>
value="${memories.dataflash.default}"/>
<property key="programoptions.preserveeeprom" value="false"/>
<property key="programoptions.preserveeeprom.ranges" value="0-ff"/>
<property key="programoptions.preserveprogram.ranges" value=""/>
Expand Down Expand Up @@ -282,6 +282,7 @@
value="${communication.interface.default}"/>
<property key="communication.interface.jtag" value="2wire"/>
<property key="communication.speed" value="${communication.speed.default}"/>
<property key="debugoptions.simultaneous.debug" value="false"/>
<property key="debugoptions.useswbreakpoints" value="true"/>
<property key="freeze.timers" value="false"/>
<property key="memories.aux" value="false"/>
Expand Down
5 changes: 1 addition & 4 deletions spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@

#if defined(_16LF1829)
#include <xc.h>

//#include <pic16lf1823.h>

#include <pic16lf1829.h>
//#include "spi.h"
#include "spi.h"
#include "Click_7Seg.h" // using Click_latch_data function

/* Pin connections from PIC to 74HC595
Expand Down
Loading

0 comments on commit 3263f1f

Please sign in to comment.