• AVR Freaks

PIC16F1934 Timer0, ADC .. etc etc

Author
GeorgeAD
New Member
  • Total Posts : 2
  • Reward points : 0
  • Joined: 2019/11/13 10:59:04
  • Location: 0
  • Status: offline
2019/11/13 12:13:58 (permalink)
0

PIC16F1934 Timer0, ADC .. etc etc

Hey folks,
 
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:
 
#include <xc.h>
#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;
void timer_isr()
{
if(TMR0IF==1) // Timer flag has been triggered due to timer overflow
{
TMR0 = 100; //Load the timer Value
TMR0IF=0; // Clear timer interrupt flag
count++;
}

if (count == 250)
{
PORTAbits.RA0 = 1;
count=0;

}
}

void main()
{

/** 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
PEIE=1;

/** PORTA configured as output **/
TRISA = 0;
PORTA = 1;


//*** Main loop
while(1)
{
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:


void ADC_Initialization()
{

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!
George
 
#1

1 Reply Related Threads

    ric
    Super Member
    • Total Posts : 24605
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: PIC16F1934 Timer0, ADC .. etc etc 2019/11/14 17:53:43 (permalink)
    0
    GeorgeAD
     
    Is the above one correct ?

    You have not declared the ISR correctly, which must be why you are calling it from your main loop instead.
    The correct syntax depends upon which version of XC8 you are using (1.xx or 2.xx),
    and if it is 2.xx, which mode it is in ("C90" or "C99")
    Also, never set the GIE bit until AFTER you have finished initialising everything.
    You are setting it before initialising TRISA.
     
    You have comments about your config settings, but the settings are not actually there.
    You should have a series of "#pragma config" lines actually doing the settings.
     

    This line PORTA=1; -> means setting whole 8 registers of this port as output, couldn't I just say PORTAbits.RA0 = 1; ?

    Never set pin values via PORTAbits. Use LATAbits instead. So:
    LATAbits.LAT0 =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 ?

    Probably because your setting the pin via the wrong register.
     

    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 ?

    Just follow the procedure described in the datasheet.
    • i.e. select the appropriate input channel, wait the required "acquisition time" (if your PIC doesn't have a facility to do that for you), probably 5-20 us.
    • Start the conversion (by setting the GO/DONE bit)
    • Wait for the conversion to complete (GO/DONE bit will return to zero)
    • Read the result from the ADRES register.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #2
    Jump to:
    © 2019 APG vNext Commercial Version 4.5