Pic18f2685

Thread Starter

digitalkid

Joined Nov 6, 2009
1
Hi Guys,

New to this, just starting out so be nice.

I am trying to get the above PIC just to light up aan LED on RA1, the code compiles but does not do anything when I program the PIC. Could you look over the code to see if anything is wrong.

#include <p18f2685.h>
#include <delays.h>
#include <string.h>

// delay routines
void delay_ms(int atime);
void delay_us(int dtime);

//void main (void)
void main()
{

TRISA = 0; // set PORTA as OUTPUT
TRISB = 0;

for(;;) // forever
{
TRISA = (0xFF);
PORTA = (0b11111111);
TRISB = (0xFF);
PORTB = (0b11111111);
//PORTA = 0xff ; // turn all LEDs ON
delay_ms(500) ; // wait 500 ms
//PORTA = 0 ; // turn all LEDs OFF
//delay_ms(500) ; // wait 500 ms
}
return(0);
}

//DELAYS//
void delay_us(int dtime)
{
int a;
for (a=0; a<dtime; a++)
{
Nop();
Nop();
Nop();
Nop();
}
}
void delay_ms(int atime)
{
delay_us(200);
delay_us(200);
delay_us(200);
delay_us(200);
delay_us(200);
}
 

mik3

Joined Feb 4, 2008
4,843
for(;;) // forever
{
TRISA = (0xFF);
PORTA = (0b11111111);
TRISB = (0xFF);
PORTB = (0b11111111);
//PORTA = 0xff ; // turn all LEDs ON
delay_ms(500) ; // wait 500 ms
//PORTA = 0 ; // turn all LEDs OFF
//delay_ms(500) ; // wait 500 ms
}
return(0);
}
In the for loop you have TRISA register equal to 0xFF which sets portA pins as inputs.The same is true for TRISB. Remove the TRISA and TRISB instruction from the loop if you want your pins to be defined as outputs (you have previously defined them).
 

AlexR

Joined Jan 16, 2008
732
All PIC ports that are capable of performing analogue functions as well as digital functions will default to the analogue mode. In your case this means that if you want to use portA as a digital port you first have to turn off and analogue functions for that port. This includes things like disabling the AtoD converter, and any comparator functions.

Take a look at the section on portA in the data sheet and see which registers control port A configuration.
 

thatoneguy

Joined Feb 19, 2009
6,359
You need to disable comparators (CMPCON) and Analog features on Port A for the outputs.

Also, You set TRISA = 0x0, then in the main loop, you set it to 0xFF (All Input)
 
Top