智能车制作

标题: 求PWM控制二相步进电机程序,谢谢 [打印本页]

作者: yzst    时间: 2010-2-27 18:55
标题: 求PWM控制二相步进电机程序,谢谢
求PWM控制二相步进电机程序,谢谢
作者: laugh007smile    时间: 2010-3-1 23:03
等待答案的出现...自己动手!
作者: yzst    时间: 2010-3-13 12:03


复制代码
[qq]897227738[/qq]
  1. #include <hidef.h> /* common defines and macros */
  2. #include <MC9S12XS128.h> /* derivative information */
  3. #pragma LINK_INFO DERIVATIVE "mc9s12xs128"
  4. //unsigned char g,s,b,q;
  5. unsigned char table[]={0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90,0xff };

  6. void delay(unsigned int t)
  7. { unsigned int i,j;
  8. for(i=0;i<t;i++)
  9. for(j=0;j<110;j++);

  10. }

  11. void disp(unsigned char g,unsigned char s,unsigned char b,unsigned char q)
  12. { DDRM=0xff;
  13. DDRH=0xff;
  14. PTM=0b00000001;
  15. PTH=table[g];
  16. delay(20);
  17. PTM=PTM<<1;
  18. PTH=table[s];
  19. delay(20);
  20. PTM=PTM<<1;
  21. PTH=table[b];
  22. delay(20);
  23. PTM=PTM<<1;
  24. PTH=table[q];
  25. delay(20);
  26. }

  27. void PLL_Init(void) //PLLCLK=2*OSCCLK*(SYNR+1)/(REFDV+1)

  28. { //锁相环时钟=2*16*(2+1)/(1+1)=48MHz

  29. REFDV=1; //总线时钟=48/2=24MHz

  30. SYNR=2;

  31. while(!(CRGFLG&0x08));

  32. CLKSEL=0x80;

  33. }

  34. void pwminitial0()
  35. {
  36. PWMPOL=0X81; //通道0、7输出波形开始极性为高 output waveform which high first then low when the duty counter is reached
  37. PWMCAE=0x80; //0左对齐输出模式、7居中对齐 output left align waveform
  38. PWMCLK=0X81; //PWM时钟源为0 SA,7-SB SAClock SA is the clock source for PWM channel 0
  39. PWMPRCLK=0X23; //时钟A8分频 Clock A is 24MHz/8=3MHz
  40. PWMSCLA=0X96; //时钟SA为3MHz/300=10khz
  41. PWMSCLB=0X96;
  42. PWMPER0=100; //设定输出周期=通道时钟周期*20 f=100hz
  43. PWMPER7=100;
  44. PWMDTY0=50; //占空比初始为0 Duty is 50%,and PWM waveform's frequent is 16
  45. PWMDTY7=50;
  46. PWME=0X81; //通道0使能 enable pwm channel 0

  47. }

  48. /*void pwminitial1()
  49. {
  50. PWMPOL=0X02; //通道1输出波形开始极性为1 output waveform which high first then low when the duty counter is reached
  51. PWMCAE=0x02; //居中对齐输出模式 output left align waveform
  52. PWMCLK=0X03; //PWM时钟源为 SAClock SA is the clock source for PWM channel 1
  53. PWMPRCLK=0X06; //时钟A8分频 Clock A is 8MHz/8=1MHz
  54. PWMSCLA=0Xff; //时钟SA为1MHz/128/2 Clock SA is 1MHz/128/2=4KHz

  55. PWMPER1=122; //设定输出周期=通道时钟周期*256
  56. PWMDTY1=61; //占空比初始为0.5 Duty is 50%,and PWM waveform's frequent is 16
  57. PWME=0X02; //通道1使能 enable pwm channel 0

  58. }*/
  59. void main()
  60. {

  61. pwminitial0();
  62. DDRA=0xff;
  63. // pwminitial1();
  64. for(;;) {

  65. PORTA_PA1=PTP_PTP7;
  66. PORTA_PA3=PTP_PTP0;
  67. PORTA_PA4=0;
  68. PORTA_PA5=0;
  69. PORTA_PA6=0;
  70. PORTA_PA7=0;
  71. PORTA_PA0=1;

  72. // PORTA=PTP;

  73. if(PORTA_PA3==0&&PORTA_PA1==1)
  74. disp(1,10,10,10) ;
  75. else if(PORTA_PA3==0&&PORTA_PA1==0)
  76. disp(10,2,10,10) ;

  77. else if(PORTA_PA3==1&&PORTA_PA1==0)
  78. disp(10,10,3,10) ;

  79. else//(PTP_PTP0==1&&PTP_PTP1==1)
  80. disp(10,10,10,4) ;

  81. }
  82. /* please make sure that you never leave this function */
  83. }
复制代码

作者: jiao19880804    时间: 2010-3-17 12:25
谢谢
作者: 632693521    时间: 2010-4-3 15:19
谢谢yzst 了
作者: zhijin323    时间: 2010-12-3 13:14
谢谢

作者: wsxb    时间: 2011-4-9 10:17
谢谢大侠
作者: 反对幂指三    时间: 2012-6-2 10:42
谢啦!
作者: 断翅at雄鹰    时间: 2012-9-24 12:32
谢谢 我也非常需要啊
作者: 萍1185279685    时间: 2012-11-20 14:45
#include <hidef.h>      /* common defines and macros */
#include <mc9s12xs128.h>     /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12xs128"

void initpll(void)//40MHz,外部时钟为16MHz
{
     CLKSEL_PLLSEL=0;
     PLLCTL_PLLON=0;
     SYNR=0xc0 | 0x07;//PLLCLK =2*OSCCLK*(SYNR + 1)/(REFDV + 1)
     REFDV=0x80 | 0x01;
     PLLCTL_PLLON=1;
     asm  NOP;
     asm  NOP;
     asm  NOP;
     while((CRGFLG&0X08)==0);//时钟校正同步
     CLKSEL_PLLSEL=1;
}

void main(void)
  {   initpll();
      PWME=0x00;
      PWMCTL=0X10;    //01连起来供给舵机
      PWMCAE=0X00;
      PWMPOL=0XFF;    //先输出高电平
      PWMCLK=0X03;    //01选择SA时钟,23选择B,45选择A
      PWMPRCLK=0X55;  //对A时钟进行32分频,B时钟进行32分频 A=2M B=2M
      PWMSCLA= 4;     //SA=2M/(2*4)=250K
      PWMPER01=5000;  // 周期为 250K/5000=50HZ  舵机
      PWMDTY01=430;  //  舵机的中间位置440,增大往右530转差不多40度,减小往左380转差不多 舵机从最左到最右的时间为160MS
      PWMPER3=200;    //  周期为 2M/200=10KHZ   电机左 正转
      PWMPER2=200;
      PWMDTY2=50;    //  左电机
     PWMDTY3=0;          //电机左  正转
      PWMPER4=200 ;
      PWMPER5=200;    //  占空比为 0% 左电机
      PWMDTY5=0;    //   电机右 正转
      PWMDTY4=100;      ///电机右  反转
      //PWMDTY4=0 ;  
      //占空比为 0% 右电机
      PWME=0XFF;   //通道使能
   
   
  EnableInterrupts;
  for(;;) {} /* wait forever */
  /* please make sure that you never leave this function */
}

作者: joicejoy    时间: 2013-5-25 10:37
楼上的程序能同时输出2路PWM波么。
作者: 蓝天下的心    时间: 2013-7-27 22:15
:):):):):):):):):):)
作者: fengerzxl    时间: 2016-12-12 21:12
感谢感谢~
作者: 创世神    时间: 2017-2-19 22:12
[attach]89672[/attach]    请问这种步进电机  怎么接入  L298N电机驱动板???[attach]89673[/attach]




欢迎光临 智能车制作 (http://111.231.132.190/) Powered by Discuz! X3.2