summaryrefslogtreecommitdiffstats
path: root/app/buzzer.c
blob: 4c45630c32811fccbd680d2c1951dd809eae5c06 (plain)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "project.h"

static int period;
static int place;
int ringing;


#define C6 1047
#define E6 1319

void buzzer_on(void)
{
	gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ,
		      GPIO_CNF_OUTPUT_ALTFN_PUSHPULL,
		      GPIO_TIM1_CH1);
	gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ,
		      GPIO_CNF_OUTPUT_ALTFN_PUSHPULL,
		      GPIO_TIM1_CH1N);

  timer_reset(TIM1);

  timer_set_mode(TIM1, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_CENTER_1, TIM_CR1_DIR_UP);
  timer_set_oc_mode(TIM1, TIM_OC1, TIM_OCM_PWM2);
  timer_enable_oc_output(TIM1, TIM_OC1);
  timer_enable_oc_output(TIM1, TIM_OC1N);
  timer_enable_break_main_output(TIM1);

  timer_set_oc_value(TIM1, TIM_OC1, period/2);
  timer_set_period(TIM1, period);

  timer_enable_counter(TIM1);
}
void buzzer_off(void)
{
  gpio_set_mode (GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO8);
  gpio_clear(GPIOA,GPIO8);
  gpio_set_mode (GPIOB, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO13);
  gpio_clear(GPIOB,GPIO13);
  timer_disable_counter(TIM1);
}

void buzzer_set_freq(int hz)
{
  period=(48000000/4)/hz;
  timer_set_oc_value(TIM1, TIM_OC1, period/2);
  timer_set_period(TIM1, period);
}

void buzzer_init(void)
{

  /*buzzer*/
  buzzer_off();
  buzzer_set_freq(440);

}


void ring_tick(void)
{
static int t;

if (!ringing) return;
ringing--;

if (!ringing) {
	place=0;
	buzzer_off();
	return;
}



t++;
if (t<50) return;
t=0;


switch(place){
case 1:	
case 3:
case 5:
case 7:

case 13:
case 15:
case 17:
case 19:
	place++;
	buzzer_set_freq(C6);
	break;

case 0:
case 12:
	place++;
	buzzer_set_freq(E6);
	buzzer_on();
	break;
case 2:
case 4:
case 6:
case 14:
case 16:
case 18:
	place++;
	buzzer_set_freq(E6);
	break;

case 8:
case 20:
	place++;
	buzzer_off();
	break;

default:
	place++;
	break;
case 59:
	place=0;
}




}


void ring_off(void)
{
place=0;
ringing=0;
buzzer_off();
}

void ring(int l)
{
//mb();
ringing=l;
}