Sunday, March 24, 2013

Interfacing with MATLAB

We can use MATLAB to talk to the instruments in lab via an UART (Universal Asynchronous Receiver/Transmitter). We used the RS-232 serial communications protocol because of its simplicity. Only two lines are required, connecting the tx and rx lines of the two devices. One byte can be transmitted at a time. RS-232 is idle high (i.e. it transmits high voltage when no data is being sent) and active high (i.e. high voltage is a logical one). When a byte is transmitted, the start bit, a logical zero is sent, followed by the byte to be transmitted, and then the stop bit, a logical one. Each bit lasts for a predetermined amount of time, the bit time, which both devices must be aware of. (In reality, the baud rate, which is the inverse of the bit time, is more commonly used.)

We can set up the LogoChip to use serial communication by running the serialcomm.pb PicoBlocks program from the website. Two output pins of the LogoChip are now rx2 and tx2 lines for use in serial communication. (The rx2 is also connected to +4V via a 10 kOhm pullup resistor, while the tx2 does not need to be connected to anything else.) Note that only one set of rx and tx lines may be in use at the same time!!!

We can send the following numbers to the LogoChip (via PicoBlocks) and observe the resulting transmitted byte on an oscilloscope



10 

39 

100 

157 
Pictures taken by Hannah

Since the bit time was  52 microseconds, the baud rate is 19200

We can vary the baud rate by varying the init block in PicoBlocks. In particular, we can change the value of X in the line "write $238 X". From the actual tech specs, we know that the baud rate is 1/(X+1). 

The LogoChip records 10 bit sensor values, so we need to break the number into a low byte and high byte number. The low byte has the 8 LSB, and the high byte has the 2 MSB (and some 0s). 

The PicoBlocks program to implement this is pretty short

We also wanted to have the LogoChip transmit light sensor data at 10 Hz. Every 100 ms, it transmits the low, then high bytes.

Note that we will be using two's complement to transmit negative numbers.

We can use MATLAB to collect and graph the light sensor data! The LogoChip will then turn on the LED depending on how much light is detected by the sensor. Here is the MATLAB code. (Note that I am linking to Hannah's site here, and for the other MATLAB codes)


Video by Hannah

We then use a USB-GPIB interface to connect a function generator to our computer, using the MATLAB code here.

We can play lots of things, including Mary Had a Little Lamb! (MATLAB code for Mary Had a Little Lamb here; code for playing an octave here.)

Video by Hannah

No comments:

Post a Comment