专家
公告
财富商城
电子网
旗下网站
首页
问题库
专栏
标签库
话题
专家
NEW
门户
发布
提问题
发文章
51单片机
单片机如何模拟16路PWM
2019-07-15 10:10
发布
×
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮
站内问答
/
51单片机
4792
17
1012
RT。。最近整了个灯的样品在玩,通过逻辑分析仪抓到的16路PWM波形,16路周期都为5ms,其中有6路PWM是持续输出,剩下的10路间断输出,每一路的占空比都在3个周期后改变。观察到的占空比无任何规律,求解应如何做才比较好。。
友情提示:
此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
17条回答
wulinwl
2019-07-17 09:04
saiyqx 发表于 2017-7-25 22:05
额..8位机..不是32位机。。
这是STC-ISP提供的例程,使用定时器0做16路软件PWM
/*---------------------------------------------------------------------*/
/* --- STC MCU Limited ------------------------------------------------*/
/* --- STC 1T Series MCU Demo Programme -------------------------------*/
/* --- Mobile: (86)13922805190 ----------------------------------------*/
/* --- Fax: 86-0513-55012956,55012947,55012969 -----------------------*/
/* --- Tel: 86-0513-55012928,55012929,55012966 ------------------------*/
/* --- Web: www.STCMCU.com --------------------------------------------*/
/* --- Web: www.GXWMCU.com --------------------------------------------*/
/* --- QQ: 800003751 -------------------------------------------------*/
/* 如果要在程序中使用此代码,请在程序中注明使用了STC的资料及程序 */
/*---------------------------------------------------------------------*/
/************* 功能说明 **************
使用Timer0模拟16通道PWM驱动程序。
输出为 P1.0 ~ P1.7, P2.0 ~ P2.7, 对应 PWM0 ~ PWM15.
定时器中断频率一般不要超过100KHZ, 留足够的时间给别的程序运行.
本例子使用11.0592MHZ时钟, 25K的中断频率, 250级PWM, 周期为10ms.
中断里处理的时间不超过6us, 占CPU时间大约为15%.
******************************************/
#include <reg52.h>
#define MAIN_Fosc 11059200UL //定义主时钟
#define Timer0_Rate 25000 //中断频率
typedef unsigned char u8;
typedef unsigned int u16;
typedef unsigned long u32;
sfr AUXR = 0x8E;
sfr P1M1 = 0x91; //PxM1.n,PxM0.n =00--->Standard, 01--->push-pull
sfr P1M0 = 0x92; // =10--->pure input, 11--->open drain
sfr P0M1 = 0x93;
sfr P0M0 = 0x94;
sfr P2M1 = 0x95;
sfr P2M0 = 0x96;
sfr P3M1 = 0xB1;
sfr P3M0 = 0xB2;
sfr P4M1 = 0xB3;
sfr P4M0 = 0xB4;
sfr P5M1 = 0xC9;
sfr P5M0 = 0xCA;
sfr P6M1 = 0xCB;
sfr P6M0 = 0xCC;
sfr P7M1 = 0xE1;
sfr P7M0 = 0xE2;
#define Timer0_Reload (65536UL -(MAIN_Fosc / Timer0_Rate)) //Timer 0 重装值
//************** PWM8 变量和常量以及IO口定义 ***************
//******************** 8通道8 bit 软PWM ********************
#define PWM_DUTY_MAX 250 // 0~255 PWM周期, 最大255
#define PWM_ON 1 // 定义占空比的电平, 1 或 0
#define PWM_OFF (!PWM_ON)
#define PWM_ALL_ON (0xff * PWM_ON)
u8 bdata PWM_temp1,PWM_temp2; //影射一个RAM,可位寻址,输出时同步刷新
sbit P_PWM0 = PWM_temp1^0; // 定义影射RAM每位对应的IO
sbit P_PWM1 = PWM_temp1^1;
sbit P_PWM2 = PWM_temp1^2;
sbit P_PWM3 = PWM_temp1^3;
sbit P_PWM4 = PWM_temp1^4;
sbit P_PWM5 = PWM_temp1^5;
sbit P_PWM6 = PWM_temp1^6;
sbit P_PWM7 = PWM_temp1^7;
sbit P_PWM8 = PWM_temp2^0;
sbit P_PWM9 = PWM_temp2^1;
sbit P_PWM10 = PWM_temp2^2;
sbit P_PWM11 = PWM_temp2^3;
sbit P_PWM12 = PWM_temp2^4;
sbit P_PWM13 = PWM_temp2^5;
sbit P_PWM14 = PWM_temp2^6;
sbit P_PWM15 = PWM_temp2^7;
u8 pwm_duty; //周期计数值
u8 pwm[16]; //pwm0~pwm15 为0至15路PWM的宽度值
bit B_1ms;
u8 cnt_1ms;
u8 cnt_20ms;
/**********************************************/
void main(void)
{
u8 i;
P0M0 = 0x00;
P0M1 = 0x00;
P1M0 = 0x00;
P1M1 = 0x00;
P2M0 = 0x00;
P2M1 = 0x00;
P3M0 = 0x00;
P3M1 = 0x00;
P4M0 = 0x00;
P4M1 = 0x00;
P5M0 = 0x00;
P5M1 = 0x00;
P6M0 = 0x00;
P6M1 = 0x00;
P7M0 = 0x00;
P7M1 = 0x00;
AUXR |= (1<<7); // Timer0 set as 1T mode
TMOD &= ~(1<<2); // Timer0 set as Timer
TMOD &= ~0x03; // Timer0 set as 16 bits Auto Reload
TH0 = Timer0_Reload / 256; //Timer0 Load
TL0 = Timer0_Reload % 256;
ET0 = 1; //Timer0 Interrupt Enable
PT0 = 1; //高优先级
TR0 = 1; //Timer0 Run
EA = 1; //打开总中断
cnt_1ms = Timer0_Rate / 1000; //1ms计数
cnt_20ms = 20;
for(i=0; i<16; i++) pwm
= i * 15 + 15; //给PWM一个初值
while(1)
{
if(B_1ms) //1ms到
{
B_1ms = 0;
if(--cnt_20ms == 0) //PWM 20ms改变一阶
{
cnt_20ms = 20;
for(i=0; i<16; i++) pwm
++;
}
}
}
}
/********************** Timer0 1ms中断函数 ************************/
void timer0 (void) interrupt 1
{
P1 = PWM_temp1; //影射RAM输出到实际的PWM端口
P2 = PWM_temp2;
if(++pwm_duty == PWM_DUTY_MAX) //PWM周期结束,重新开始新的周期
{
pwm_duty = 0;
PWM_temp1 = PWM_ALL_ON;
PWM_temp2 = PWM_ALL_ON;
}
ACC = pwm_duty;
if(ACC == pwm[0]) P_PWM0 = PWM_OFF; //判断PWM占空比是否结束
if(ACC == pwm[1]) P_PWM1 = PWM_OFF;
if(ACC == pwm[2]) P_PWM2 = PWM_OFF;
if(ACC == pwm[3]) P_PWM3 = PWM_OFF;
if(ACC == pwm[4]) P_PWM4 = PWM_OFF;
if(ACC == pwm[5]) P_PWM5 = PWM_OFF;
if(ACC == pwm[6]) P_PWM6 = PWM_OFF;
if(ACC == pwm[7]) P_PWM7 = PWM_OFF;
if(ACC == pwm[8]) P_PWM8 = PWM_OFF;
if(ACC == pwm[9]) P_PWM9 = PWM_OFF;
if(ACC == pwm[10]) P_PWM10 = PWM_OFF;
if(ACC == pwm[11]) P_PWM11 = PWM_OFF;
if(ACC == pwm[12]) P_PWM12 = PWM_OFF;
if(ACC == pwm[13]) P_PWM13 = PWM_OFF;
if(ACC == pwm[14]) P_PWM14 = PWM_OFF;
if(ACC == pwm[15]) P_PWM15 = PWM_OFF;
if(--cnt_1ms == 0)
{
cnt_1ms = Timer0_Rate / 1000;
B_1ms = 1; // 1ms标志
}
}
评分
参与人数 1
积分
+2
收起
理由
elecfansor
+ 2 您的帖子很精彩,期待您分享的下一个帖子!.
查看全部评分
加载中...
查看其它17个回答
一周热门
更多
>
相关问题
【东软载波ESF0654 PDS开发板活动】开箱
1 个回答
东软载波ESF0654 PDS开发板外部中断
1 个回答
东软载波ESF0654 PDS开发板高级控制定时器AD16C4T
1 个回答
用串口调试助手为什么只能在hex模式接收发送而在文本模式不行
9 个回答
触摸芯片SC02B/SC04B在地砖灯的设计方案
1 个回答
相关文章
51单片机与蓝牙模块连接
0个评论
51单片机的硬件结构
0个评论
基于51单片机的无线遥控器制作
0个评论
51单片机 AD转换
0个评论
51单片机数码管递增显示
0个评论
如何实现对单片机寄存器的访问
0个评论
基于51单片机的指纹密码锁
0个评论
×
关闭
采纳回答
向帮助了您的知道网友说句感谢的话吧!
非常感谢!
确 认
×
关闭
编辑标签
最多设置5个标签!
51单片机
保存
关闭
×
关闭
举报内容
检举类型
检举内容
检举用户
检举原因
广告推广
恶意灌水
回答内容与提问无关
抄袭答案
其他
检举说明(必填)
提交
关闭
×
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮
×
付费偷看金额在0.1-10元之间
确定
×
关闭
您已邀请
0
人回答
查看邀请
擅长该话题的人
回答过该话题的人
我关注的人
这是STC-ISP提供的例程,使用定时器0做16路软件PWM
/*---------------------------------------------------------------------*/
/* --- STC MCU Limited ------------------------------------------------*/
/* --- STC 1T Series MCU Demo Programme -------------------------------*/
/* --- Mobile: (86)13922805190 ----------------------------------------*/
/* --- Fax: 86-0513-55012956,55012947,55012969 -----------------------*/
/* --- Tel: 86-0513-55012928,55012929,55012966 ------------------------*/
/* --- Web: www.STCMCU.com --------------------------------------------*/
/* --- Web: www.GXWMCU.com --------------------------------------------*/
/* --- QQ: 800003751 -------------------------------------------------*/
/* 如果要在程序中使用此代码,请在程序中注明使用了STC的资料及程序 */
/*---------------------------------------------------------------------*/
/************* 功能说明 **************
使用Timer0模拟16通道PWM驱动程序。
输出为 P1.0 ~ P1.7, P2.0 ~ P2.7, 对应 PWM0 ~ PWM15.
定时器中断频率一般不要超过100KHZ, 留足够的时间给别的程序运行.
本例子使用11.0592MHZ时钟, 25K的中断频率, 250级PWM, 周期为10ms.
中断里处理的时间不超过6us, 占CPU时间大约为15%.
******************************************/
#include <reg52.h>
#define MAIN_Fosc 11059200UL //定义主时钟
#define Timer0_Rate 25000 //中断频率
typedef unsigned char u8;
typedef unsigned int u16;
typedef unsigned long u32;
sfr AUXR = 0x8E;
sfr P1M1 = 0x91; //PxM1.n,PxM0.n =00--->Standard, 01--->push-pull
sfr P1M0 = 0x92; // =10--->pure input, 11--->open drain
sfr P0M1 = 0x93;
sfr P0M0 = 0x94;
sfr P2M1 = 0x95;
sfr P2M0 = 0x96;
sfr P3M1 = 0xB1;
sfr P3M0 = 0xB2;
sfr P4M1 = 0xB3;
sfr P4M0 = 0xB4;
sfr P5M1 = 0xC9;
sfr P5M0 = 0xCA;
sfr P6M1 = 0xCB;
sfr P6M0 = 0xCC;
sfr P7M1 = 0xE1;
sfr P7M0 = 0xE2;
#define Timer0_Reload (65536UL -(MAIN_Fosc / Timer0_Rate)) //Timer 0 重装值
//************** PWM8 变量和常量以及IO口定义 ***************
//******************** 8通道8 bit 软PWM ********************
#define PWM_DUTY_MAX 250 // 0~255 PWM周期, 最大255
#define PWM_ON 1 // 定义占空比的电平, 1 或 0
#define PWM_OFF (!PWM_ON)
#define PWM_ALL_ON (0xff * PWM_ON)
u8 bdata PWM_temp1,PWM_temp2; //影射一个RAM,可位寻址,输出时同步刷新
sbit P_PWM0 = PWM_temp1^0; // 定义影射RAM每位对应的IO
sbit P_PWM1 = PWM_temp1^1;
sbit P_PWM2 = PWM_temp1^2;
sbit P_PWM3 = PWM_temp1^3;
sbit P_PWM4 = PWM_temp1^4;
sbit P_PWM5 = PWM_temp1^5;
sbit P_PWM6 = PWM_temp1^6;
sbit P_PWM7 = PWM_temp1^7;
sbit P_PWM8 = PWM_temp2^0;
sbit P_PWM9 = PWM_temp2^1;
sbit P_PWM10 = PWM_temp2^2;
sbit P_PWM11 = PWM_temp2^3;
sbit P_PWM12 = PWM_temp2^4;
sbit P_PWM13 = PWM_temp2^5;
sbit P_PWM14 = PWM_temp2^6;
sbit P_PWM15 = PWM_temp2^7;
u8 pwm_duty; //周期计数值
u8 pwm[16]; //pwm0~pwm15 为0至15路PWM的宽度值
bit B_1ms;
u8 cnt_1ms;
u8 cnt_20ms;
/**********************************************/
void main(void)
{
u8 i;
P0M0 = 0x00;
P0M1 = 0x00;
P1M0 = 0x00;
P1M1 = 0x00;
P2M0 = 0x00;
P2M1 = 0x00;
P3M0 = 0x00;
P3M1 = 0x00;
P4M0 = 0x00;
P4M1 = 0x00;
P5M0 = 0x00;
P5M1 = 0x00;
P6M0 = 0x00;
P6M1 = 0x00;
P7M0 = 0x00;
P7M1 = 0x00;
AUXR |= (1<<7); // Timer0 set as 1T mode
TMOD &= ~(1<<2); // Timer0 set as Timer
TMOD &= ~0x03; // Timer0 set as 16 bits Auto Reload
TH0 = Timer0_Reload / 256; //Timer0 Load
TL0 = Timer0_Reload % 256;
ET0 = 1; //Timer0 Interrupt Enable
PT0 = 1; //高优先级
TR0 = 1; //Timer0 Run
EA = 1; //打开总中断
cnt_1ms = Timer0_Rate / 1000; //1ms计数
cnt_20ms = 20;
for(i=0; i<16; i++) pwm = i * 15 + 15; //给PWM一个初值
while(1)
{
if(B_1ms) //1ms到
{
B_1ms = 0;
if(--cnt_20ms == 0) //PWM 20ms改变一阶
{
cnt_20ms = 20;
for(i=0; i<16; i++) pwm++;
}
}
}
}
/********************** Timer0 1ms中断函数 ************************/
void timer0 (void) interrupt 1
{
P1 = PWM_temp1; //影射RAM输出到实际的PWM端口
P2 = PWM_temp2;
if(++pwm_duty == PWM_DUTY_MAX) //PWM周期结束,重新开始新的周期
{
pwm_duty = 0;
PWM_temp1 = PWM_ALL_ON;
PWM_temp2 = PWM_ALL_ON;
}
ACC = pwm_duty;
if(ACC == pwm[0]) P_PWM0 = PWM_OFF; //判断PWM占空比是否结束
if(ACC == pwm[1]) P_PWM1 = PWM_OFF;
if(ACC == pwm[2]) P_PWM2 = PWM_OFF;
if(ACC == pwm[3]) P_PWM3 = PWM_OFF;
if(ACC == pwm[4]) P_PWM4 = PWM_OFF;
if(ACC == pwm[5]) P_PWM5 = PWM_OFF;
if(ACC == pwm[6]) P_PWM6 = PWM_OFF;
if(ACC == pwm[7]) P_PWM7 = PWM_OFF;
if(ACC == pwm[8]) P_PWM8 = PWM_OFF;
if(ACC == pwm[9]) P_PWM9 = PWM_OFF;
if(ACC == pwm[10]) P_PWM10 = PWM_OFF;
if(ACC == pwm[11]) P_PWM11 = PWM_OFF;
if(ACC == pwm[12]) P_PWM12 = PWM_OFF;
if(ACC == pwm[13]) P_PWM13 = PWM_OFF;
if(ACC == pwm[14]) P_PWM14 = PWM_OFF;
if(ACC == pwm[15]) P_PWM15 = PWM_OFF;
if(--cnt_1ms == 0)
{
cnt_1ms = Timer0_Rate / 1000;
B_1ms = 1; // 1ms标志
}
}
评分
查看全部评分
一周热门 更多>