elct706 microlab session #4 uart usage for bluetooth ...eee.guc.edu.eg/courses/electronics/elct706...
TRANSCRIPT
ELCT706 MicroLabSession #4
UART Usage for Bluetooth connection PC - PIC
ELCT 706 Session #4Dr. Mohamed Abdel GhanyEng. Salma HeshamEng. Ahmed Atteya
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 #4Dr. Mohamed Abdel GhanyEng. Salma HeshamEng. Ahmed Atteya
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 #4Dr. Mohamed Abdel GhanyEng. Salma HeshamEng. Ahmed Atteya
UART in PIC16F877A
PIC16F877A has 4 Specific Function 8-bit Registers for USART module: 1. TXSTA2. RCSTA 3. SPBRG4. TXREG
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 Transmitter Register
ELCT 706 Session #4Dr. Mohamed Abdel GhanyEng. Salma HeshamEng. Ahmed Atteya
UART Library in MikroC-ProProvides 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 #4Dr. Mohamed Abdel GhanyEng. Salma HeshamEng. Ahmed Atteya
UART Library in MikroC-Pro
ELCT 706 Session #4Dr. Mohamed Abdel GhanyEng. Salma HeshamEng. Ahmed Atteya
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: char 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 reading0 if there is no data in the receive register
- Example: if(UART1_Data_Ready()){// do what ever}
ELCT 706 Session #4Dr. Mohamed Abdel GhanyEng. Salma HeshamEng. Ahmed Atteya
UART Library in MikroC-Pro
Library Functions: char 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 reading0 if there is no data in the receive register
- Example: if(UART1_Tx_Idle()){// do what ever}
ELCT 706 Session #4Dr. Mohamed Abdel GhanyEng. Salma HeshamEng. Ahmed Atteya
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 beinitialized before using this function
- It returns the received byte.
- Example: x = UART1_Read(); // x is a char
ELCT 706 Session #4Dr. Mohamed Abdel GhanyEng. Salma HeshamEng. Ahmed Atteya
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 charactersdelimiter: 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 #4Dr. Mohamed Abdel GhanyEng. Salma HeshamEng. Ahmed Atteya
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 #4Dr. Mohamed Abdel GhanyEng. Salma HeshamEng. Ahmed Atteya
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 beforeusing this function.
- Example: UART1_Write(“Start”);
ELCT 706 Session #4Dr. Mohamed Abdel GhanyEng. Salma HeshamEng. Ahmed Atteya
Control LEDs from PC/ mobile-phone using the UART module
Task 1Write the MikroC code to use the UART module to control 8 LEDs on Port B through PC/mobilephoneBluetooth such that if: -received data = “Blink” LEDs blink 3 times together with 1 second delay-Received data = “Shift”LEDs get turned ON one after the other- Received data = “ON” LEDs are turned ON- Any other received data LEDs are turned OFF
ELCT 706 Session #4Dr. Mohamed Abdel GhanyEng. Salma HeshamEng. Ahmed Atteya
Easy Bluetooth Connection
ELCT 706 Session #4Dr. Mohamed Abdel GhanyEng. Salma HeshamEng. 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 Via the UART module. Start by initializing the Baud Rate and wait for 100ms to intialize 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. Power up your Bluetooth module.3. Pair your Bluetooth module with your PC/ Mobile-phone.4. Open the X-CTU/Bluetooth terminal on PC/Mobile-phone.5. Burn the MikroC program on your PIC and plug it in the circuit.6. Send and receive through the terminal and the LEDs should be
controlled through the received strings.
ELCT 706 Session #4Dr. Mohamed Abdel GhanyEng. Salma HeshamEng. Ahmed Atteya
Guiding Steps for Using UART Module with Bluetooth Connection
ELCT 706 Session #4Dr. Mohamed Abdel GhanyEng. Salma HeshamEng. Ahmed Atteya
Guiding Steps for Using UART Module with Bluetooth Connection
ELCT 706 Session #4Dr. Mohamed Abdel GhanyEng. Salma HeshamEng. Ahmed Atteya
Test text sent from Bluetooth module to PC based on the
MikroC code