-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.c
199 lines (155 loc) · 5.55 KB
/
init.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <msp430fr6989.h>
#include "init.h"
/*
* init.c
*
* Created on: Apr 6, 2021
* Author: Milton Pagan
*/
void init_clock(void)
{
// Initializes LFXTCLK used for the RTC
// For MCLK and SMCLK default DCO configuration is used (1MHz)
PJSEL0 = BIT4 | BIT5; // Initialize LFXT pins
// Configure LFXT 32kHz crystal
CSCTL0_H = CSKEY >> 8; // Unlock CS registers
CSCTL4 &= ~LFXTOFF; // Enable LFXT
do
{
CSCTL5 &= ~LFXTOFFG; // Clear LFXT fault flag
SFRIFG1 &= ~OFIFG;
} while (SFRIFG1 & OFIFG); // Test oscillator fault flag
CSCTL0_H = 0; // Lock CS registers
}
void init_sched_syst(void)
{
// Scheduling system setup
//*** BUZZER SETUP ***
// Pin
P1DIR |= BUZZER;
P1OUT &= ~BUZZER;
P1SEL1 |= BUZZER;
P1SEL0 |= BUZZER;
// Timer
// Could also use RTC
// Use CCR1
TA0CCTL1 = OUTMOD_7; // set/reset
TA0CTL = (TACLR | TASSEL__SMCLK | ID__1 | MC__UP);
TA0CCR0 = 3000;
//*** RTC SETUP ***
// *** BUTTONS SETUP ***
P2DIR &= ~(UP | DOWN | ENTER | BACK);
P2IFG &= ~(UP | DOWN | ENTER | BACK);
// P2REN |= UP | DOWN | ENTER | BACK;
// P2OUT |= UP | DOWN | ENTER | BACK;
P2IES |= UP | DOWN | ENTER | BACK;
//P2IES &= ~(DOWN | ENTER | BACK);
P2IE |= UP | DOWN | ENTER | BACK;
// Configure RTC_C
// parametros de a~o, mes, dia, dia emana, hora, minutos
RTCCTL0_H = RTCKEY_H; // Unlock RTC
RTCCTL0_L = RTCTEVIE | RTCRDYIE | RTCAIE; // enable RTC read ready interrupt
// enable RTC time event interrupt
RTCCTL1 = RTCBCD | RTCHOLD | RTCMODE; // RTC enable, BCD mode, RTC hold
RTCYEAR = 0x00;
RTCMON = 0x00;
RTCDAY = 0x00;
RTCDOW = 0x00;
RTCHOUR = 0x00;
RTCMIN = 0x00;
RTCSEC = 0x00;
RTCCTL1 &= ~(RTCHOLD); // Start RTC
// *** I2C SETUP ***
// NOTE: eUSCI1_B is used
// Pins
P4SEL1 |= SDA | SCL;
P4SEL0 &= ~SDA & ~SCL;
// I2C registers
}
void init_disp_mech(void)
{
// Dispensing mechanism setup
// Pin for IR
P2DIR &= ~IR; //set as input
P2REN |= IR; //enable resistor
P2OUT |= IR; //set output high to select pull up configuration
P2IES |= IR; //select edge from high to low
P2IFG &= ~IR; //clear any pending interrupt requests
P2IE |= IR; //enable interrupt
//configure PWM output pins for servo motors
P2DIR |= DISPENSER_SERVO + LINEAR_SERVO; //set as output
P2OUT &= ~DISPENSER_SERVO & ~LINEAR_SERVO; //initialize as logic low
//select timer compare functionality
P2SEL1 &= ~DISPENSER_SERVO & ~LINEAR_SERVO;
P2SEL0 |= DISPENSER_SERVO + LINEAR_SERVO;
// stepper pins set up
P3DIR |= BIT0 + BIT1 + BIT2 + BIT3; //set as output
P3OUT &= ~BIT0 & ~BIT1 & ~BIT2 & ~BIT3; //initialize as logic low
// Timers
// Timer for PWM
TB0CCTL5 = OUTMOD_7; //select reset/set (dispenser servo)
TB0CCTL6 = OUTMOD_7; //select reset/set (linear actuator)
TB0CCTL0 &= ~CAP; //set CAP = 0 for compare mode
// initialize at 90 deg = ~1.5ms (testing with 1700)
TB0CCR5 = 1000; //used to be 1700
TB0CCR6 = 1600;
TB0CTL |= TBSSEL__SMCLK + MC__UP + ID_0 + TBCLR; //select SMCLK, divider /1
TB0CCR0 = 20000 - 1; // 1000000/50Hz=20000 for 50Hz PWM period - make TB0CCR0 different from 0 to start the timer
// Timer for stage sequence, 1s period
TA3CTL = TACLR | TASSEL__SMCLK | MC__UP | ID__8;
TA3EX0 = TAIDEX_4; // Divider is 8x5, producing 25000Hz from the 1MHz clock
TA3CCR0 = 38000; // Count for 1.5s interval
// Timer for stepper
TA1CCTL0 &= ~CAP; //compare mode
TA1CCR0 = 2500; //20ms delay for stepper steps: 125000/1000=125 -> 125*30ms=3750
TA1CTL |= TASSEL__SMCLK + MC__UP + ID__8 + TACLR; //SMCLK, /8: 1000000/8 = 125000Hz
// Timer for dispenser servo, 2s period
TA2CTL = TACLR | TASSEL__SMCLK | MC__UP | ID__8;
TA2EX0 = TAIDEX_4; // Divider is 8x8?, producing 25000Hz from the 1MHz clock
TA2CCR0 = 12500;// Count for 0.5s interval
}
void init_comms(void)
{
// Analytics and communication setup
// *** UART Setup ***
// Pins
P4SEL1 &= ~(TX | RX);
P4SEL0 |= TX | RX;
// Configure UART mode
// NOTE: USCI_A0 is used
UCA0CTLW0 |= UCSWRST; //eUSCI in reset
UCA0CTLW0 |= UCSSEL__SMCLK; //select SMCLK as source for TX/RX
// Baud Rate 115200
UCA0BRW = 8; // 1000000/115200 = 8.68
UCA0MCTLW = 0xD600; // 1000000/115200 - INT(1000000/115200)=0.68
// UCBRSx = 0.68 = 0xD6
UCA0CTLW0 &= ~UCSWRST; // init eUSCI
UCA0IE |= UCRXIE; // Enable USCI_A0 RX ISR
UCA0IFG &= ~UCRXIFG; //clear interrupt flags
// *** ADC Setup ***
P8DIR &= FSR;
// Pin
P8SEL1 |= FSR;
P8SEL0 |= FSR;
ADC12CTL0 |= ADC12SHT0_2 | ADC12ON; // 16 clock cycles for sample and hold, ADC12 on
ADC12CTL1 |= ADC12SHP;
ADC12CTL2 |= ADC12RES__12BIT;
ADC12IER0 |= ADC12IE0;
ADC12MCTL0 |= ADC12INCH_7 | ADC12VRSEL_3; //using A7 analog pin
ADC12CTL0 |= ADC12ENC | ADC12SC; //Enable and start conversion
}
void init_unused(void)
{
// Initializes unused pins to reduce power consumption
// Pins
P1DIR |= ~BUZZER;
P2DIR |= ~(UP | DOWN | ENTER | BACK | LIN_SERVO | CONT_SERVO | IR);
// P3DIR |= ~(DR_0 | DR_1 | DR_2 | DR_3);
P4DIR |= ~(SDA | SCL | TX | RX);
P5DIR |= 0xFF;
P6DIR |= 0xFF;
P7DIR |= 0xFF;
P8DIR |= 0xFF;
P9DIR |= 0xFF;
P10DIR |= 0xFF;
}