分享好友 天南地北首页 网站导航

51单片机设计渐变流水灯

网友发布 2023-07-19 21:38 · 头闻号仪器机械

#include<reg51.h> // 试试 不过 仿真只能看波形图 实际电路 才有效果

sbit P1_0 = P1^0; // 8路PWM输出

sbit P1_1 = P1^1;

sbit P1_2 = P1^2;

sbit P1_3 = P1^3;

sbit P1_4 = P1^4;

sbit P1_5 = P1^5;

sbit P1_6 = P1^6;

sbit P1_7 = P1^7;

unsigned char PWM0 = 1; // 占空比调整

unsigned char PWM1 = 3;

unsigned char PWM2 = 5;

unsigned char PWM3 = 7;

unsigned char PWM4 = 9;

unsigned char PWM5 = 11;

unsigned char PWM6 = 13;

unsigned char PWM7 = 15;

unsigned char counter = 0; // 计数的

unsigned char tt1; // 标志位

void main()

{

TMOD=0x01;

TH0=(65536-2000)/256; // 定时时间 可以修改

TL0=(65536-2000)%256;

EA=1;

ET0=1;

TR0=1;

while(1)

{ // 开关调整 PWM 占空比

if(tt1==200)

{

tt1=0;

PWM0++;PWM1++;PWM2++;PWM3++;

PWM4++;PWM5++;PWM6++;PWM7++;

if(PWM0==17) PWM0=0;

if(PWM1==17) PWM1=0;

if(PWM2==17) PWM2=0;

if(PWM3==17) PWM3=0;

if(PWM4==17) PWM4=0;

if(PWM5==17) PWM5=0;

if(PWM6==17) PWM6=0;

if(PWM7==17) PWM7=0;

}

}

}

void Timer0(void) interrupt 1 // 定时器0 PWM 控制

{

TH0=(65536-2000)/256; // 定时时间 可以修改

TL0=(65536-2000)%256;

counter++;

tt1++;

if(counter >= 16) counter = 0; // PWM 16级 可以修改

if(counter >= PWM0) P1_0 = 0; else P1_0 = 1;

if(counter >= PWM1) P1_1 = 0; else P1_1 = 1;

if(counter >= PWM2) P1_2 = 0; else P1_2 = 1;

if(counter >= PWM3) P1_3 = 0; else P1_3 = 1;

if(counter >= PWM4) P1_4 = 0; else P1_4 = 1;

if(counter >= PWM5) P1_5 = 0; else P1_5 = 1;

if(counter >= PWM6) P1_6 = 0; else P1_6 = 1;

if(counter >= PWM7) P1_7 = 0; else P1_7 = 1;

}

免责声明:本平台仅供信息发布交流之途,请谨慎判断信息真伪。如遇虚假诈骗信息,请立即举报

举报
反对 0
打赏 0
更多相关文章

收藏

点赞