I need help with PIC18

Kagan_x

Joined Apr 4, 2021
9
Hey I'm new in here.

I have a problem.

I want to write a program. I designed a hardware and software for monitoring button connected to use PORTB. If there is press on buton then turn LED on. If you no press on the button the turn LED off.
So, I write code and I designed circuit to proteus.

Problem is LED always lights up.

When I do not press on button, LED is on, When I do press on button still LED is on. LED is always lights up.
I attach here two picture. Codes and circuits.

What is problem? is there anyone who can help

I use PIC18F45K22

Attachments

• 362 KB Views: 14
• 277.9 KB Views: 14

AlbertHall

Joined Jun 4, 2014
11,102
Note the extract below from the datasheet. So at power up RB3 will be an analog input and it will read '0' telling the program that the button is pressed. To fix this you can set the PBADEN config bit which will disable all analog input on PORTB, or you can set bit 3 of ANSELB as part of the pin setup section of your code.
There is no analog capability on RC0 so that is OK.

Kagan_x

Joined Apr 4, 2021
9
Note the extract below from the datasheet. So at power up RB3 will be an analog input and it will read '0' telling the program that the button is pressed. To fix this you can set the PBADEN config bit which will disable all analog input on PORTB, or you can set bit 3 of ANSELB as part of the pin setup section of your code.
There is no analog capability on RC0 so that is OK.

View attachment 234448
The firstly, thank you for help. I'm beginner to microcontroller.

How can I set the PBADEN config settings ?

AlbertHall

Joined Jun 4, 2014
11,102
In the CONFIG3H section add the line

Kagan_x

Joined Apr 4, 2021
9
In the CONFIG3H section add the line
Thank you for everything. I changed config settings, I did turn on after it is working.

But I still have a problem.

My friend wrote same code and he designed circuit. everything is same. (Ports, bits..)

My friend config set is CONFIG PBADEN = ON, but if he simulation circuit on protheus, circuit is working properly.

I did not understand. codes are same circuit is same, CONFIG PBADEN = ON, there is no problem, but my config is ON doesnt working.

what could be the reason for this ?

AlbertHall

Joined Jun 4, 2014
11,102
Maybe something IS different or maybe this is something to do with simulator settings. I don't know that simulator so I really can't comment on that.

Kagan_x

Joined Apr 4, 2021
9
Maybe something IS different or maybe this is something to do with simulator settings. I don't know that simulator so I really can't comment on that.
I have a another problem.

I want to light up 2 led, I wrote code.
When I simulation code on proteus, only one led lights up.

But if I use different ports, code is working properly.
So, if I use PORTB for 2 led only one is light up. other doesn't work.
but I use PORTB to led1 and use PORTC to led2 code is working.
I attached codes, what is problem ?

Attachments

• 171.6 KB Views: 4
• 192.7 KB Views: 4

AlbertHall

Joined Jun 4, 2014
11,102
Do you have the PBADEN turned off?
When you have two LEDs on port B, which pin is the one that doesn't work connected to?
Please attach all the code, preferably in code tags as below:

the word 'CODE' in square brackets
some code here
After the code '/CODE in square brackets.

Kagan_x

Joined Apr 4, 2021
9
Thank you so much, I try PBADEN turned off after it is working properly.
I'm beginner to microcontrollers. So, how can I improve myself about microcontrollers.
If you dont mind could you give me advise for develop myself, for example a book, platform or website.

And what is PBADEN, if it is ON or OFF what is difference ? I dont know what is for. How can I learn this settings

I am thankful fot your help

AlbertHall

Joined Jun 4, 2014
11,102
And what is PBADEN, if it is ON or OFF what is difference ? I dont know what is for. How can I learn this settings

I learnt my initial programming on the Sinclair Spectrum and got a job on the strength of that. Then I learnt as I went along - Commodore Pet, an HP compiter which used basic and had an IEEE488 interface built in and so on.

Kagan_x

Joined Apr 4, 2021
9
I have a problem, I wrote a simple code. When I debug proje, output is not correct.

I attached code and picture.

Code:
LIST    P=18f45k22
INCLUDE    "P18f45k22.INC"

ORG 0X00

CLRF TRISB
CLRF TRISC
MOVLW 0X55
MOVWF PORTB
MOVWF PORTC

COMF PORTB, F
COMF PORTC, F

END
my friend wrote same code, outputs are different again. But my teacher output is correct.

Attachments

• 151.3 KB Views: 2

AlbertHall

Joined Jun 4, 2014
11,102
The first thing that strikes me is that the COMF instruction will read the value of the port register, complement it, and write the value back again.
It will also hit the read-modify-write problem.
Another solution would be to write to, and complement the LATB and LATC registers. That gets around both problems and is better programming practice.

Kagan_x

Joined Apr 4, 2021
9
But my teacher wrote same code, code is working correct. I attached a picture.
He did not use PBADEN ( PBADEN = ON) . just write same code and click debug.

Maybe my Program MPLAB is not working properly ?
what is problem do you think ?

Attachments

• 128.3 KB Views: 3

AlbertHall

Joined Jun 4, 2014
11,102
The two programs you show are for different processors. PIC18F45K22 versus PIC18F452

Kagan_x

Joined Apr 4, 2021
9
The two programs you show are for different processors. PIC18F45K22 versus PIC18F452
We try tow microcontroller. I did PBADEN = OFF But still have a problem. This time PORTB result is correct, but PORTC result is not correct. I attached a picture.

Attachments

• 276.8 KB Views: 3

AlbertHall

Joined Jun 4, 2014
11,102
Add ANSELC = 0 to the setup code. This sets PORTC to digital I/O.

Kagan_x

Joined Apr 4, 2021
9
Thank you so much. Really How can I learn all of things I dont know..

AlbertHall

Joined Jun 4, 2014
11,102
Reading datasheets is a very good place to start.
Then learn by doing and ask questions about what you don't understand or what doesn't work.
You should realise that all the 'experts' here were not born as experts. We have all learnt. My first explorations were with valves (aka vacuum tubes). It's a long walk from there to microcontrollers!