Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

pic18f8722 - need some help with c code in mplab

Status
Not open for further replies.

sahr

Newbie level 2
Joined
Sep 17, 2016
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
22
Hi, i am starting a project useing pic18 , i'm at the beginnig.
I use in proximity sensor (GP2Y0A21YK0F).
I have this code:
Code:
#include <p18f8722.h>
#pragma config OSC=HS // high speed oscillator
#pragma config WDT=OFF // watch dog off
 
void main()
{ 

    TRISA=0xff; // port A input
    TRISD=0x00; // port D output
    PORTD=0x00; // port lights off
    PORTA=0x00;
    
    MEMCONbits.EBDIS=1; // make port d digital
    
    ADCON0=0b00001001; 
    // (00) unimplemented (0000) channel AN0 (0)don't start yet (1) turn on analog to digital module, see page 270
    
    ADCON1=0b00000000; 
    // (00) unimplemented (00) using AVDD and AVSS (0000) A/D port bits, see page 271
    
    ADCON2=0b1000000;
     // (0) left justified (0) unimplemented (000) 0 TAD (010) FOSC/32, see page 272
        
    while(1)
    {
        ADCON0bits.GO_DONE=1; // starts conversion
        while(ADCON0bits.GO_DONE==1); // wait, it's converting
        
        PORTD=ADRESH; // this is outise of the while loop    
    }
}
I need add a few lines in the code, when i use RA0 (TRIS RA) as outpot and RA5 as input (RA5=sensor)
Which means, when the sensor is activated (PORT RA5=1) then input RA0=0 (PORT RA0) else RA0=1.
For example:
Code:
void main ()
{ 
TRISAbits.TRISA0 = 0; // PORTA IS OUTPUT
TRISBbits.TRISB0 = 1; // PORTB IS INPUT 
while (1)                // INFINITY WHILE
{ 
if (PORTBbits.RB0==1)  
{
PORTAbits.RA0=0;
}
else
{
PORTAbits.RA0=1;
}
}
}
Someone can help me?
Thx.
 

Not sure if this will fix it but try:

1. change all output statements from PORTXbits.xxx to LATXbits.xxx. On PIC18 always write to LAT and read from PORT.
2. if the second code is being tested in hardware, make sure RB0 has a pull-down resistor or is actively driven.

Brian.
 

What about 'LATAbits.LATA0 = ~PORTBbits.RB0;' instead of the 'if' statement.
Are you actually have a problem with the code you have written?
Susan
 

Not sure if this will fix it but try:
1. change all output statements from PORTXbits.xxx to LATXbits.xxx. On PIC18 always write to LAT and read from PORT.
2. if the second code is being tested in hardware, make sure RB0 has a pull-down resistor or is actively driven.
Brian.
What about 'LATAbits.LATA0 = ~PORTBbits.RB0;' instead of the 'if' statement.
Are you actually have a problem with the code you have written?
Susan

I do this code:
Code:
#include <p18f8722.h>
#include <delays.h>

#pragma config OSC = HSPLL
#pragma config WDT = OFF
#pragma config PWRT = ON
#pragma config LVP = OFF
 
void main()
{ 

    TRISA=0xff; // port A input
    TRISD=0x00; // port D output
    PORTD=0x00; // port lights off
    PORTA=0x00;
    
    MEMCONbits.EBDIS=1; // make port d digital
    
    ADCON0=0b00001001; 
    // (00) unimplemented (0000) channel AN0 (0)don't start yet (1) turn on analog to digital module, see page 270
    
    ADCON1=0b00000000; 
    // (00) unimplemented (00) using AVDD and AVSS (0000) A/D port bits, see page 271
    
    ADCON2=0b1000000;
     // (0) left justified (0) unimplemented (000) 0 TAD (010) FOSC/32, see page 272
        
    while(1)
    {
        ADCON0bits.GO_DONE=1; // starts conversion
        while(ADCON0bits.GO_DONE==1); // wait, it's converting
        
        PORTD=ADRESH; // this is outise of the while loop
    }

[B]TRISAbits.TRISA0 = 0; // PORTA IS OUTPUT
TRISAbits.TRISA5 = 1; // PORTB IS INPUT 
while (1)                // INFINITY WHILE
{ 
if (LATAbits.RA5==1)  
{
LATAbits.LATA0=0;
}
else
{
LATAbits.LATA0=1;
}
}
}[/B]
There no problem with the code - it works.
I check if it work with the PIC -to check if it work i put LED light to RA0 and the proximity sensor to RA5, and the LED always work.
when i play with the potentiometer the led turn off /on, and its not good for me.
I need that the led will work just when the sensor will be active.
What i dont do right??
 
Last edited by a moderator:

WRITE to the LAT registers.
READ from the PORT registers.

Code:
TRISAbits.TRISA0 = 0; // PORTA IS OUTPUT
TRISAbits.TRISA5 = 1; // PORTB IS INPUT
while (1) // INFINITY WHILE
{
[COLOR="#FF0000"]if (LATAbits.RA5==1)[/COLOR] // should be "if(PORTAbits.RA5 == 1)"
{
LATAbits.LATA0=0;
}
else
{
LATAbits.LATA0=1;
}
}
}

you are currently reading back what you last wrote to the LAT register (if anything), not the present state of the pin itself.

Brian.
 

@sahr - the last part of your code (in bold, and that betwixt has commented on) will not get executed as it is outside the main 'while' loop. I think you need to check your overall logic here.
You have started to talk about a potentiometer but I have no idea how it is connected. Can you please explain this part to us.
The proximity sensor you have mentioned outputs a voltage - where is that connected?
I decoded the ADC settings and I'm not convinced that you have them correct.
You have set ADCON0 to 0x09 which makes the CHS bits 0b0010 and that will select AN2 as the input channel. Your comments say that you are selecting AN0
Setting ADCON1 to 0x00 will make the PCFG bits 0b0000 which puts ALL pins with analog capability into analog mode. However RA5(which you seem to want to use as a digital input) also has AN4 on it. Therefore you will always read digital '0' on that port pin (see the 'Note' in section 11.1 of the data sheet). If you are using AN2 as the input then ADCON1 must have the PCFG bits set to 0b1100 at least; if you really did mean to use AN0 as the input then PCFG should be 0b1110 at least.
You have set ADCON2 to 0x80 which makes the result right justified (but see later), the acquisition time as 0 Tad and the conversion clock to be Fosc/2 - this is at odds with your comments.
With a right justified result in the ADRESH/ADRESL registers, the 10 bit result of the conversion will have 8 bits in the ADRESL register (which is on the right) and only the top 2 bits in the ADRESH register which you are actually reading. Do you really mean this?
Susan
 

I changed the quote tags to code tags so the original indentation would show but it isn't correct anyway.

Sahr, when you post code, please use the "#" tags to encapsulate it and preserve your original screen formatting. You used the quote tags which strip leading spaces so all lines start at the left margin.

I agree with Susan, your code doesn't 'flow' properly. I assumed the bold print part for a second program for debugging purposes but if you untended it to be part of the main program, the while(1) loops need to be expanded to hold the remaining code. Because your 'while(1)' loops never exit, there is no way the rest of the code can be reached at the moment.

Brian.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top