Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
A REPORT SUBMITTED FOR VOCATIONAL TRAINING IN PARTIAL FULFILLMENT OF B.TECH DEGREE IN ELECTRONICS AND COMMUNICATION ENGINEERING ETERNEL UNIVERSITY, BARU SAHIB, DISTT. SOLAN (H.P)
ACKNOWLEDGEMENT
This project report is an acknowledgment to the sincere efforts of all the individuals who have contributed for the completion of my industrial training report. It gives me immense pleasure to express my gratitude to all staff of All India Radio FM station Dharamshala, Himachal Pradesh. who went through the manuscript personally and contributed his valuable suggestion in editing and reviewing. Without their active support it would not be possible to complete this report. I am confident that this project will be useful and dependable guide for incoming student. The constructive suggestion for further improvement will be much appreciable.
INDEX
Introduction to microcontroller PIC Microcontroller Architecture Memory Concepts Programming (Byte Oriented Instruction) Programming (Bit Oriented Instruction) Programming (Literal and control Oriented Instruction) Port and Power supply LED and Relay Interfacing Switch Interfacing LCD Interfacing
INTRODUCTION
What is a Microcontroller.
It is a device that has been developed to work on specific application. It is an advancement on microproccesor . Reduce chip count. Many applications do not require as much computing power. Reduced power consumption. Reduced design cost Microcontrollers come as 4-bit , 8-bit , 16-bit and 32-bit Execute a stored set of instructions to carry out user defined tasks. Ability to interface with I/O Devices.
The benefit of uCs in embedded systems is that complete systems can be built with a very low component count, frequently just one chip.
MICROCONTROLLER ARCHITECTURE
Memory is needed for programs and data. Program memory used in embedded systems is typically of non-volatile type. Data memory used is SRAM- or DRAM- type volatile memory.
Von Neumann-architecture:
Program and data located in same address space.
Harvard-architecture:
Separate address spaces and buses for program and data memories.
Examples Harvard: 8051 Microchip PIC families Atmel AVR Von Neumann: PCs (Intel 80x86/Pentium) Motorola 68000 Motorola 68xx uC families
High current source/sink for direct LED drive software and individually programmable pull-up resistor Interrupt-on-Change pin In-Circuit Serial Programming Option 3 independent timers/counters Analogue comparator module with Two analogue comparators Programmable on-chip voltage reference Enhanced USART module Supports RS-485, RS-232 and LIN2.0 Auto-Baud Detect Oscillator & Clock Circuit
NAME MCLR/VPP
NUMBER 1
DESCRIPTION Master Clear (Reset) input. This pin is active low Programming voltage input. Digital I/O. Analog input 0 Digital I/O. Analog input 1. Digital I/O. Analog input 2. A/D reference voltage (Low) input. Comparator VREF output. Digital I/O. Analog input 3. A/D reference voltage (High) input. Digital I/O Open-drain when configured as output. Timer0 external clock input. Comparator 1 output. Digital I/O. Analog input 4. SPI slave select input. Comparator 2 output. Digital I/O.
RA0/AN0
RA0 AN0
RA1/AN1
RA1 AN1
RA2/AN2/VREF-/CVREF
RA3/AN3/VREF+
RA4/T0CKI/C1OUT
RA5/AN4/SS/C2OUT
RE0/RD/AN5
RE0
RD AN5 RE1/WR/AN6 9 RE1 WR AN6 RE2/CS/AN7 10 RE2 CS AN7 VDD VSS OSC1/CLKI 11 12 13 OSC1 CLKI
Read control for Parallel Slave Port. Analog input 5. Digital I/O. Write control for Parallel Slave Port. Analog input 6. Digital I/O. Chip select control for Parallel Slave Port. Analog input 7. Positive supply Ground Oscillator crystal input External clock source input. Always associated with pin function OSC1
OSC2/CLKO
14
OSC2 CLKO
Oscillator crystal output. In RC mode, OSC2 pin outputs CLKO, which has 1/4 the frequency of OSC1
RC0/T1OSO/T1CKI
15
Digital I/O. Timer1 oscillator output. Timer1 external clock input Digital I/O. Timer1 oscillator input. Capture2 input, Compare2 output, PWM2 output.
RC1/T1OSI/CCP2
16
RC2/CCP1
17
RC2 CCP1
Digital I/O. Capture1 input, Compare1 output, PWM1 output. Digital I/O. Synchronous serial clock input/output for SPI mode. Synchronous serial clock input/output for I2C mode.
RC3/SCK/SCL
18
RC3
SCK
SCL
RD0/PSP0
19
RD0 PSP0
RD1/PSP1
20
RD1 PSP1
RD2/PSP2
21
RD2 PSP2
RD3/PSP3
22
RD3 PSP3
RC4/SDI/SDA
23
RC4 SDI
SDA
RC5/SDO
24
RC5 SDO
RC6/TX/CK
25
RC6
Digital I/O.
TX CK
RC7/RX/DT
26
RC7 RX DT
Digital I/O. USART asynchronous receive. USART synchronous data. Digital I/O. data. RD5 PSP5 Digital I/O. data. Digital I/O.
RD4/PSP4
27
RD4 PSP4
RD5/PSP5
28
RD6/PSP6
29
RD6
PSP6 RD7/PSP7 30 RD7 PSP7 VSS VDD RB0/INT 31 32 33 RB0 INT RB1 RB2 RB3/PGM 34 35 36 RB3 PGM RB4 RB5 RB6/PGC 37 38 39 RB6
data. Digital I/O. data. Ground Positive supply Digital I/O. External interrupt. Digital I/O. Digital I/O. Digital I/O. Low-voltage ICSP programming enable pin. Digital I/O. Digital I/O. Digital I/O. In-circuit debugger and ICSP programming clock. Digital I/O. In-circuit debugger and ICSP programming data.
PGD
Machine Cycle: Smallest interval of time to accomplish any simple instruction or part of
complex instruction.
State: Basic time interval for discrete operations of machine cycles, such as fetching op-code
byte, decoding an opcode etc.
2 clock cycles. It means that if the microcontrollers operating speed is 20MHz, execution time of each instruction will be 200nS, i.e. the program will be executed at the speed of 5 million instructions per second! STATUS REGISTER Bit7 Bit0
bit 7 IRP: Register Bank Select bit (used for indirect addressing) 1 = Bank 2, 3 (100h-1FFh) 0 = Bank 0, 1 (00h-FFh) bit 6-5 RP1:RP0: Register Bank Select bits (used for direct addressing) 11 = Bank 3 (180h-1FFh) 10 = Bank 2 (100h-17Fh) 01 = Bank 1 (80h-FFh) 00 = Bank 0 (00h-7Fh) Each bank is 128 bytes. bit 4 TO: Time-out bit 1 = After power-up, CLRWDT instruction or SLEEP instruction 0 = A WDT time-out occurred bit 3 PD: Power-down bit 1 = After power-up or by the CLRWDT instruction 0 = By execution of the SLEEP instruction bit 2 Z: Zero bit 1 = The result of an arithmetic or logic operation is zero 0 = The result of an arithmetic or logic operation is not zero bit 1 DC: Digit carry/borrow bit (ADDWF, ADDLW,SUBLW,SUBWF instructions) (for borrow, the polarity is reversed)
1 = A carry-out from the 4th low order bit of the result occurred 0 = No carry-out from the 4th low order bit of the result bit 0 C: Carry/borrow bit (ADDWF, ADDLW,SUBLW,SUBWF instructions) 1 = A carry-out from the Most Significant bit of the result occurred 0 = No carry-out from the Most Significant bit of the result occurred
Program Memory
The program memory in the PIC16F877A has a total of 8K words. . After the program memory has been programmed it will retain the program even if power is lost. Therefore program memory is said to be nonvolatile. The 8K words or program memory are made up of 4 sections or Pages of 2K words each. Within each page every 14 bit word has an address with the first one starting at 000hex and ending with 7FFhex which corresponds to 0 through 2047 decimal. The MCU can
only use one of the program memory pages at a time. To read or write to a different page requires the programmer to do page switching within his program.
EEPROM Memory
This memory is nonvolatile meaning that it will retain its data even if the power is lost. EEPROM is normally used to store parameter data that needs to be saved over a long period over many power-up and power-down cycles. Writing to EEPROM involves a more complex series of steps than writing to the File Registers. Each write cycles is very slow compared to writing to the File Registers. However reading is much simpler and much faster. The procedures for using this type of memory will be covered in an intermediate course. The PIC16F877A has 256 bytes of EEPROM memory. The EEPROM memory is separate from program memory and data memory. It has it own address space. To read or write to EEPROM memory the Special Function Registers are used such as EEDATA and EEADR
which contain the General Purpose Registers and the Special Function Registers. Bits RP1 (Status<6>) and RP0 (Status<5>) are the bank select bits. Bank3 The concept of register banks adds a great level of flexibility to the PIC16f877A, especially when dealing with interrupts Four Banks: Bank0, Bank1, Bank2,
Core (CPU) registers - control and monitor operation and processes in the central processor. Even though there are only a few of them, the operation of the whole microcontroller depends on their contents. Peripheral SFRs- control the operation of peripheral units (serial communication module, A/D converter etc.). Input /Output Ports PORTA and the TRISA Register PORTA is a 6-bit wide, bidirectional port. Data direction register is TRISA. Set TRISA bit (= 1) will make the corresponding PORTA pin input Clear TRISA bit (= 0) will make the corresponding PORTA pin output PORTB and the TRISB Register PORTB is an 8-bit wide, bidirectional port. Data direction register is TRISB. Set TRISA bit (= 1) will make the corresponding PORTA pin input Clear TRISA bit (= 0) will make the corresponding PORTA pin output PORTC and the TRISC Register PORTC is an 8-bit wide, bidirectional port. Data direction register is TRISC. Set TRISC bit (= 1) will make the corresponding PORTC pin input Clear a TRISC bit (= 0) will make the corresponding PORTC pin output PORTD and the TRISD Register PORTD is an 8-bit wide, bidirectional port. Data direction register is TRISD. Set TRISD bit (= 1) will make the corresponding PORTD pin input
Clear TRISD bit (= 0) will make the corresponding PORTD pin an output PORTE and the TRISE Register PORTE is an 3-bit wide, bidirectional port. Data direction register is TRISE. Set TRISE bit (= 1) will make the corresponding PORTE pin input Clear TRISE bit (= 0) will make the corresponding PORTE pin output
Addressing modes
The various ways of accessing data are called addressing modes. The data could be in a register or in memory, or be provided as an immediate value. 1. Direct Addressing 2. Indirect Addressing 1.Direct Addressing Mode: In direct addressing mode, the operand data is in a RAM memory location whose address is known and this address is given as a part of instruction. Direct Addressing is done through a 9-bit address. This address is obtained by connecting 7th bit of direct address of an instruction with two bits (RP1, RP0) from STATUS register as is shown in example. Any access to SFR registers is an example of direct addressing. Eg. Bsf STATUS, RP0 ;select bankl movlw 0xFF ;w=0xFF movwf TRISA ;address of TRISA register is from ;instruction movwf
taken
2.Indirect Addressing Mode:Indirect addressing is possible by using the INDF register. Any instruction using the INDF register actually accesses the register pointed to by the File Select Register,FSR. Reading the INDF register itself, indirectly (FSR = 0) will read 00h. An effective 9-bit address is obtained by concatenating the 8-bit FSR register and the IRP bit. Eg. Lets take one general purpose register (GPR) at address 0Fh contains a value of 20. By writing a value of 0Fh in FSR register we will get a register indicator at address 0Fh, and by reading from INDF register, we will get a value of 20, which means that we have read from the first register its value without accessing it directly (but via FSR and INDF). It appears that this type of addressing does not have any advantages over direct addressing, but certain needs do exist during programming which can be solved smoothly only through indirect addressing. Eg: Temp Equ 0x030 ;address of temp is 0x030 Movlw 0x30 Movwf FSR Movlw 5 Movwf INDF
Instruction Set
The PIC16 instruction set is highly orthogonal and is comprised of three basic categories: Byte-oriented operations Bit-oriented operations Literal and control operations
BYTE-ORIENTED OPERATIONS
ADDWF f,d Add W and f
Add the contents of the W register with register f. If d is 0, the result is stored in the W register. If d is 1, the result is stored back in register f. ANDWF f,d AND W with f
AND the W register with register f. If d is 0, the result is stored in the W register. If d is 1, the result is stored back in register f. CLRF f Clear f
The contents of register f are incremented. If d is 0, the result is placed in the W register. If d is 1, the result is placed back in register f. INCFSZ f,d Increment f, Skip if 0
The contents of register f are incremented. If d is 0, the result is placed in the W register. If d is 1, the result is placed back in register f. If the result is 1, the next instruction is executed. If the result is 0, a NOP is executed instead, making it a 2 TCY instruction. IORWF f,d Inclusive OR W with f
Inclusive OR the W register with register f. If d is 0, the result is placed in the W register. If d is 1, the result is placed back in register f. MOVF f,d mov f
MOVF instruction is used to bring data into WREG register from I/O pins Eg: MOVF PORTB,W MOVWF f ; Bring data from portB into WREG mov W to F
Mov the content of WREG register to F register Eg: MOVLW 55H MOVWF PORTB NOP RLF f,d ;WREG= 55H : Copy WREG to PortB (PortB=55H)
The contents of register f are rotated one bit to the left through the Carry flag. If d is 0, the result is placed in the W register. If d is 1, the result is stored back in register f. RRF f,d Rotate Right f through Carry
The contents of register f are rotated one bit to the right through the Carry flag. If d is 0, the result is placed in the W register. If d is 1, the result is placed back in register f. SUBWF f,d Subtract W from f
Subtract (2s complement method) W register from register f. If d is 0, the result is stored in the W register. If d is 1, the result is stored back in register f.
SWAPF f,d
Swap Nibbles in f
The upper and lower nibbles of register f are exchanged. If d is 0, the result is placed in the W register. If d is 1, the result is placed in register f. Eg: MYREG EQU 72h MOVWF MYREG SWAPF MYREG,F ; MYREG = 72H ; MYREG = 27H
XORWF f,d
Exclusive OR W with f
Exclusive OR the contents of the W register with register f. If d is0, the result is stored in the W register. If d is 1, the result is stored back in register f. DECFSZ f,d Decrement f, Skip if 0
The contents of register f are decremented. If d is 0, the result is placed in the W register. If d is 1, the result is placed back in register f. If the result is 1, the next instruction is executed. If the result is 0, then a NOP is executed instead, making it a 2 TCY instruction. DECFSZ MYREG, F ; decrement MYREG and skip if MYREG =0 (Here F=1)
BIT-ORIENTED FILE REGISTER OPERATIONS BCF f,d Bit Clear f Eg: BCF TRISB , 2 ; bit=0,make RB2 an output pin
Bit b in register f is set. Eg: BSF PORTD, 0 BTFSS f,d ; bit set turn on RD0 Bit Test f, Skip if Set
If bit b in register f is 0, the next instruction is executed. If bit b is 1, then the next instruction is discarded and a NOP is executed instead, making this a 2 TCY instruction. Eg: BTFSS PORTB,3 BRA HERE BSF PORTB,2 BTFSC f,d Bit Test, Skip if Clear ;keep monitoring RB3 for high
If bit b in register f is 1, the next instruction is executed. If bit b in register f is 0, the next instruction is discarded. Eg: BTFSC PORTB,3 BRA HERE BSF PORTB,2 ;keep monitoring RB3 for low
The contents of W register are ANDed with the eight-bit literal k. The result is placed in the W register. Eg: ADDLW k Add Literal and W
The contents of the W register are added to the eight-bit literal k and the result is placed in the W register. Eg: MOVLW 25H ADDLW34H ; ; Load 25H into the WREG add value 34 to W
Call Subroutine. First, return address (PC+1) is pushed onto the stack. The eleven-bit immediate address is loaded into PC bits <10:0>. The upper bits of the PC are loaded from PCLATH. CALL is a two-cycle instruction. CLRWDT Clear Watchdog Timer
CLRWDT instruction resets the Watchdog Timer. It also resets the prescaler of the WDT. Status bits, TO and PD, are set. GOTO k Unconditional Branch
GOTO is an unconditional branch. The eleven-bit immediate value is loaded into PC bits <10:0>. The upper bits of PC are loaded from PCLATH<4:3>. GOTO is a two-cycle instruction. IORLW k Inclusive OR Literal with W
The contents of the W register are ORed with the eight-bit literal k. The result is placed in the W register. RETURN Return from Subroutine
Return from subroutine. The stack is POPed and the top of the stack (TOS) is loaded into the program counter. This is a two-cycle instruction. SLEEP Go into the standby mode
The power-down status bit, PD,is cleared. Time-out status bit, TO, is set. Watchdog Timer and its prescaler are cleared.The processor is put into Sleep mode with the oscillator stopped. SUBLW k Subtract W from Literal
eight-bit literal k. The result is placed in the W register. XORLW k Exclusive OR Literal with W
The contents of the W register are XORed with the eight-bit literal k. The result is placed in the W register. Move literal to W
MOVLW K
Move literal value which is of 8-bit that can be range from 0-255 into the WREG register Eg: MOVLW 25h : RETFIE Mov 25h value into WREG register Return from interrupt
After an interrupt this instruction returns back to the main program. It does this by pulling from the stack the program counter. RETLW K Return with literal in W or subprogram with constant in W 8-bit constant K is stored in W register .Value off the top of the stack is stored in program counter Return from a
Relay Interfacing
Relays are devices which allow low power circuits to switch a relatively high Current/Voltage ON/OFF. For a relay to operate a suitable pull-in & holding current should be passed through its coil. Generally relay coils are designed to operate from a particular voltage often its 5V or 12V. The function of relay driver circuit is to provide the necessary current (typically 25 to 70ma) to energize the relay coil.
LCD Interfacing
Pin Description
Pin No 1 2 3 4 5 6 7-14 15 16 Description Vss Vcc Contrast Voltage RS R/W EN Data Input/Output Pins (D0-D7) Anode (K) Cathode (K)
1) Vss- Vss is connected to the ground. 2) Vcc- Vcc is connected to +5V. 3) Contrast Voltage- Vee is used for controlling LCD Contrast. Variable pin of Variable Resistor of 10K is connected to this pin. 4) RS- RS is used for the selection of internal resistors of LCD.
Command Code Register It allows the user to send command such as clear display, cursor at home etc. Data Register It allows the user to send data to be displayed on LCD. If RS=0, then Command Register is selected. If RS=1, then Data Register is selected. 5) R/W- R/W input allows user to write information to the LCD or read information from it. R/W=1 when Reading R/W=0 when Writing 6) EN- This enable pin is used by the LCD to latch information presented to its data pins. When data is supplied to data pins, a High-to-low pulse must be applied to this pin. 7) Pin 7-14 The 8-bit data pins, D0-D7 are used to send information to LCD or read the contents of the LCDs internal registers. To Display letters and numbers, ASCII codes for letters and numbers are sent to these pins while making RS=1.