1、PWM控制输出代码#ifndef INCLUDE_H #define INCLUDE_H #include AT89X51.h #include intrins.h #define uchar unsigned char #define uint unsigned int #define _BV(x) (1(x) #define GET_BIT(x,y) (x)&(1(y)=0?0:1) /*取x变量y位的数据*/ #define SET_BIT(x,y) (x)|=(0x0001(y) /*置位x变量y位*/ #define CLR_BIT(x,y) (x)&=(0x0001(y) /*清零
2、x变量y位*/ #define LET_BIT(x,y,z) (x)=(x)&(0x0001(y)|(z) stillTimesMax) stillTimes = 0; #if REPEAT_MODE if(FIRST_TIMES = stillTimesMax) stillTimesMax = OTHER_TIMES; #endif return keyValueTemp; #else stillTimes = 0; return 0; #endif else stillTimes = 0; return 0; return 0; /* *函数名称:get_data_1_count(ucha
3、r number) *函数功能:计算uchar类型变量中的数量,用于检测是否多键按下 *函数入口:number,需要检测数量的变量 *函数出口:返回变量bumber中的数量 */ uchar get_data_1_count(uchar number) register uchar i,j = 0; for(i = 0; i 8; i +) if( (number&_BV(i) ) j +; return j; /* *函数名称:delay_us(uint us) *函数功能:毫秒级延时 us 函数入口:延时计数变量*函数出口:无 */ void delay_us(uint us) uchar
4、 delayi; while(-us) for(delayi=0;delayi= 1)&(NewPwmValue = 99) if/一个周期里 pwmH = NewPwmValue; else /若占空比不在 .99范围内则输出一个尖脉冲 pwmH =0x01; /* *函数名称:pwmProduce(void) *函数功能:定时器溢出中断设置 *函数入口:无 *函数出口:无 */ void pwmProduce(void) interrupt 3 pwmPeriodCont+; /PWM定时周期计数加 if(pwmPeriodCont= PWM_Period)一个频率周期结束 pwmPeri
5、odCont=0; /* *驱动功能:产生pwm,12MHZ晶振下,总周期为*1ms *版本:.0.1 *作者:lhw *时间:.06.11 */ #ifndef PWM_H #define PWM_H #include Include.h #ifndef PWM_GLOBAL #define PWM_EXT extern #else #define PWM_EXT #endif #define PWM_Period 100 /100%占空比时标数 /*端口定义* sbit PWM_OUT=P12; /*pwm输出端口映射*/ /* PWM_EXT uchar pwmPeriodCont; /
6、 PWM 占空比计数器 PWM_EXT uchar pwmH; PWM_EXT void pwm_init(void); /*PWM初始化,设置定时器*/ PWM_EXT void pwm_set(const uchar NewPwmValue); /*PWM占空比设置*/ #endif #include led.h #include key.h #include pwm.h static uchar pwmScale; /pwm占空比,199 void main() uchar keyValue; pwm_init(); /初始化pwm,pwm高电平默认值为 key_init(); led_init(); pwmScale=0x01; /初始化占空比变量,初始值为 sei(); while(1) keyValue=get_key_value(); if(keyValue!=KEY_NULL) if(keyValue=KEY_UP)
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1