fantech
Newbie level 3
- Joined
- May 23, 2013
- Messages
- 4
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,281
- 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:
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.
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.