-
Notifications
You must be signed in to change notification settings - Fork 0
/
Piepsie210320.c
110 lines (97 loc) · 2.37 KB
/
Piepsie210320.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
* Piepsie210320.c
*
* Created: 20.03.2021 10:58:47
* Author : 25mmHg
* Device : ATtiny13 / ATtiny2313A
*
* _
* || | |
* o---------o-----. .-----||-----. .--|| ||--.
* | | | || | | |_| |
* | | |100n | | |
* | | | 1 __ 8 | | Piezo |
* | | | -o| |o---o | |
* --- '--)---o| |o---)--' |
* --- .--)---o| |o---)--. |
* 100n | | o---o|__|o- | | |
* | | | | | |
* | | | ATttiny13 | | |
* | | | | | 100 |
* | | | | | | ___ |
* o---------o-----' '-----||-----' '--|___|--'
* 0V | 3V
*
* CR2032
*
*
* (created by AACircuit v1.28.7 beta 10/23/16 www.tech-chat.de)
*
*/
#define F_CPU 4800000UL
#define PIEPA PB1
#define PIEPB PB2
#define PIEPDDR DDRB
#define PIEPPORT PORTB
#define PIEPPIN PINB
#define DET5 PB3
#define DET0 PB4
#define DETDDR DDRB
#define DETPORT PORTB
#define DETPIN PINB
#define GLITCH 2
#define LOOPS 10
#define ERRORS 2
#include <avr/io.h>
#include <avr/sleep.h>
#include <avr/interrupt.h>
#include <util/delay.h>
//Variable delay
void var_delay_10us(uint8_t ten_us)
{
for (uint8_t i=0; i<ten_us; i++) _delay_us(10);
}
//Pieps
void piepsen(uint16_t n, uint8_t t)
{
PIEPPORT |= 1<<PIEPA;
PIEPPORT &= ~(1<<PIEPB);
while(n)
{
n--;
PIEPPIN = (1<<PIEPB | 1<<PIEPA);
var_delay_10us(t);
}
}
//Pin-Change-Interrupt-Routine
ISR(PCINT0_vect)
{
}
int main(void)
{
//init
PIEPDDR |= 1<<PIEPA;
PIEPDDR |= 1<<PIEPB;
DETDDR &= ~(1<<DET5);
DETDDR |= 1<<DET0;
DETPORT |= 1<<DET5;
DETPORT &= ~(1<<DET0);
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
PCMSK |= 1<<PCINT3;
//Job
sei();
while (1)
{
GIMSK |= 1<<PCIE;
sleep_mode();
GIMSK &= ~(1<<PCIE);
uint8_t contact = 0;
for (uint8_t i=0; i < LOOPS; i++)
{
_delay_us(GLITCH);
contact += (DETPIN & (1<<DET5));
}
if(contact <= ERRORS) piepsen(500, 10);
if(contact >= (LOOPS - ERRORS)) piepsen(250, 20);
}
}