Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <main.h>
- /*
- ### BAD/TRASH CODE. Replaced with: pastebin.com/2e85Lv2B
- This code allows you to slowly drive a stepper motor with microstepping. The precision over small
- intervals is low, as the PWM is software generated, but it should be enough to reduce vibration.
- The main purpose of this is to drive the motor of an simple equatorial mount for astrophotography.
- Still work in progress and very unreliable.
- Another possible idea is to use the hardware generator and select the pins to switch. That allows
- you only to control one pin with PWM, not allowing you to use circular microstepping (that requires
- at least two), but it still should work fine. The cons of that is that you will need nine pins, 4
- for selecting the coils, 4 for selecting the PWM channel plus another for the CCP.
- However, I plan to use two 74HC595 for controlling a 4 digit 7 segments display (look up the code
- at pastebin.com/2e85Lv2B), and, luckly, there will be 4 pins left of the shift register without any
- use. My current thoughts are on using those pins for selecting the coil to switch, as there isn't
- any need to switch that quickly (I intend to change coils every ~100ms or so, giving plenty of
- time to send the data to the shift register).
- Though I plan to apply that idea in another code.
- */
- void soft_pwm(int tm1, int tm2, int tm3, int dir, int trisa);
- void main()
- {
- int16 tcnt;
- //const int duty1[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, };
- const int duty2[] = { 20, 15, 10, 5, 0, 5, 10, 15, };
- const int rest[] = { 0, 5, 10, 15, 20, 15, 10, 5, };
- int1 dir;
- const int trisa[] = { 0x05, 0x09, 0x0A , 0x06 };
- int count;
- int tcount;
- enable_interrupts(global);
- while(true)
- {
- setup_timer_1(t1_disabled);
- clear_interrupt(int_timer1);
- count++;
- if(count>7){
- tcount=(tcount>2)?0:++tcount;
- count=0;
- }
- if(count==4) dir^=1;
- set_timer1(tcnt);
- setup_timer_1(t1_internal | t1_div_by_1);
- soft_pwm(0,duty2[count],rest[count],dir,trisa[tcount]);
- }
- }
- void soft_pwm(int tm1, int tm2, int tm3, int1 pdir, int ptrisa)
- {
- output_toggle(pin_b0);
- int fpins = pdir ? 0x03 : 0x0C;
- #use fast_io(A)
- set_tris_a(ptrisa);
- while(!interrupt_active(int_timer1))
- {
- delay_us(tm1);
- output_a(fpins);
- delay_us(tm2);
- output_a(0x0F);
- delay_us(tm3);
- output_a(0x00);
- }
- #use standard_io(A)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement