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.

PIC PORTA digital input HOW

Status
Not open for further replies.

LUNAR

New Member
hello I am using pic18f4550 and i am trying to use portA as digital input on pins RA0,RA1,RA2,RA3
i have disabled the analog input
i have set the pins as output

i am getting digital input only on RA0 and RA1 but not on RA2 andRA3
what could be the problem
 
hello I am using pic18f4550 and i am trying to use portA as digital input on pins RA0,RA1,RA2,RA3
i have disabled the analog input
i have set the pins as output

i am getting digital input only on RA0 and RA1 but not on RA2 andRA3
what could be the problem

hi,
I expect you mean input.:)

Have you set CMCON as 0x07.?
 
thanks for reply
ya thats definitely input.
yes i have set the cmcon reg.
but the strange thing is, that in the header file #include <p18f4450.h>
this register is not defined so i had to use #include <p18cxxx.h> header file.
do u get anything from this
 
this is my code
Code:
 //#include <p18f4450.h>
#include <p18cxxx.h>
//#pragma config WDT = OFF, LVP = OFF, FOSC = INTOSC_HS  //internal oscillator

#pragma config WDT=OFF,LVP=OFF,FOSC=HSPLL_HS,PLLDIV=5,CPUDIV=OSC2_PLL3 //run with external oscillator

void main (void);
void InterruptHandlerLow (void);

#define LCD_DATA   PORTD
#define LCD_EN    PORTCbits.RC2
#define LCD_RW    PORTCbits.RC1
#define LCD_RS     PORTCbits.RC0

#define A PORTAbits.RA0
#define B PORTAbits.RA1
#define C PORTAbits.RA2
#define D PORTAbits.RA3


unsigned char data[]="123456789     90";

unsigned char dd[]="DDDDDDDDDDDDD123";
unsigned char aa[]="AAAAAAAAAAAAA123";
unsigned char bb[]="BBBBBBBBBBBBB123";
unsigned char cc[]="CCCCCCCCCCCCC123";
unsigned char xx[]="xxxxxxxxxxxxxxxx";



void delay(int);
void WRT_COMM(void);
void WRT_DATA(void);
unsigned char *varr , var;

void LCD_sendstring(unsigned char *varr);

void LCD_dt();
void LCD_senddata(unsigned char var );

void initilizeLCD(void);
void lcd_clear(void);

void main ()
{
char DATA;


varr=&data[0];
ADCON1 = 0xFF; // turn all of port a to digital I/O
 

TRISCbits.TRISC6=1;
TRISAbits.TRISA3=1;
TRISAbits.TRISA2=1;
TRISAbits.TRISA1=1;
TRISAbits.TRISA0=1;
 

TRISAbits.TRISA0=1;
TRISAbits.TRISA1=1;
TRISAbits.TRISA2=1;
TRISAbits.TRISA3=1;
 

CMCON  = 0x07;  // CM0, CM1 and CM2 set to 1
 
initilizeLCD();
lcd_clear();
LCD_dt(); 



while(1)
{
	if(A==1 && B==0 && C==0 && D==0)
		{
	varr=&aa[0];
	LCD_dt();
		}
	else if(A==1 && B==1 && C==0 && D==0)
		{
	varr=&bb[0];
	LCD_dt();
		}
	else 
	if(A==1 && B==0 && C==1 && D==0)
		{
	varr=&cc[0];
	LCD_dt();
		}
	else if(A==1 && B==0 && C==0 && D==1)
		{
	varr=&dd[0];
	LCD_dt();
		}
	else
	varr=&xx[0];
	LCD_dt();

 }

}
 
Hi,
Sorry can't help with 'C', others will.:)
 
Lunar my datasheet
PIC18F2455/2550/4455/4550

Data Sheet

has info about the CMCON register in many places. What are you looking at ?

First did you setup TRISA correctly? I found this in your code
Code:
TRISAbits.TRISA3=1;
TRISAbits.TRISA2=1;
TRISAbits.TRISA1=1;
TRISAbits.TRISA0=1;
 
 
TRISAbits.TRISA0=1;
TRISAbits.TRISA1=1;
TRISAbits.TRISA2=1;
TRISAbits.TRISA3=1;

You set RA0-RA3 as inputs twice. Once would have been enough.

Next check the table 10-2
SUMMARY OF REGISTERS ASSOCIATED WITH PORTA

to determine what other registers could be causing problems.

it looks like you have CMCON correct.

Look at what bits ADCON1 effects. You used
Code:
ADCON1 = 0xFF; // turn all of port a to digital I/
The value 0x0F does that. But 0xFF (bits 4 and 5) makes RA2 and RA3 into voltage ref pins.









 
This code works in the simulator. The stimulus file set RA bits to 0x2A after 40 cycles.

My guess is that you LCD code is changing the TRISA settings or possibly ADCON1 or CMCON. Check initilizeLCD first.

Code:
//#include <p18f4450.h>  // not sure why this include does not work
#include <p18cxxx.h>


void main ()
{
  unsigned char DATA;
  
  ADCON1 = 0x0F; // *** turn all of port a to digital I/O
  CMCON  = 0x07;  // CM0, CM1 and CM2 set to 1 
  
  TRISA=0xFF;
  
  
  DATA = PORTA;  //  Works
  Nop();
  Nop();
  while(1);
}
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top