Partial progress on period measurer.

This commit is contained in:
Jonathan Chan 2018-03-06 17:52:48 -08:00
parent 081695bbd5
commit adf1512ca7
11 changed files with 2091 additions and 14688 deletions

File diff suppressed because it is too large Load Diff

21
project/Makefile Executable file
View File

@ -0,0 +1,21 @@
OBJECTS=main.o
DEVICE = msp430g2553
INSTALL_DIR=$(HOME)/ti/msp430_gcc
GCC_DIR = $(INSTALL_DIR)/bin
SUPPORT_FILE_DIRECTORY = $(INSTALL_DIR)/include
CC = $(GCC_DIR)/msp430-elf-gcc
GDB = $(GCC_DIR)/msp430-elf-gdb
CFLAGS = -I $(SUPPORT_FILE_DIRECTORY) -mmcu=$(DEVICE) -O2 -g
LFLAGS = -L $(SUPPORT_FILE_DIRECTORY) -T $(DEVICE).ld
all: ${OBJECTS}
$(CC) $(CFLAGS) $(LFLAGS) $? -o main.elf
main: main.c
$(CC) $(CFLAGS) $(LFLAGS) $? -S -o main.asm
debug: all
$(GDB) main.elf

1179
project/main.asm Normal file

File diff suppressed because it is too large Load Diff

59
project/main.c Normal file
View File

@ -0,0 +1,59 @@
#include "msp430.h"
#define TXD BIT2
#define RXD BIT1
#define US 1000000
unsigned int TXByte;
void main(void) {
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
BCSCTL1 = CALBC1_1MHZ; // Set range
DCOCTL = CALDCO_1MHZ;
BCSCTL2 &= ~(DIVS_3); // SMCLK = DCO = 1 MHz
P1SEL = BIT1 + BIT2; // P1.1 = RXD, P1.2=TXD
P1SEL2 = BIT1 + BIT2; // P1.1 = RXD, P1.2=TXD
UCA0CTL1 |= UCSSEL_2; // Use SMCLK
UCA0BR0 = 104; // Set baud rate to 9600 with 1MHz clock (Data Sheet 15.3.13)
UCA0BR1 = 0; // Set baud rate to 9600 with 1MHz clock
UCA0MCTL = UCBRS0; // Modulation UCBRSx = 1
UCA0CTL1 &= ~UCSWRST; // Initialize USCI state machine
P1DIR |= TXD;
P1OUT |= TXD;
P1DIR |= BIT0;
TACTL = TACLR; // reset clock
TACTL = TASSEL_2 | MC_2; // set SMCLK timer to count up at 1 MHz
TACCTL0 = CM1 | SCS | CAP | CCIE; // set capture mode to rising edge and enable interrupts
while (1) {
TAR = 0;
P1OUT ^= BIT0;
__enable_interrupt();
__bis_SR_register(LPM0_bits + GIE);
TXByte = US/TACCR0; // frequency in Hz
TXByte /= 2; // frequency won't fit in a byte
while (!(IFG2 & UCA0TXIFG)); // wait for TX buffer to be ready for new data
UCA0TXBUF = TXByte;
__delay_cycles(100000); // wait >10 ms before measuring again
}
}
#if defined(__TI_COMPILER_VERSION__)
#pragma vector=TIMER0_A0_VECTOR
__interrupt void timer0_a0_isr(void)
#else
void __attribute__ ((interrupt(TIMER0_A0_VECTOR))) timer0_a0_isr (void)
#endif
{
TACCTL0 &= ~CCIFG; // set interrupt flag to 0
__disable_interrupt(); // disable interrupts
__bic_SR_register_on_exit(LPM0_bits); // take us out of low power mode
}

BIN
project/main.o Normal file

Binary file not shown.

29
project/measurements.csv Normal file
View File

@ -0,0 +1,29 @@
Distance (cm), Measured (cm),,Difference (cm)
1,3,2,1
1.5,3,2,0.5
2,3,2,0
2.5,3,2,-0.5
3,4,3,0
3.5,4,3,-0.5
4,5,4,0
4.5,5,4,-0.5
5,6,5,0
6,7,6,0
7,8,7,0
8,9,8,0
9,10,9,0
10,11,10,0
15,16,15,0
20,21,20,0
25,26,25,0
30,31,30,0
40,41,40,0
50,51,50,0
60,61,60,0
80,81,80,0
100,101,100,0
120,122,121,1
140,142,141,1
160,162,161,1
180,183,182,2
200,203,202,2
1 Distance (cm) Measured (cm) Difference (cm)
2 1 3 2 1
3 1.5 3 2 0.5
4 2 3 2 0
5 2.5 3 2 -0.5
6 3 4 3 0
7 3.5 4 3 -0.5
8 4 5 4 0
9 4.5 5 4 -0.5
10 5 6 5 0
11 6 7 6 0
12 7 8 7 0
13 8 9 8 0
14 9 10 9 0
15 10 11 10 0
16 15 16 15 0
17 20 21 20 0
18 25 26 25 0
19 30 31 30 0
20 40 41 40 0
21 50 51 50 0
22 60 61 60 0
23 80 81 80 0
24 100 101 100 0
25 120 122 121 1
26 140 142 141 1
27 160 162 161 1
28 180 183 182 2
29 200 203 202 2

BIN
project/measurements.xlsx Normal file

Binary file not shown.

90
project/python-serial-plot.py Executable file
View File

@ -0,0 +1,90 @@
#!/usr/bin/python2.7
import serial # for serial port
import numpy as np # for arrays, numerical processing
from time import sleep,time
import gtk #the gui toolkit we'll use:
# graph plotting library:
from matplotlib.figure import Figure
from matplotlib.backends.backend_gtkagg import FigureCanvasGTKAgg as FigureCanvas
#needs: python2, pyserial, numpy, matplotlib, pygtk
#0) flash the serial temperature measurement program into the msp430
#1) start this program
#2) press the button the Launchpad to enter 'applcation mode'
#3) warm the chip (eg with a light bulb or your fingers)
#4) when you've seen enough, press the reset button on the launchpad
#5) exit the program by pressing 'q' or clicking on the x
#define the serial port. Pick one:
port = "/dev/ttyACM0" #for Linux
#port = "COM5" #For Windows?
#port = "/dev/tty.uart-XXXX" #For Mac?
#function that gets called when a key is pressed:
def press(event):
print('press', event.key)
if event.key == 'q':
print ('got q!')
quit_app(None)
return True
def quit_app(event):
outFile.close()
ser.close()
quit()
#start our program proper:
#open the serial port
try:
ser = serial.Serial(port,2400,timeout = 0.050)
ser.baudrate=9600
# with timeout=0, read returns immediately, even if no data
except:
print ("Opening serial port",port,"failed")
print ("Edit program to point to the correct port.")
print ("Hit enter to exit")
raw_input()
quit()
#create a window to put the plot in
win = gtk.Window()
#connect the destroy signal (clicking the x in the corner)
win.connect("destroy", quit_app)
win.set_default_size(400,300)
yvals = np.zeros(50) #array to hold last 50 measurements
times=np.arange(0,50,1.0) # 50 from 0 to 49.
#create a plot:
fig = Figure()
ax = fig.add_subplot(111,xlabel='Time Step',ylabel='Distance (cm)')
ax.set_ylim(0,255) # set limits of y axis.
canvas = FigureCanvas(fig) #put the plot onto a canvas
win.add(canvas) #put the canvas in the window
# define a callback for when a key is pressed
fig.canvas.mpl_connect('key_press_event',press)
#show the window
win.show_all()
win.set_title("ready to receive data");
line, = ax.plot(times,yvals)
#open a data file for the output
outFile = open("time_and_dist.txt","w")
start_time = time()
ser.flushInput()
while(1): #loop forever
data = ser.read(1) # look for a character from serial port, will wait up to timeout above.
if len(data) > 0: #was there a byte to read? should always be true.
yvals = np.roll(yvals,-1) # shift the values in the array
yvals[49] = ord(data) # take the value of the byte
outFile.write(str(time()-start_time)+" "+str(yvals[49])+"\n") #write to file
line.set_ydata(yvals) # draw the line
fig.canvas.draw() # update the canvas
win.set_title("Distance: "+str(yvals[49])+" cm")
while gtk.events_pending(): #makes sure the GUI updates
gtk.main_iteration()
# sleep(.05) # don't eat the cpu. This delay limits the data rate to ~ 200 samples/s

37
project/python-serial-print.py Executable file
View File

@ -0,0 +1,37 @@
#!/usr/bin/python2.7
import serial # for serial port
import numpy as np # for arrays, numerical processing
#needs: python2, pyserial, numpy,
#0) flash the serial temperature measurement program into the msp430
#1) start this program
#2) press the button the Launchpad to enter 'applcation mode'
#3) warm the chip (eg with a light bulb or your fingers)
#4) when you've seen enough, press the reset button on the launchpad
#5) exit the program by pressing 'q' or clicking on the x
#define the serial port. Pick one:
port = "/dev/ttyACM0" #for Linux
#port = "COM5" #For Windows?
#port = "/dev/tty.uart-XXXX" #For Mac?
#start our program proper:
#open the serial port
try:
ser = serial.Serial(port,2400,timeout = 0.050)
ser.baudrate=9600
# with timeout=0, read returns immediately, even if no data
except:
print ("Opening serial port",port,"failed")
print ("Edit program to point to the correct port.")
print ("Hit enter to exit")
raw_input()
quit()
ser.flushInput()
while(1): #loop forever
data = ser.read(1) # look for a character from serial port - will wait for up to 50ms (specified above in timeout)
if len(data) > 0: #was there a byte to read?
print ord(data)

100
project/serial_duplex.c Normal file
View File

@ -0,0 +1,100 @@
/* Example code demonstrating the use of the hardware UART on the MSP430G2553 to receive
* and transmit data back to a host computer over the USB connection on the MSP430
* launchpad.
* Note: After programming it is necessary to stop debugging and reset the uC before
* connecting the terminal program to transmit and receive characters.
* This demo will turn on the Red LED if an R is sent and turn it off if a r is sent.
* Similarly G and g will turn on and off the green LED
* It also transmits the received character back to the terminal.
FROM: https://bennthomsen.wordpress.com/engineering-toolbox/ti-msp430-launchpad/msp430g2553-hardware-uart/
*/
#include "msp430.h"
void UARTSendArray(char *TxArray, char ArrayLength);
static char data;
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
P1DIR |= BIT0 + BIT6; // Set the LEDs on P1.0, P1.6 as outputs
P1OUT = BIT0; // Set P1.0
BCSCTL1 = CALBC1_1MHZ; // Set DCO to 1MHz
DCOCTL = CALDCO_1MHZ; // Set DCO to 1MHz
/* Configure hardware UART */
P1SEL = BIT1 + BIT2 ; // P1.1 = RXD, P1.2=TXD
P1SEL2 = BIT1 + BIT2 ; // P1.1 = RXD, P1.2=TXD
UCA0CTL1 |= UCSSEL_2; // Use SMCLK
UCA0BR0 = 104; // Set baud rate to 9600 with 1MHz clock (Data Sheet 15.3.13)
UCA0BR1 = 0; // Set baud rate to 9600 with 1MHz clock
UCA0MCTL = UCBRS0; // Modulation UCBRSx = 1
UCA0CTL1 &= ~UCSWRST; // Initialize USCI state machine
IE2 |= UCA0RXIE; // Enable USCI_A0 RX interrupt
__bis_SR_register(LPM0_bits + GIE); // Enter LPM0, interrupts enabled
}
// Echo back RXed character, confirm TX buffer is ready first
#if defined(__TI_COMPILER_VERSION__)
#pragma vector=USCIAB0RX_VECTOR
__interrupt void USCI0RX_ISR(void)
#else
void __attribute__ ((interrupt(USCIAB0RX_VECTOR))) uci0rx_isr(void)
#endif
{
data = UCA0RXBUF;
UARTSendArray("Received command: ", 18);
UARTSendArray(&data, 1);
UARTSendArray("\n\r", 2);
switch(data){
case 'R':
{
P1OUT |= BIT0;
}
break;
case 'r':
{
P1OUT &= ~BIT0;
}
break;
case 'G':
{
P1OUT |= BIT6;
}
break;
case 'g':
{
P1OUT &= ~BIT6;
}
break;
default:
{
UARTSendArray("Unknown Command: ", 17);
UARTSendArray(&data, 1);
UARTSendArray("\n\r", 2);
}
break;
}
}
void UARTSendArray(char *TxArray, char ArrayLength){
// Send number of bytes Specified in ArrayLength in the array at using the hardware UART 0
// Example usage: UARTSendArray("Hello", 5);
// int data[2]={1023, 235};
// UARTSendArray(data, 4); // Note because the UART transmits bytes it is necessary to send two bytes for each integer hence the data length is twice the array length
while(ArrayLength--){ // Loop until StringLength == 0 and post decrement
while(!(IFG2 & UCA0TXIFG)); // Wait for TX buffer to be ready for new data
UCA0TXBUF = *TxArray; //Write the character at the location specified py the pointer
TxArray++; //Increment the TxString pointer to point to the next character
}
}

View File