Help needed with PIC18F4520

Status
Not open for further replies.

fantech

Newbie level 3
Joined
May 23, 2013
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,310
Hi!
I'm new here and this is my first post, and I'm asking for some help.
I'm not really experienced with microcontrollers as it's not really my field of work. However, I need to use it in one of my projects, so I've had to learn bits of it.

So, my basic goal is that : I have 2 voltages, that vary from 3.5 V to 0.3 V each. I need to light up a LED whenever one of them falls below a certain voltage (about 1.2V). I'm using the PIC18F4520. I also understand that a whole micro controller is not required for such a trivial operation, however I've been told to use it by my professor, and so I have to.

Here's what I came up with:
Code:
#include<htc.h>
#include<pic18f4520.h>

void main()
{
           float vol;
           TRISA = 0xFF; //input
           TRISB = 0; //output
           PORTB = 0;
           ADCON1 = 0b00001101; //AN0 and AN1 are analog rest digital
           ADCON0 = 0b00000000;
           ADCON2 = 0b10001000; //Right Justified, 2TAD, FOSC/2
           ADCON0bits.ADON = 1; //module on
           while(1)
           {
                      ADCON0bits.GO = 1; //conversion start
                      while(ADCON0bits.GO != 0); //loop till end of conversion
                      vol = ((ADRES*256) + ADRESL) * 0.00489; //conversion to volts
                      if(vol < 1.2)
                              PORTBbits.RB0 = 1;    //i dont want the leds to switch off after this
                      
                      ADCON0 = 0b00000100; // AN1 channel
                      ADCON0bits.ADON = 1; //module on
                      ADCON0bits.GO = 1; // conversion start
                      while(ADCON0bits.GO != 0);
                      vol = ((ADRES*256) + ADRESL) * 0.00489;
                      if(vol < 1.2)
                              PORTBbits.RB1 = 1;
          }
}

I am using 2 470pF capacitors with a 12MHz crystal oscillator in the oscillator pins. Also, if someone could help me choose the configuration bits, that'd be great.

Thanks a lot, any help would be great.
 

Hi,

Cannot help you directly with the C but this should get you going.

The config, think C uses #pragma, try these options.
CONFIG OSC=INTIO67, PWRT=ON, BOREN=OFF, WDT=OFF, MCLRE=OFF, LPT1OSC=OFF, PBADEN=OFF
CONFIG LVP=OFF, XINST=OFF, DEBUG=OFF

Not that the OSC setting shown if for a 1mhz internal oscillator, you can change that frequency by using the OSCON register - see the datasheet.

For a 12mhz crystal then OSC=HS should be used.

The capacitors for the crystal sould be around 15 or 22 pf, doubt it will work properly with the 470pf you are using
 

Thanks wp100, will change the capacitors.
As for the configuration bits, do I have to write them in the code itself, of is it ok if I write them during the burning of the uC using the burning software (from 300000H - 30000DH IIRC)?
 

Hi,

You can enter the config parameters either way, however I always think its best to put them in your program code so you can easily see their settings.
 

So, if I program them into my code, I should leave them to their default values (FF) in the burner software?

Sorry, but I'm a complete novice.
 

I'm not sure actually. I'm not getting the correct (any) output. How do I debug/ simulate the code on the computer?
 

Include the configuration bits in your code, and in your PIC programmer set them as well to the same values you set in your code. Once you do that, you should be good to go.
Also, try to make a simple test program to see if the PIC is working (a LED test is usually better suited for the purpose).
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…