ELCT706 MicroLab Session #4 UART Usage for Bluetooth connection PC - PIC
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)
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 Most commonly used Asynchronous: - One pin for transmission and another for reception - Full duplex asynchronous operation (both transmission and reception can occur at the same time)
UART in PIC16F877A PIC16F877A has 4 Specific Function 8-bit Registers for USART module: 1. TXSTA 2. RCSTA 3. SPBRG 4. 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 TXREG Baud Rate Generator Register USART Transmitter Register
UART Library in MikroC-Pro Provides comfortable work with the Asynchronous mode Built-in UART functions which set the proper bits for the specific UART PIC registers at each required command Library Functions: UART1_Init UART1_Data_Ready UART1_Tx_Idle UART1_Read UART1_Read_Text UART1_Write UART1_Write_Text UART_Set_Active
UART Library in MikroC-Pro 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 reading 0 if there is no data in the receive register - Example: if(uart1_data_ready()){ // do what ever }
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 reading 0 if there is no data in the receive register - Example: if(uart1_tx_idle()){ // do what ever }
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
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;
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);
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 );
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 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
Easy Bluetooth Connection
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.
Guiding Steps for Using UART Module with Bluetooth Connection
Guiding Steps for Using UART Module with Bluetooth Connection Test text sent from Bluetooth module to PC based on the MikroC code