庆阳网站建设报价,互联网项目有哪些,应用之星在线制作app,科技传承学会电机调速#xff0c;掌握中国速度
PWM的生成方法
先用户设定一个比较值#xff0c;然后计数器定时自增。 当计数器比较值#xff0c;输出0 当计数器比较值#xff0c;输出1
main.c
#include REGX52.H
#includedelay.h
#include掌握中国速度
PWM的生成方法
先用户设定一个比较值然后计数器定时自增。 当计数器比较值输出0 当计数器比较值输出1
main.c
#include REGX52.H
#includedelay.h
#includekey.h
#includeNixie.h
#includeTimer0.hunsigned char Counter,Compare;
unsigned char KeyNum,Speed;
sbit MotorP1^0;void main()
{Timer0_Init();while(1){KeyNumKey();if(KeyNum1){Speed;Speed%4;if(Speed0){Compare0;}if(Speed1){Compare50;}if(Speed2){Compare75;}if(Speed3){Compare100;}}Nixie(1,Speed);} }void Timer0_Routine() interrupt 1
{TL0 0x9C; //设置定时初值TH0 0xFF; //设置定时初值Counter;Counter%100;if(CounterCompare){Motor1;}else{Motor0;}
}delay.c
void Delay(unsigned int xms) //12.000MHz
{while(xms){unsigned char i, j;i 2;j 239;do{while (--j);} while (--i);xms--;
}
}delay.h
#ifndef _DELAY_H__
#define _DELAY_H__
void Delay(unsigned int xms);#endifkey.c
#include REGX52.H
#include Delay.h/*** brief 获取独立按键键码* param 无* retval 按下按键的键码范围0~4无按键按下时返回值为0*/
unsigned char Key()
{unsigned char KeyNumber0;if(P3_10){Delay(20);while(P3_10);Delay(20);KeyNumber1;}if(P3_00){Delay(20);while(P3_00);Delay(20);KeyNumber2;}if(P3_20){Delay(20);while(P3_20);Delay(20);KeyNumber3;}if(P3_30){Delay(20);while(P3_30);Delay(20);KeyNumber4;}return KeyNumber;
}key.h
#ifndef __KEY_H__
#define __KEY_H__unsigned char Key();#endifNixie.c
#include REGX52.H
#include Delay.h //包含Delay头文件//数码管段码表
unsigned char NixieTable[]{0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};//数码管显示子函数
void Nixie(unsigned char Location,Number)
{switch(Location) //位码输出{case 1:P2_41;P2_31;P2_21;break;case 2:P2_41;P2_31;P2_20;break;case 3:P2_41;P2_30;P2_21;break;case 4:P2_41;P2_30;P2_20;break;case 5:P2_40;P2_31;P2_21;break;case 6:P2_40;P2_31;P2_20;break;case 7:P2_40;P2_30;P2_21;break;case 8:P2_40;P2_30;P2_20;break;}P0NixieTable[Number]; //段码输出//Delay(1); //显示一段时间//P00x00; //段码清0消影
}Nixie.h
#ifndef __NIXIE_H__
#define __NIXIE_H__void Nixie(unsigned char Location,Number);#endifTimer0.c
#include REGX52.H/*** brief 定时器0初始化100us12.000MHz* param 无* retval 无*/
void Timer0_Init(void)
{TMOD 0xF0; //设置定时器模式TMOD | 0x01; //设置定时器模式TL0 0x9C; //设置定时初值TH0 0xFF; //设置定时初值TF0 0; //清除TF0标志TR0 1; //定时器0开始计时ET01;EA1;PT00;
}Timer0.h
#ifndef __TIMER0_H__
#define __TIMER0_H__void Timer0_Init(void);#endif