#define uchar unsigned char
#define uint unsigned int
sbit s2=P3^7
sbit s3=P3^6
sbit fm=P2^3
uchar pwm,num
void delay(uint ms)
{
uchar i
while(ms--)
{
for(i=0i<120i++)
}
}
void keyscan()
{
if(s2==0)
{
delay(80)
if(s2==0)
{
if(pwm<32)
{
pwm++
delay(150)
}
else //如果禅纳冲大于32 蜂鸣器想茄桐
{
bee( )
}
}
}
if(s3==0)
{
delay(80)
if(s3==0)
{
if(pwm>1)
{
pwm--
delay(50)
}
else //如贺歼果小于1 蜂鸣器想
{
bee( )
}
}
}
}
void init_t0( )
{
TMOD=0x01
TH0=(65536-500)/256 //500ms
TL0=(65536-500)%256
EA=1
ET0=1
TR0=1
}
void t0( ) interrupt 1
{
TR0=0//关计时器
TH0=(65536-500)/256
TL0=(65536-500)%256
num++
if(num>32)
{
num=0
}
if(num<=pwm )
{
P1=0xff
}
else
{
P1=0x00
}
TR0=1
}
void main( )
{
init_t0( )
num=0
pwm=0
while(1)
{
keyscan()
}
}
下面这个程序是应用在stc上的pwm程序,测试成功#includesfr CCON =0xD8 //PCA control register
sfr CMOD =0xD9
sfr CL =0xe9
sfr CH =0xF9
sfr CCAP0L=0xea
sfr CCAP1L=0xeb
sfr CCAP0H=0xfa
sfr CCAP1H=0xfb
sfr CCAPM0=0xda
sfr CCAPM1=0xdb
sbit CR=0xde//位寻址
void main(void)
{
CMOD=0x02 //使用系统时钟,时钟输入源中李返频率fosc/2。PWM频率=fosc/2/256
/************************************
7 6 5 4 3 21 0
CIDL- - - cps2CPS1 CPS0 ECF
CIDL: =0时,空闲模式下PCA计数器继续工作;=1时,空闲模式下PCA计数卖饥器停止工作。
CPS2,CPS1,CPS0:PCA计数脉冲选择。010,定时器0的溢出,可以实现可调频率的PWM输出。
ECF: =1时,使能寄存器CCON CF位的中断;=0时,禁止该功能。
***************************************/
CL=0x00
CH=0x00
CCAP0L=0xc0//当CL小于CCAP0L时,输出为低大于时,输出为高。
CCAP0H=0x7f//当CL由ff变为00溢出时,CCAP0H装载到CCAP0L中,实现无干扰的更新PWM。
CCAP1L=0xc0//当CL小于CCAP1L时,输出为低大于时,输出为高。
CCAP1H=0xc0//当CL由ff变为00溢出时,CCAP1H装载到CCAP1L中,实现扰告无干扰的更新PWM。
CCAPM0=0X42//使能PWM模式,PWM0=1,ECOM0=1
CCAPM1=0X42//使能PWM模式,PWM1=1,ECOM1=1
CR=1 //启动PCA计数器
while(1)
{
CCAP0H=RF4432_TxRxBuf[4] //此处改变占空比
CCAP1H=RF4432_TxRxBuf[5]
CCAP2H=RF4432_TxRxBuf[6]/2.5
}
}
#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()}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)