如何利用定时器产生PWM信号?其c程序是怎样的?

如何利用定时器产生PWM信号?其c程序是怎样的?,第1张

/*******************************************************************

模块名称:PWM.c

功能:可调PWM波

说明:按键AN1增加占空比,按键AN2减小占空比,P0^2口输出PWM波

*********************************************************************/

#include <reg52.h >

#define uchar unsigned char

#define uint unsigned int

uint T_1 = 10000 //定时器1初值变量

uchar flag //按键返回变量

sbit PWM = P0^2 //PWM输出口

sbit AN1 = P0^0 //增加占空比

sbit AN2 = P0^1 //减小占空比

//调节占空比函数

/********************************************************************

函 数 名:timer_init()

功能:定时器初始化函数

说明:无

入口参数:无

返 回 值:无

********************************************************************/

void timer_init()

{

TMOD = 0x11 //定时器0、定时器1工作于模式1

EA = 1 //开总中断

ET0 = 1 //定时中断0允许

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

TH0 = (65536-20000)/256 //定时器0装初值:20ms

TL0 = (65536-20000)%256

TH1 = (65536-T_1)/256 //定时器1装初值:10ms

TL1 = (65536-T_1)%256

TR0 = 1 //开启定时中断0

}

/********************************************************************

函 数 名:timer0()

功能:定时中断0中断服务函数

说明:产生高电平,中断固定时间20ms,频率50HZ

入口参数:无

返 回 值:无

********************************************************************/

void timer0() interrupt 1

{

TR0 = 0 //关闭定时中断0

PWM = 1 //脉冲高电平

TH0 = (65536-20000)/256 //定时器0装初值:20ms

TL0 = (65536-20000)%256

TR1 = 1 //开启定时中断1

TR0 = 1 //开启定时中断0

}

/********************************************************************

函 数 名:timer1()

功能:定时中断1中断服务函数

说明:产生低电平,中断时间可变

入口参数:无

返 回 值:无

********************************************************************/

void timer1() interrupt 3

{

TR1 = 0 //关闭定时中断1

PWM = 0 //脉冲低电平

TH1 = (65536-T_1)/256 //定时器1装初值:10ms

TL1 = (65536-T_1)%256

}

/********************************************************************

函 数 名:keyscan()

功能:键盘扫描函数

说明:无

入口参数:无

返 回 值:无

********************************************************************/

uchar keyscan(void)

{

if(AN1==0) //AN1按下

{

while(AN1==0) //AN1松开

return 1 //返回 1

}

if(AN2==0) //AN2按下

{

while(AN2==0) //AN2松开

return 2 //返回 2

}

else return 0 //返回 0

}

/********************************************************************

函 数 名:change()

功能:调节占空比函数

说明:无

入口参数:无

返 回 值:无

********************************************************************/

void change(void)

{

flag = keyscan() //按键返回值保存

if(flag==1) //按键返回值为1,即AN1按下

{

if(T_1>=65435) //定时器初值变量大于等于65525

{

T_1 = 65530 //定时器初值变量等于65535

}

if(T_1<65525) //定时器初值变量小于65525

{

T_1 = T_1+100 //定时器初值变量加10

}

}

if(flag==2) //按键返回值为2,即AN2按下

{

if(T_1<=100) //定时器初值变量小于等于10

{

T_1 = 5 //定时器初值变量等于0

}

if(T_1>100) //定时器初值变量大于10

{

T_1 = T_1-100 //定时器初值变量减10

}

}

}

/********************************************************************

函 数 名:main()

功能:主函数

说明:无

入口参数:无

返 回 值:无

***********************************************************************/

void main()

{

timer_init() //定时器初始化

while(1)

{

change() //改变占空比

}

}

你用Protues软件的示波器按照程序引脚图连一下,就是一个PWM波了,且占空比可调

#include <intrins.h>

#include <stdio.h>

#include <math.h>

#include "UPSD3200.H"

#include "upsd_pwm.h"

#define uint unsigned int

#define uchar unsigned char

PSD_REGS PSD8xx_reg _at_ csiop

#define KEYIO (~(PSD8xx_reg.DATAIN_A)) &0x07

sbit CP=P4^7

sbit U_D=P1^1

void init_system()// 系统初始化

uchar keyboard()

void uPSD_PWM4_Variable(unsigned char PWM_Period, unsigned char PWM_PulseWidth)

uchar Adjust_station=NO

unsigned char data x,flag

unsigned char keyboard() //键盘输入

{

unsigned char a=0xff,b=0x01,c=0xf8,d//有按键的位置

uint i

do{

a=KEYIO|c

}while(a==0xff)

if (a == 0xfe){d = '1'}

else if (a == 0xfd){d = '2'}

else if (a == 0xfb){d = '3'}

else {d = '0'}

return(d)

}

void uPSD_PWM4_Init(void)

unsigned int PWM_prescaler

P4SFS |= 0x80

PWM_prescaler = (unsigned int)((FREQ_OSC/2)/PWM4_INPUT_freq)//PWM4输入脉冲频率 K

PSCL1L = PWM_prescaler &0x00ff

PSCL1H = (PWM_prescaler >>8)

}

void StepMotor_GO(bit direction,uint speed) {

unsigned char PWM_Period, PWM_PulseWidth

uint temp_uint

U_D = direction//方向

temp_uint = (PWM4_INPUT_freq*1000)/speed

PWM_Period = (unsigned char)temp_uint

PWM_PulseWidth = PWM_Period/2

uPSD_PWM4_Variable(PWM_Period,PWM_PulseWidth)

}

void StepMotor_STOP(void)

{

P4SFS &= 0x7f

}

void uPSD_PWM4_Variable(unsigned char PWM_Period, unsigned char PWM_PulseWidth)

{

P4SFS |= 0x80

PWMVP = PWM_Period

PWMVPW = PWM_PulseWidth

PWMCON |= 0x20

}

void initkey(void){

PSD8xx_reg.DATAOUT_A=0xFF

PSD8xx_reg.CONTROL_A=0x00

PSD8xx_reg.DIRECTION_A=0x00

}

void init_system() // 系统初始化

{

WDKEY=0x55

P4SFS = (unsigned char) (0x08 <<3)

uPSD_PWM_Channel_8bit(3,243)

问题补充:(接着上面的序)

void uPSD_PWM_Init_8bit(unsigned char PWM_channel_no, unsigned int PWM_freq8, unsigned char PWMCON_value)

{

unsigned int PWM_prescaler

unsigned char output_polarity_mode

output_polarity_mode = PWMCON_value &0x8F

PWMCON = (PWMCON &0x70) | output_polarity_mode

P4SFS = (unsigned char) (0x08 <<PWM_channel_no)

PWM_prescaler = (unsigned int) ( (((unsigned int) FREQ_OSC) / 2) / PWM_freq8)

PSCL0L = PWM_prescaler &0x00ff

PSCL0H = (PWM_prescaler >>8)

PSCL1L = PWM_prescaler &0x00ff

PSCL1H = (PWM_prescaler >>8)

}

void uPSD_PWM_Disable(void)

{

PWMCON &= 0xDF

}

void main(void){

x=0

init_system()

flag=1

while(flag==1){

x=keyboard()

if(x=='1') {StepMotor_GO(0,100)}

else if(x=='2') {StepMotor_GO(1,500)}

else if(x=='3') { StepMotor_STOP()uPSD_PWM_Disable()}

else if(x=='0') {uPSD_PWM_Disable()}

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11745431.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-18
下一篇 2023-05-18

发表评论

登录后才能评论

评论列表(0条)

保存