ECE3301L LAB 2-Basic Input/Output using Microcontroller parallel ports Solved

35.00 $

Category:
Click Category Button to View Your Next Assignment | Homework

You'll get a download link with a: zip solution files instantly, after Payment

Securely Powered by: Secure Checkout

Description

5/5 - (1 vote)

LAB2
Basic Input/Output using Microcontroller parallel ports

To perform the lab below, you need to download the spec of the processor PIC18F4620 that we are using in the lab. Go to the following link:

http://ww1.microchip.com/downloads/en/devicedoc/39626e.pdf

Remember that this processor is an upgrade to the processor PIC18f4321.

Download the datasheet and save somewhere (like Desktop) that you can easily have access to it.

MA TERIALS:

Provided by instructor:

  • 3 RGB LEDs
  • (4) regular LEDs (any color)
  • 1 bank of 4 minimum DIP Switches

    Student must get the following:

• (4) 1K or higher resistors

PART 1)

Write a program that will input from a bank of 4 switches (DIP switches) and output each corresponding input to an equivalent LED. There would be a total of 4 LEDs. The input switches are connected from PORT A bits 0-3 while the output LEDs are tied to PORT B bits 0-3.

Hint:

a) Use the downloaded datasheet of the PIC18F4620, go to chapter 9 ‘I/O Ports’. Study the definitions of the TRISx registers where ‘x’ represents the PORT name. Set the right value of each bit in the TRIS register to setup whether the corresponding pin is an input or output. A ‘1’ will make the pin an input while a

‘0’ makes it an output. Reading something from a switch to a pin of a port will make that pin an input. Controlling a LED from a pin of a port will make that pin an output. Based on those definitions, you should be able to determine what port should be setup as an input port or an output port. From there, you should be able to figure the value to be placed into the TRISx registers. In this case, you should deal with the registers TRISA and TRISB.

  1. b)  If you use PORTA or PORTB for the inputs and/or outputs, you need to study the definitions of the register ADCON1 (see the chapter 19 ’10-bit Analog-to-Digital Converter A/D Module’ in the datasheet) and its PCFG3:PCFG0 bits. You need to make sure that the pins from ports A and B are setup in digital mode and not analog meaning all the pins must be digital.
  2. c)  Write an endless loop to read the input switches and output the values to the LEDs. Based on the schematics shown on ‘LAB2_Schematics.pdf’, the four inputs should be port B bits 0 through 3 while the four LEDs should be connected to port A bits 0 through 3.

Example:

void main() {
char in;

TRISA = 0x??; TRISB = 0x??; ADCON1 = 0x??;

while (1) {

// Use variable ‘in’ as char
// fill out the ?? with the proper values // fill out the ?? with the proper values // fill out the ?? with the proper values

// read data from PORTA and save it

// into ‘in’
// Mask out the unused upper four bits // Output the data to PORTB

} }

PART 2)

in = PORTA;

in = in & 0x0F; PORTB = in;

Connect the provided Common-Cathode RGB LED at D1 to the pins RC0 through RC2. Pin RC0 will be used to turn on the RED color of LED D1, RC1 is for the GREEN and RC2 is for the BLUE.

Write an endless loop where the lowest three pins of the DIP Switch SW1 (pins connected to port A bits 0 through 2) are read and then these three bits are outputs to the port C whereas the following color will be generated on the LED D1:

Inputs (SW1)

RA2 RA1 RA0 000 001 010 011 100 101 110 111

Outputs (D1)

RC2 RC1 RC0 Color
0 0 0 Nolight 001Red 010Green 011Yellow 100Blue 101Purple 110Cyan 111White

Don’t forget to add the TRISC command for this port C in your code.

Show the operation of this part by changing the switch settings of SW1 and check that the color of the RGB LED D1 matches with the combination of the switch. Make sure that the most significant bit of SW1 is on the left side of that switch and the LSB is on the right side.

PART 3)

Write an endless loop that will continuously generate the list of colors shown on Part 2) with about 1 second delay between each color. There is no input to read.

Hint:
Write the subroutine Delay_One_Sec() below:

void Delay_One_Sec() {

for(int I=0; I <17000; I++); }

Once this delay routine is written, use it to generate the time delay. You would need an overall endless loop that will output the 8 different colors spaced by 1 second delay. To achieve this task, you will need to add a FOR loop with an 8-count value (count from 0 to 7). Use the counter value as the color of the output to be generated. In the FOR loop, after you have output the color to the port C, add the Delay_One_Sec() routine to wait 1 second. Failure to have this delay will cause the colors to be generated at a very high rate so that your eyes cannot differentiate.

PART 4)

Based on the code on Part 3), add another Common-Cathode RGB D2 to the PORT D bits 0 through 2. Don’t forget to add the TRISD command for this port D in your code.

Take the code from part 3) and add the needed coded to generate the colors for the LED D2. Notice the pattern of the colors for D2 with respect to the ones for D1 so that there is a simple formula to generate those colors for D2.

Step #

0 1 2 3 4 5 6 7

PART 5)

Color @RGB LED D1

No light (off) Red
Green
Y ellow

Blue Purple Cyan White

Color @RGB LED D2

White
Cyan
Purple
Blue
Y ellow Green
Red
No light (off)

Now, this part will add another RGB LED D3 to Part 4. D3 has its three pins connected to PORTD bit 5 through 7. Here are the sequence of colors for the LEDs D1, D2 and D3.

  1. 0  No light (off)
  2. 1  Red
  3. 2  Green
  4. 3  Y ellow
  5. 4  Blue
  6. 5  Purple
  7. 6  Cyan
  8. 7  White

Cyan
Y ellow Purple
Green
No light (off) White
Red
Blue

Purple
Red
No Light (off) Blue
Cyan
Y ellow
Green
White

Due to the random assignments of the colors for D2 and D3, I suggest the use of an array to contain the proper values.

General reminder:

Make sure that for this lab create a general folder marked as ‘lab2’. From there, create sub-folders one for each part. Since we are going to have five parts, then we should have five sub-folders marked for example lab2p1, lab2p2, … lab2p5.

  • Lab-2-2h7h4v.zip