Multisim and Ultiboard

cancel
Showing results for 
Search instead for 
Did you mean: 

Lighting up a LED with PIC16F84A controller

So basically we have a truth table for our randomly generated sequence of bits, we used logical elements in multimedia logic to light up all the LED's and it was fine and it worked. Now we need to do the same thing in multisim use a microcontroller AND program the logic for it in C, and the professor, didn't bother teaching us anything, just told us to do it and thats all.

So yeah, I'm stuck and here is the situation:

Create a circuit with 4 switches that connect to microcontroller and a LED. The LED needs to light up when a correct combination of switches are flipped according to your truth table.

And that's the gist of it, I really have no idea how to make that circuit ( attaching screenshot of what I did )

Nor I know how to correctly program the microcontroller in C to make it work, any help would be appreciated.

0 Kudos
Message 1 of 4
(126 Views)

 

Hi Wowy,

 

 

Regarding your circuit:

 

Switches S1 to S4 use the same key (A) for toggling. Choose other keys for S2 to S4.

 

The value of R5 (10 kΩ) is too high, use smaller value such as 750 Ω. Lower values may be used to increase the brightness of LED, the lower limit is determined by the drive capability of Port RB0.

 

Instead of 8 kΩ for R1 to R4, you can use 8.2 kΩ or 7.5 kΩ which are easier to obtain.

 

Make sure that you selected internal RC oscillator option for clock source.

 

 

Regarding your code:

 

Posting a screenshot is fine because it shows editor features such as syntax highlighting but you should also post the code as text.

 

You have to show the truth table that you mentioned:

 

  • Without the table it's confusing to determine whether you turned a simple ORing to longer and incorrect if..else if..else block or you inappropriately named the function OR4.
  • The table will help determine if the if..else block which sets Port B to either 0x00 or 0xFF is correct or inverted.

 

 

Best regards,

G. Goodwin

 

0 Kudos
Message 2 of 4
(80 Views)

Hey,

I made the adjustments you suggested ( hopefully ), except for the clock part, as I don't know where to find it.

( Adding the new circuit screenshot )

 

As requested the code and the truth table:

#include "pic.h"

#define bitset(var,bitno)((var) |=   1 << (bitno))
#define bitclr(var,bitno)((var) &= ~(1 << (bitno))

void init(void)
{
	bitclr(STATUS, RP0);	// Select Bank 0
	PORTA = 0x00;
	PORTB = 0x00;
	bitset(STATUS, RP0);	// Select Bank 1
	TRISA = 0xFF;			// Set Port A pins to input mode
	TRISB = 0x00;			// Set Port B pins to output mode
}

int OR4(int a, int b, int c, int d)
{
	int result;

	if(a == 0 && b == 0 && c == 0 && d == 1){
		result = 1;
	} else if(a == 0 && b == 1 && c == 0 && d == 0){
		result = 1;
	} else if(a == 0 && b == 1 && c == 0 && d == 1){
		result = 1;
	} else if(a == 0 && b == 1 && c == 1 && d == 1){
		result = 1;
	} else if(a == 1 && b == 0 && c == 0 && d == 1){
		result = 1;
	} else if(a == 1 && b == 0 && c == 1 && d == 0){
		result = 1;
	} else if(a == 1 && b == 0 && c == 1 && d == 1){
		result = 1;
	} else {
		result = 0;
	}

	return result;
}

void main()
{	
	init();

	while(1)
	{
		int result = OR4(RA0,RA1,RA2,RA3);

		if(result == 1){
			PORTB = 1;
		} else {
			PORTB = 0;
		}
	}
}

W

X

Y

Z

F

0

0

0

0

0

0

0

0

1

1

0

0

1

0

0

0

0

1

1

0

0

1

0

0

1

0

1

0

1

1

0

1

1

0

0

0

1

1

1

1

1

0

0

0

0

1

0

0

1

1

1

0

1

0

1

1

0

1

1

1

1

1

0

0

0

1

1

0

1

0

1

1

1

0

0

1

1

1

1

0

 

I made absolutely zero progress with this assignment and I'm getting desperate, so again any help is welcome. Thanks.

Message 3 of 4
(66 Views)


I'll start with the circuit, again. I recommend that you open the datasheet of PIC16F84A for you to verify the information I'm posting here. I'm currently using a low-end smartphone which has a problem opening pdf files so I can't do the verification.

 

In your circuit ~MCLR is directly connected to Vdd (5 V). This is not unusual for earlier PICmicro circuits, however, in newer PICmicros MICROCHIP does not recommend this anymore. Verify from the datasheet otherwise you can do these:

 

  • Connect a 1.5 kΩ to 2 kΩ resistor to ~MCLR.
  • At the other end of the resistor mentioned above, connect a 1 μF capacitor to ground (Vss) and a 4.7 kΩ to 10 kΩ resistor to +5 V (Vdd). Pay attention to polarity of capacitor terminals, negative electrode should be to Vss (ground).

 

In my previous reply, I mentioned that you should make sure that the internal RC oscillator is selected. This is because you don't have external components for clock generation. You have to verify from the datasheet if PIC16F84A has an internal RC aside from the RC oscillator otherwise you have to add an external RC network. You don't need crystal oscillator because the "application" does not need accurate clocking.

 

 

From the truth table it's clear to me now that the function OR4 is just inappropriately named. Perhaps function name such as EvalInputVars is better than OR4.

 

The code is readable from the screenshots that you attached previously, in fact, I stated that it is fine. The reason I requested for it to be inserted as text is for it to be readily copied and revised by any participant who would like to help you. I hope you've done it like this.

 

Anyway, I can see from the code that you posted that you already interchanged the setting of Port B to 0x00 and 0xFF. Moreover, the if .. else if .. else block corresponds to the truth table. The input switches can be evaluated faster but it's better to preserve the individual comparison and ANDing because you exerted an effort for it, that effort will help you learn.

 

0 Kudos
Message 4 of 4
(46 Views)