help with code and hardware PIC18F452

Thread Starter

InnocentOfTheWorld

Joined Apr 15, 2010
47
I have two switches attached to pic18f452
one is on PORTA pin 0 other on PORTA pin 1.
I want to monitor timing for how much long they are on (both , single and none).
LCD is attached to show count of the loop so i can calculate time with the help of machine cycle.

I tried first only to send a character after recording values of the events(switch on off or both ) four times and then averaging it.

Both i need help on proteus circuit and code. Kindly tell me the errors.
 

Attachments

absf

Joined Dec 29, 2010
1,968
Unable to open your .DSN file as my version of ISIS is older than yours.

Anyway I am posting your code here so others dont need to unzip.

Rich (BB code):
#include <p18f452.h>
#define heel PORTAbits.RA0    // Sensor input (Limit switch at heel)
#define toe PORTAbits.RA1     // Sensor input (Limit switch at toe)
#define ldata PORTD           // PORT D = LCD data pins
#define rs PORTBbits.RB0      // rs = PORTB.0
#define rw PORTBbits.RB1      // rw = PORTB.1
#define en PORTBbits.RB2      // en = PORTB.2
#define busy PORTDbits.RD7    // busy = PORTD.7
unsigned int dorsiflexion = 0;
unsigned int plantarflexion = 0;
unsigned int swing =0;
void LCD_data( unsigned int ,unsigned int,unsigned int);
void lcdcmd(unsigned char);
void lcddata(unsigned char);
void lcdready(void);
void MSDelay (unsigned int );

void main(void)
{
 int cycle = 0;
 int change=1; 
 TRISA = 0xff ;
 TRISD = 0;
 TRISB = 0;
 PORTB = 0;
 en = 0;

while(cycle!=5)
{ 

if(heel==1 & toe==1)
   { 
      PORTBbits.RB3 = 1 ;
	  if(change==1)
		{	
            
            cycle++;
            change=0;
         
        }
    }   
else if(cycle==5) 
	LCD_data(swing , plantarflexion, dorsiflexion);	 
else if(heel==0 && toe==1)
      { 
         plantarflexion++;
      }
else if (heel==0 && toe==0)
      {
         swing++;
      }
else if (heel==1 && toe==0)
      {  
         dorsiflexion++;
         change=1;
      }
} 
}
void LCD_data(unsigned int a,unsigned int b,unsigned int c)
    {
        unsigned int d,e,f;
        unsigned char h[2];
        unsigned char i[2];
        unsigned char j[2];
		d=a/4; e=b/4; f=c/4;
        h[0]= d & 0xff;
		h[1]=(d>>8)& 0xff;
		i[0]= e & 0xff;
        i[1]=(e>>8)& 0xff;
		j[0]= f & 0xff;
        j[1]=(f>>8)& 0xff;
		MSDelay(250);
		lcdcmd(0x38);
		MSDelay(250);
		lcdcmd(0x0E);
		lcdready();
		lcdcmd(0x01);
		lcdready();
		lcdcmd(0x06);
		lcdready();
		lcdcmd(0x86);
		lcdready();
		lcddata('a');
}

void lcdcmd(unsigned char value  )

{
	ldata = value;
	rs = 0;
	rw = 0;
	en = 1;
	MSDelay (1);
	en = 0;
}

void lcddata(unsigned char value  )

{
	ldata = value;
	rs = 1;
	rw = 0;
	en = 1;
	MSDelay (1);
	en = 0;
}

void lcdready()
{
	TRISD= 0xff;
	rs = 0 ;
	rw = 1;
	do
		{
			en = 1;
			MSDelay(1);
			en = 0;
		} while (busy==1);
		TRISD=0;



}

void MSDelay(unsigned int itime)

{
	unsigned int k,l;
	for(k=0;k<itime;k++)
	for(l=0;l<135;l++);

}
I think you forgot to mention what is the problem that you have encountered. Hardware or software ?

Allen
 
Last edited:

Thread Starter

InnocentOfTheWorld

Joined Apr 15, 2010
47
consider this simple code.

#include <p18f452.h>
#define heel PORTAbits.RA0 // Sensor input (Limit switch at heel)
#define toe PORTAbits.RA1 // Sensor input (Limit switch at toe)
#define ldata PORTB // PORT D = LCD data pins

void delay_s(int);

void main(void)
{
int cycle = 0;
int change=1;
TRISA = 3 ;
TRISB = 0;
PORTAbits.RA6=0;

while(1)
{

if(PORTA==3)
{
ldata=255;
delay_s(250);
}
else
{ ldata=0;
delay_s(250);
}
}
}
void delay_s(int itime)
{
unsigned int k,l;
for(k=0;k<itime;k++)
for(l=0;l<135;l++);

}

i make both pins of PORTA 0 and 1 as input and set them high.
as in my code all of PORTB should get high but that is not happening in Proteus they all are low. Is my code not ok?
In simple my problem is that controller is not reading input from its pins.
 
Last edited:

absf

Joined Dec 29, 2010
1,968
if(PORTA==3)
Port A is an 8 bit port. How are you sure that RA2-RA7 are all zeros?
I think you should mask (logical AND) portA with 03H first before testing to be safe.

Are PortA set to analogue by default in 452? In the 16F series you have to set the input to digital before using it.

Allen
 
Last edited:

GopherT

Joined Nov 23, 2012
8,009
Yes, clear ANCON0 register, then set TRISA to 0x03

No need to set input pins high, they don't respond - they only measure the voltage connected to them from the circuit.
 
Top