problem with pic30f2010

Discussion in 'Embedded Systems and Microcontrollers' started by rychy, Nov 5, 2010.

  1. rychy

    Thread Starter New Member

    Nov 5, 2010
    1
    0
    iam trying to control the speed of a BLDC sensored motor and am using the pic30f2010, iam using mikroc software. The problem is, i keep on getting errors. i do not know what exactly iam doing wrong.
    note: iam very new to PIC and DSPIC in particular and this is my first encounter with these microcontrollers.
    Can anyone help me out please. i have less than a week to get this thing working so that i can implement fuzzy logic but first i need to get this one working.
    here is the sample of the code iam trying to build.

    Code ( (Unknown Language)):
    1.  
    2. #define __dsPIC30F2010__
    3. #include "c:\program files\microchip\MPLAB C30\support\h\p30F2010.h"
    4. #include "c:\program files\microchip\MPLAB C30\support\gld\p30F2010.gld"
    5. #define FCY 10000000// xtal = 5.0Mhz; PLLx8
    6. #define MILLISEC FCY/10000// 1 mSec delay constant
    7. #define FPWM 16000
    8. #define Kp 1200
    9. #define Ki 10
    10. #define RPMConstant  60*(FCY/256)
    11. #define S2 !PORTCbits.RC14
    12. void InitTMR3(void);
    13. void InitADC10(void);
    14. void AverageADC(void);
    15.  
    16. void InitMCPWM(void);
    17. void CalculateDC(void);
    18. void GetSpeed(void);
    19. struct {
    20. unsigned RunMotor : 1;
    21. unsigned Minus : 1;
    22. unsigned unused : 14;
    23. } Flags;
    24. unsigned int HallValue;
    25. int Speed;
    26. unsigned int Timer3;
    27. unsigned char Count;
    28. unsigned char SpeedCount;
    29. int DesiredSpeed;
    30. int ActualSpeed;
    31. int SpeedError;
    32. int DutyCycle;
    33. int SpeedIntegral;
    34.  
    35.  
    36. unsigned int StateLoTable[] = {0x0000, 0x1002, 0x0420, 0x0402,
    37. 0x0108, 0x1008, 0x0120, 0x0000};
    38.  
    39. void _ISR _CNInterrupt(void)
    40. {
    41. IFS0bits.CNIF = 0; // clear flag
    42. HallValue = PORTB & 0x0038; // mask RB3,4 & 5
    43. HallValue = HallValue >> 3; // shift right 3 times
    44. OVDCON = StateLoTable[HallValue];// Load the overide control register
    45. }
    46.  
    47. void _ISR _ADCInterrupt(void)
    48. {
    49. IFS0bits.ADIF = 0;
    50. DesiredSpeed = ADCBUF0;
    51. if (!Flags.RunMotor)
    52. {
    53. PDC1 = ADCBUF0; // get value ...
    54. PDC2 = PDC1; // and load all three PWMs ...
    55. PDC3 = PDC1; // duty cycles
    56. }
    57. }
    58.  
    59. int main(void)
    60. {
    61. LATE = 0x0000;
    62. TRISE = 0xFFC0; // PWMs are outputs
    63. CNEN1 = 0x00E0; // CN5,6 and 7 enabled
    64. CNPU1 = 0x00E0; // enable internal pullups
    65. IFS0bits.CNIF = 0; // clear CNIF
    66. IEC0bits.CNIE = 1; // enable CN interrupt
    67. SpeedError = 0;
    68. SpeedIntegral = 0;
    69. InitTMR3();
    70. InitMCPWM();
    71. InitADC10();
    72. while(1)
    73. {
    74. while (!S2); // wait for start key hit
    75. while (S2) // wait till key is released
    76. Delay_ms(10);
    77. // read hall position sensors on PORTB
    78. HallValue = PORTB & 0x0038; // mask RB3,4 & 5
    79. HallValue = HallValue >> 3; // shift right to get value 1, 2 ... 6
    80. OVDCON = StateLoTable[HallValue];// Load the overide control register
    81. PWMCON1 = 0x0777; // enable PWM outputs
    82. Flags.RunMotor = 1; // set flag
    83. T3CON = 0x8030; // start TMR3
    84. while (Flags.RunMotor) // while motor is running
    85. if (!S2) // if S2 is not pressed
    86. {
    87. if (HallValue == 1) //IF in sector 1
    88. {
    89. HallValue = 0xFF; // force a new value as a sector
    90. if (++Count == 5) // do this for 5 electrical revolutions or 1
    91. // mechanical revolution for a 10 pole motor
    92. {
    93. Timer3 = TMR3;// read latest tmr3 value
    94. TMR3 = 0;
    95. Count = 0;
    96. GetSpeed();// determine spped
    97. }
    98. }
    99. }
    100. else // else S2 is pressed to stop motor
    101. {
    102. PWMCON1 = 0x0700;// disable PWM outputs
    103. OVDCON = 0x0000; // overide PWM low.
    104. Flags.RunMotor = 0;// reset run flag
    105. while (S2)// wait for key release
    106. DelayNmSec(10);
    107. }
    108. } // end of while (1)
    109. }
    110.  
    111. {
    112. ADPCFG = 0xFFF8; // all PORTB = Digital;RB0 to RB2 = analog
    113. ADCON1 = 0x0064; // PWM starts conversion
    114. ADCON2 = 0x0000; // sample CH0 channel
    115. ADCHS = 0x0002; // Connect RB2/AN2 as CH0 = pot.
    116. ADCON3 = 0x0080; // Tad = internal RC (4uS)
    117. IFS0bits.ADIF = 0; // clear flag
    118. IEC0bits.ADIE = 1; // enable interrupt
    119. ADCON1bits.ADON = 1; // turn ADC ON
    120. }
    121.  
    122. void InitMCPWM(void)
    123. {
    124. PTPER = FCY/FPWM - 1;
    125. PWMCON1 = 0x0700; // disable PWMs
    126. OVDCON = 0x0000; // allow control using OVD
    127. PDC1 = 100; // init PWM 1, 2 and 3 to 100
    128. PDC2 = 100;
    129. PDC3 = 100;
    130. SEVTCMP = PTPER; // special trigger is 16 period values
    131. PWMCON2 = 0x0F00; // 16 postscale values
    132. PTCON = 0x8000; // start PWM
    133. }
    134.  
    135. void InitTMR3(void)
    136. {
    137. T3CON = 0x0030; // internal Tcy/256 clock
    138. TMR3 = 0;
    139. PR3 = 0x8000;
    140. }
    141.  
    142. void GetSpeed(void)
    143. {
    144. if (Timer3 > 23000) // if TMR3 is large ignore reading
    145. return;
    146. if (Timer3 > 0)
    147. Speed = RPMConstant/(long)Timer3;// get speed in RPM
    148. ActualSpeed += Speed;
    149. ActualSpeed = ActualSpeed >> 1;
    150. if (++SpeedCount == 1)
    151. {SpeedCount = 0;CalculateDC();}
    152. }
    153.  
    154. void CalculateDC(void)
    155. {
    156. DesiredSpeed = DesiredSpeed*3;
    157. Flags.Minus = 0;
    158. if (ActualSpeed > DesiredSpeed)
    159. SpeedError = ActualSpeed - DesiredSpeed;
    160. else
    161. {
    162. SpeedError = DesiredSpeed - ActualSpeed;
    163. Flags.Minus = 1;
    164. }
    165. SpeedIntegral += SpeedError;
    166. if (SpeedIntegral > 9000)
    167. SpeedIntegral = 0;
    168. DutyCycle = (((long)Kp*(long)SpeedError + (long)Ki*(long)SpeedIntegral) >> 12);
    169. DesiredSpeed = DesiredSpeed/3;
    170. if (Flags.Minus)
    171. DutyCycle = DesiredSpeed + DutyCycle;
    172. else DutyCycle = DesiredSpeed - DutyCycle;
    173. if (DutyCycle < 100)
    174. DutyCycle = 100;
    175. if (DutyCycle > 1250)
    176. {DutyCycle = 1250;SpeedIntegral = 0;}
    177. PDC1 = DutyCycle;
    178. PDC2 = PDC1;
    179. PDC3 = PDC1;
    180. }
    181.  
    182. void Delay_ms(unsigned int N)
    183. {
    184. unsigned int j;
    185. while(N--)
    186. for(j=0;j < MILLISEC;j++);
    187. }
    188.  
    189.  
    This is the error message it returns;

    iould not copy it directly from the compiler but this is what it reads

    ;expected, but '_attribute_' found
    specifier needed
    syntax error:')'expected, but ',' found
    [​IMG][​IMG][​IMG][​IMG][​IMG]syntax error:')'expected, but '_deprecated_' found
    [​IMG];expected, but ',' found
    specifier needed
    Invalid declarator expected'('or identifier
    ;expected, but ')' found
    specifier needed
    Invalid declarator expected'('or identifier
    "indentifier redefined
    Internal error "
    [​IMG]

    All these errors are only on one line 66 and the specified unit is p30f2010.h unit. If anyone knows what they really mean and how i can rectify them, please help me out.
    [​IMG]
     
    Last edited by a moderator: Nov 6, 2010
Loading...