-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCAR SIM MSP.txt
320 lines (270 loc) · 9.68 KB
/
CAR SIM MSP.txt
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include <msp430.h>
#define NUM_BYTES_TX 2 // How many bytes?
#define NUM_BYTES_RX 6
int RXByteCtr, RPT_Flag = 0; // enables repeated start when 1
volatile unsigned char RxBuffer[6]; // Allocate 128 byte of RAM
unsigned char *PTxData; // Pointer to TX data
unsigned char *PRxData; // Pointer to RX data
unsigned char TXByteCtr, RX = 0;
unsigned char MSData[3];
int x,y,z,i,c[5];
//======ULTRASONIC VARIABLES============//
int g,k;
long int width_l,sensor1,sensor2;
//==================================//
void Setup_TX(void);
void Setup_RX(void);
void Transmit(unsigned char, unsigned char);
void Receive(void);
//==================== (UART Codes) =====================================================//
//------------------------------------------------------------------------------------------------------------------------//
void serialInit()
{
P1SEL |= BIT1 + BIT2; //P1.1 = RXD P1.2=TXD
P1SEL2 |= BIT1 +BIT2; // P1.1=RXD & P1.2=TXD
UCA0CTL1|= UCSSEL_2; // SMCLK
UCA0BR0=104; // BAUDRATE AT 1 MHz 9600
UCA0BR1=0;//1MHz 9600
UCA0MCTL= UCBRS0; // MODULATION UCBRSx=1
UCA0CTL1&=~UCSWRST; // ** INITIALIZE USCI STATE MACHINE
IE2|= UCA0RXIE; // ENABLE VSCI_A0 RX INTERRUPT
return;
}
//------------------------------------------------------------------------------------------------------------------------//
unsigned char serialRead()
{
while(!(IFG2&UCA0RXIFG)); //USCI_A0 RX buffer ready ?
return UCA0RXBUF;
}
//------------------------------------------------------------------------------------------------------------------------//
void serialWrite(unsigned char c)
{
while(!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready ?
UCA0TXBUF=c; // TX
return;
}
//------------------------------------------------------------------------------------------------------------------------//
void serialwriteString(const char *str)
{
while(*str)
serialWrite(*str++);
return;
}
void serial_write_data(unsigned int v)
{
for(i=0;i<5;i++)
{
c[i]=v%10;
v=v/10;
}
serialWrite(c[4]+48);
serialWrite(c[3] +48);
serialWrite(c[2] +48);
serialWrite(c[1] + 48);
serialWrite(c[0] + 48);
serialWrite(' ');
serialWrite('\n');
return;
}
//================================== (ULTRASONIC FUNCTIONS) ===========================//
void trigger (int x);
void get_width_l(int x);
void get_width_l(int x) //Gets Distance from sensor
{
trigger(x);
if(x==1){
while((P1IN & BIT5) !=BIT5)
{
k++;
if(k==5000)
{ k=0; return; }
}
while((P1IN & BIT5)==BIT5)
{
width_l++;
}
sensor1=width_l;
}
else
{
while((P2IN & BIT0) !=BIT0)
{
k++;
if(k==5000)
{ k=0; return; }
}
while((P2IN & BIT0)==BIT0)
{
width_l++;
}
sensor2 = width_l;
}
return;
}
void trigger (int x) // Triggering HCSR04
{
if(x==1){
P1OUT |=BIT3 ;
for(g=0;g<=1500;g++); //delay of 15 micro seconds
P1OUT &=~(BIT3);
}
else{
P1OUT |=BIT4 ;
for(g=0;g<=1500;g++); //delay of 15 micro seconds
P1OUT &=~(BIT4);
}
return;
}
//======================================================================================//
int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
BCSCTL1 = CALBC1_1MHZ;
DCOCTL = CALDCO_1MHZ;
P1DIR |= BIT3 +BIT4 ; //P1.3(sensor1) and P1.4(sensor2) triggers for ultrasonic
//Echo Pins for ultrasonic -p1.5 (sensor1) & p2.0(sensor2)
P1OUT &= 0X0;
P2OUT &= 0x0;
P1SEL |= BIT6 + BIT7; // Assign I2C pins to USCI_B0
P1SEL2|= BIT6 + BIT7; // Assign I2C pins to USCI_B0
//======================( Initital Setup )=======================================================//
serialInit();
Setup_TX();
RPT_Flag = 1;
Transmit(0x2D,0x00);
while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
Setup_TX();
RPT_Flag = 1;
Transmit(0x2D,0x10);
while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
Setup_TX();
RPT_Flag = 1;
Transmit(0x2D,0x08);
while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
Setup_TX();
RPT_Flag = 1;
Transmit(0x31,0x03); // Range Select at add 0x31 write {0x00 for 2g(default)}{0x01 for 4g}{0x02 for 8g}{0x03 for 16g}
while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
//=================================================================================================//
while(1){
//Transmit process
Setup_TX();
RPT_Flag = 1;
Transmit(0x32,0xFE);
while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
//Receive process
Setup_RX();
Receive();
while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
x = (((int)RxBuffer[1]) << 8) | RxBuffer[0];
//x+=256;
y = (((int)RxBuffer[3]) << 8) | RxBuffer[2];
//y+=256;
z = (((int)RxBuffer[5]) << 8) | RxBuffer[4];
//z+=256;
serialwriteString("\033[2J"); // clear screen
serialwriteString("\033[0;0H"); // set cursor to 0,0
serialwriteString("X Axis:");
serial_write_data(x);
serialwriteString("Y Axis:");
serial_write_data(y);
serialwriteString("Z Axis:");
serial_write_data(z);
get_width_l(1);
width_l=0;
get_width_l(2);
width_l=0;
serialwriteString("Sensor1:");
serial_write_data(sensor1);
serialwriteString("Sensor2:");
serial_write_data(sensor2);
__delay_cycles(500000);
}
}
//-------------------------------------------------------------------------------
// The USCI_B0 data ISR is used to move received data from the I2C slave
// to the MSP430 memory. It is structured such that it can be used to receive
// any 2+ number of bytes by pre-loading RXByteCtr with the byte count.
//-------------------------------------------------------------------------------
#pragma vector = USCIAB0TX_VECTOR
__interrupt void USCIAB0TX_ISR(void)
{
if(RX == 1){ // Master Recieve?
RXByteCtr--; // Decrement RX byte counter
if (RXByteCtr)
{
*PRxData++ = UCB0RXBUF; // Move RX data to address PRxData
}
else
{
if(RPT_Flag == 0)
UCB0CTL1 |= UCTXSTP; // No Repeated Start: stop condition
if(RPT_Flag == 1){ // if Repeated Start: do nothing
RPT_Flag = 0;
}
*PRxData = UCB0RXBUF; // Move final RX data to PRxData
__bic_SR_register_on_exit(CPUOFF); // Exit LPM0
}}
else{ // Master Transmit
if (TXByteCtr) // Check TX byte counter
{
UCB0TXBUF = MSData[TXByteCtr]; // Load TX buffer
TXByteCtr--; // Decrement TX byte counter
}
else
{
if(RPT_Flag == 1){
RPT_Flag = 0;
TXByteCtr = NUM_BYTES_TX; // Load TX byte counter
__bic_SR_register_on_exit(CPUOFF);
}
else{
UCB0CTL1 |= UCTXSTP; // I2C stop condition
IFG2 &= ~UCB0TXIFG; // Clear USCI_B0 TX int flag
__bic_SR_register_on_exit(CPUOFF); // Exit LPM0
}
}
}
}
void Setup_TX(void){
_DINT();
RX = 0;
IE2 &= ~UCB0RXIE;
while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent// Disable RX interrupt
UCB0CTL1 |= UCSWRST; // Enable SW reset
UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC; // I2C Master, synchronous mode
UCB0CTL1 = UCSSEL_2 + UCSWRST; // Use SMCLK, keep SW reset
UCB0BR0 = 12; // fSCL = SMCLK/12 = ~100kHz
UCB0BR1 = 0;
UCB0I2CSA = 0x53; // Slave Address is 048h
UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation
IE2 |= UCB0TXIE; // Enable TX interrupt
}
void Setup_RX(void){
_DINT();
RX = 1;
IE2 &= ~UCB0TXIE;
UCB0CTL1 |= UCSWRST; // Enable SW reset
UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC; // I2C Master, synchronous mode
UCB0CTL1 = UCSSEL_2 + UCSWRST; // Use SMCLK, keep SW reset
UCB0BR0 = 12; // fSCL = SMCLK/12 = ~100kHz
UCB0BR1 = 0;
UCB0I2CSA = 0x53; // Slave Address is 048h
UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation
IE2 |= UCB0RXIE; // Enable RX interrupt
}
void Transmit(unsigned char Addr, unsigned char Data){
MSData[2] = Addr; // TX array start address
MSData[1]= Data;
TXByteCtr = NUM_BYTES_TX; // Load TX byte counter
while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
UCB0CTL1 |= UCTR + UCTXSTT; // I2C TX, start condition
__bis_SR_register(CPUOFF + GIE); // Enter LPM0 w/ interrupts
}
void Receive(void){
PRxData = (unsigned char *)RxBuffer; // Start of RX buffer
RXByteCtr = NUM_BYTES_RX-1; // Load RX byte counter
while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
UCB0CTL1 |= UCTXSTT; // I2C start condition
__bis_SR_register(CPUOFF + GIE); // Enter LPM0 w/ interrupts
}