Using USART features in PIC18F45K20 Microcontrollers
Purpose: The purpose of this lab is to learn how to interface a PIC via the serial interface and send/receive data to/from a terminal. This lab contains the following parts:
- Programing USART module
- Interfacing your PIC with a Computer
- Learn how to write a Matlab program to plot the data
Parts Required:
- Jumper cables (35-40)
- Potentiometer (1)
- PICKit3 and USB connector (1)
- Breadboard (minimum of 40 rows are required) (1)
- PIC18F45K20 chip (through-hole) (1)
- Push-button switch (2)
- LEDs
- LCD
- Android Smart Phone!
- Bluetooth Module
- PICKit3 Demo Board (optional)
Useful Links:
- To learn about various functionalities of this PIC chip please refer to the data sheet for PIC18F45K20 microcontroller.
- ADC-Assignment
Procedure:
Part I - Interfacing your PIC with a Computer (PIC --> PC)
Purpose: In this section you learn how to transmit data from your PIC to a computer using a RealTerm or QuickTerm for MAC users. Here is the list of terminal emulators you can use for this lab. You can also try your Chrome Browser to read the serial port by adding Serial-monitor. If you are using a Bluetooth Module , you can use an application such as Blue tooth Terminal for your smart phone to monitor the transmitted signal from your PIC.
- Read the lecture slides on EUSART carefully.
- Read this USART tutorial carefully.
- Here is the USART code to try out. You may need to make minor changes depending on which compiler you use.
- Make sure you setup the circuit as mentioned in the tutorial. You can watch this video; special thanks to Eric Zaro. Note that you may need a USB-Serial converter. You can also use a Bluetooth Module. Note that in this case you may have to "invert the transmitted bits"
- Change the program to print your first name on the terminal and show the program to your instructor.
- Using the scope capture the actual signal representing your first name. Explain what you see. This will be similar to what we explained in our lecture slides on EUSART . Take a snapshot of the scope. Clearly, indicate the ASCII characters on the snapshot.
- Submission: Submit the following:
- Scope results. You should show all bits in one of the characters and justify that the correct character is being sent. Clearly explain on your diagram what is happening. I will pay special attention to this part! Mark your plot if needed!
- Using your Scope results, find out your clock speed. Justify your answer. One way to do this is to measure the period of each bit you are transmitting.
- Make sure your program works. You are responsible to demonstrate how the program works.
- What is the maximum distance your PIC can be separated from the terminal in order to still communicate with it?
- What ist he power consumtion of your system with and without Bluetooth? This is similar to what you did with LCD - review ADC-Assignment. You must make at least five measurements. Pay attention to your numbers. You must have units; the legends must be correct.
- Read about Bluetooth - answer the following quesitons:
- What is a piconet?
- What is BLE?
- What is Bluetooth 5?
- What is the baud rate of Bluetooth?
- How many channels of operations are available in Bluetooth?
Part II - Communicating from PC to your PIC (PC-->PIC)
Purpose: In this section you should transmit an "L" or "S" to turn an LED ON or OFF, respectively.
Using your code above, add a function such that if you transmit an "L" or "S" from the terminal, an LED will be turnd ON or OFF. Note that in this section you need to configure the RECEIVE portion of the USART.
Submission: Submit the following:
- demonstrate to your instructor that your code works.
Part III - Transmit ADC data from PIC to a PC and plot the data using Matlab (PIC--> MATLAB)
Purpose: In this section connect the potentiometer to your PIC, as described in ADC-Assignment. Instead of sending the data to the LCD, use a Bluetooth Module (as explained above) and send the recorded input voltage value to your PC.
Write a Matlab program (or any other program such as python, LabVIEW, etc.) to read the data from your USB/Bluetooth and plot the data. Here is how: Real-time Data Plot using Matlab. Do some searching and you can find more examples.
Submission: Submit the following:
- Sketch a complete schematic for your design. This should include Power sources, Potentiameter, GND, Bluetooth, and all TX/RX pins. Use one of the following:
- Demonstrate your system: You should change the potentiometer and as you change the potentiometer the Matlab plot must change. Note that if you are using MAC or Linux the Matlab code must be modified.
- Include your Matlab code.
Extra Credit (20 points; You can use it towards other homeworks) - Part III - Transmit data from your Android smart phone (this project is very cool!)
Purpose: The purpose of this section is to learn how to communicate FROM a PC or an ANDROID phone TO a PIC using the USART module.
- Connect your TX/RX to a Bluetooth Module. Connect an LED to a GPIO port on your PIC. Setup your USART module to work at 9600 baud rate.
- Using your Smartphone go to this page and download the the SSU_ENG Serial Application by clicking on the link. Download the file. Then, try to open the file; you are actually installing this package. After the program was installed, find the application and OPEN it. You may need to change some permissions on your phone - if you change any permission make sure you change them back. We used MIT App Inventor to develop these programs. Note that in this application when you press the Left button the application sends character "L", Stop button sends "S", etc.
- Change the program in your PIC such that when you press the Left button on the SSU_ENG phone App the LED is ON and when you press the Stop button LED is OFF.
Submission: Submit the following:
- Make sure your program works. I will check this in class.
If you don't have access to a cell phone use your PC terminal to transmit an "L" or "S" to turn the LED ON or OFF, respectively. You will not receive extra credit for just doing this!
************************ OTHER Project Ideas *********************
- Modify the LabVIEW program, your hardware, and programing code such that you can turn on an LED directly from your LabVIEW program on the PC.
- Create a wireless link between your PIC and PC using Bluetooth. Use MDFLY Bluetooth to interface your PIC to your PC via LabVIEW. Make sure you have connected a Bluetooth receiver to the computer.
- Create a wireless link between your PIC and PC using Zigbee. Use ZigBee Series 2 to interface your PIC to PC via LabVIEW. Use X-CTU to configure your ZigBee chip. Here is how to configure your ZigBee.
-------------------------------- UP TO HERE ---------------------------------------------
Part III - Using SPI Interface
Purpose: In this section you learn how to transmit data between two PIC using SPI communication.
- Read this SPI & I2C Interface tutorial (class notes)
- Learn about how the two different PICs can be connected together.
- Download the sample SPI code here. This is a zip file. Change the extension from txt to zip and then unzip the folder.
- This example code contains two sets of codes. In the Master forder use the main.c file. In the Slave folder use the salve.c file. Download each file in the master adn slave PIC
- Connect the Master node to an LCD.
- This example allows the Master node to transmit something, say a single character "R", to the Slave node;s SSPBUF. The Slave node sends back the character to the Master node.
- You shoud modify the code on the Master node to continiously send the character and once it receives it, it should display it one the LCD.
- If you cannot get it to work initially, connect teh LCD to the Slave node and see if you can receive the transmitted character form the Master node.
- Sketch a complete schematic for your design. This should include LCD, both PIC, Power sources, GND, and all SPI pins. Make sure you identify Master and Slave nodes and all other pins clearly. Make sure your schematic is readable, avoid running power and GND everywhere! Use ProSchematic or another similar software (such as Eagle Schematic Capture or KiCad EDA Software (KiCad EDA or Fritzing) to draw your schematic. You will not receive grade for using any drawing software such as Paintbrush or Visio.
- Using a digital scope (logic analyzer) examine the timing between Clock, MOSI and MISO. Assume you are sending character 'A'. Take an snapshot of the timing.
- Submission: Submit the following:
- Scope results. You should see something like this. Speciafy the ASCII value you are transmitting.
- Answer the following questions:
- What is your clock period?
- What is the period of each data bit?
- What is the relationship between each data bit and clock?
- What is the status of the CS when data is received by the Slave node? - this is important!
- What is the status of the CS when data is received by the Master node? - this is important!
- Schematic capture - read the above description carefully.
- Make sure your program works. I will check this in class.
Part II - Interfacing your PIC with a Computer
Purpose: In this section you learn how to transmit data from your PIC to a computer using Labview.
- Pre-lab:
- Download the following files in a directory called USART_Lab: usartzip and PIC_RS232_LabVIEW.vi / Learm more about Labiew: LabView Tutorials
- Read the USART lab very carefully. Make sure you understand the example codes and you can implement them.
- Modify the the USART example code and demonstrate that you can receive data on your PC using the provided PIC_RS232_LabVIEW.vi program.
- Modify the C program such that you when you change the potentiometer the values on the graph change accordingly.
Part III - Programing USART module
Purpose: In this section you learn how to communicate between two PIC microcontrollers using the USART module.
- Pre-lab:
- Make sure you have complete Part I of the previous lab (ADC in Lab 9): You should be able to display a number between 0-9 on a 7-Segment device indicating the value of the potentiometer on the board. Make sure when the potentiometer reaches its maximum value, the 7-segement displays 9.
- Find out what the clock frequency of your PIC is. To do so refer to the previous lab. You can simply generate a square waveform and measure its period. It is very important that you know your clock frequency (Fosc)
- Read the USART lab very carefully. Make sure you understand the example codes and you can implement them.
- Modify your ADC program such that the reading from the ADC register is directed to the USART TX port. Make sure the displayed value on your 7-Segement is based on the value received by the RX pin of the USART module. Therefore, when you connect the TX and RX pins of the USART module together (RC6 and RC7) the displayed value on the 7-Segment must change as you change the potentiometer. See teh figure below.
- Notes: Based on the value of Fosc you can set the baud rate to 4800 or 9600.
- If your program is working properly, you should be able to connect your TX pin to RX pin of a different board (remote board) and change the displayed value on the 7-Segement on the remote board by changing the potentiometer on your board. Make you you can do this. Note: It is important that the baud rate on both boards are consistent. See figure below.
- Submission: You must submit the following:
- Demonstrate that your project properly operates.
- A snapshot of all the register configurations. In your program you must clearly identify how the related registers in order to setup the ADC module and EUSART module are setup.
- Sketch a complete schematic for your design. This should include 7-Segment, PIC, Power sources, GND, Potentiometer, RX and TX pins connected to one another, and all related pin numbers. Make sure your schematic is readable, avoid running power and GND everywhere! Use ProSchematic or another similar software (such as Eagle Schematic Capture or KiCad EDA Software (KiCad EDA or Fritzing) to draw your schematic. You will not receive grade for using any drawing software such as Paintbrush or Visio.
|
|
|
|
Part II -Using serial TX and RX on your PIC
Purpose: In this section you learn how to transmit data from your PIC and receive it on your board or another board. Use the figure below. You may want to use LCD. You can eliminate the potentiometer and generate random characters.
- Connect the RX and TX pins together. Generate a random character and serially send it out. The received character should be displayed on the LCD - (See the figure below - Replace the 7-segment with an LCD. You can eliminate the potentiometer and generate random characters.)
- Complete one of the following exercises:
- Download the the SSU_ENG Serial Application and install it on your Android phone. Download the Serial application. Read about MIT App Inventor. Note that in this application when you press the Left button the application sends character "L", Stop button sends "S", etc.
- Alternatively, connect your PIC to a PC. Write a program such that when you press character "A" from the computer terminal the PIC transmits a random number between 0-100 to the PC.
- Submission: Submit the following:
- Make sure your program works. I will check this in class.
Project Ideas:
- Modify the LabVIEW program, your hardware, and programing code such that you can turn on an LED directly from your LabVIEW program on teh PC.
- Create a wireless link between your PIC and PC using Bluetooth. Use MDFLY Bluetooth to interface your PIC to your PC via LabVIEW. Make sure you have connected a Bluetooth receiver to the computer.
- Create a wireless link between your PIC and PC using Zigbee. Use ZigBee Series 2 to interface your PIC to PC via LabVIEW. Use X-CTU to configure your ZigBee chip. Here is how to configure your ZigBee.