Serial Communication with UART and Interfacing Bluetooth HC-05

                               

 

MICROPROCESSOR AND INTERFACING

 

 

 

LAB REPORT#11

 

 

 

 

SUBMITTED TO:   SIR Khiyam Iftikhar

 

SUBMITTEED BYIbrar Ahmed   FA19-BEE-083

                                   Jarrar Malik     FA19-BEE-087

                                   Junaid Ahmed  FA19-BEE-089

 

CLASS:   BEE-5B. 

 

 

 

 

 

 

 

Lab # 11 Serial Communication with UART and Interfacing Bluetooth HC-05

EXPLANATION OF SERIAL COMMUNICATION:

Microcontrollers must often exchange data with other microcontrollers or devices. Data may be exchanged by using parallel or serial communication links. With parallel techniques, an entire byte of data is typically sent simultaneously from the transmitting device to the receiver device. Although this is efficient from a time point of view, it requires eight separate lines for the data transfer. In serial transmission, a byte of data is sent a single bit at a time. Once 8 bits have been received at the receiver, the data byte is reconstructed. Although this is inefficient from a time point of view, it only requires a line (or two) to transmit the data. The Universal Synchronous and Asynchronous Receiver Transmitter (USART) provides full duplex (two-way) communication between the MCU and another device

In Lab:

Task 1:

You will configure (i.e. set baud rate, frame format, parity settings etc ) UART present in Atmega328P for Asynchronous Serial Communication.

CODE :

/*

 * lab_11_task_01.c

 *

 * Created: 03-Dec-21 9:29:00 AM

 * Author : AC

 */

#include <avr/io.h>              // Most basic include files

#include <avr/interrupt.h>       // Add the necessary ones

#include <util/delay.h>

#include <string.h>

#define F_CPU 16000000UL             // a System clock of 16 MHz

// ***** Definistion for USART *********

#define BAUD 9600.00                    // Baud Rate Declared as a

 

#define UBRR_VAL (F_CPU/(8*BAUD))-1   // Corresponding UBRR Value

// *********************

void USART_Init(unsigned int ubrr); // function to Initialize the USART

void USART_Transmit(unsigned char data); // Transmit Data via USART

unsigned char USART_Receive(void);        // Receive Data via USART

void USART_send_str(char * string1);    // Transmit a string

void USART_read_str(char * );           // Read the USART buffer

// *********************

unsigned int data_available = 0;    // A flag to indicate that data is available in the receive buffer

// Main program

int main(void)

{

            USART_Init((unsigned int)UBRR_VAL);

            unsigned char data;

            USART_send_str("hhh");

            while(1)

            {             // Infinite loop; define here the

                        data = USART_Receive();

                        _delay_ms(20);         // a 20 ms Delay between receiving and sending data

                        USART_Transmit(data);

            }

}

void USART_Init(unsigned int ubrr)

{

            /* Set baud rate */

            UBRR0H = (unsigned char)(ubrr>>8);

            UBRR0L = (unsigned char)ubrr;

            /Complete this code/

            /set double speed operation to reduce Baudrate Error/

            UCSR0A|=(1<<U2X0); /* Enable receiver and transmitter */

            UCSR0B|= (1<<RXEN0)|(1<<TXEN0);  /* Set frame format: 8data, 1stop bit, odd parity */

    UCSR0C|= (1<<UPM01)|(1<<UPM00)|(1<<UCSZ00)|(1<<UCSZ01);

}

void USART_Transmit(unsigned char data)

{

            /* Wait for empty transmit buffer */

            while((UCSR0A&(1<<UDRE0))==0);

            UDR0 = data;

            /* Put data into buffer, sends the data */

}

unsigned char USART_Receive(void)

{

            /* Wait for data to be received */

            while((UCSR0A&(1<<RXC0))==0);

            /* Get and return received data from buffer */

            return UDR0;

}

void USART_send_str(char * str) // Transmit a string

{

            while (*str)

            {

                        USART_Transmit(*str);

                       

                       

                        str++;;

            }

}

void USART_read_str(char * str)  // Read a string from UART

{

            char ch;

            char i;

            do

            {

                        ch = USART_Receive();

                        str[i] = ch;

            }

            while(ch != '\0');

}

 

 

 

 

 

 

 

 

 

 

 

 

CIRCUIT DIAGRAM AND SIMULATIONS:



Task 2:

 Interfacing HC-05 Bluetooth device and Communicating with an Android Device

CODE:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // (Rx,TX)

void setup()

{ Serial.begin(9600); // the Arduino Baud rate

delay(1000);

mySerial.begin(9600);

delay(1000);

Serial.println("Hello world");

}

void loop()

{

if(Serial.available())

mySerial.print ((char) Serial.read());

if(mySerial.available())

Serial.print((char) mySerial.read()) ;

}




OUTPUT ON SERIAL MONITOR:




CRITICAL ANALYSIS:

In this lab experiment I got to know about serial communication in UART, In serial transmission, a byte of data is sent a single bit at a time. Once 8 bits have been received at the receiver, the data byte is reconstructed. Although this is inefficient from a time point of view, it only requires a line (or two) to transmit the data. The ATmega328P is equipped with a host of different serial communication subsystems, including the serial USART, SPI, and TWI, hence in this lab we used USART as communication subsystem for microcontroller and its in 2nd task we interfaced the Bluetooth module with Arduino and observed the serial monitor of Arduino software as well as we can also connect it to our mobile phones as shown above.

 

إرسال تعليق

Post a Comment (0)

أحدث أقدم