完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
`想用IAP 15W4K58S4芯片做一个PWM输出,占空比为50.8% 频率为13.57KHZ..怎么弄? 下面程序是官方的,有老多地方看不懂,请高手指点下。这个程序可以做到吗? #include #define CYCLE 0X1000L #define PWMC (*(unsigned int volatile xdata*)0xfff0) #define PWMCH (*(unsigned char volatile xdata*)0xfff0) #define PWMCl (*(unsigned char volatile xdata*)0xfff1) #define PWMCKS (*(unsigned char volatile xdata*)0xfff2) #define PWM2T1 (*(unsigned int volatile xdata*)0xff00) #define PWM2T1H (*(unsigned char volatile xdata*)0xff00) #define PWM2T1L (*(unsigned char volatile xdata*)0xff01) #define PWM2T2 (*(unsigned int volatile xdata*)0xff02) #define PWM2T2H (*(unsigned char volatile xdata*)0xff02) #define PWM2T2L (*(unsigned char volatile xdata*)0xff03) #define PWM2CR (*(unsigned char volatile xdata*)0xff04) #define PWM3T1 (*(unsigned int volatile xdata*)0xff10) #define PWM3T1H (*(unsigned char volatile xdata*)0xff10) #define PWM3T1L (*(unsigned char volatile xdata*)0xff11) #define PWM3T2 (*(unsigned int volatile xdata*)0xff12) #define PWM3T2H (*(unsigned char volatile xdata*)0xff12) #define PWM3T2L (*(unsigned char volatile xdata*)0xff13) #define PWM3CR (*(unsigned char volatile xdata*)0xff14) #define PWM4T1 (*(unsigned int volatile xdata*)0xff20) #define PWM4T1H (*(unsigned char volatile xdata*)0xff20) #define PWM4T1L (*(unsigned char volatile xdata*)0xff21) #define PWM4T2 (*(unsigned int volatile xdata*)0xff22) #define PWM4T2H (*(unsigned char volatile xdata*)0xff22) #define PWM4T2L (*(unsigned char volatile xdata*)0xff23) #define PWM4CR (*(unsigned char volatile xdata*)0xff24) #define PWM5T1 (*(unsigned int volatile xdata*)0xff30) #define PWM5T1H (*(unsigned char volatile xdata*)0xff30) #define PWM5T1L (*(unsigned char volatile xdata*)0xff31) #define PWM5T2 (*(unsigned int volatile xdata*)0xff32) #define PWM5T2H (*(unsigned char volatile xdata*)0xff32) #define PWM5T2L (*(unsigned char volatile xdata*)0xff33) #define PWM5CR (*(unsigned char volatile xdata*)0xff34) #define PWM6T1 (*(unsigned int volatile xdata*)0xff40) #define PWM6T1H (*(unsigned char volatile xdata*)0xff40) #define PWM6T1L (*(unsigned char volatile xdata*)0xff41) #define PWM6T2 (*(unsigned int volatile xdata*)0xff42) #define PWM6T2H (*(unsigned char volatile xdata*)0xff42) #define PWM6T2L (*(unsigned char volatile xdata*)0xff43) #define PWM6CR (*(unsigned char volatile xdata*)0xff44) #define PWM7T1 (*(unsigned int volatile xdata*)0xff50) #define PWM7T1H (*(unsigned char volatile xdata*)0xff50) #define PWM7T1L (*(unsigned char volatile xdata*)0xff51) #define PWM7T2 (*(unsigned int volatile xdata*)0xff52) #define PWM7T2H (*(unsigned char volatile xdata*)0xff52) #define PWM7T2L (*(unsigned char volatile xdata*)0xff53) #define PWM7CR (*(unsigned char volatile xdata*)0xff54) sfr P_SW2=0XBA; sfr P0M1=0X93; sfr P0M0=0X94; sfr P1M1=0X91; sfr P1M0=0X92; 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; sfr PWMCFG=0XF1; sfr PWMCR=0XF5; sfr PWMIF=0XF6; sfr PWMFDCR=0XF7; ***it PWM2=P3^7; ***it PWM3=P2^1; ***it PWM4=P2^2; ***it PWM5=P2^3; ***it PWM6=P0^7; ***it PWM7=P0^6; void PWM_config(void); void PWM2_SetPwmWide(unsigned short Wide); void PWM3_SetPwmWide(unsigned short Wide); void PWM4_SetPwmWide(unsigned short Wide); void PWM5_SetPwmWide(unsigned short Wide); void PWM6_SetPwmWide(unsigned short Wide); void PWM7_SetPwmWide(unsigned short Wide); void main() { PWM_config(); PWM2_SetPwmWide(0); PWM3_SetPwmWide(1); PWM4_SetPwmWide(CYCLE); PWM5_SetPwmWide(CYCLE-1); PWM6_SetPwmWide(CYCLE/2); PWM7_SetPwmWide(CYCLE/3); while(1); } void PWM_config(void) { P0M0&=~0XC0; P0M1&=~0XC0; P0&=~0XC0; P2M0&=~0X0E; P2M1&=~0X0E; P2&=~0X0E; P3M0&=~0X80; P3M1&=~0X80; P3&=~0X80; P_SW2|=0X80; PWMCKS=0X00; PWMC=CYCLE; PWM2T1=1; PWM2T2=0; PWM2CR=0X00; PWM3T1=1; PWM3T2=0; PWM3CR=0X00; PWM4T1=1; PWM4T2=0; PWM4CR=0X00; PWM5T1=1; PWM5T2=0; PWM5CR=0X00; PWM6T1=1; PWM6T2=0; PWM6CR=0X08; PWM7T1=1; PWM7T2=0; PWM7CR=0X08; PWMCFG=0X00; PWMCR=0X3F; PWMCR|=0X80; P_SW2&=~0X80; } void PWM2_SetPwmWide(unsigned short Wide) { if(Wide==0) { PWMCR&=~0X01; PWM2=0; } else if(Wide==CYCLE) { PWMCR&=~0X01; PWM2=1; } else { P_SW2|=0X80; PWM2T1=Wide; P_SW2&=~0X80; PWMCR|=0X01; } } 高手帮帮忙!!! ` ![]() |
|
相关推荐
5个回答
|
|
我等着用,顶起
|
|
|
|
完全看不懂!正在学这款单片机
|
|
|
|
//想用IAP 15W4K58S4芯片做一个PWM输出,占空比为50.8% 频率为13.57KHZ..怎么弄?
//假设单片机使用内部RC振荡器,为12MHz,没有进行系统时钟分频 //那么系统时钟sysclk就等于12MHz //根据设置,PWM时钟源就是系统时钟,那么PWM计数一次的时间是:1/12MHz=1/12微秒 //如果PWM波的频率为13.57KHz,那么它的周期是1/13.57KHz=73.692微秒 //73.692/(1/12)=884个PWM计数 //占空比884*50.8%=450 //也就是设置 CYCLE=884; WIDE=450; //就可得到 13.57KHz 占空比50.8% 的PWM波 #include "STC15W4K.H" #define uchar unsigned char #define CYCLE 884 //定义PWM周期(最大值为32767) #define WIDE 450 //定义高电平时间 ***it P2_2=P2^2; //PWM4第一输出 ***it P3_3=P3^3; //普通输入输出管腿 ***it P1_6=P1^6; //PWM6第一输出 void Delay500ms() //@12.000MHz { unsigned char i, j, k; i = 23; j = 205; k = 120; do { do { while (--k); } while (--j); } while (--i); } void port_mode() // 端口模式 将所有端口设置为准双向弱上拉 { P0M1=0x00; P0M0=0x00;P1M1=0x00; P1M0=0x00;P2M1=0x00; P2M0=0x00;P3M1=0x00; P3M0=0x00; P4M1=0x00; P4M0=0x00;P5M1=0x00; P5M0=0x00;P6M1=0x00; P6M0=0x00;P7M1=0x00; P7M0=0x00; } void main() { unsigned char i; //经过设置后,端口工作模式为 准双向弱上拉 //根据实际需要,设置为强推挽也可以输出PWM波,只是一定要串上电阻 port_mode();//将所有端口初始化为准双向弱上拉 for(i=0; i<3; i++) { P1_6=0; Delay500ms(); P1_6=1; Delay500ms(); } //将P_SW2寄存器的最高位置1, //也就是使能访问外部扩展特殊功能寄存器XSFR,而不是访问外部数据寄存器XRAM P_SW2|=0x80; //设置PWM配置寄存器 //其他各位都为1,只有C6INI=0,也就是设置PWM6的初始输出电平为低电平 PWMCFG&=0xEF; //设置PWM计数器的时钟源为 系统时钟分频,而不是定时器T2溢出,设置为系统时钟的0+1=1分频 PWMCKS=0x00; //PWM计数器设置计数值 PWMC=CYCLE; //第一次翻转的计数值 PWM6T1=WIDE; //第二次翻转的计数值 PWM6T2=0; //52 //PWM输出使能为禁止 //PWM计数器归零中断使能为禁止 //PWM6输出使能位位禁止 PWM6CR=0; //由于前面已经设置完XSFR,这里设置为禁止访问XSFR //也就是设置为对外部数据存储器XRAM的访问 P_SW2&=0x0F; //开启总PWM 波形发生器功能,开启PWM4输出,或者说是使能也可以 PWMCR|=0x90; while(1); } //实测大致上符合要求 |
|
|
|
上面的程序有点问题,应为这样就对了
//PWM计数器设置计数值 PWMC=CYCLE; //设置周期总长 //第一次翻转的计数值 PWM6T1=0; //由起始低电平立刻翻转为高电平 //第二次翻转的计数值 PWM6T2=(CYCLE-WIDE); //设置高电平时间 |
|
|
|
抱歉,程序没错,是表笔接反了,哎。。。
|
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
飞凌嵌入式-ELFBOARD 解决PCB布线时无法捕捉到焊盘中心的问题
600 浏览 0 评论
Openharmony软件评估指南-米尔瑞芯微RK3568开发板
876 浏览 0 评论
STM32CUBEMX(7)--移植雅特力AT32F403AVGT(兼容STM32F103VGT6),DAC输出电压
1126 浏览 0 评论
【RA-Eco-RA0E1-32PIN-V1.0开发板试用】开发板简介与上电测试
1095 浏览 0 评论
嵌入式学习-飞凌嵌入式ElfBoard ELF 1板卡-shell脚本编写之传参
620 浏览 0 评论
【youyeetoo X1 windows 开发板体验】少儿AI智能STEAM积木平台
11056 浏览 31 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-9-12 04:20 , Processed in 0.819577 second(s), Total 82, Slave 63 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191