当前位置:网站首页>Prepare for the Blue Bridge Cup Day10__ PWM control light brightness
Prepare for the Blue Bridge Cup Day10__ PWM control light brightness
2022-07-01 11:42:00 【The God of C language】
One ,PWM
PWM Pulse width modulation , In this topic, the brightness of the lamp is controlled by controlling the duty cycle , Such as title PWM One cycle of pulse width signal is 10000us, When the Blue Bridge Cup development platform is at low level LED Light up , So we can control the brightness of the lamp by adjusting the time occupied by the low level in a cycle , The essence is that the low level is on , High level off , But because of the short period , Cannot be recognized by human eyes , What the human eye sees is only the change of brightness .、
Two , Code section
#include <reg52.h>
#include <intrins.h>
sbit L1=P0^0;
sbit S7=P3^0;
void Inisystem()
{
P2=(P2&0x1f)|0xa0;
P0=0x00;
P2=(P2&0x1f)|0x80;
P0=0xff;
}
void Delay100ms() //@12.000MHz
{
unsigned char i, j, k;
_nop_();
_nop_();
i = 5;
j = 144;
k = 71;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void HC138()
{
P2=(P2&0x1f)|0x80;
}
unsigned char PWM=0;
unsigned char state=0;
void S7Work()
{
if(S7==0)
{
Delay100ms();
if(S7==0)
{
while(S7==0);
switch(state)
{
case 0:
L1=0;
TR0=1;
PWM=10;
state=1;
break;
case 1:
PWM=50;
state=2;
break;
case 2:
PWM=90;
state=3;
break;
case 3:
L1=1;
TR0=0;
state=0;
break;
}
}
}
}
void Initimer()
{
TMOD=0x01;
TH0=(65535-100)/256;
TL0=(65535-100)%256;
ET0=1;
EA=1;
}
unsigned char count=0;
void Servicetimer() interrupt 1
{
TH0=(65535-100)/256;
TL0=(65535-100)%256;
count++;
if(count==PWM)
{
L1=1;
}
else if(count==100)
{
L1=0;
count=0;
}
}
void main()
{ Inisystem();
HC138();
Initimer();
while(1)
{
S7Work();
}
}
From the title we can see PWM One cycle of pulse width signal is 10000us, So we pass the timer , Divide it into 100 Share , So timing 100us, Every time 100us Execute the timer interrupt service function once , This time in initializing the timer did not make TR0=1, Because I haven't pressed S7 No counting required , etc. S7 Press and turn on . Timer interrupt service function here implements a 100 Periodic , When count When we reach the desired duty cycle , Give Way L1 Extinguish , So we need to make sure to press S7 when ,L1 It's on . Finally, when count=100 That is, the end of a cycle , Give Way L1 Light up again , And count=0 Release . The next step is 、S7 Working function of , First, we need to deal with the shake elimination with a key , ad locum Delay The time of function is more exquisite , The later light brightness display function is normal , But there is a problem when switching , For the most part Delay The function did not choose a correct time , And notice while(S7==0) The location of . Then we use state This state variable and switch To achieve , It's easier , Details are the first press S7 Remember to make the timer work, that is TR0=1, Last case3 Remember to turn off the timer and release state.
3、 ... and , summary
I'm too lazy in recent days , It's time for pressure .
边栏推荐
猜你喜欢
Chen Gong: Micro service, is it still so pure?
Exploration and practice of inress in kubernetes
Wonderful! MarkBERT
Brief analysis of edgedb architecture
ABBIRB120工业机器人机械零点位置
Explore the contour detection function findcontours() of OpenCV in detail with practical examples, and thoroughly understand the real role and meaning of each parameter and mode
2022/6/29学习总结
Introduction to unittest framework and the first demo
邻接矩阵无向图(一) - 基本概念与C语言
Istio, ebpf and rsocket Broker: in depth study of service grid
随机推荐
ABBIRB120工业机器人机械零点位置
MySQL in and not in() empty list error
Question: what professional qualities should test engineers have?
二叉堆(一) - 原理与C实现
The developer said, "this doesn't need to be tested, just return to the normal process". What about the testers?
redis中value/hush
我在中山,到哪里开户比较好?实际上网上开户安全么?
redis中value/list
Redis启动与库进入
Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing
证券账户销户后果 开户安全吗
Activity workflow engine
Exposure: a white box photo post processing framework reading notes
Shangtang entered the lifting period: the core management voluntarily banned and strengthened the company's long-term value confidence
epoll介绍
Mysql的四个隔离级别是如何实现的 (简要)
[Maui] add click events for label, image and other controls
activity工作流引擎
内核同步机制
Can I open an account today and buy stocks today? Is it safe to open an account online?