Question
Demo section How to create and use functions in C, pass variables, return variable, do delays Problem section 1. Create your own project for a
Demo section
How to create and use functions in C, pass variables, return variable, do delays
Problem section
1. Create your own project for a PIC18F4620 processor
Define the crystal frequency to 10 MHz for the delay and turn off the WDT
#define _XTAL_FREQ 10000000
#pragma config WDT = OFF
Set the TRISD register for PORTD as outputs Include a main program with an endless loop Toggle the value on LATD to make the LEDS change once per second Use a loop to create a 1 second delay from 100x 10ms delays _delay_ms(10); Test on the PICSimLab simulator Set the speed of the simulator to 10 MHz
2. Create a subroutine which achieves a one second delay Call that from the main program instead of the inline delay you had before
3. Break the program into two files - the main and the delay routine Create a header file the provides the function prototype Use a #include in the main program
4. Alter the program to pass a parameter to your delay routine where the parameter is the number of seconds to delay.
Homework section
Create a function that reads the state of buttons on PORTB bits 0 to 3 returning an integer value of 0 when no button is pressed, 1 when PBO==0, 2 for PB1==0, 3 for PB2==0 and 4 for PB3==0.
Create a function to write that return value to PORTD, so it can be seen on the LEDS.
Create a new file mylo.c in your project and copy both routines above into it. Create a mylo.h flie to define the function prototypes for the routines. Include that .h file into your project and remove the routines from being in the same file as your main.
Include some #define statements in your mylo.h which define suitable names for the button values 1 to 4 and use them in the main program for a test or two.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started