dspic30f2010 problem

Discussion in 'Embedded Systems and Microcontrollers' started by compuKidd, Feb 9, 2009.

  1. compuKidd

    Thread Starter New Member

    Feb 5, 2009
    2
    0
    hi guys I'm programming a dspic30f2010 to do "clock following" I have an external clock inputted at pin RE8 which is the INT0 (interrupt 0) bit I enable interrupt zero and set RE8 to input write a ISR for it that reads then resetes 32bit timer2/3 and estimate the period of the input clock interms of conter tics I then enable timer1 to count to a prespecified delay and in timer1 interrupt service routine I raise RB0 and set the timer to a period and then lower RB1 and disables timer1 and so on now this procedure occures every 2nd or more rising edge of the clock at INT0 (RE8).

    the problem is that the output is zero and stays zero and it seems that the ICObits.T1IE is not being set in the INT0 ISR while I'm clearly setting it why???

    is it because I have 3 interrupts working one for INT0 (priority 7) one for timer2/3 (priority 6) one for timer1 (priority 5)??? or not and if not why????

    please help, any help will be appriciated
    thx

    Code ( (Unknown Language)):
    1.  
    2. #include <stdio.h>
    3. #include <p30f2010.h>
    4. #include <math.h>
    5.  
    6. double maxDutyCycle=0.5;
    7. double dutyCycle=0.5;
    8. long int period=10000;
    9. short int delay=100,minDelay=30;
    10.  
    11. int Next,High;
    12. unsigned short mode=2;
    13. unsigned short count=2;
    14.  
    15. inline void T1InterruptHelp()
    16. {
    17.     PORTBbits.RB0=Next;
    18.     Next=!Next;
    19.     IFS0bits.T1IF = 0; // clear interrupt flag
    20.  
    21.     if(Next==0) PR1=High;
    22.     else IEC0bits.T1IE = 0; // disable interrupt
    23. }
    24.  
    25. #define _InterruptServiceRoutine __attribute__((interrupt,shadow))
    26. void _InterruptServiceRoutine _T1Interrupt(void)
    27. {
    28.     T1InterruptHelp();
    29. }
    30.  
    31. void _InterruptServiceRoutine _T3Interrupt(void)
    32. {
    33.     IFS0bits.T3IF=0;    // clear interrupt flag
    34. }
    35.  
    36. double T=-1.0;
    37. double LR=0.1;
    38.  
    39. inline long int myRound(double d)
    40. {
    41.     if(d-floor(d)>=0.5) return (long int)ceil(d);
    42.     return (long int)floor(d);
    43. }
    44.  
    45. void setLH();
    46.  
    47. void _InterruptServiceRoutine _INT0Interrupt(void)
    48. {
    49.     unsigned short LSB=TMR2;
    50.     unsigned short MSB=TMR3HLD;
    51.  
    52.     unsigned long Val=MSB;
    53.     Val=(Val<<16);
    54.     Val=Val|LSB;
    55.  
    56.     TMR3HLD=0;          // clear timer3 register
    57.     TMR2=0;             // clear timer2 register
    58.  
    59.     if(count==mode)
    60.     {
    61.         if(Next!=0)
    62.         {
    63.             // ERROR!!!!!
    64.         }
    65.         if(delay>minDelay)
    66.         {
    67.             // ???? //IFS0bits.T1IF = 0;    // clear interrupt flag
    68.             TMR1=0;             // clear timer1 register
    69.             PR1=delay;          // set timer
    70.         }
    71.         else
    72.         {
    73.             T1InterruptHelp(); 
    74.         }
    75.         IEC0bits.T1IE = 1;  // enable interrupts
    76.         count=(count%mode)+1;
    77.     }
    78.     else count=(count%mode)+1;
    79.    
    80.     IFS0bits.T3IF=0;    // clear interrupt flag
    81.     if(T>0)
    82.     {
    83.         if(T/Val<2.0 && T/Val>0.5)
    84.         {
    85.             T=T*(1.0-LR)+Val*LR;
    86.         }// else is an outlier
    87.     }
    88.     else T=Val;
    89.     period=myRound(T);
    90.     setLH();
    91. }
    92.  
    93. void setLH()
    94. {
    95.     maxDutyCycle=1.0/mode;
    96.     High=floor(period*dutyCycle);
    97.     if(dutyCycle>maxDutyCycle) dutyCycle=maxDutyCycle;
    98. }
    99.  
    100. int tmp=9;
    101. int main (void)
    102. {
    103.     int inc=1;
    104.  
    105.     unsigned char RB1=PORTBbits.RB1;
    106.     unsigned char RB2=PORTBbits.RB2;
    107.     unsigned char RB3=PORTBbits.RB3;
    108.     unsigned char RB4=PORTBbits.RB4;
    109.     unsigned char RB5=PORTBbits.RB5;
    110.  
    111.     // timer 1 settings
    112.     TMR1=0;             // clear timer1 register
    113.     PR1=0XFF;           // set timer
    114.     T1CONbits.TCS = 0;  // set internal clock source
    115.     IPC0bits.T1IP = 6;  // set priority level
    116.     IFS0bits.T1IF = 0;  // clear interrupt flag
    117.     IEC0bits.T1IE = 0;  // no interrupts yet
    118.     T1CONbits.TON = 1;  // start the timer
    119.    
    120.     // timer 2/3 settings
    121.     TMR2=0;             // clear timer2 register
    122.     TMR3=0;             // clear timer3 register
    123.     T2CONbits.T32=1;    // set to timer2/3 32 bit timer
    124.     PR2=0xFFFF;         // set timer
    125.     PR3=0xFFFF;         // set timer
    126.     IPC1bits.T2IP = 5;  // set priority level
    127.     IPC1bits.T3IP = 5;  // set priority level
    128.     IFS0bits.T3IF=0;    // clear interrupt flag
    129.     IEC0bits.T3IE=1;    // enable interrupt
    130.     T2CONbits.TCS = 0;  // set internal clock source   
    131.     T2CONbits.TON = 1;  // start the timer
    132.  
    133.     // external interrupt 0 settings
    134.     TRISEbits.TRISE8 = 1;// set port to input
    135.     IFS0bits.INT0IF=0;  // clear interrupt flag
    136.     IPC0bits.INT0IP=7;  // highest priority
    137.     IEC0bits.INT0IE=1;  // enable external interrupt 0
    138.  
    139.     setLH();
    140.  
    141.     TRISBbits.TRISB0 = 0;   // set pin 0 on port b to output
    142.     PORTBbits.RB0=0;        // initialise pin 0 of port b to 0 (this pin will be used to output the clock)
    143.     Next=1;
    144.  
    145.     // pins 1-5 of port b are set to inputs each with special job
    146.     TRISBbits.TRISB1 = 1;   // increment delay
    147.     TRISBbits.TRISB2 = 1;   // decrement delay
    148.     TRISBbits.TRISB3 = 1;   // increment duty cycle by 0.0078125 (since this number is a decimal power of 2 i.e. can be represented exactly with no round off error)
    149.     TRISBbits.TRISB4 = 1;   // decrement duty cycle by 0.0078125
    150.     TRISBbits.TRISB5 = 1;   // increment mode modulo 5
    151.  
    152.     TRISEbits.TRISE0 = 1;   // LSB for increment
    153.     TRISEbits.TRISE1 = 1;   // MSB for increment
    154.  
    155.     while(1==1)
    156.     {
    157.         tmp=0;
    158.         setLH();
    159.         for(;tmp<100;tmp++);    // small delay
    160.  
    161.         if(PORTEbits.RE1==0 && PORTEbits.RE0==0) inc=1;
    162.         else if(PORTEbits.RE1==0 && PORTEbits.RE0==1) inc=20;
    163.         else if(PORTEbits.RE1==1 && PORTEbits.RE0==0) inc=400;
    164.         else if(PORTEbits.RE1==1 && PORTEbits.RE0==1) inc=8000;
    165.  
    166.         //increment delay by inc at rising edge of portb pin 1
    167.         if(RB1==0 && PORTBbits.RB1==1)
    168.         {
    169.             RB1=1;
    170.             delay=delay+inc;
    171.             if(delay>period) delay=period;
    172.             setLH();
    173.         }
    174.         if(RB1==1 && PORTBbits.RB1==0)
    175.         {
    176.             RB1=0;
    177.         }
    178.  
    179.         //decrement delay by inc at rising edge of portb pin 2
    180.         if(RB2==0 && PORTBbits.RB2==1)
    181.         {
    182.             RB2=1;
    183.             delay=delay-inc;
    184.             if(delay<0) delay=0;
    185.             setLH();
    186.         }
    187.         if(RB2==1 && PORTBbits.RB2==0)
    188.         {
    189.             RB2=0;
    190.         }
    191.  
    192.         //increment dutycycle by 0.0078125 at rising edge of portb pin 3
    193.         if(RB3==0 && PORTBbits.RB3==1)
    194.         {
    195.             RB3=1;
    196.             dutyCycle=dutyCycle+0.00390625;
    197.             if(dutyCycle>maxDutyCycle) dutyCycle=maxDutyCycle; // max dutyCycle=0.9375 (again no round off error)
    198.             setLH();
    199.         }
    200.         if(RB3==1 && PORTBbits.RB3==0)
    201.         {
    202.             RB3=0;
    203.         }
    204.  
    205.         tmp=1;
    206.  
    207.         //decrement dutycycle by 0.0078125 at rising edge of portb pin 4
    208.         if(RB4==0 && PORTBbits.RB4==1)
    209.         {
    210.             RB4=1;
    211.             dutyCycle=dutyCycle-0.00390625;
    212.             if(dutyCycle<0.00390625) dutyCycle=0.00390625;  // min dutyCycle=0.00390625 (you guess)
    213.             setLH();
    214.         }
    215.         if(RB4==1 && PORTBbits.RB4==0)
    216.         {
    217.             RB4=0;
    218.         }
    219.  
    220.         //increment mode at rising edge of portb pin 5 modulo 5
    221.         if(RB5==0 && PORTBbits.RB5==1)
    222.         {
    223.             RB5=1;
    224.             mode=mode+1;
    225.             if(mode>5) mode=2;      // min dutyCycle=0.00390625 (you guess)
    226.             setLH();
    227.         }
    228.         if(RB5==1 && PORTBbits.RB5==0)
    229.         {
    230.             RB5=0;
    231.         }
    232.     }
    233.  
    234.     while(1)
    235.     {
    236.         tmp=0;
    237.     }
    238.     return 0;
    239. }
    240.  
     
  2. compuKidd

    Thread Starter New Member

    Feb 5, 2009
    2
    0
    Hi guys I'll make it a lot easier, check this one out:

    Code ( (Unknown Language)):
    1.  
    2. #include <stdio.h>
    3. #include <p30f2010.h>
    4. #include <math.h>
    5.  
    6. #define _InterruptServiceRoutine __attribute__((interrupt,shadow))
    7.  
    8. void _InterruptServiceRoutine _INT0Interrupt(void)
    9. {
    10.     PORTBbits.RB0=!PORTBbits.RB0;
    11. }
    12.  
    13.  
    14. int main (void)
    15. {
    16.     // external interrupt 0 settings
    17.     TRISEbits.TRISE8 = 1;// set port to input
    18.     IFS0bits.INT0IF=0;  // clear interrupt flag
    19.     IPC0bits.INT0IP=6;  // highest priority
    20.     IEC0bits.INT0IE=1;  // enable external interrupt 0
    21.  
    22.     TRISBbits.TRISB0 = 0;   // set pin 0 on port b to output
    23.     PORTBbits.RB0=0;        // initialise pin 0 of port b to 0 (this pin will be used to output the clock)
    24.  
    25.     while(1);
    26.  
    27.     return 0;
    28. }
    29.  
    30.  
    i'm using the C30 compiler
    now here I'm just flipping RB0 on every rising edge of RE8 (INT0/external interrupt 0) so you'd expect the output at RB0 to be a square with half the frequency, but what I'm getting is a square wave independent of RE0
    my input is a ~70Hz signal and the output at RB0 is a square signal with 16.67KHz (notice the kilo) that is seemless of the input at RE0, why???

    can any one please help me I'm down to my last hair (pulled all out) :)

    any help will be apriciated
    thanks
     
Loading...