lab embedd

Upload: afiq-ali

Post on 02-Jun-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/11/2019 Lab Embedd

    1/11

    LAB: 6

    TITLE: Using PIC18 Timer to generate time delay and counter

    Learning Outcomes:

    1. Write PIC18 timer in C language to:

    a. Generate time delays

    b. Event counter

    Laboratory Equipment:

    1. Labsheet

    2. MPLAB IDE Software

    3. PC

    Procedure:

    Write the program below

    PROGRAM 1

  • 8/11/2019 Lab Embedd

    2/11

  • 8/11/2019 Lab Embedd

    3/11

    PROGRAM 2

  • 8/11/2019 Lab Embedd

    4/11

  • 8/11/2019 Lab Embedd

    5/11

  • 8/11/2019 Lab Embedd

    6/11

    PROGRAM 4

    1. Build the program and fix the errors if any.

    2. Analyze the result by using WATCH function.

  • 8/11/2019 Lab Embedd

    7/11

    Exercise:

    1. Write a C18 program to generate a frequency of 250Hz on all bits of PORTC. Use Time0,16-bit

    mode ,and no prescale to create the frequency.Assume XTAL = 10 MHz

  • 8/11/2019 Lab Embedd

    8/11

    2. Write C 18 program to create a frequency of 2500Hz on pin PORTB.1 .Use Time1 to create the

    delay.

  • 8/11/2019 Lab Embedd

    9/11

    3. Assume that a 60-Hz external clock is beeing fed into pin T0CKI(RA40. Write a C program forCounter 0 in 8 bit mode to display the seconds and minutes on PORTB and PORTD ,respectively.

  • 8/11/2019 Lab Embedd

    10/11

  • 8/11/2019 Lab Embedd

    11/11

    Question:

    1. What is the dif ferences between Program 1 dan Program 2?

    -Program 1 uses counter

    -Program 2 uses time delay.

    2. What is the function of time delay

    Time delay relays are simply control relays with a time delay built in. Their purpose is to control

    an event based on time.

    3. What is the function of counter

    The function of the program counter register is to hold the address of the instruction that is beingexecuted and (later) to hold the address of the instruction that will be executed next.

    4. Di fferentiate between time delay using C language and timer.

    - Time delay

    Using a simple for loop

    Delays can be observed either on the oscilloscope or using a simulator.

    In creating a time delay using for loop factors need to be considered

    Number of machine cycles and number of clock periods per machine cycle.

    Crystal frequency connected between XTAL1 and XTAL2. Duration of the clockperiod for the machine cycle is the function of crystal frequency.

    Compiler selected.

    Accuracy of the time delay is mainly due to the compiler used .

    -Timer

    Timers start and stop by either by software or hardware control.

    The start and stop of the timer are controlled by way of software by the TR (timer

    start) bits TR0 and TR1. The SETB instruction starts it, and it is stopped by the CLRinstruction. These instructions start and stop the timers as long as GATE=0 in theTMOD register.

    The hardware control of start and stopp of the timer is by an external source used to

    make GATE=1 in the TMOD register.