Advertisement
Francoo

Software PWM prototype

Jan 30th, 2014
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.47 KB | None | 0 0
  1. #include <main.h>
  2.  
  3. /*
  4. ### BAD/TRASH CODE. Replaced with: pastebin.com/2e85Lv2B
  5. This code allows you to slowly drive a stepper motor with microstepping. The precision over small
  6. intervals is low, as the PWM is software generated, but it should be enough to reduce vibration.
  7. The main purpose of this is to drive the motor of an simple equatorial mount for astrophotography.
  8. Still work in progress and very unreliable.
  9. Another possible idea is to use the hardware generator and select the pins to switch. That allows
  10. you only to control one pin with PWM, not allowing you to use circular microstepping (that requires
  11. at least two), but it still should work fine. The cons of that is that you will need nine pins, 4
  12. for selecting the coils, 4 for selecting the PWM channel plus another for the CCP.
  13. However, I plan to use two 74HC595 for controlling a 4 digit 7 segments display (look up the code
  14. at pastebin.com/2e85Lv2B), and, luckly, there will be 4 pins left of the shift register without any
  15. use. My current thoughts are on using those pins for selecting the coil to switch, as there isn't
  16. any need to switch that quickly (I intend to change coils every ~100ms or so, giving plenty of
  17. time to send the data to the shift register).
  18. Though I plan to apply that idea in another code.
  19. */
  20.  
  21. void soft_pwm(int tm1, int tm2, int tm3, int dir, int trisa);
  22.  
  23. void main()
  24. {
  25.    int16 tcnt;
  26.    //const int duty1[] = {  0,  0,  0,  0,  0,  0,  0,  0,  0,  };
  27.    const int duty2[] = { 20, 15, 10,  5,  0,  5, 10, 15,  };
  28.    const int rest[]  = {  0,  5, 10, 15, 20, 15, 10,  5,  };
  29.    int1 dir;
  30.    const int trisa[] = { 0x05, 0x09, 0x0A , 0x06 };
  31.    int count;
  32.    int tcount;
  33.    
  34.    
  35.    enable_interrupts(global);
  36.  
  37.    while(true)
  38.    {  
  39.       setup_timer_1(t1_disabled);
  40.       clear_interrupt(int_timer1);
  41.      
  42.       count++;
  43.      
  44.       if(count>7){
  45.          tcount=(tcount>2)?0:++tcount;
  46.          count=0;
  47.       }
  48.      
  49.       if(count==4) dir^=1;
  50.      
  51.       set_timer1(tcnt);
  52.       setup_timer_1(t1_internal | t1_div_by_1);
  53.       soft_pwm(0,duty2[count],rest[count],dir,trisa[tcount]);
  54.    }
  55. }
  56.  
  57. void soft_pwm(int tm1, int tm2, int tm3, int1 pdir, int ptrisa)
  58. {
  59.    output_toggle(pin_b0);
  60.    int fpins = pdir ? 0x03 : 0x0C;
  61.    #use fast_io(A)
  62.    set_tris_a(ptrisa);
  63.    while(!interrupt_active(int_timer1))
  64.    {
  65.       delay_us(tm1);
  66.       output_a(fpins);
  67.       delay_us(tm2);
  68.       output_a(0x0F);
  69.       delay_us(tm3);
  70.       output_a(0x00);
  71.    }
  72.    #use standard_io(A)
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement