~gabe/flashlight-firmware/anduril2

« back to all changes in this revision

Viewing changes to ToyKeeper/battcheck/temperature.c

  • Committer: Selene Scriven
  • Date: 2017-09-12 23:34:36 UTC
  • mto: (188.1.3 trunk)
  • mto: This revision was merged to the branch mainline in revision 331.
  • Revision ID: bzr@toykeeper.net-20170912233436-d3w6nln0ts1subue
Added Flintrock's Bistro-HD 1.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This firmware simply helps calibrate values for temperature readings.
 
3
 * It is not intended to be used as an actual flashlight.
 
4
 *
 
5
 * It will read the voltage, then read out the raw value as a series of
 
6
 * blinks.  It will provide up to three groups of blinks, representing the
 
7
 * hundreds digit, the tens digit, then the ones.  So, for a raw value of 183,
 
8
 * it would blink once, pause, blink eight times, pause, then blink three times.
 
9
 * It will then wait longer and re-read the voltage, then repeat.
 
10
 *
 
11
 * Attiny25/45/85 Diagram
 
12
 *           ----
 
13
 *   RESET -|1  8|- VCC
 
14
 *  Star 4 -|2  7|- Voltage ADC
 
15
 *  Star 3 -|3  6|- PWM
 
16
 *     GND -|4  5|- Star 2
 
17
 *           ----
 
18
 */
 
19
 
 
20
//#define ATTINY 13
 
21
//#define ATTINY 25
 
22
#define FET_7135_LAYOUT  // specify an I/O pin layout
 
23
// Also, assign I/O pins in this file:
 
24
#include "tk-attiny.h"
 
25
 
 
26
/*
 
27
 * =========================================================================
 
28
 * Settings to modify per driver
 
29
 */
 
30
 
 
31
#define BLINK_PWM   10
 
32
 
 
33
/*
 
34
 * =========================================================================
 
35
 */
 
36
 
 
37
#define OWN_DELAY           // Don't use stock delay functions.
 
38
#define USE_DELAY_S         // use _delay_s()
 
39
#define USE_DELAY_MS        // Also use _delay_ms()
 
40
#include "tk-delay.h"
 
41
 
 
42
#include <avr/pgmspace.h>
 
43
#include <avr/interrupt.h>
 
44
#include <avr/sleep.h>
 
45
 
 
46
#define THERMAL_REGULATION
 
47
#define TEMP_10bit
 
48
#include "tk-voltage.h"
 
49
 
 
50
void noblink() {
 
51
    PWM_LVL = (BLINK_PWM>>2);
 
52
    _delay_ms(5);
 
53
    PWM_LVL = 0;
 
54
    _delay_ms(200);
 
55
}
 
56
 
 
57
void blink() {
 
58
    PWM_LVL = BLINK_PWM;
 
59
    _delay_ms(100);
 
60
    PWM_LVL = 0;
 
61
    _delay_ms(200);
 
62
}
 
63
 
 
64
int main(void)
 
65
{
 
66
    // Set PWM pin to output
 
67
    DDRB = (1 << PWM_PIN);
 
68
 
 
69
    // Set timer to do PWM for correct output pin and set prescaler timing
 
70
    TCCR0A = 0x21; // phase corrected PWM is 0x21 for PB1, fast-PWM is 0x23
 
71
    TCCR0B = 0x01; // pre-scaler for timer (1 => 1, 2 => 8, 3 => 64...)
 
72
 
 
73
    // Turn features on or off as needed
 
74
    ADC_on_temperature();
 
75
    ACSR   |=  (1<<7); //AC off
 
76
 
 
77
    // blink once on receiving power
 
78
    PWM_LVL = 255;
 
79
    _delay_ms(5);
 
80
    PWM_LVL = 0;
 
81
    _delay_s();
 
82
 
 
83
    uint16_t value;
 
84
    uint8_t i;
 
85
    value = get_temperature();
 
86
 
 
87
    while(1) {
 
88
        PWM_LVL = 0;
 
89
 
 
90
        // get an average of several readings
 
91
        value = 0;
 
92
        for (i=0; i<8; i++) {
 
93
            value += get_temperature();
 
94
            PWM_LVL = 2;
 
95
            _delay_ms(25);
 
96
            PWM_LVL = 0;
 
97
            _delay_ms(25);
 
98
        }
 
99
        value = value >> 3;
 
100
        _delay_s();
 
101
 
 
102
        // thousands
 
103
        while (value >= 1000) {
 
104
            value -= 1000;
 
105
            blink();
 
106
        }
 
107
        _delay_s();
 
108
 
 
109
        // hundreds
 
110
        while (value >= 100) {
 
111
            value -= 100;
 
112
            blink();
 
113
        }
 
114
        _delay_s();
 
115
 
 
116
        // tens
 
117
        if (value < 10) {
 
118
            noblink();
 
119
        }
 
120
        while (value >= 10) {
 
121
            value -= 10;
 
122
            blink();
 
123
        }
 
124
        _delay_s();
 
125
 
 
126
        // ones
 
127
        if (value <= 0) {
 
128
            noblink();
 
129
        }
 
130
        while (value > 0) {
 
131
            value -= 1;
 
132
            blink();
 
133
        }
 
134
        _delay_s();
 
135
 
 
136
        // ... and wait a bit for next time
 
137
        _delay_s();
 
138
        _delay_s();
 
139
 
 
140
    }
 
141
}