Tuesday, October 29, 2013

Tutorial 3 PIC Microcontroller

All the questions asked below are related to the programming of PIC16F877A.
  1. If any line of the program is added with semicolon in front, how does the assembler interpret it?

Assembler will ignored it

  1. Write a short program to add the content in memory address 0x20 with 5, and save the result in 0x22 address.

LIST P=16F877A
#INCLUDE “P16F877A.INC”

__CONFIG _CP_OFF & _WDT_OFF & _XT_OSC & _PWRTE_ON

P1 EQU 0X20
P2 EQU 0X22

ORG 0X00

MAIN NOP
MOVF  P1,W
ADDLW 0X5
MOVWF P2
SLEEP
END

  1. In what condition instruction of  “GOTO $”  or “SLEEP”, is normally put in the end of the program?

If a program does not run in an infinite loop the last instruction must be SLEEP.
(or goto $)

  1. “END” at the end of the program is a directive or instruction?

Directive.

  1. #include is normally put at the beginning of a program, what will happen during the compilation if it is not included in the program?

In PIC16F877A.inc file, all the parameters used in programming are well defined. If the program did not include PIC16F877A.inc file, compiler would not be able to translate the parameters in the program, translation to machine code will fail.

  1. List down four possible fields on the source code line
There are four possible fields on a source code line:
·         The label is the address in the program memory where the instruction is stored. A label always starts in the first position of the line.
·         The mnemonic of the instruction (for example MOVF, BSF).
·         Operands which always belong to the instruction.
·         The comment is always preceded by a semicolon.
  1. Instruction set of PIC16F877A is consisted of 36 instructions. It can be divided into three categories, namely, byte-oriented file register operations, bit-oriented file register operations and literal and control operations. Refer to datasheet, show the differences among these categories by giving an examples for each of it.

Byte-Oriented File Register Operations


f = Register file address (0x00 to 0x7F)
Destination select; d = 0: store result in W,
d = 1: store result in file register f.
Default is d = 1.

Example: ADDWF f, d

Bit-Oriented File Register Operations


b = 3-bit bit address
f = 7-bit file register address

Example: BCF f, d

Literal and Control Operations
           
Example:  ADDLW k
            k = Literal field, constant data or label


  1. Write a “one millisecond delay” program
If the fosc = 4MHz, one instruction cyle = 1us
(refer to LED.VBB)

DELAY1                     ;1ms
            MOVLW          .250
            MOVWF          DelayCounter
DELAY.Loop
            NOP
            DECFSZ          DelayCounter,F
            GOTO  DELAY.Loop
            RETURN
  1. Refer the program below explain what will happen to output at PortB.
      MAIN
                  MOVLW          .255                
                  XORWF           PORTB,1
                  CALL  DELAY
      GOTO  MAIN

All the pins at port B will be toggled to “1” or “0” alternatively. Every time after it toggles port B, it will delay for a while before another toggle.

  1. Write a subprogram to set port D as output and port C as input.
;======Begin PORT Configuration
            BSF STATUS, RP0      ;PORT TRIS Registers are on page 1
            ;======PORT C
            MOVLW   .255            ;PORTC Configuration Bits
            MOVWF   TRISC
            ;======PORT D
            MOVLW   .0                ;PORTd Configuration Bits
            MOVWF   TRISD
            BCF STATUS, RP0      ;Restore Page0

  1. Write a subprogram to input the signal from port C and output it to port D.
MOVF             PORTC,W
MOVWF          PORTD

  1. Write a subprogram which enables external interrupt from pin RB0/INT.
BCF     INTCON, INTF           ;Clear Interrupt flag
BSF      INTCON,INTE            ;Enable the INB0 Interrupt
BSF      INTCON,GIE               ;Enable the Interrupts

  1. Write a subprogram to trigger the interrupt when falling edge occur at pin RB0/INT.

BCF INTCON, INTF                            ;Clear Interrupt flag
BSF STATUS,RP0                               ;Access bank 1
BCF OPTION_REG,INTEDG  ;Falling Edge Detect
BCF STATUS,RP0                               ;Restore bank 0
BSF INTCON,INTE                             ;Enable the INB0 Interrupt

  1. For keypad interfacing, which port of PIC16F877A is the most suitable to be used? Why?(Refer to 16F877 datasheet)

RB4:RB7 have interrupt-on-mismatch feature, together with software
configureable pull-ups on these four pins, allow easy interface to a keypad and make it possible for wake-up on key depression.

  1. Write a program to enable Port B pull-ups.

BSF STATUS,RP0                   ;Access Bank1 Configuration Bits
BCF   OPTION_REG,NOT_RBPU
BCF STATUS,RP0

  1. When the LCD display is not enabled, data lines are tri-state, what is meant by tri-state?

data lines are tri-state means they are in a state of high impendence (as though they are disconnected) and this means they do not interfere with the operation of the microcontroller when the display is not being addressed. 

  1. The LCD requires 3 "control" lines from the microcontroller, namely Enable (E), Read/Write (R/W) and Register select (RS). Describe the function of each of the line.

Enable (E)
This  line allows access to the display through R/W and RS lines. When this line is low, the LCD is disabled and ignores signals from R/W and RS. When (E) line is high, the LCD checks the state of the two control lines and responds accordingly.
Read/Write (R/W)
This line determines the direction of data between the LCD and microcontroller. When it is low, data is written to the LCD. When it is high, data is read from the LCD.
Register select (RS)
With the help of this line, the LCD interprets the type of data on data lines. When it is low, an instruction is being written to the LCD. When it is high, a character is being written to the LCD.
 
  1. The LCD controller needs 40 to 120 microseconds (uS) for writing and reading. Other operations can take up to 5 mS. During that time, the microcontroller can not access the LCD, so a program needs to know when the LCD is busy. Suggest two methods to overcome this problem.

One way is to check the BUSY bit found on data line D7. This is not the best method because LCD's can get stuck, and program will then stay forever in a loop checking the BUSY bit. The other way is to introduce a delay in the program. The delay has to be long enough for the LCD to finish the operation in process.







No comments:

Post a Comment