elct706 microlab session #4 uart usage for bluetooth connection pc...

21
ELCT706 MicroLab Session #4 UART Usage for Bluetooth connection PC - PIC ELCT 706 Session #4 Dr. Mohamed Abdel Ghany Eng. Salma Hesham

Upload: others

Post on 10-Mar-2020

9 views

Category:

Documents


0 download

TRANSCRIPT

ELCT706 MicroLab Session #4

UART Usage for Bluetooth connection PC - PIC

ELCT 706 Session #4

Dr. Mohamed Abdel Ghany Eng. Salma Hesham

USART in PIC16F877A

Universal Synchronous/Asynchronous Receiver Transmitter - Can receive and transmit - Can be synchronous or Asynchronous Synchronous:

- Uses a clock and one data signal

Asynchronous: - One pin for transmission and another for reception - Full duplex asynchronous operation (both transmission and reception can occur at the same time)

ELCT 706 Session #4

Dr. Mohamed Abdel Ghany Eng. Salma Hesham

USART in PIC16F877A

Universal Synchronous/Asynchronous Receiver Transmitter - Can receive and transmit - Can be synchronous or Asynchronous Synchronous:

- Uses a clock and one data signal

Asynchronous: - One pin for transmission and another for reception - Full duplex asynchronous operation (both transmission and reception can occur at the same time)

Most commonly used

ELCT 706 Session #4

Dr. Mohamed Abdel Ghany Eng. Salma Hesham

UART in PIC16F877A Specific Function 8-bit Registers for PIC16F877A USART module: 1. TXSTA 2. RCSTA 3. SPBRG 4. TXREG 5. RCREG Name Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0

TXSTA CSRC TX9 TXEN SYNC - BRGH TRMT TX9D

RCSTA SPEN RX9 SREN CREN - FERR OERR RX9D

SPBRG Baud Rate Generator Register

TXREG USART Transmit Register

RCREG USART Receive Register

ELCT 706 Session #4

Dr. Mohamed Abdel Ghany Eng. Salma Hesham

UART Library in MikroC-Pro Provides comfortable work with the Asynchronous mode

Library Functions: UART1_Init UART1_Data_Ready UART1_Tx_Idle UART1_Read UART1_Read_Text UART1_Write UART1_Write_Text UART_Set_Active

Built-in UART functions which set the proper bits for the specific UART PIC registers at each required command

ELCT 706 Session #4

Dr. Mohamed Abdel Ghany Eng. Salma Hesham

UART Library in MikroC-Pro

ELCT 706 Session #4

Dr. Mohamed Abdel Ghany Eng. Salma Hesham

Library Functions: void UART1_Init(unsigned long baud_rate); - It Initializes the desired UART module with the given baud rate. For PIC16F877A with 4MHz oscillator, use baud rate = 9600. -It returns nothing

- Example: UART1_Init(2400);

UART Library in MikroC-Pro

Library Functions: Bit UART1_Data_Ready(); - It tests if there is a ready data in receive register for reading. The baud rate of the UART module must be initialized before using this function - It returns: 1 if data is ready for reading

0 if there is no data in the receive register

- Example: if(UART1_Data_Ready()){ // do what ever }

ELCT 706 Session #4

Dr. Mohamed Abdel Ghany Eng. Salma Hesham

UART Library in MikroC-Pro

Library Functions: char UART1_Read(); - It receives one byte through the UART when data is ready in the received register. The baud rate of the UART module must be initialized before using this function - It returns the received byte.

- Example: x = UART1_Read(); // x is a char

ELCT 706 Session #4

Dr. Mohamed Abdel Ghany Eng. Salma Hesham

UART Library in MikroC-Pro

Library Functions: void UART1_Read_Text(char *Output, char * Delimiter, char Attempts); - It reads characters via UART until the delimiter sequence is detected. The parameters of this function are: output: contains the received text of successive characters delimiter: sequence of characters which defines the end of the received string. Attempts: defines the number of characters in which the delimiter is expected to be found, otherwise, the procedure will exit anyway. -Example: UART1_Read_Text(output,“OK”,10); //output is defined previously as char *output;

ELCT 706 Session #4

Dr. Mohamed Abdel Ghany Eng. Salma Hesham

UART Library in MikroC-Pro

Library Functions: Bit UART1_Tx_Idle(); - It tests if the transmit shift register is empty or not. The baud rate of the UART module must be initialized before using this function - It returns: 1 if data is ready for reading

0 if there is no data in the receive register

- Example: if(UART1_Tx_Idle()){ // do what ever }

ELCT 706 Session #4

Dr. Mohamed Abdel Ghany Eng. Salma Hesham

UART Library in MikroC-Pro

Library Functions: void UART1_write(char x); - It transmits one byte through the UART module. The parameter x is the byte to be sent. The baud rate of the UART module must be initialized before using this function. - It returns nothing.

- Example: char x = 0xFF; UART1_Write(x);

ELCT 706 Session #4

Dr. Mohamed Abdel Ghany Eng. Salma Hesham

UART Library in MikroC-Pro

Library Functions: void UART1_Write_Text(char *text); - It sends text via UART. The parameter text is the text to be sent. The Baud rate of the UART module should be initialized before using this function. - Example: UART1_Write(“Start”);

ELCT 706 Session #4

Dr. Mohamed Abdel Ghany Eng. Salma Hesham

Control LEDs from PC/ mobile-phone using the UART module

Task 1 Write the MikroC code to use the UART module to control 8 LEDs on Port B through PC/Mobilephone Bluetooth such that if: -received data = “Blink” / ‘B’ LEDs blink 3 consecutive times all together with 1 second delay -Received data = “Shift” / ‘S’ LEDs get turned ON one after the other - Received data = “ON” / ‘O’ LEDs are turned ON - Any other received data LEDs are turned OFF

ELCT 706 Session #4

Dr. Mohamed Abdel Ghany Eng. Salma Hesham

Bluetooth Module Connection

ELCT 706 Session #4

Dr. Mohamed Abdel Ghany Eng. Salma Hesham

Easy Bluetooth Connection

ELCT 706 Session #4

Dr. Mohamed Abdel Ghany Eng. Salma Hesham Eng. Ahmed Atteya

Guiding Steps for Using UART Module with Bluetooth Connection

1. Write the MikroC code to control the LEDs on Port B according to the received string/character Via the UART module.

Start by initializing the Baud Rate and wait for 100ms to initialize Send a test text from Bluetooth to PC/Mobile-phone, followed by new line

(0x0A) and a carriage return (0x0D) to reset the cursor position to the start of a new line.

Start your while(1) where you check if data ready, then read it and control the PORTB LEDs based on the received text.

2. Burn the MikroC program on your PIC and plug it in the circuit 3. Power up your Bluetooth module. 4. Pair your Bluetooth module with your PC/ Mobile-phone. 5. Open the X-CTU/Bluetooth terminal on PC/Mobile-phone. 6. Send and receive through the terminal and the LEDs should be

controlled through the received strings. ELCT 706 Session #4

Dr. Mohamed Abdel Ghany Eng. Salma Hesham

Guiding Steps for Using UART Module with Bluetooth Connection

ELCT 706 Session #4

Dr. Mohamed Abdel Ghany Eng. Salma Hesham

Guiding Steps for Using UART Module with Bluetooth Connection

ELCT 706 Session #4

Dr. Mohamed Abdel Ghany Eng. Salma Hesham

Guiding Steps for Using UART Module with Bluetooth Connection

ELCT 706 Session #4

Dr. Mohamed Abdel Ghany Eng. Salma Hesham

Guiding Steps for Using UART Module with Bluetooth Connection

ELCT 706 Session #4

Dr. Mohamed Abdel Ghany Eng. Salma Hesham

Test text sent from Bluetooth module to PC/MobilePhone based on the MikroC code

Components for Next Lab

• Remote Control Car • Resistances 330 Ohm • 2 Mini USB cables • 10 switches/push buttons • 1 (+1 extra) Voltage regulator 3.3V (LD1117V33) • 1 battery 9V • H-Bridge (L298) • Ultrasonic Sensor

ELCT 706 Session #4

Dr. Mohamed Abdel Ghany Eng. Salma Hesham