summer training report (2)

24
EMBEDDED SYSTEMS USING PIC MICROCONTROLLER WINTER TRAINING REPORT Submitted by Daman Singh Walia, Roll No: N082134, Kavya Gupta ELECTRONICS AND COMMUNICATION ENGINEERING Maharaja Agrasen Institute of Technology Guru Gobind Singh Indraprastha University, Delhi 2013-201

Upload: kavya-gupta

Post on 12-Apr-2017

110 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: summer training report (2)

EMBEDDED SYSTEMS USING PIC MICROCONTROLLER

WINTER TRAINING REPORT

Submitted byDaman Singh Walia, Roll No: N082134,

Kavya Gupta

ELECTRONICS AND COMMUNICATION ENGINEERING

Maharaja Agrasen Institute of Technology

Guru Gobind Singh Indraprastha University, Delhi2013-201

Page 2: summer training report (2)

I would like to show my greatest appreciation to LOGIC POWER. I can’t thank them

enough for their tremendous support and help. I feel motivated and encouraged every time I

worked with them. Without their encouragement and guidance this internship would not have

materialized.

The guidance and support received from all the members and engineers who contributed and

who were vital for the success of my training in their prestigious company. I am grateful for

their constant support and help.

Embedded Systems

An embedded system is some combination of computer hardware and software, either fixed

in capability or programmable, that is specifically designed for a particular function.

Industrial machines, automobiles, medical equipment, cameras, household appliances,

airplanes, vending machines and toys (as well as the more obvious cellular phone and PDA)

are among the myriad possible hosts of an embedded system. Embedded systems that are

programmable are provided with programming interfaces, and embedded systems

programming is a specialized occupation.

Embedded systems are considered when the cost of implementing a product designed in

software on a microprocessor and some small amount of hardware, is cheaper, more reliable,

or better for some other reason than a discrete hardware design. It is possible for one small

and relatively cheap microprocessor to replace dozens or even hundreds of hardware logic

gates, timing circuits, input buffers, output drivers, etc. It also happens that one generic

embedded system with a standard input and output configuration can be made to perform in a

completely different manner simply by changing the software

Parts of Embedded Systems:

Input device

Processing unit

Output device

INPUT DEVICE: These devices provide input to the microcontroller. In general, we use

sensors as input devices while dealing with microcontrollers.

Page 3: summer training report (2)

PROCESSING UNIT: These devices process the input provided by the input devices and

produces the output. All the decisions are taken by this unit depending upon the algorithm

provided by the user. Microcontrollers are used as the basic processing device.

OUTPUT DEVICE: These devices show the result of our algorithm. We use LEDs, Motors,

LCD etc. as the output devices.

INTRODUCTION TO

MICROCONTROLLER

A microcontroller is basically a computer on a

chip. A microcontroller contains CPU,

input/output interface, memory, clock, timer and an assortment of other peripherals.

Microcontrollers are designed for embedded applications, in contrast to the microprocessors

used in personal computers or other general purpose applications.

Microcontrollers are used in automatically controlled products and devices, such as

automobile engine control systems, implantable medical devices, remote controls, office

machines, appliances, power tools, toys and other embedded systems. By reducing the size

and cost compared to a design that uses a separate microprocessor, memory, and input/output

devices, microcontrollers make it economical to digitally control even more devices and

processes. Mixed signal microcontrollers are common, integrating analog components needed

to control non-digital electronic systems.

PIC16F877A

Page 4: summer training report (2)

PIC16F877A pin configuration for a DIP package

PIC16F877A FEATURES:

High performance RISC CPU. ONLY 35 simple word instructions. All single cycle instructions except for program branches which are two cycles. Operating speed: clock input (200MHz), instruction cycle (200nS). Up to 368×8bit of RAM (data memory), 256×8 of EEPROM (data memory), 8k×14

of flash memory. Pin out compatible to PIC 16C74B, PIC 16C76, PIC 16C77. Eight level deep hardware stack. Interrupt capability (up to 14 sources). Different types of addressing modes (direct, Indirect, relative addressing modes). Power on Reset (POR). Power-Up Timer (PWRT) and oscillator start-up timer. Low power- high speed CMOS flash/EEPROM. Fully static design. Wide operating voltage range (2.0 – 5.56) volts. High sink/source current (25mA). Commercial, industrial and extended temperature ranges. Low power consumption (<0.6mA typical @3v-4MHz, 20µA typical @3v-32MHz

and <1 A typical standby)

Page 5: summer training report (2)

TRAINING

While doing my training in LOGICPOWER I studied the Architecture, Memory organisation, Interrupts (not all), Timers, Oscillators of PIC microcontroller.

Here is a list of things I did during my training in LOGIC POWER:

Softwares learned:

MPLABX IDE , MPLABX IPE , Real Term / Tera Term ,Proteus simulations

Hardware implemented:

Interfacing of :

Led, Push buttons, LCD , USART , BM77(Bluetooth module)

Theoretical study:

Pic microntroller , embedded C, I2C and SPI protocol , how to interface bluetooth with pic(usart mode of communication),LCD(4bit and 8 bit modes).

Development boards/Hardware/programmer

Page 6: summer training report (2)

Pic Dem 2 , Pic-18 explorer , Pickit3(programmer),Usb to Usart module,bm-77 module

INTERFACING

LED

A light-emitting diode (LED) is a two-lead semiconductor light source. It is a p–n

junction diode, which emits light when activated. The LED is turned on when the positive

(anode) is at higher potential and the negative (cathode) is at a lower potential. The voltage

difference required between anode and cathode leads is different for different color LED.

Symbol of a Light Emitting Diode

Page 7: summer training report (2)

Program for a blinking 4 LEDs:

#include <stdio.h>#include <stdlib.h>#include <pic16f877a.h>

// PIC16F877A Configuration Bit Settings

// 'C' source line config statements

#include <xc.h>

// #pragma config statements should precede project file includes.// Use project enums instead of #define for ON and OFF.

// CONFIG#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)#pragma config WDTE = ON // Watchdog Timer Enable bit (WDT enabled)#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)#pragma config LVP = ON // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)

void MSDelay(unsigned int itime){unsigned int i,j;for (i=0;i<itime;i++) // TIME IN MILLISEC{for (j=0;j<2000;j++);}}int main (){

TRISB = 0xF0; //RB0 as Output PIN

while(1){MSDelay(10);

Page 8: summer training report (2)

RB0 = 1; // LED ONMSDelay(10); // 1 Second DelayRB1 = 1;MSDelay(10); // 1 Second DelayRB2 = 1; // LED ONMSDelay(10); // 1 Second DelayRB3 = 1; // LED OFFMSDelay(10); // 1 Second DelayRB3 = 0; // LED OFFMSDelay(10);RB2 = 0;// 1 Second DelayMSDelay(10);RB1 = 0;MSDelay(10);RB0 = 0;}return 0;}

SWITCH

A push-button or simply button is a simple switch mechanism for controlling some aspect of

a machine or a process. Buttons are typically made out of hard material,

usually plastic or metal. The surface is usually flat or shaped to accommodate the human

finger or hand, so as to be easily depressed or pushed. Buttons are most often biased

switches, though even many un-biased buttons (due to their physical nature) require

a spring to return to their un-pushed state.

Page 9: summer training report (2)

A Push-button

Program for interfacing a switch with an LED

Pin 1 of PortB has an LED connected to it Pin 2 of PortA is connected to a push button.

/* * File: button.c * Author: Kavya * * Created on January 6, 2016, 10:39 PM */

#include <stdio.h>#include <stdlib.h>#include <pic16f877a.h>

void Delay(unsigned int itime){unsigned int i,j;for (i=0;i<itime;i++) // TIME IN MILLISEC{for (j=0;j<5000;j++);}}

int main(){TRISB0 = 0; //RB0 as Output PINTRISA4 = 1; //RA4 as Input PINRB0 = 0; //LED Offwhile(1){if(RA4 == 0) //If Switch Pressed{RB0 = 1; //LED ON Delay(1);//3 Second DelayRB0 = 0; //LED OFF}}return 0;}

Page 10: summer training report (2)

LCD

A liquid-crystal display (LCD) is a flat panel display, electronic visual display, or video

display that uses the light modulating properties of liquid. Liquid crystals do not emit light

directly. LCDs are available to display arbitrary images (as in a general-purpose computer

display) or fixed images which can be displayed or hidden, such as preset words, digits,

and 7-segment displays as in a digital clock. They use the same basic technology, except that

arbitrary images are made up of a large number of small pixels, while other displays have

larger elements.

Page 11: summer training report (2)

WORKING OF LCD

 It is combination of two states of matter, the solid and the liquid. LCD uses a liquid crystal to

produce a visible image. Liquid crystal display is composed of several layers which include

two polarized panel filters and electrodes. In liquid crystal displays (LCDs), use is made of

linear polarisers, familiar to most as the glass in polarising sunglasses. If you “cross” two

polarisers (i.e. arrange them at 90 degrees to each other) then light does not pass through.

This is the basis of the LCD, but between the cross polars the liquid crystals are arranged

with a “twist”. This twist allows light to pass through. However, when an electric field is

passed through the liquid crystals, the twist is removed, and so light cannot pass through – the

area appears black. 

PIN DIAGRAM FOR LCD

PIN DESCRIPTION FOR LCD

Pin Symbol I/O Description1

VSS--- Ground

2 VCC --- +5V Power Supply

3 VEE --- Contrast control

4 RS I RS=0 to select command register,

RS=1 to select data register

5 R/W I 0 = for write,

1 = for read

6 E I/O Enable (Clock)

7 DB0 I/O The 8-bit data bus

Page 12: summer training report (2)

8 DB1 I/O The 8-bit data bus

9 DB2 I/O The 8-bit data bus

10 DB3 I/O The 8-bit data bus

11 DB4 I/O The 8-bit data bus

12 DB5 I/O The 8-bit data bus

13 DB6 I/O The 8-bit data bus

14 DB7 I/O The 8-bit data bus

Page 13: summer training report (2)

LCD Command Codes

Code (Hex) Command on LCD Register

1 Clear display screen

2 Return home

4 Decrement cursor (shift cursor to left)

6 Increment cursor (Shift cursor to right)

5 Shift display right

7 Shift display left

8 Display off, cursor off

A Display off, cursor on

C Display on, cursor off

E Display on, cursor on

F Display on, cursor blinking

10 Shift cursor position to left

14 Shift cursor position to right

18 Shift the entire display to left

1C Shift the entire display to right

80 Force cursor to beginning of 1st line

C0 Force cursor to beginning of 2nd line

38 2 lines and 5x7 matrix

Program to display a string of characters on LCD(4BIT MODE)

//CODE BY KAVYA GUPTA//HERE WE ARE INITIALISING THE LCD IN 4 BIT MODE WHICH PROVIDES AN ADVANTAGE IN MINIMISING THE PINS OF THE MICROCONTROLLER//COMMAND SEND TO THE LCD ARE SEND NIBBLE BY NIBBLE, I.E FIRST THE HIGHER NIBBLE THEN THE LOWER NIBBLE// LCD CONNECTIONS /* D4 - RD0 D5 - RD1 D6 - RD2 D7 - RD3 RS - RA1 R/W - RA2 EN - RA3 */

Page 14: summer training report (2)

#define _XTAL_FREQ 8000000

#include <xc.h>#include <pic16f877a.h>void Lcd_Port(char a) //THIS FUNCTION IS USED TO SET THE LOWER BITS FOR COMMANDS SEND TO LCD{

if(a & 1) RD0 = 1;

elseRD0 = 0;

if(a & 2)RD1 = 1;

elseRD1 = 0;

if(a & 4)RD2 = 1;

elseRD2 = 0;

if(a & 8)RD3 = 1;

elseRD3 = 0;

}void command(char a){

RA3 = 0; // => RS = 0Lcd_Port(a);RA1 = 1; // => E = 1

__delay_ms(4); RA1 = 0; // => E = 0}

Lcd_Clear(){

command(0);command(1);

}

void Lcd_Set_Cursor(char a, char b){

char temp,z,y;if(a == 1){ temp = 0x80 + b - 1;

z = temp>>4;y = temp & 0x0F;command(z);command(y);

}else if(a == 2){

temp = 0xC0 + b - 1;z = temp>>4;y = temp & 0x0F;command(z);command(y);

Page 15: summer training report (2)

}}

void Lcd_Init(){ Lcd_Port(0x00); // __delay_ms(20); command(0x03);

__delay_ms(5); command(0x03);

__delay_ms(11); command(0x03); command(0x02); ///////////////////////////////////////////////////// command(0x02);// THIS INITIALISE LCD IN 4 BIT MODE,2 LINES,5x7 MATRIX command(0x08);// SENDING 0x28 COMMAND command(0x00);// DISPLAY ON CURSOR OFF command(0x0C);// SENDING 0x0C COMMAND command(0x00);// INCREMENT CURSOR command(0x06);// SENDING 0x06 COMMAND}

void Lcd_Write_Char(char a){ char temp,y; temp = a&0x0F; y = a&0xF0; RA3 = 1; // => RS = 1 Lcd_Port(y>>4); //Data transfer RA1 = 1; __delay_us(40); RA1 = 0; Lcd_Port(temp); RA1 = 1; __delay_us(40); RA1 = 0;}

void Lcd_Write_String(char *a){

int i;for(i=0;a[i]!='\0';i++) Lcd_Write_Char(a[i]);

}

void rightshift(){

command(0x01);//SENDING 0x1C COMMAND TO THE LCD RESGISTER BY SENDING IT NIBBLE BY NIBBLE)

command(0x0C);// FISRT 0x01 THEN 0x0C = 0x1C}

void leftshift(){

command(0x01);//SENDING 0x18 COMMAND TO THE LCD RESGISTER BY SENDING IT NIBBLE BY NIBBLE)

command(0x08);// FISRT 0x01 THEN 0x08 = 0x18}

int main()

Page 16: summer training report (2)

{ int i; ADCON1 = 0x06; //SWITCHING ALL THE PORTA PINS TO DIGITAL PINS TRISD = 0x00; // PORTD TO OUTPUT TRISA = 0x00; // PORTA TO OUTPUT RA2=0; // R/W = 0 Lcd_Init(); // INITIALISING THE LCD while(1) { Lcd_Clear(); Lcd_Set_Cursor(1,6); Lcd_Write_String("HELLO"); Lcd_Set_Cursor(2,3); Lcd_Write_String("LOGIC POWER"); __delay_ms(200); for(i=0;i<15;i++) { __delay_ms(50); leftshift(); }

for(i=0;i<15;i++) { __delay_ms(50); rightshift(); }

Lcd_Clear(); } return 0;}

In 4-bit mode the data is sent in nibbles, first we send the higher nibble and then the lower

nibble. To enable the 4-bit mode of LCD, we need to follow special sequence of initialization

that tells the LCD controller that user has selected 4-bit mode of operation. We call this

special sequence as resetting the LCD. Following is the reset sequence of LCD.

Wait for abour 15mS

Send the first init value (0x30)

Wait for about 4.1mS

Send second init value (0x30)

Wait for about 1mS

Send third init value (0x30)

Wait for 4.1mS

Select bus width (0x20 for 4-bit)

Page 17: summer training report (2)

After the last instruction, lcd switches to 4-bit operation, and the control bytes are sent on

consecutive enable cycles. The most significant nibble is sent first, followed by the

immediately by the least significant nibble.

The busy flag will only be valid after the above reset sequence. Usually busy flag is not used

in 4-bit mode as to write code for reading two nibbles from the LCD. Instead simply put a

certain amount of delay usually 300 to 600uS.

Page 18: summer training report (2)

USART (universal synchronous asynchronous receiver transmitter)

USART stands for Universal Synchronous Asynchronous Transmitter/Receiver. The USART can transmit data using a buffer and a shift register, receive data using a shift register and buffer, create a frame of data that is recognized on both the receiving end and the transmitting end. All of this works according to an agreed upon speed from both sides, or with synchronous mode where the clock line is directly connected.

The data is sent to buffer and then onto the shift registers. This is done after the previous data has left the shift registers. From the shift registers, the data moves along the transmit wire. Receiving information is same but in reverse. In the microcontroller reception of data gets off the wire and goes straight into shift registers and then to the buffer.

USART Baud Rate Generator (BRG )

The BRG supports both the Asynchronous and Synchronous modes of the USART. It is a

dedicated 8-bit baud rate generator. The SPBRG register controls the period of a free running

8-bit timer. In asynchronous mode bit BRGH (TXSTA<2>) also controls the baud rate. In

synchronous mode bit BRGH is ignored.

Different USART modes which only apply in master mode (internal clock). Given the desired

baud rate and Fosc, the nearest integer value for the SPBRG register can be calculated using

the formula in , where X equals the value in the SPBRG register (0 to 255). from this, the

error in baud rate can be determined.

SYNC BRGH = 0 (Low Speed) BRGH = 1 (High Speed)

0 (Asynchronous) Baud Rate = FOSC/(64(X+1)) Baud Rate= FOSC/(16(X+1))

1 (Synchronous) Baud Rate = FOSC/(4(X+1)) NA

REGISTERS OF USART

1. TXTA (Transfer Status and Control Register)

Bit No 7 6 5 4 3 2 1 0Name CSRC TX9 TXEN SYNC -- BRGH TRMT TX9D

CSRC:Clock Source Select bitAsynchronous modeDon’t careSynchronous mode1 = Master mode (Clock generated internally from BRG)

Page 19: summer training report (2)

0 = Slave mode (Clock from external source)TX9 9-bit Transmit Enable bit1 = Selects 9-bit transmission0 = Selects 8-bit transmissionTXENTransmit Enable bit1 = Transmit enabled0 = Transmit disabledNote:SREN/CREN overrides TXEN in SYNC mode.SYNC: USART Mode Select bit1 = Synchronous mode0 = Asynchronous modeUnimplemented (--)Read as '0'BRGH High Baud Rate Select bitAsynchronous mode1 = High speed0 = Low speedSynchronous modeUnused in this modeTRMT Transmit Shift Register Status bit1 = TSR empty0 = TSR fullTX9D:9th bit of transmit data. Can be parity bit.

2. RXTA(Receive Status and Control Register)

Bit No 7 6 5 4 3 2 1 0Name RXCIE TXCIE UDRIE RXEN TXEN UCSZ2 RXB8 TXB8

SPEN:Serial Port Enable bit1 = Serial port enabled (Configures RX/DT and TX/CK pins as serial port pins)0 = Serial port disabledRX9: 9-bit Receive Enable bit1 = Selects 9-bit reception0 = Selects 8-bit receptionSREN: Single Receive Enable bitAsynchronous modeDon’t careSynchronous mode - master1 = Enables single receive0 = Disables single receiveThis bit is cleared after reception is complete.Synchronous mode - slaveUnused in this modeCREN

Page 20: summer training report (2)

: Continuous Receive Enable bitAsynchronous mode1 = Enables continuous receive0 = Disables continuous receiveSynchronous mode1 = Enables continuous receive until enable bit CREN is cleared (CREN overrides SREN)0 = Disables continuous receiveUnimplemented:Read as '0'FERR: Framing Error bit1 = Framing error (Can be updated by reading RCREG register and receive next valid byte)0 = No framing errorOERR: Overrun Error bit1 = Overrun error (Can be cleared by clearing bit CREN)0 = No overrun errorRX9D:9th bit of received data, can be parity bit.

Program can’t be included!!