找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 8115|回复: 0
打印 上一主题 下一主题
收起左侧

avr单片机PWM控制直流电机转速和正反转 带proteus仿真和源程序

[复制链接]
跳转到指定楼层
楼主
用uln2003控制,下面是仿真原理图:


源程序:

  1. /*****************************************************
  2. This program was produced by the
  3. CodeWizardAVR V2.03.4 Standard

  4. Project :
  5. Version :
  6. Date    : 7/26/2009
  7. Author  :
  8. Company :
  9. Comments:


  10. Chip type           : ATmega32
  11. Program type        : Application
  12. Clock frequency     : 12.000000 MHz
  13. Memory model        : Small
  14. External RAM size   : 0
  15. Data Stack size     : 512
  16. *****************************************************/

  17. #include <mega32.h>
  18. #include <delay.h>

  19. // Declare your global variables here

  20. void Motor_Speed (int x)
  21. {
  22. switch (x)
  23.    {
  24.     case 0:    PORTB = 0x00;
  25.                break;
  26.                
  27.     case 6:    PORTB = 0x08;
  28.                break;               
  29.    
  30.     case 12:   PORTB = 0x28;
  31.                break;
  32.                
  33.     case -6:   PORTB = 0x10;
  34.                break;                        
  35.                
  36.     case -12:  PORTB = 0x30;
  37.                break;                    
  38.    }
  39. }
  40. //////////////////////////////////////////////////////////////////
  41. void main(void)
  42. {
  43. // Declare your local variables here

  44. // Input/Output Ports initialization
  45. // Port A initialization
  46. // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
  47. // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
  48. PORTA=0x00;
  49. DDRA=0x00;

  50. // Port B initialization
  51. // Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
  52. // State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
  53. PORTB=0x00;
  54. DDRB=0xFF;

  55. // Port C initialization
  56. // Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
  57. // State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
  58. PORTC=0x00;
  59. DDRC=0xFF;

  60. // Port D initialization
  61. // Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
  62. // State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
  63. PORTD=0x00;
  64. DDRD=0xFF;

  65. // Timer/Counter 0 initialization
  66. // Clock source: System Clock
  67. // Clock value: Timer 0 Stopped
  68. // Mode: Normal top=FFh
  69. // OC0 output: Disconnected
  70. TCCR0=0x00;
  71. TCNT0=0x00;
  72. OCR0=0x00;

  73. // Timer/Counter 1 initialization
  74. // Clock source: System Clock
  75. // Clock value: Timer 1 Stopped
  76. // Mode: Normal top=FFFFh
  77. // OC1A output: Discon.
  78. // OC1B output: Discon.
  79. // Noise Canceler: Off
  80. // Input Capture on Falling Edge
  81. // Timer 1 Overflow Interrupt: Off
  82. // Input Capture Interrupt: Off
  83. // Compare A Match Interrupt: Off
  84. // Compare B Match Interrupt: Off
  85. TCCR1A=0x00;
  86. TCCR1B=0x00;
  87. TCNT1H=0x00;
  88. TCNT1L=0x00;
  89. ICR1H=0x00;
  90. ICR1L=0x00;
  91. OCR1AH=0x00;
  92. OCR1AL=0x00;
  93. OCR1BH=0x00;
  94. OCR1BL=0x00;

  95. // Timer/Counter 2 initialization
  96. // Clock source: System Clock
  97. // Clock value: Timer 2 Stopped
  98. // Mode: Normal top=FFh
  99. // OC2 output: Disconnected
  100. ASSR=0x00;
  101. TCCR2=0x00;
  102. TCNT2=0x00;
  103. OCR2=0x00;

  104. // External Interrupt(s) initialization
  105. // INT0: Off
  106. // INT1: Off
  107. // INT2: Off
  108. MCUCR=0x00;
  109. MCUCSR=0x00;

  110. // Timer(s)/Counter(s) Interrupt(s) initialization
  111. TIMSK=0x00;

  112. // Analog Comparator initialization
  113. // Analog Comparator: Off
  114. // Analog Comparator Input Capture by Timer/Counter 1: Off
  115. ACSR=0x80;
  116. SFIOR=0x00;

  117. while (1)
  118.       {
  119.             Motor_Speed(6);
  120.             delay_ms(3000);
  121.       
  122.             Motor_Speed(12);
  123.             delay_ms(3000);
  124.       
  125.             Motor_Speed(-6);
  126.             delay_ms(3000);
  127.       
  128.             Motor_Speed(-12);
  129.             delay_ms(3000);
  130.       
  131.             Motor_Speed(0);
  132.             delay_ms(3000);     
  133.       };
  134. }
复制代码

DC_Motor_Multi_Speed_Simulation.7z

25.58 KB, 下载次数: 44, 下载积分: 黑币 -5

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表