At a loss with dsPic30f4013 Timer...

Thread Starter

odm4286

Joined Sep 20, 2009
265
Ok, I know probably a bad chip to start off with but hey my boss let me borrow the dsPICDEM2 board to practice :)

So here's my problem, I'm taking baby steps to learn. First I started with making RB2 high, then making RB2 high or low depending on a A/D conversion(that a/d is tricky the first time),and now id like to have RB2 stay on for 5 seconds then turn off. However, the LED just stays on once my analog conversion goes over 2000. I appreciate all the help in advance, I'm using the x16 compiler from microchip and I'd really like to stick with it as I feel it allows for JUST enough abstraction to learn the low level stuff without being too tedious. My guess is I'm screwing up with the prescaler somehow...anyway time to walk away for a bit.

Thanks again!

C:
/* 
 * File:   main.c
 * Author: user
 *
 * Created on August 22, 2015, 9:41 PM
 */

#include <stdio.h>
#include <stdlib.h>
#include <p30fxxxx.h>

/*
 * 
 */
int main(int argc, char** argv) 
{
    /* Playing around with the internal OSC a little... 
     * Attempting to set my click at 7.37Mhz which would make 
     * My timer increment 1.8425 Million times per second (FOSC/4)
     * Using the 1:256 prescaler this becomes 7197.265625 times per second
     */
    OSCCONbits.COSC = 0b001;                            // clock set at 7.37Mhz
                 
    // Setup timer2 
    T2CONbits.TGATE = 1;                                // allows FOSC/4 To increment timer, 1.85425M times a second
    T2CONbits.TCS = 0;                                  // timer gate config
   

    TRISBbits.TRISB2 = 1;                               // using RB2 as input
    TRISBbits.TRISB0 = 0;                               // using RB0 as output
    ADPCFG = 0xFFFB;                                    // setting RB2 as Analog in, port read input disabled
    ADCON2bits.VCFG = 0;                                // using VSS and VDD
    ADCON1bits.ASAM = 1;                                // automatic sampling
    ADCON1bits.SSRC = 0b111;                            // auto conversion
    ADCON3bits.ADRC = 1;                                // using RC clock
    ADCON1bits.ADON = 1;                                // lets turn it on....
    ADCHS = 0b10;                                       // selects which channel is connected to input mux, in this case chan 2 to mux a
   
   
    while(1)
    {
       
        while(!ADCON1bits.DONE);                        // wait until sample is done
        if(ADCBUF0 > 2000)
        {  
            PORTBbits.RB0 = 1;
            TMR2 = 0;                                           // start counting from 0
            T2CONbits.TON = 1;                                  // starting timer
            T2CONbits.TCKPS = 0b11;                             // 1:256 prescaler
            while(TMR2 < 35987);                                // wait about 5 sec....
            PORTBbits.RB0 = 0;                                  // turn off LED
            T2CONbits.TON = 0;
        }
        else 
            PORTBbits.RB0 = 0;
    }
    return (EXIT_SUCCESS);
}
 

Thread Starter

odm4286

Joined Sep 20, 2009
265
blah...looks like I found my error

C:
if(ADCBUF0 > 2000)
        { 
            PORTBbits.RB0 = 1;
            TMR2 = 0;                                           // start counting from 0
            T2CONbits.TON = 1;                                  // starting timer
            T2CONbits.TCKPS = 0b11;                             // 1:256 prescaler
            while(TMR2 < 35987);                                // wait about 5 sec....
            PORTBbits.RB0 = 0;                                  // turn off LED
            T2CONbits.TON = 0;
        }
I completely missed that the code was doing EXACTLY what it was supposed to do, keep the LED on for 5 seconds...turn it off...then go RIGHT BACK ON lol. Here is the updated if statement.

C:
if(ADCBUF0 > 2000)
        {  
            PORTBbits.RB0 = 1;
            TMR2 = 0;                                           // start counting from 0
            T2CONbits.TON = 1;                                  // starting timer
            T2CONbits.TCKPS = 0b11;                             // 1:256 prescaler
            while(TMR2 < 35987);                                // wait about 5 sec....
            PORTBbits.RB0 = 0;                                  // turn off LED
            TMR2 = 0;                                               // reset timer
            while(TMR2 < 7197);                                // turn off for about a second...
        }
 
Top