当前位置:网站首页>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 .
边栏推荐
- Istio, ebpf and rsocket Broker: in depth study of service grid
- Width and widthstep of iplimage
- openinstall:微信小程序跳转H5配置业务域名教程
- 树莓派4B安装tensorflow2.0[通俗易懂]
- Unittest框架中跳过要执行的测试用例
- redis中value/hush
- 邻接矩阵无向图(一) - 基本概念与C语言
- TEMPEST HDMI泄漏接收 5
- Brief explanation of the working principle, usage scenarios and importance of fingerprint browser
- No statements may be issued when any streaming result sets are open and in use on a given connection
猜你喜欢
随机推荐
Oneconnect plans to be listed in Hong Kong on July 4: a loss of nearly 3 billion in two years, with a market capitalization evaporation of more than 90%
Custom grpc plug-in
Mingchuang plans to be listed on July 13: the highest issue price is HK $22.1, and the net profit in a single quarter decreases by 19%
如何看懂开发的查询语句
Numpy的矩阵
ACLY与代谢性疾病
How to set decimal places in CAD
Extended tree (I) - concept and C implementation
JS date format conversion method
Comment Nike a - t - il dominé la première place toute l'année? Voici les derniers résultats financiers.
Test case writing specification in unittest framework and how to run test cases
Adjacency matrix undirected graph (I) - basic concepts and C language
241. Design priority for operational expressions: DFS application questions
分享psd格式怎么预览的方法和psd文件缩略图插件[通俗易懂]
8款最佳实践,保护你的 IaC 安全!
JS日期格式化转换方法
Activity workflow engine
Redis startup and library entry
8 best practices to protect your IAC security!
自定義 grpc 插件









