PIC16F1934 Timer0, ADC .. etc etc
Enriching my knowledge (had some previous experience with TI MSP430), decided to get it started with a PIC controller.
First things first, I tried flashing LED with Timer0 interrupts. I do have the hardware scheme in front of me and the LED does blink but I'd rather ask if I am setting the whole thing correctly:
#define _XTAL_FREQ 20000000 //Specify the XTAL crystall FREQ
/***** CONFIGURATION *****/
// ext reset, internal oscillator (no clock out), no watchdog timer
// brownout resets enabled, low brownout voltage, no low-power brownout reset
// no power-up timer, no code protect, no write protection
// stack resets on, high-voltage programming
int count = 0;
if(TMR0IF==1) // Timer flag has been triggered due to timer overflow
TMR0 = 100; //Load the timer Value
TMR0IF=0; // Clear timer interrupt flag
if (count == 250)
PORTAbits.RA0 = 1;
/** TIMER0 setup **/
OPTION_REG = 1;
OPTION_REGbits.PS2 = 0;
OPTION_REGbits.PSA = 0;
OPTION_REGbits.TMR0CS = 0;
INTCONbits.TMR0IE = 1; //Enable timer interrupt bit in PIE1 register
GIE=1; //Enable Global Interrupt
/** PORTA configured as output **/
TRISA = 0;
PORTA = 1;
//*** Main loop
timer_isr(); // LED on during interrupt
PORTAbits.RA0 = 0; // LED off
Is the above one correct ? This line PORTA=1; -> means setting whole 8 registers of this port as output, couldn't I just say PORTAbits.RA0 = 1; ? (if I do it the LED doesnt flash)
To continue further, I came across a IR Sharp 2Y0A21 sensor and here I will need your help.
I know it returns volts for certain value of cm (range is 10 - 80 ) as far as I remember. I came up with the following, I'll wire up the IR and depends on how far/close and object is, the LED will be on or off. I need an ADC, setting one however is something that eludes me, this is how to set it up am I thinking for the following:
ADCON0bits.ADON = 1;
ADCON1bits.ADFM = 1;
ADCON1bits.ADCS2 = 1;
I will be wiring up the IR sensor to PORTAbits.RA1, and here something funny happens, whenever I stick up the GND and Vcc of the IR sensor wires (Gnd on the PIC's ground pin, Vcc on the PIC's Vcc pin and IR's control wire on the PORTAbits.RA1), the LED on PORTAbits.RA0 starts shining way stronger than before, and I can still see ticks on the LED - any idea why ?
Further more how can I read from the ADC what values did the sensor returns so I can compare them to certain numbers to fire up the LED ?
Thanks in advance!