At a loss with dsPic30f4013 Timer...

Discussion in 'Embedded Systems and Microcontrollers' started by odm4286, Aug 23, 2015.

  1. odm4286

    Thread Starter Active Member

    Sep 20, 2009
    155
    5
    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!

    Code (C):
    1.  
    2. /*
    3.  * File:   main.c
    4.  * Author: user
    5.  *
    6.  * Created on August 22, 2015, 9:41 PM
    7.  */
    8.  
    9. #include <stdio.h>
    10. #include <stdlib.h>
    11. #include <p30fxxxx.h>
    12.  
    13. /*
    14.  *
    15.  */
    16. int main(int argc, char** argv)
    17. {
    18.     /* Playing around with the internal OSC a little...
    19.      * Attempting to set my click at 7.37Mhz which would make
    20.      * My timer increment 1.8425 Million times per second (FOSC/4)
    21.      * Using the 1:256 prescaler this becomes 7197.265625 times per second
    22.      */
    23.     OSCCONbits.COSC = 0b001;                            // clock set at 7.37Mhz
    24.                  
    25.     // Setup timer2
    26.     T2CONbits.TGATE = 1;                                // allows FOSC/4 To increment timer, 1.85425M times a second
    27.     T2CONbits.TCS = 0;                                  // timer gate config
    28.    
    29.  
    30.     TRISBbits.TRISB2 = 1;                               // using RB2 as input
    31.     TRISBbits.TRISB0 = 0;                               // using RB0 as output
    32.     ADPCFG = 0xFFFB;                                    // setting RB2 as Analog in, port read input disabled
    33.     ADCON2bits.VCFG = 0;                                // using VSS and VDD
    34.     ADCON1bits.ASAM = 1;                                // automatic sampling
    35.     ADCON1bits.SSRC = 0b111;                            // auto conversion
    36.     ADCON3bits.ADRC = 1;                                // using RC clock
    37.     ADCON1bits.ADON = 1;                                // lets turn it on....
    38.     ADCHS = 0b10;                                       // selects which channel is connected to input mux, in this case chan 2 to mux a
    39.    
    40.    
    41.     while(1)
    42.     {
    43.        
    44.         while(!ADCON1bits.DONE);                        // wait until sample is done
    45.         if(ADCBUF0 > 2000)
    46.         {  
    47.             PORTBbits.RB0 = 1;
    48.             TMR2 = 0;                                           // start counting from 0
    49.             T2CONbits.TON = 1;                                  // starting timer
    50.             T2CONbits.TCKPS = 0b11;                             // 1:256 prescaler
    51.             while(TMR2 < 35987);                                // wait about 5 sec....
    52.             PORTBbits.RB0 = 0;                                  // turn off LED
    53.             T2CONbits.TON = 0;
    54.         }
    55.         else
    56.             PORTBbits.RB0 = 0;
    57.     }
    58.     return (EXIT_SUCCESS);
    59. }
    60.  
     
  2. odm4286

    Thread Starter Active Member

    Sep 20, 2009
    155
    5
    blah...looks like I found my error

    Code (C):
    1.  
    2. if(ADCBUF0 > 2000)
    3.         {
    4.             PORTBbits.RB0 = 1;
    5.             TMR2 = 0;                                           // start counting from 0
    6.             T2CONbits.TON = 1;                                  // starting timer
    7.             T2CONbits.TCKPS = 0b11;                             // 1:256 prescaler
    8.             while(TMR2 < 35987);                                // wait about 5 sec....
    9.             PORTBbits.RB0 = 0;                                  // turn off LED
    10.             T2CONbits.TON = 0;
    11.         }
    12.  
    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.

    Code (C):
    1.  
    2. if(ADCBUF0 > 2000)
    3.         {  
    4.             PORTBbits.RB0 = 1;
    5.             TMR2 = 0;                                           // start counting from 0
    6.             T2CONbits.TON = 1;                                  // starting timer
    7.             T2CONbits.TCKPS = 0b11;                             // 1:256 prescaler
    8.             while(TMR2 < 35987);                                // wait about 5 sec....
    9.             PORTBbits.RB0 = 0;                                  // turn off LED
    10.             TMR2 = 0;                                               // reset timer
    11.             while(TMR2 < 7197);                                // turn off for about a second...
    12.         }
    13.  
     
Loading...