fork download
  1. //-----------------------------------------------------------------------------
  2. // Servo control with dsPIC 30F4012
  3. // L. BAGHLI 30/10/2015
  4. //-----------------------------------------------------------------------------
  5. #include "p30F4012.h"
  6.  
  7. //Configuration bits
  8. /// pas de Quartz !
  9. // _FOSC(CSW_FSCM_OFF & FRC_PLL8); // 7.37Mhz *8 = 58.96 /4 = 14.74 MIPS maxi pour ce pic
  10. _FOSC(CSW_FSCM_OFF & FRC); // 7.37Mhz /4 = 1.8425 MIPS pour ce pic
  11. _FWDT(WDT_OFF);
  12. _FBORPOR(PBOR_OFF & RST_PWMPIN & PWMxH_ACT_HI & PWMxL_ACT_LO & PWRT_64 & MCLR_EN);
  13. //_FGS(CODE_PROT_OFF);
  14. //-----------------------------------------------------------------------------
  15. //Program Specific Constants
  16. //#define FCY 14740000 //Instruction cycle rate (Osc x PLL / 4) = 14.74 MIPS
  17. #define FCY 1842500 //Instruction cycle rate (Osc / 4 ) = 1.8425 MIPS
  18. #define MILLISEC FCY/1842 // 1 mSec delay constant
  19.  
  20. // Pour FRC_PLL8 FCY 14740000
  21. ////#define FullDuty 18425 // 14.74 MHz*20 ms =294800 / 16 (prescaler 1:16) = 18425
  22. //#define FullDuty 16583 // 18 ms
  23. //#define Duty100 3869 // 2.1 ms --> 1934.625 x2
  24. //#define Duty50 2764 // 1.5 ms --> 1381.875 x2
  25. //#define Duty0 1658 // 0.9 ms --> 829.125 x2
  26. //#define coefadc 2.1591796875 // rapport (Duty100-Duty0) / 1024
  27.  
  28. // Pour FRC FCY 1842500
  29. #define FullDuty 9212 // 1842500 Hz*20 ms =294800 / 4 (prescaler 1:4) = 9212.5
  30. //#define FullDuty 16583 // 18 ms
  31. #define Duty100 1935 // 2.1 ms --> 967.3125 x2 = 1934.625 resolution PWM double
  32. #define Duty50 1382 // 1.5 ms --> x2
  33. #define Duty0 829.125 // 0.9 ms --> x2 829.125
  34. #define coefadc 1.07958984375 // rapport (Duty100-Duty0) / 1024
  35. // AN0 entrée Potar
  36. // PWM1H sortie servo
  37.  
  38. void setup_ports(void);
  39. void InitADC10();
  40. void InitMCPWM();
  41. void DelayNmSec(unsigned int N);
  42. void Delay(unsigned int N);
  43.  
  44. int ValPot;
  45.  
  46. //-----------------------------------------------------------------------------
  47. // Setup
  48. //-----------------------------------------------------------------------------
  49. void setup_ports(void)
  50. {
  51. ADPCFG=0xFFFE; // AN0 entrée Potar, all others PORTB = Digital(1)
  52. // Clear All Ports Prior to defining I/O
  53. PORTB=0; //Initialize LED pin data to off state
  54. PORTC=0;
  55. PORTD=0;
  56. PORTE=0;
  57. // Now set pin direction registers
  58. TRISB = 0xFFFF; // entrées
  59. TRISC = 0xFFFF; // entrées
  60. TRISD = 0xFFFF; // entrées
  61. TRISE = 0xFFFD; // PWM1H sortie : 1101
  62. TRISF = 0xFFFF; // entrées
  63. }
  64. //-----------------------------------------------------------------------------
  65. void InitADC10()
  66. {
  67. ADCON1 = 0x006F; // ADC Sampling auto , SOC by PWM
  68. ADCON2 = 0x0000; // CH0 conversion only
  69. ADCHS = 0x0000; // Channel 0 positive input is AN0 et negative input is VREF-
  70. ADCON3 = 0x0080; // Tad = internal RC (4uS)
  71. _ADIF = 0; // Adc int flag Off
  72. _ADIE = 1; // Adc int On
  73. _ADON = 1; // turn ADC ON
  74. }
  75. //-----------------------------------------------------------------------------
  76. // InitMCPWM, intializes the PWM as follows:
  77. // FPWM = 16 khz voir en haut
  78. // Independant PWMs
  79. // Set ADC to be triggered by PWM special trigger
  80. //-----------------------------------------------------------------------------
  81. void InitMCPWM()
  82. {
  83. PTPER = FullDuty; // TPWM = 20 ms
  84. OVDCON = 0xFFFF; // Cmde MLI, no effect of OVDCON
  85. PWMCON1= 0x0110; // enable PWM outputs PWM1H only independant
  86. PDC1=Duty50; // init sans rien, apres une regul ça change
  87. SEVTCMP = FullDuty; // set ADC to trigeer at ...
  88. PWMCON2 = 0x0000; // 1 PWM values
  89. //PTCON = 0x8008; // start PWM asymetrique prescale 1:16
  90. PTCON = 0x8004; // start PWM asymetrique prescale 1:4
  91. }
  92. //?????????????????????????????????????????????????????????????????????
  93. // The ADC interrupt reads the demand pot value.
  94. // tt est synchrone % à cette int de ADC int =2*PWMperiod=2*62.5 us=125 us
  95. //?????????????????????????????????????????????????????????????????????
  96. void __attribute__((interrupt, auto_psv)) _ADCInterrupt ()
  97. {
  98. _ADIF = 0;
  99. ValPot = coefadc*ADCBUF0; // ADC read, potentiometer value
  100. PDC1 = Duty0+ValPot;
  101. }
  102. //-----------------------------------------------------------------------------
  103. //Main routine
  104. int main(void)
  105. {
  106. setup_ports();
  107. InitADC10();
  108. InitMCPWM();
  109. while(1)
  110. {
  111. } // end of while (1)
  112. }
  113.  
  114. //=============================================================================
  115. //Error traps
  116. //-----------------------------------------------------------------------------
  117. //Oscillator Fail Error trap routine
  118. void __attribute__((interrupt, auto_psv)) _OscillatorFail(void)
  119. {
  120. while(1); //Wait forever
  121. }
  122. //-----------------------------------------------------------------------------
  123. //Address Error trap routine
  124. void __attribute__((interrupt, auto_psv)) _AddressError(void)
  125. {
  126. while(1); //Wait forever
  127. }
  128. //-----------------------------------------------------------------------------
  129. //Stack Error trap routine
  130. void __attribute__((interrupt, auto_psv)) _StackError(void)
  131. {
  132. while(1); //Wait forever
  133. }
  134. //-----------------------------------------------------------------------------
  135. //Math (Arithmetic) Error trap routine
  136. void __attribute__((interrupt, auto_psv)) _MathError(void)
  137. {
  138. while(1); //Wait forever
  139. }
  140. //---------------------------------------------------------------------
  141. // This is a generic 1ms delay routine to give a 1mS to 65.5 Seconds delay
  142. // For N = 1 the delay is 1 mS, for N = 65535 the delay is 65,535 mS.
  143. // Note that FCY is used in the computation. Please make the necessary
  144. // Changes(PLLx4 or PLLx8 etc) to compute the right FCY as in the define
  145. // statement above.
  146. //---------------------------------------------------------------------
  147. void DelayNmSec(unsigned int N)
  148. {
  149. unsigned int j;
  150. while(N--)
  151. for(j=0;j < MILLISEC;j++);
  152. }
  153. //---------------------------------------------------------------------
  154. void Delay(unsigned int N)
  155. {
  156. unsigned int j;
  157. for(j=0;j < N; j++) __builtin_nop();
  158. }
  159. //---------------------------------------------------------------------
  160.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:5:22: fatal error: p30F4012.h: No such file or directory
compilation terminated.
stdout
Standard output is empty