dcpwm.cpp

来自「大学微型计算机接口技术实验源代码,直接就可以运行,包含了C++和asm代码,比较」· C++ 代码 · 共 64 行

CPP
64
字号
//PWM.CPP
#include<dos.h>
#include<conio.h>
#include<stdio.h>
char *title="Test dc motor use PWM method";
#define PC 0x302
#define CR 0x303
void rotate(float per);    /*相当于画波形函数*/
void main()
{
  char ch;
  outportb(CR,0x90);  /*8255状态字,PC低四位输出*/
  outportb(PC,0xff);   /*PC口各位写1*/

  clrscr();
  puts(title);
  puts("1------>100% duty cycle");
  puts("2------>50% duty cycle");
  puts("3------>25% duty cycle");
  puts("4------>12% duty cycle");
  puts("5------>exit");

  while(1)
  if(kbhit())
    {
			ch=getch();
			switch(ch)
			  {
				 case '1': rotate(1.0);
                           break;
				 case '2': rotate(0.75);
                           break;
				 case '3': rotate(0.5);
                           break;
				 case '4': rotate(0.25);
                           break;
				 case '5': return ;
				 default:break;
			     }
		}
  outportb(PC,0xff);
  }
/*-----------------------------------*/
void  rotate(float per)
  {
    int c,ton,toff;
    gotoxy(10,10);
    printf("duty: %2.2f ",per);
    
    ton=(int) 100*per;
    toff=100-ton;
    
    for(c=0;c<20;c++)
       {
	 outportb(PC,0);
	 delay(ton);
	 
	 outportb(PC,1);
	 delay(toff);
	}
    delay(200);	
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?