/
main.c
171 lines (132 loc) · 3.66 KB
/
main.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <msp430.h>
#include "print.h"
#include "lcd.h"
#include "function_generator.h"
#include "delay.h"
#define FREQ_SEL_BUTTON BIT1
#define WAVE_SEL_BUTTON BIT2
#define DUTY_SEL_BUTTON BIT3
#define PERIOD (1 << 14)
int main(void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
if (CALBC1_16MHZ==0xFF) // If calibration constant erased
{
while(1); // do not load, trap CPU!!
}
DCOCTL = 0; // Select lowest DCOx and MODx settings
BCSCTL1 = CALBC1_16MHZ; // Set range
DCOCTL = CALDCO_16MHZ; // Set DCO step + modulation*/
//configure ACLK to use clock crystal
//DIVA is the ACLK divider.
//When DIVA0 and DIVA1 Bits are zero, ACLK is divided
//by 1
BCSCTL1 &= ~(DIVA0 | DIVA1);
//No touch, BCSTL2, only handles SMCLK and MCLK
//clear the Low Freq XTal 1 sel bits 0 and 1 to select
//32768Hz clk for LFXT1 oscillator
//This also routes LFXT1 into ACLK. if LFXT1Sx = 10, ACLK uses VLOCLK instead
BCSCTL3 &= ~(LFXT1S0 | LFXT1S1);
//set XCAP to 11 to select a 12.5pF crystal capacitance
BCSCTL3 |= XCAP1 | XCAP0;
BUTTONS_INIT:
//configure P1.3 to switch on falling edge interrupts
//force the pin to use the typical GPIO
P1SEL &= ~(FREQ_SEL_BUTTON | WAVE_SEL_BUTTON | DUTY_SEL_BUTTON);
P1SEL2 &= ~(FREQ_SEL_BUTTON | WAVE_SEL_BUTTON | DUTY_SEL_BUTTON);
//select HIGH to LOW transition
P1IES |= FREQ_SEL_BUTTON | WAVE_SEL_BUTTON | DUTY_SEL_BUTTON;
P1IE |= FREQ_SEL_BUTTON | WAVE_SEL_BUTTON | DUTY_SEL_BUTTON;
P1IFG &= ~(FREQ_SEL_BUTTON | WAVE_SEL_BUTTON | DUTY_SEL_BUTTON);
P1DIR &= ~(FREQ_SEL_BUTTON | WAVE_SEL_BUTTON | DUTY_SEL_BUTTON);
P1REN |= FREQ_SEL_BUTTON | WAVE_SEL_BUTTON | DUTY_SEL_BUTTON;
lcd_init();
display_reset();
//FUNCTION_GENERATOR_INIT:
fg_init();
//why not, just go back to home clear everything again
P1IFG |= FREQ_SEL_BUTTON;
_BIS_SR(LPM0_bits + GIE); // Enter LPM0 w/ interrupt
}
void print_frequency(u8 freq)
{
char foo = freq | 0x30;
lcd_put_char(foo);
lcd_print("00Hz");
}
u8 debounce(void)
{
u8 i;
u8 count = 0;
u8 buttons;
for (i = 0; i < 40; ++i)
{
buttons = P1IN & (FREQ_SEL_BUTTON | WAVE_SEL_BUTTON | DUTY_SEL_BUTTON);
if (buttons < (FREQ_SEL_BUTTON | WAVE_SEL_BUTTON | DUTY_SEL_BUTTON)) ++count;
delay_ms(10);
}
if (count > 30) return 1;
return 0;
}
#pragma vector=PORT1_VECTOR
__interrupt void read_buttons(void)
{
if(!debounce()) return;
//wave state can be 0, 1, 2
// 0 is SINE
// 1 is SAW TOOTH
// 2 is SQUARE
static u8 wave_type = 1;
//frequency can be 1, 2, 3, 4, 5
//1 100Hz
//2 200Hz
//3 300Hz
//4 400Hz
//5 500Hz
static u8 frequency = 1;
//duty cycle can be 1, 2, 3, 4, 5, 6, 7, 8, 9
//for 10, 20, 30, 40, 50.. % duty cycle
static u8 duty_cycle = 10;
u8 flags = P1IFG & (FREQ_SEL_BUTTON | WAVE_SEL_BUTTON | DUTY_SEL_BUTTON);
display_reset();
switch(flags)
{
case WAVE_SEL_BUTTON:
LCD_FIRST_LINE();
lcd_print("Set Wave Type");
if (wave_type >= 3) wave_type = 1;
else wave_type++;
LCD_SECOND_LINE();
switch(wave_type)
{
case 1:
lcd_print("Square");
break;
case 2:
lcd_print("Tri");
break;
case 3:
lcd_print("Sine");
break;
}
fg_change_waveform(wave_type);
P1IFG &= ~WAVE_SEL_BUTTON;
break;
case FREQ_SEL_BUTTON:
LCD_FIRST_LINE();
if (frequency >= 5) { frequency = 1; }
else {frequency ++; }
lcd_print("Freq:");
LCD_SECOND_LINE();
print_frequency(frequency);
fg_change_frequency(frequency);
P1IFG &= ~FREQ_SEL_BUTTON;
break;
case DUTY_SEL_BUTTON:
lcd_print("Duty:");
P1IFG &= ~DUTY_SEL_BUTTON;
break;
default:
P1IFG = 0;
}
}