Question

Assignment 2 OBJECTIVE: Flashing LEDs connected to port pin (PIC16F877): The objective of this practical assignment...

Assignment 2

OBJECTIVE: Flashing LEDs connected to port pin (PIC16F877):
The objective of this practical assignment is to familiarize the learner with some of the PIC microcontroller commands and to learn how fixed time delay routines are generated. Fixed time delay routines are used quite often in a variety of I/O interfacing tasks such as switch contact debouncing, periodic pulse width generation, slowing down of micro-controller for human interfacing etc…

PROGRAM DESCRIPTION:
1. Making use of MPLAB IDE, write firmware code to switch all LED’s connected to port pins RD0-RD7
continuously on for one second and then off for one second.
2. First use a flowchart to determine the program structure and then generate your commented firmware
source code.

PROCEDURE:
1. Making use of the text editor in MPLAB IDE, enter the given program extract and save the source file with   
an “.asm” extension.
2. Create a Project within MPLAB and attach the source code file.
3. Compile the source code by building the project. This will then produce a hex-file version of the original
source code.
4. Identify and rectify any code errors and then recompile the source code.
5. Interrogate the generated list file and hex file to familiarize yourself with the structure of these files.
6. Simulate the program by stepping though the program in “single-step” mode and hence verifying that the
program is working correctly.

Program:

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Hi-Tech C Code

#include <htc.h>
#define _XTAL_FREQ 8000000
void main()
{
  TRISB=0X00;
  PORTB=0X00;
  while(1)
  { 
    PORTB=0XFF;
    __delay_ms(1000);
    PORTB=0X00;
 __delay_ms(1000);
  }
}

 

This is the core style of writing the microcontroller program. As you can see, the style is completely similar to that of the normal C programs. Just some additional keywords here. Remember that all the syntax and mathematical and logical operations supported by stdio and conio libraries are accepted here in htc library with some additional ones also.

  1. In the first line, we start the program by including the library. This step is nothing new.
  2. Now we need to define the frequency of oscillator used in the system. This is the style of defining the oscillator frequency. The value 8000000 means its frequency is 8MHz.
  3. Start of the main program code
  4. TRIS is the command used for initializing the ports of the microcontroller. TRISB is an 8 bit register, in fact every word written in capital represents a register inside the microcontroller and you can get all the information about the register in the datasheet. Using this line will start the use of PORT B and if the value is ‘1’ in the respective register bit, that pin will be made input and if the value is ‘0’, the pin will be made output. One more thing, the use of ‘0x’ in that line represents that we are giving the data in hexadecimal number system. You can use ‘0b’ if you wish to give the data in binary and use nothing if you use decimal number system. So this line makes each 8 lines of port B output. Other way to give the same command is: TRISB=0; (decimal) and TRISB=0b00000000; (binary).
  5. PORTB is also a register like TRISB. This register passes the value out of port B. This means PORTB=0x00 will give low output from each eight line of port B. This is equivalent to clearing of port B at the start from any stray values. This line is not needed because there are no stray values at the start.
  6. Start of while loop. We write the entire program inside the while loop. After all initializations have been done, the main code is written here. This is done because this segment of program has to repeat over and over but the initializations do not need to be repeated. That is why the main program is written inside the while loop.
  7. PORTB is also a register like TRISB. This register passes the value out of port B. This means PORTB=0xff will give high output from each eight line of port B.
  8. This line is written to have a delay of 1000ms. So high logic will pass from port B pins for 500ms.
  9. This line will make the output from port B get down to 0V, ie logic ‘0’. It is to be considered that until the value in PORTB or any other port register is changed, the output from the corresponding port will also not change. So the output will drop from 5V (logic 1) to 0V (logic 0) only when this line is executed.
  10. Again a delay of 1000ms.
  11. End
  
 
Add a comment
Know the answer?
Add Answer to:
Assignment 2 OBJECTIVE: Flashing LEDs connected to port pin (PIC16F877): The objective of this practical assignment...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT