大虾们,我想要一个单片机控制LED灯由暗到亮的程序
同求,怎么做?
我操,你们这是典型的伸手党啊
/*
*************************************************************************************************************
* (c) Copyright 2014-, LanWei Company
* All Rights Reserved
* 文件名:main.c
* 描 述:工程主文件已完成
* 功 能:PWM信号控制灯的亮灭程度OK
* 用定时器0 和 定时器1,模拟PWM(Pulse Width Modulation)
* 作者:MADE BY DA HE TAO
* Q Q :597627977
* 版本号:1.0.2(2015.2.17)
*************************************************************************************************************
*/
#include"reg52.h"
/*
*描述:数据类型定义
*/
typedef unsigned char uint8;
typedef unsigned int uint16;
typedef unsigned long uint32;
/*
*描述:位声明
*/
sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
sbit ENLED = P1^4;
sbit IR_INPUT = P3^3;
/*
*描述:全局变量定义
*/
bit irflag = 0;
uint8 IrCode[4];
uint32 PeriodCnt = 0;//周期计数值
uint8 HighRH = 0;
uint8 HighRL = 0;
uint8 LowRH = 0;
uint8 LowRL = 0;
/*
*描述:函数前置声明
*/
//void delay_ms(uint16 n);
void InitInfrared(void);
void ConfigPWM(uint16 frq,uint8 dc);
uint16 GetHighTime(void);
uint16 GetLowTime(void);
void ClosePWM(void);
void IrCodeAction(void);
/*
*描述:主函数
*/
void main(void)
{
EA = 1;
ENLED = 0;
ADDR3 = 1;
ADDR0 = 0;
ADDR1 = 1;
ADDR2 = 1;
InitInfrared();
while(1)
{
if(irflag)
{
irflag = 0;
IrCodeAction();
}
}
}
/*
*描述:延时函数
*/
//void delay_ms(uint16 n)
//{
// uint8 m = 120;
//
// while(n--)
// while(m--);
//}
void IrCodeAction(void)
{
static uint8 a = 0;
if((IrCode[2] == 0x11) && a == 0)
{
ConfigPWM(100,99);
a++;
}
else if((IrCode[2] == 0x11) && a == 1)
{
ConfigPWM(100,50);
a++;
}
else if((IrCode[2] == 0x11) && a == 2)
{
ConfigPWM(100,1);
a++;
}
else if((IrCode[2] == 0x11) && a == 3)
{
ClosePWM();
a = 0;
}
}
/*
*描述:红外初始化函数
*/
void InitInfrared(void)
{
IR_INPUT = 1;
TMOD &= 0X0F;
TMOD |= 0X10;
TR1 = 0;
ET1 = 0;
IT1 = 1;
EX1 = 1;
}
/*
*描述:PWM配置函数
*/
void ConfigPWM(uint16 frq,uint8 dc)
{
uint16 High,Low;
PeriodCnt = (11059200 / 12) / frq;//周期计数次数
High = (PeriodCnt * dc) / 100;//高电平的计数次数
Low = PeriodCnt - High;//低电平的计数次数
HighRH = (65536 - High + 12) / 256;
HighRL = (65536 - High + 12) % 256;
LowRH = (65536 - Low + 12) / 256;
LowRL = (65536 - Low + 12) % 256;
TMOD &= 0XF0;
TMOD |= 0X01;
TH0 = HighRH;
TL0 = HighRL;
TR0 = 1;
ET0 = 1;
P0 = 0XFF;
}
/*
*描述:高电平获取函数
*/
uint16 GetHighTime(void)
{
TH1 = 0;
TL1 = 0;
TR1 = 1;
while(IR_INPUT)
{
if(TH1 >= 0X40)
{
break;
}
}
TR1 = 0;
return (TH1 * 256 + TL1);
}
/*
*描述:低电平获取函数
*/
uint16 GetLowTime(void)
{
TH1 = 0;
TL1 = 0;
TR1 = 1;
while(!IR_INPUT)
{
if(TH1 >= 0X40)
{
break;
}
}
TR1 = 0;
return (TH1 * 256 + TL1);
}
/*
*描述:关闭PWM
*/
void ClosePWM(void)
{
TR0 = 0;
ET0 = 0;
P0 = 0XFF;
}
/*
*描述:外部中断1解码函数
*/
void EXINT1_ISR() interrupt 2
{
uint8 i,j;
uint8 byt;
uint16 time;
time = GetLowTime();
if((time < 7833) || (time > 8755))
{
IE1 = 0;
return;
}
time = GetHighTime();
if((time < 3866)||(time > 4608))
{
IE1 = 0;
return;
}
for(i = 0;i < 4; i++)
{
for(j = 0; j < 8; j++)
{
time = GetLowTime();
if((time < 313) || (time > 718))
{
IE1 = 0;
return;
}
time = GetHighTime();
if((time > 313) && (time < 718))/////
{
byt >>= 1;
}
else if((time > 1345) && (time < 1751))
{
byt >>= 1;
byt |= 0x80;
}
else
{
IE1 = 0;
return;
}
}
IrCode[i] = byt;
}
irflag = 1;
IE1 = 0;
}
/*
*描述:定时器0中断
*/
void TIM0_IRQ_Handler() interrupt 1
{
if(P0 == 0XFF)
{
TH0 = LowRH;
TL0 = LowRL;
P0 = 0XC0;
}
else if(P0 == 0XC0)
{
TH0 = HighRH;
TL0 = HighRL;
P0 = 0XFF;
}
}
/******************* (C) COPYRIGHT 2015 蓝为电子科技 *****END OF FILE****/
/*
*************************************************************************************************************
* (c) Copyright 2014-, LanWei Company
* All Rights Reserved
* 文件名:main.c
* 描 述:工程主文件已完成
* 功 能:PWM信号控制灯的亮灭程度OK
* 用定时器0 和 定时器1,模拟PWM(Pulse Width Modulation)
* 作者:MADE BY DA HE TAO
* Q Q :597627977
* 版本号:1.0.2(2015.2.17)
*************************************************************************************************************
*/
#include"reg52.h"
/*
*描述:数据类型定义
*/
typedef unsigned char uint8;
typedef unsigned int uint16;
typedef unsigned long uint32;
/*
*描述:位声明
*/
sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
sbit ENLED = P1^4;
sbit IR_INPUT = P3^3;
/*
*描述:全局变量定义
*/
bit irflag = 0;
uint8 IrCode[4];
uint32 PeriodCnt = 0;//周期计数值
uint8 HighRH = 0;
uint8 HighRL = 0;
uint8 LowRH = 0;
uint8 LowRL = 0;
/*
*描述:函数前置声明
*/
//void delay_ms(uint16 n);
void InitInfrared(void);
void ConfigPWM(uint16 frq,uint8 dc);
uint16 GetHighTime(void);
uint16 GetLowTime(void);
void ClosePWM(void);
void IrCodeAction(void);
/*
*描述:主函数
*/
void main(void)
{
EA = 1;
ENLED = 0;
ADDR3 = 1;
ADDR0 = 0;
ADDR1 = 1;
ADDR2 = 1;
InitInfrared();
while(1)
{
if(irflag)
{
irflag = 0;
IrCodeAction();
}
}
}
/*
*描述:延时函数
*/
//void delay_ms(uint16 n)
//{
// uint8 m = 120;
//
// while(n--)
// while(m--);
//}
void IrCodeAction(void)
{
static uint8 a = 0;
if((IrCode[2] == 0x11) && a == 0)
{
ConfigPWM(100,99);
a++;
}
else if((IrCode[2] == 0x11) && a == 1)
{
ConfigPWM(100,50);
a++;
}
else if((IrCode[2] == 0x11) && a == 2)
{
ConfigPWM(100,1);
a++;
}
else if((IrCode[2] == 0x11) && a == 3)
{
ClosePWM();
a = 0;
}
}
/*
*描述:红外初始化函数
*/
void InitInfrared(void)
{
IR_INPUT = 1;
TMOD &= 0X0F;
TMOD |= 0X10;
TR1 = 0;
ET1 = 0;
IT1 = 1;
EX1 = 1;
}
/*
*描述:PWM配置函数
*/
void ConfigPWM(uint16 frq,uint8 dc)
{
uint16 High,Low;
PeriodCnt = (11059200 / 12) / frq;//周期计数次数
High = (PeriodCnt * dc) / 100;//高电平的计数次数
Low = PeriodCnt - High;//低电平的计数次数
HighRH = (65536 - High + 12) / 256;
HighRL = (65536 - High + 12) % 256;
LowRH = (65536 - Low + 12) / 256;
LowRL = (65536 - Low + 12) % 256;
TMOD &= 0XF0;
TMOD |= 0X01;
TH0 = HighRH;
TL0 = HighRL;
TR0 = 1;
ET0 = 1;
P0 = 0XFF;
}
/*
*描述:高电平获取函数
*/
uint16 GetHighTime(void)
{
TH1 = 0;
TL1 = 0;
TR1 = 1;
while(IR_INPUT)
{
if(TH1 >= 0X40)
{
break;
}
}
TR1 = 0;
return (TH1 * 256 + TL1);
}
/*
*描述:低电平获取函数
*/
uint16 GetLowTime(void)
{
TH1 = 0;
TL1 = 0;
TR1 = 1;
while(!IR_INPUT)
{
if(TH1 >= 0X40)
{
break;
}
}
TR1 = 0;
return (TH1 * 256 + TL1);
}
/*
*描述:关闭PWM
*/
void ClosePWM(void)
{
TR0 = 0;
ET0 = 0;
P0 = 0XFF;
}
/*
*描述:外部中断1解码函数
*/
void EXINT1_ISR() interrupt 2
{
uint8 i,j;
uint8 byt;
uint16 time;
time = GetLowTime();
if((time < 7833) || (time > 8755))
{
IE1 = 0;
return;
}
time = GetHighTime();
if((time < 3866)||(time > 4608))
{
IE1 = 0;
return;
}
for(i = 0;i < 4; i++)
{
for(j = 0; j < 8; j++)
{
time = GetLowTime();
if((time < 313) || (time > 718))
{
IE1 = 0;
return;
}
time = GetHighTime();
if((time > 313) && (time < 718))/////
{
byt >>= 1;
}
else if((time > 1345) && (time < 1751))
{
byt >>= 1;
byt |= 0x80;
}
else
{
IE1 = 0;
return;
}
}
IrCode[i] = byt;
}
irflag = 1;
IE1 = 0;
}
/*
*描述:定时器0中断
*/
void TIM0_IRQ_Handler() interrupt 1
{
if(P0 == 0XFF)
{
TH0 = LowRH;
TL0 = LowRL;
P0 = 0XC0;
}
else if(P0 == 0XC0)
{
TH0 = HighRH;
TL0 = HighRL;
P0 = 0XFF;
}
}
/******************* (C) COPYRIGHT 2015 蓝为电子科技 *****END OF FILE****/
PWM定频调宽,大体思路是可以实现,具体程序建议自己写写。