Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] Problem with PIC 18F4550

Status
Not open for further replies.

Naseer Ahmed

Newbie level 5
Joined
Feb 19, 2015
Messages
9
Helped
1
Reputation
2
Reaction score
1
Trophy points
3
Location
Islamabad, Pakistan, Pakistan
Activity points
113
Dear Friends,

I am interested to utilize PIC 18F4550 for a project.
I have written the following code in MikroC compiler to make PORTB.F1 and PORTB.F2 of PIC18F4550 high (1).

Code:
void main()
{
   TRISB = 0x01;
   PORTB = 0;
   while (PORTB.F0 == 0){}
   PORTB.F1 = 1;
   DELAY_MS(100);
   PORTB.F2 = 1;
   DELAY_MS(100);
}

The PORTB.F0 has been made input and a 5 V supply has been connected to it through a push button. While it is zero, the controller polls across "while (PORTB.F0 == 0){}" . When I press the push button, it must make PORTB.F1 and PORTB.F2 high but it doesn't. I mean it doesn't sense the push button on PORTB.F0.

Can any one tell me that how is to program other registers associated with PORTB to utilize it just as input and output PORT?

I am interested to utilize this port only for this purpose, not others.

Thanks
 

PORTB has ADC function. You have to configure ADCON1 register and disable ADC function. Zip and post your complete mikroC PRO PIC project files and your circuit in PDF format or Proteus format.
 

Also, you are writing '0' on all pins of PORTB, including that set to be Input, which may have been inadvertently changed the TRISB just before reading the value of the pin 0. You don't know exactly what compiler decided do to build your code. Considering that PIC core has instruction for access all the pins individually, you could write to only the outputs of interest.
 

You need a run loop in the 'main' function. Embedded code is not expected to return from the 'main' function and typically (not sure about MikroC but probably correct) this causes a reset of the device and the 'main' being called again.
Assuming you put in a while loop after the initialisation code, you probably need to test for when the button is released as well and then lower the output pins.
Do you have a pull-down on the pin? If not then it will float and, as it is set as an input, can float high which will be seen by your code as a button press. The pull-down will keep the pin low until the button is pushed.
Also, you may get the program to work correctly but for the wrong reasons as you are not taking debounce into account.
The two 100mSec delays on the first high value seen on the pin will mask the bounces that can last for a number of mSec but can lead to bad habits later on when debounce is important.
Susan
 

Thanks a lot all of you...

Dear Okada,

I have attached circuit image in proteus..... Image.png
 

I suggest that you connect the button directly between the pin and Vcc and hte resistor between the pin and Gnd.
Alternatively, that device can use internal pull-up on the PORTB by setting the RBPU bit to 0. However that means you need to connect the button between the pin and ground and reverse your logic.
As this is a simulation, you may not get the contact bounce I mentioned above - but you will as soon as you use real hardware (one of the many limitations of a simulator).
Susan
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top