【【51单片机直流电机调速】】

学会电机调速,掌握中国速度

PWM的生成方法

先用户设定一个比较值,然后计数器定时自增。

当计数器<比较值,输出0

当计数器>比较值,输出1

main.c

c 复制代码
#include <REGX52.H>
#include"delay.h"
#include"key.h"
#include"Nixie.h"
#include"Timer0.h"

unsigned char Counter,Compare;
unsigned char KeyNum,Speed;
sbit Motor=P1^0;


void main()
{
	Timer0_Init();
	
	while(1)
	{
		KeyNum=Key();
		if(KeyNum==1)
		{
			Speed++;
			Speed%=4;
			if(Speed==0){Compare=0;}
			if(Speed==1){Compare=50;}
			if(Speed==2){Compare=75;}
			if(Speed==3){Compare=100;}
			
		}
		Nixie(1,Speed);
		
		
		
		
		
		
	}	
	
}



void Timer0_Routine() interrupt 1
{
	
	TL0 = 0x9C;		//设置定时初值
	TH0 = 0xFF;		//设置定时初值
	Counter++;
	Counter%=100;
	if(Counter<Compare)
	{
		Motor=1;
	}
	else
	{
		Motor=0;
	}
}

delay.c

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

c 复制代码
#ifndef _DELAY_H__
#define _DELAY_H__
void Delay(unsigned int xms);
 
 #endif

key.c

c 复制代码
#include <REGX52.H>
#include "Delay.h"

/**
  * @brief  获取独立按键键码
  * @param  无
  * @retval 按下按键的键码,范围:0~4,无按键按下时返回值为0
  */
unsigned char Key()
{
	unsigned char KeyNumber=0;
	
	if(P3_1==0){Delay(20);while(P3_1==0);Delay(20);KeyNumber=1;}
	if(P3_0==0){Delay(20);while(P3_0==0);Delay(20);KeyNumber=2;}
	if(P3_2==0){Delay(20);while(P3_2==0);Delay(20);KeyNumber=3;}
	if(P3_3==0){Delay(20);while(P3_3==0);Delay(20);KeyNumber=4;}
	
	return KeyNumber;
}

key.h

c 复制代码
#ifndef __KEY_H__
#define __KEY_H__

unsigned char Key();

#endif

Nixie.c

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_4=1;P2_3=1;P2_2=1;break;
		case 2:P2_4=1;P2_3=1;P2_2=0;break;
		case 3:P2_4=1;P2_3=0;P2_2=1;break;
		case 4:P2_4=1;P2_3=0;P2_2=0;break;
		case 5:P2_4=0;P2_3=1;P2_2=1;break;
		case 6:P2_4=0;P2_3=1;P2_2=0;break;
		case 7:P2_4=0;P2_3=0;P2_2=1;break;
		case 8:P2_4=0;P2_3=0;P2_2=0;break;
	}
	P0=NixieTable[Number];	//段码输出
	//Delay(1);				//显示一段时间
	//P0=0x00;				//段码清0,消影
}

Nixie.h

c 复制代码
#ifndef __NIXIE_H__
#define __NIXIE_H__

void Nixie(unsigned char Location,Number);

#endif

Timer0.c

c 复制代码
#include <REGX52.H>

/**
  * @brief  定时器0初始化,100us@12.000MHz
  * @param  无
  * @retval 无
  */
void Timer0_Init(void)
{
	TMOD &= 0xF0;		//设置定时器模式
	TMOD |= 0x01;		//设置定时器模式
	TL0 = 0x9C;		//设置定时初值
	TH0 = 0xFF;		//设置定时初值
	TF0 = 0;		//清除TF0标志
	TR0 = 1;		//定时器0开始计时
	ET0=1;
	EA=1;
	PT0=0;
}

Timer0.h

c 复制代码
#ifndef __TIMER0_H__
#define __TIMER0_H__

void Timer0_Init(void);

#endif
相关推荐
_Ningye6 小时前
STM32 — 6.1 TIM定时中断
stm32·单片机·嵌入式硬件
小白学电子_6 小时前
proteus仿真51单片机通过矩阵按键和数码管制作简单计算器
嵌入式硬件·51单片机·proteus
FreakStudio8 小时前
把 Flask 搬进 ESP32,高中生自研嵌入式 Web 框架 MicroFlask !
python·单片机·嵌入式·cortex-m3·异步编程·电子diy
AnalogElectronic8 小时前
RP2040 pico 实验6,光敏电阻传感器模块(LM393 比较器版)
单片机
17(无规则自律)9 小时前
【Linux驱动实战】:字符设备之ioctl与mutex全解析
linux·c语言·驱动开发·嵌入式硬件
电子工程师成长日记-C519 小时前
51单片机4乘4计算器
单片机·嵌入式硬件·51单片机
梅尔文.古9 小时前
ADCU-Ethernet-以太网在AUTOSAR与Linux架构下对比
arm开发·单片机·汽车
没有医保李先生9 小时前
esp32和stm32的工程宏定义
stm32·单片机·嵌入式硬件
炸膛坦客10 小时前
单片机/C/C++八股:(十五)内存对齐、结构体内存对齐
c语言·开发语言·单片机
szxinmai主板定制专家10 小时前
基于ZYNQ MPSOC船舶数据采集仪器设计(一)总体设计方案,包括振动、压力、温度、流量等参数
arm开发·人工智能·嵌入式硬件·fpga开发