51单片机如何产生8路PWM波

核心提示我来说一下我的方案,这个我验证过,方法如下: 其实用一个定时器就够了,外部中断接按键,一个用来频率加,一个用来频率减,即做调节频率用,8路频率从P0口输出,定时器产生中断,比如1us,那么我到1us时对P0.0取反,同时中断里在定义一个变量

我来说一下我的方案,这个我验证过,方法如下:

其实用一个定时器就够了,外部中断接按键,一个用来频率加,一个用来频率减,即做调节频率用,8路频率从P0口输出,定时器产生中断,比如1us,那么我到1us时对P0.0取反,同时中断里在定义一个变量t1,那么t1计中断次数,假如计到5时我让P1.0取反,儿至于计到几有外部中断来定义,比如定义一个全局变量f,INT0按一次则f++,INT1S按一次f- -,如此频率得以控制,定时器处理如下:

void TIME0_ISR(void) interrupt 2

{

t1++;

if(t1==f) p00=~p00;

if(t1==f) p01=~p01;

if(t1==f) p02=~p02;

if(t1==f) p03=~p03;

if(t1==f) p04=~p04;

if(t1==f) p05=~p05;

if(t1==f) p06=~p06;

if(t1==f) {p07=~p07; f=0;}

}

当然这只是简单的用51本身资源产生而已,还可以用专门的外围电路来实现,我给你一个四路频率产生程序,思想如上,已验证过

#include<reg52.h>

#define uchar unsigned char

#define uint unsigned int

sbit pwm1=P1^0; //第一路频率 1s内产生5000个脉冲

sbit pwm2=P1^1; //第二路频率 1s内产生1000个脉冲

sbit pwm3=P1^2; //第三路频率 1s内产生2500个脉冲

sbit pwm4=P1^3; //第四路频率 1s内产生500个脉冲

uchar cout1,cout2; //分别用来定义时间宽度

uchar t0_max=10,t1_max=100;

void time0_ini() //定时0初始化

{

TMOD=0X02; //采用定时器0,选择模式2

TH0=0xA3; //0.1ms定时

TL0=0Xa3;

ET0=1; //允许定时器溢出中断

TR0=1; //启动定时器

}

void INT0_ini()

{

EX0=1; //外部中断0允许

IT0=1; //选择边沿触发方式

}

void time1_ini()

{

TMOD=0X20; //采用定时器1,选择模式2

TH1=0XA3 ; //定时0.1ms

TL1=0XA3;

ET1=1; //允许定时器中断

TR1=1; //启动定时器1

}

void main()

{

time0_ini(); //调用定时器0初始化函数

time1_ini(); //调用定时器1初始化函数

INT0_ini();

cout1=0;

cout2=0;

pwm1=0; //没一路频率都从低电平开始

pwm2=0;

pwm3=0;

pwm4=0;

EA=1; //打开总中断

while(1); //一直停留在主函数中

}

void tim0_ISR() interrupt 1 //定时0中断服务程序

{

cout1++; //计数变量加1

if(cout1==t0_max)

{

pwm1=~pwm1; //每当到了1ms时取反一次电平,即周期为2ms,1s内产生500个脉冲

}

if(cout1==(t0_max+10))

{

cout1=0; //每当到了2ms时取反一次电平,即周期为4ms,1s内产生250个脉冲

pwm2=~pwm2;

}

}

void time1_ISR() interrupt 3 //定时器3中断服务程序

{

cout2++;

if(cout2==t1_max)

{

pwm3=~pwm3; //每当到了10ms时取反一次电平,即周期为20ms,1s内产生50个脉冲

}

if(cout2==(t1_max+150))

{

cout2=0; //每当到了25ms时取反一次电平,即周期为50ms,1s内产生20个脉冲

pwm4=~pwm4;

}

}

void INT0_ISR() interrupt 0

{

if(t0_max>200) t0_max=10; //清楚上限

if(t1_max>2000) t1_max=100;

t0_max=t0_max+10;

t1_max=t1_max+100;

}

Microchip 公司的MCP4017/18/19是通用的数字电位器,具有7位电阻网络分辨率,有127个电阻,具有低的温度系数:绝对值(0-70度C)为50ppm,比值为10ppm,工作电压1.8V到5.5V. MCP4017/18/19可应用在设定电或失调调整,传感器校准,替代机械式电位计和可选择增益和失调放大器设计.本文介绍了MCP4017/18/19的主要特性, 方框图, 和微控制器(MCU) 典型连接框图, 采用MCP4017调整非反相放大器失调和增益方框图以及采用MCP4018的可编滤波器和采用MCP4017的惠斯通电桥调整框图.

The MCP4017/18/19 devices are general purpose digital potentiometers intended to be used in applications where a programmable resistance with moderate bandwidth is desired.

This Data Sheet covers a family of three Digital Potentiometer and Rheostat devices. The MCP4018 device is the Potentiometer configuration, while the MCP4017 and MCP4019 devices are the Rheostat configuration.

MCP4017/18/19主要特性:

Potentiometer or Rheostat configuration options

7-bit: Resistor Network Resolution

127 Resistors (128 Steps)

Zero Scale to Full Scale Wiper operation

RAB Resistances: 5 kΩ, 10 kΩ, 50 kΩ, or 100 kΩ

Low Wiper Resistance: 100Ω (typical)

Low Tempco:

Absolute (Rheostat): 50 ppm typical (0℃ to 70℃)

Ratiometric (Potentiometer): 10 ppm typical

Simple I2C Protocol with read & write commands

Brown-out reset protection (1.5V typical)

Power-on Default Wiper Setting (Mid-scale)

Low-Power Operation:

2.5 μA Static Current (typical)

Wide Operating Voltage Range:

2.7V to 5.5V - Device Characteristics Specified

1.8V to 5.5V - Device Operation

MCP4017/18/19应用:

Applications generally suited for the MCP401X devices include:

Set point or offset trimming

Sensor calibration

Selectable gain and offset amplifier designs

Cost-sensitive mechanical trim pot replacement

 
友情链接
鄂ICP备19019357号-22