Added comments and changed labels of plot.

This commit is contained in:
Jonathan Chan 2018-02-13 20:46:51 -08:00
parent 2686fb6938
commit 7d88e8635d
2 changed files with 21 additions and 21 deletions

View File

@ -1,22 +1,22 @@
#include "msp430.h"
#define TRIG BIT4
#define ECHO BIT5
#define TXD BIT2
#define RXD BIT1
#define TRIG BIT4
#define ECHO BIT5
#define SOUND 34029 // speed of sound in cm/s
#define US 1000000 // microseconds in a second
unsigned int TXByte;
void main(void) {
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
BCSCTL1 = CALBC1_1MHZ; // Set range
DCOCTL = CALDCO_1MHZ;
BCSCTL2 &= ~(DIVS_3); // SMCLK = DCO = 1 MHz
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
@ -26,30 +26,30 @@ void main(void) {
UCA0MCTL = UCBRS0; // Modulation UCBRSx = 1
UCA0CTL1 &= ~UCSWRST; // Initialize USCI state machine
P1DIR |= TXD;
P1OUT |= TXD;
P1DIR |= TXD;
P1OUT |= TXD;
P1DIR |= TRIG;
P1DIR &= ~ECHO;
P1IE |= ECHO;
P1DIR |= TRIG;
P1DIR &= ~ECHO;
P1IE |= ECHO;
TACTL = TASSEL_2 | ID_0 | MC_2; // set timer to count up
TACTL = TASSEL_2 | MC_2; // set SMCLK timer to count up
__enable_interrupt();
while (1) {
P1OUT |= TRIG; // start trigger signal
__delay_cycles(15); // we need a >10 us pulse but one clock cycle is 1 us
__delay_cycles(15); // we need a >10 us pulse and one clock cycle is 1 us
P1OUT &= ~TRIG; // end trigger signal
P1IES &= ~ECHO;
P1IES &= ~ECHO; // interrupt on low to high
__bis_SR_register(LPM0_bits + GIE);
TAR = 0;
P1IES |= ECHO;
P1IES |= ECHO; // interrupt on high to low
__bis_SR_register(LPM0_bits + GIE);
TXByte = TAR; // time elapsed in us (approx)
TXByte = TAR; // time elapsed in us (no risk of overflow since echo will lower at 36000 us regardless)
while (!(IFG2 & UCA0TXIFG)); // wait for TX buffer to be ready for new data
UCA0TXBUF = TXByte * SOUND / US / 2; // curiously, this is a factor of 2 off
UCA0TXBUF = TXByte * SOUND / US / 2; // distance in cm (curiously, this is a factor of 2 off)
__delay_cycles(100000); // wait 100 ms before measuring again
}

View File

@ -57,7 +57,7 @@ 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='Temp (deg F)')
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
@ -72,7 +72,7 @@ win.set_title("ready to receive data");
line, = ax.plot(times,yvals)
#open a data file for the output
outFile = open("time_and_temp.txt","w")
outFile = open("time_and_dist.txt","w")
start_time = time()
ser.flushInput()
@ -84,7 +84,7 @@ while(1): #loop forever
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("Temp: "+str(yvals[49])+" deg F")
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