Advertisement
Guest User

Timerservo

a guest
Mar 24th, 2012
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.93 KB | None | 0 0
  1. /* Servo Pulse Reader and Generator                     */
  2. /* Uses Timer3 for pulse gen, timer 4 for pulse read    */
  3. /* Coded by N. Playle                                   */
  4. /* January 20 2012                                      */
  5.  
  6.  
  7. #define TIMER_MONITOR_CHANNELS (4)
  8.  
  9. HardwareTimer timer(3);
  10. HardwareTimer timer4(4);
  11.  
  12. // Define the values to hold the input data
  13. int SER_CH_IN_1 = 0;
  14. int SER_CH_IN_2 = 0;
  15. int SER_CH_IN_3 = 0;
  16. int SER_CH_IN_4 = 0;
  17.  
  18.  
  19. int timesTrigd = 0; // holds how many timers have been read
  20. int polarity = 0;   // capture rising or falling edge of pulse on CC channels
  21. int temp = 0;       // dummy value to clear interrupt flag
  22.  
  23.  
  24.  
  25. void setup() {
  26.  
  27.  
  28. /* TIMER 3 IS USED TO OUTPUT PULSES EVERY 20MSEC */
  29.     // Set all servo outputs as PWM pins
  30.     pinMode(12, PWM);
  31.     pinMode(11, PWM);
  32.     pinMode(27, PWM);
  33.     pinMode(28, PWM);
  34.     pinMode(7, OUTPUT);
  35.     //Pause the timer while we're configuring it
  36.     timer.pause();
  37.  
  38.     // // Set up period
  39.     timer.setPeriod(20000); // in microseconds
  40.     timer_set_prescaler(TIMER3, 21); // A prescaler of 21 gives a period of ~20msec
  41.     // // Refresh the timer's count, prescale, and overflow
  42.     timer.refresh();
  43.  
  44.     // Start the timer counting
  45.     timer.resume();
  46.     // Generate some pulses. One count ~= 0.3uSec
  47.     pwmWrite(12,2638);
  48.     pwmWrite(11,5000);
  49.     pwmWrite(27,3500);
  50.     pwmWrite(28,4000);
  51.    
  52.    
  53. /*TIMER 4 WILL BE USED TO MEASURE THE PULSES FROM TIMER3 */
  54.    
  55.     // Configure timer4[4..1] as inputs
  56.     pinMode(5, INPUT_PULLDOWN);
  57.     pinMode(9, INPUT_PULLDOWN);
  58.     pinMode(24, INPUT_PULLDOWN);
  59.     pinMode(14, INPUT_PULLDOWN);
  60.     TIMER4_BASE->CCMR1 = 0xF1F1;    //channel 2,1 configure as input, IC2,IC1 mapped on TI2,TI1
  61.     TIMER4_BASE->CCMR2 = 0xF1F1;    //channel 4,3 configure as input, IC4,IC3 mapped on TI4,TI3
  62.     TIMER4_BASE->CCER = 0x1111;     //configure CC channels [4..1] as an input
  63.     TIMER4_BASE->SMCR = 0x0000;     //all slave modes disabled, CC transitions on clock
  64.     TIMER4_BASE->CR1 = 0x0001;      //enable the counter
  65.     TIMER4_BASE->CR2 = 0x0000;      //connect TIM4_CH1 pin to TI1 input
  66.     TIMER4_BASE->PSC = 0x0015;      //set PRESCALER = 21 (0x15) FOR SERVO, 50HZ
  67.    
  68.     // Attach the interrupts to the servos. Maybe combine in future?
  69.     timer_attach_interrupt(TIMER4, TIMER_CC1_INTERRUPT, __measurePulse_irq);
  70.     timer_attach_interrupt(TIMER4, TIMER_CC2_INTERRUPT, __measurePulse_irq2);
  71.     timer_attach_interrupt(TIMER4, TIMER_CC3_INTERRUPT, __measurePulse_irq3);
  72.     timer_attach_interrupt(TIMER4, TIMER_CC4_INTERRUPT, __measurePulse_irq4);
  73.     // Restart timer4
  74.     timer_resume(TIMER4);
  75.    
  76.    
  77. }
  78.  
  79. void loop() {
  80.     delay(100);
  81.    
  82.     //prints the resulting pulse width
  83.     SerialUSB.print("ax");
  84.     SerialUSB.println(SER_CH_IN_1);
  85.     SerialUSB.print("ay");
  86.     SerialUSB.println(SER_CH_IN_2);
  87.     SerialUSB.print("az");
  88.     SerialUSB.println(SER_CH_IN_3);
  89.     SerialUSB.print("gx");
  90.     SerialUSB.println(SER_CH_IN_4);    
  91.        
  92.     pwmWrite(12,SER_CH_IN_1);
  93.     pwmWrite(11,SER_CH_IN_2);
  94.     pwmWrite(27,SER_CH_IN_3);
  95.     pwmWrite(28,SER_CH_IN_4);
  96. }
  97.  
  98. void __measurePulse_irq()
  99. {
  100.     //get the bit to see if this is an up or down pulse
  101.     polarity = TIMER4_BASE->CCER & 0x0002;
  102.  
  103.     if(polarity == 0)   //this is a rising edge capture
  104.     {
  105.         timer_set_count(TIMER4, 0x0000);        //zero the counter
  106.         temp = TIMER4_BASE->CCR1;       //clear the interrupt by reading CCR1 SER_CH_IN_
  107.         TIMER4_BASE->CCER |= 0x0002;        //change polarity to capture falling edge
  108.     }
  109.     else    //this is a falling edge capture
  110.     {
  111.         SER_CH_IN_1 = TIMER4_BASE->CCR1;        //read the captured SER_CH_IN_
  112.        
  113.         TIMER4_BASE->CCER &= ~0x0002;       //change polarity to capture rising edge
  114.         if (TIMER_MONITOR_CHANNELS <= timesTrigd + 1){     
  115.             timer_set_count(TIMER4, 0x0000);    //zero the counter
  116.             timesTrigd =0;
  117.         }
  118.         else{
  119.             timesTrigd++;
  120.         }
  121.     }
  122. }
  123.  
  124. void __measurePulse_irq2()
  125. {
  126.     //get the bit to see if this is an up or down pulse
  127.     polarity = TIMER4_BASE->CCER & 0x0020;
  128.  
  129.     if(polarity == 0)   //this is a rising edge capture
  130.     {
  131.         timer_set_count(TIMER4, 0x0000);        //zero the counter
  132.         //temp = TIMER4_BASE->CCR1;     //clear the interrupt by reading CCR1 SER_CH_IN_
  133.         temp = TIMER4_BASE->CCR2;       // clear ccr2 trigger if it was
  134.         TIMER4_BASE->CCER |= 0x0020;        //change polarity to capture falling edge
  135.     }
  136.     else    //this is a falling edge capture
  137.     {
  138.         SER_CH_IN_2 = TIMER4_BASE->CCR2;        //read the captured SER_CH_IN_
  139.         TIMER4_BASE->CCER &= ~0x0020;       //change polarity to capture rising edge
  140.         if (TIMER_MONITOR_CHANNELS <= timesTrigd + 1){
  141.             timer_set_count(TIMER4, 0x0000);    //zero the counter
  142.             timesTrigd =0;
  143.         }
  144.         else{
  145.             timesTrigd++;
  146.         }
  147.     }
  148. }
  149.  
  150. void __measurePulse_irq3()
  151. {
  152.     //get the bit to see if this is an up or down pulse
  153.     polarity = TIMER4_BASE->CCER & 0x0200;
  154.  
  155.     if(polarity == 0)   //this is a rising edge capture
  156.     {
  157.         timer_set_count(TIMER4, 0x0000);        //zero the counter
  158.         temp = TIMER4_BASE->CCR3;       // clear ccr2 trigger if it was
  159.         TIMER4_BASE->CCER |= 0x0200;        //change polarity to capture falling edge
  160.     }
  161.     else    //this is a falling edge capture
  162.     {
  163.         SER_CH_IN_3 = TIMER4_BASE->CCR3;        //read the captured SER_CH_IN_
  164.         TIMER4_BASE->CCER &= ~0x0200;       //change polarity to capture rising edge
  165.         if (TIMER_MONITOR_CHANNELS <= timesTrigd + 1){
  166.             timer_set_count(TIMER4, 0x0000);    //zero the counter
  167.             timesTrigd =0;
  168.         }
  169.         else{
  170.             timesTrigd++;
  171.         }
  172.     }
  173. }
  174.  
  175. void __measurePulse_irq4()
  176. {
  177.     //get the bit to see if this is an up or down pulse
  178.     polarity = TIMER4_BASE->CCER & 0x2000;
  179.  
  180.     if(polarity == 0)   //this is a rising edge capture
  181.     {
  182.         timer_set_count(TIMER4, 0x0000);        //zero the counter
  183.         //temp = TIMER4_BASE->CCR1;     //clear the interrupt by reading CCR1 SER_CH_IN_
  184.         temp = TIMER4_BASE->CCR4;       // clear ccr2 trigger if it was
  185.         TIMER4_BASE->CCER |= 0x2000;        //change polarity to capture falling edge
  186.     }
  187.     else    //this is a falling edge capture
  188.     {
  189.         SER_CH_IN_4 = TIMER4_BASE->CCR4;        //read the captured SER_CH_IN_
  190.         TIMER4_BASE->CCER &= ~0x2000;       //change polarity to capture rising edge
  191.         if (TIMER_MONITOR_CHANNELS <= timesTrigd + 1){
  192.             timer_set_count(TIMER4, 0x0000);    //zero the counter
  193.             timesTrigd =0;
  194.         }
  195.         else{
  196.             timesTrigd++;
  197.         }
  198.     }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement