Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

setting portA as digital I/O in pic18f2420??

Status
Not open for further replies.

ktkoota

New Member
Hi,
Iam working on PIC18F2420 ,it has 3 ports A,B,C .. I used all the bits in port B and C so I am run out of free bits.. PortA has other functions beside parallelI/O like analog, how can i define it to be for digital input/output only and what are settings for CMCON1, CM0,CM1,CM2..

best regards,
Ktkoota
 
If you look in the data sheet in section 10.1, it has the exact code required to make the port digital.

For your convenience,
Code:
	clrf	PORTA		; Initialize PORTA by
				; clearing output 
				; data latches
	clrf	LATA		; Alternate method
				; to clear output 
				; data latches
	movlw	07h		; Configure A/D
	movwf	ADCON1		; for digital inputs
	movwf	07h		; Configure comparators
	movwf	CMCON		; for digital input
	movlw	0CFh		; Value used to
				; initialize data 
				; direction
	movwf	TRISA		; Set RA<3:0> as inputs
				; RA<5:4> as outputs

Mike.
 
thank you , but I am using c languge.. can you post it in c language
Q: by this way the all portA will be digital?
 
For setting PORTA as digital input/output while turning every thing off,

ADCON1=0x0f; //Taking that you have not used the AD converter module even for PORT B, otherwise refer to page 226 of datasheet
CMCON=0x07; // Turns all the comparators off, page 235,236 of datasheet

There you have it, all ports Digital, use TRISA to set direction for PORTA.
 
#include<P18f2420.h>
void main(void)
{
CMCON=0x07;
ADCON1=0x0f;
while(1);
}

Taking that you use C18.I checked it in MPLab sim. It works fine. Have you selected correct processor from configure>select device.
 
Last edited:
View attachment pic18.h
this is my header file used until now in my project..
is there another header file needed for portA??

Iam using picc18.exe compiler not mpLab and I am writing the same code as yours but this error occurs
 
Last edited:
:(anybody please.. I need this port for finalizing the project .. I googled for this error meaning but no benifit..
 
The header file you're using is generic to te pic18f series of chips. You need to specify your exact device so target locations are known. Do this as Wond3rboy showed you above:

#include<p18f2420.h>
 
View attachment 42956
this is my header file used until now in my project..
is there another header file needed for portA??

Iam using picc18.exe compiler not mpLab and I am writing the same code as yours but this error occurs

The header file you have posted is a trimmed down version of the pic18.h header file available in hi tech C pro compiler. First i would suggest you replace it with the original one. The original one wont take up any extra code(if you haven't deleted any thing about the processor).Here is the original file. This code works fine.
Code:
#include <pic18.h>
void main(void)
{
	CMCON=0x07;
	ADCON1=0x0F;
}
 

Attachments

  • pic18.h
    20.1 KB · Views: 892
Last edited:
As you are using the compiler from a command prompt you need to tell it the include paths or copy the relevant files to your project directory. It maybe that the old version of the compiler doesn't support the 2420, try downloading the latest version.

Mike.
 
Hi 3V0 and Mike,

@ktkoota i am surprised you are using the compiler in command prompt, like 3V0 said, go for an IDE, i would say MPLab, it has got a host of other tools too.
 
Last edited:
ok.. I will try to use MPlab .. command prompt is instructed from my supervisor!! but the most important thing is to finalize the project.. I will tell you all my prgress with MPlab

Regards,
ktkoota
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top