⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lz-led-tm1809l.c

📁 LED灯带红绿蓝白渐亮渐灭(低速模式1024点)
💻 C
字号:
//========================================================================//
//-----------MCU型号为STC15F104E(程序下载时输入内置30MHz振荡)-----------//
//  程序功能:向TM1809低速模式发送3个像素点数据并检测交流过零信号以实现	  //
//  七彩同步渐变循环。          ISP程序下载:P3.0(RXD) P3.1(TXD)      //
//	交流过零检测输入:P3.2(采用外部0中断)      数据输出:P3.3           //                                            
//  编 写 人:陆  周    编写时间:2011-07-28于深圳宝安区   版本号:A-01   //
//========================================================================//

#include<reg52.h>				 //MCU资源头文件
#include<intrins.h>				 //移位函数
#define nop _nop_();			 
#define uchar unsigned char		 //宏替换,方便书写
#define uint  unsigned int		 //宏替换,方便书写

sbit DIO=P3^3;        	 		 //数据输出引脚声明

uchar bdata LED_DAT;			 //可位操作的数据发送暂存变量声明
sbit bit0=LED_DAT^0;			 //被发送的数据各位定义
sbit bit1=LED_DAT^1;
sbit bit2=LED_DAT^2;
sbit bit3=LED_DAT^3;
sbit bit4=LED_DAT^4;
sbit bit5=LED_DAT^5;
sbit bit6=LED_DAT^6;
sbit bit7=LED_DAT^7;


uint j;							 //时间控制全局变量声明
uchar RR,GG,BB;					 //RGB灰度值全局变量声明			

void h_dat0();					 //数码BIT0
void h_dat1();					 //数码BIT1
void fs_rgbdat();				 //发送RGB灰度数据

void red_jl();					 //红色渐亮
void red_jm();					 //红色渐灭
void green_jl();				 //绿色渐亮
void green_jm();				 //绿色渐灭
void blue_jl();					 //蓝色渐亮				
void blue_jm();					 //蓝色渐灭
void white_jl();				 //白色渐亮
void white_jm();				 //白色渐灭
void delay_1ms(uint z);			 //延时函数声明
//*****************************主程序开始*****************************//
void main()
{
	while(1)
	{
	    RR=0; GG=0; BB=0;
		fs_rgbdat();				 //发送RGB灰度数据
		DIO=0;				 		 //数据IO口置0		
		delay_1ms(50);			 	 //延时100毫秒等待所有MCU复位

		red_jl();					 //红色渐亮
		red_jm();					 //红色渐灭
		green_jl();					 //绿色渐亮
		green_jm();				 	 //绿色渐灭
		blue_jl();					 //蓝色渐亮				
		blue_jm();					 //蓝色渐灭
		white_jl();					 //白色渐亮
		white_jm();					 //白色渐灭
	}

}
//*****************************主程序结束*****************************//




//*****************************子程序开始*****************************//
//=======================红色渐亮=======================//
void red_jl()
{
	uint i;
	RR=0; GG=0; BB=0;
	for(i=0; i<256; i++)		 //白色渐灭
	{		
		fs_rgbdat();				 //发送RGB灰度数据
		DIO=0;					 //数据IO口置0
		delay_1ms(5);
		RR++;		
	}
	RR=255; GG=0; BB=0;					
}

//=======================红色渐灭=======================//
void red_jm()
{
	uint i;
	RR=255; GG=0; BB=0;
	for(i=0; i<256; i++)		 //白色渐灭
	{	
		fs_rgbdat();				 //发送RGB灰度数据
		DIO=0;					 //数据IO口置0	
		delay_1ms(5);
		RR--;		
	}
	RR=0; GG=0; BB=0;	
}
//=======================绿色渐亮=======================//
void green_jl()
{
	uint i;
	RR=0; GG=0; BB=0;
	for(i=0; i<256; i++)		 //白色渐灭
	{			
		fs_rgbdat();				 //发送RGB灰度数据
		DIO=0;					 //数据IO口置0	
		delay_1ms(5);
		GG++;		
	}
	RR=0; GG=255; BB=0;	
}

//=======================绿色渐灭=======================//
void green_jm()
{
	uint i;
	RR=0; GG=255; BB=0;
	for(i=0; i<256; i++)		 //白色渐灭
	{			
		fs_rgbdat();				 //发送RGB灰度数据
		DIO=0;					 //数据IO口置0	
		delay_1ms(5);
		GG--;		
	}
	RR=0; GG=0; BB=0;		
}
//=======================蓝色渐亮=======================//
void blue_jl()
{
	uint i;
	RR=0; GG=0; BB=0;
	for(i=0; i<256; i++)		 //白色渐灭
	{			
		fs_rgbdat();				 //发送RGB灰度数据
		DIO=0;					 //数据IO口置0	
		delay_1ms(5);
		BB++;		
	}
	RR=0; GG=0; BB=255;	
}					
//=======================蓝色渐灭=======================//
void blue_jm()
{
	uint i;
	RR=0; GG=0; BB=255;
	for(i=0; i<256; i++)		 //白色渐灭
	{		
		fs_rgbdat();				 //发送RGB灰度数据
		DIO=0;					 //数据IO口置0	
		delay_1ms(5);
		BB--;		
	}
	RR=0; GG=0; BB=0;		
}
//=======================白色渐亮=======================//
void white_jl()
{
	uint i;
	RR=0; GG=0; BB=0;
	for(i=0; i<256; i++)		 //白色渐灭
	{		
		fs_rgbdat();				 //发送RGB灰度数据
		DIO=0;					 //数据IO口置0	
		delay_1ms(5);
		RR++;
		GG++;
		BB++;		
	}
	RR=255; GG=255; BB=255;	
}
//=======================白色渐灭=======================//
void white_jm()
{
	uint i;
	RR=255; GG=255; BB=255;
	for(i=0; i<256; i++)		 //白色渐灭
	{			
		fs_rgbdat();				 //发送RGB灰度数据
		DIO=0;					 //数据IO口置0
		delay_1ms(5);
		RR--;
		GG--;
		BB--;		
	}
	RR=0; GG=0; BB=0;	
}

//=============低速模式数码BIT0(高电平时间:600ns 低电平时间:1940ns 周期T=2.54US)=============//
void h_dat0()
{   
  DIO=1; 
  nop; nop; nop; nop; nop; 
  nop; nop; nop;  
  DIO=0; 				
  nop; nop; nop; nop; nop; 
  nop; nop; nop; nop; nop;
  nop; nop; nop; nop; nop; 
  nop; nop; nop; nop; nop; 
  nop; nop; nop; nop; nop; 
  nop; nop; 
}				 
//=============低速模式数码BIT1(高电平时间:1840ns 低电平时间:700ns 周期T=2.54US)=============//
void h_dat1()				  
{	   
  DIO=1;
  nop; nop; nop; nop; nop; 
  nop; nop; nop; nop; nop; 
  nop; nop; nop; nop; nop; 
  nop; nop; nop; nop; nop; 
  nop; nop; nop; nop; nop; 
  nop; nop; nop; nop; nop; 
  nop; nop; nop; nop; nop;
  DIO=0; 					 
}	

//===================发送RGB灰度数据===================//
void fs_rgbdat()				 	 //发送RGB灰度数据
{
	uint k;
	for(k=0; k<1024; k++)			 //发送1024个像素点的数据 (灯的封装与PCB封装不一致,须按RBG的顺序发送数据!!!)
	{
		LED_DAT=RR;					 //红灯数据赋值给LED_DAT 
		if(bit7==1) h_dat1(); else h_dat0();
		if(bit6==1) h_dat1(); else h_dat0();	
		if(bit5==1) h_dat1(); else h_dat0();
		if(bit4==1) h_dat1(); else h_dat0();
		if(bit3==1) h_dat1(); else h_dat0();
		if(bit2==1) h_dat1(); else h_dat0();
		if(bit1==1) h_dat1(); else h_dat0();
		if(bit0==1) h_dat1(); else h_dat0();
		LED_DAT=BB;					 //蓝灯数据赋值给LED_DAT
		if(bit7==1) h_dat1(); else h_dat0();
		if(bit6==1) h_dat1(); else h_dat0();	
		if(bit5==1) h_dat1(); else h_dat0();
		if(bit4==1) h_dat1(); else h_dat0();
		if(bit3==1) h_dat1(); else h_dat0();
		if(bit2==1) h_dat1(); else h_dat0();
		if(bit1==1) h_dat1(); else h_dat0();
		if(bit0==1) h_dat1(); else h_dat0();
		LED_DAT=GG;					 //绿灯数据赋值给LED_DAT
		if(bit7==1) h_dat1(); else h_dat0();
		if(bit6==1) h_dat1(); else h_dat0();	
		if(bit5==1) h_dat1(); else h_dat0();
		if(bit4==1) h_dat1(); else h_dat0();
		if(bit3==1) h_dat1(); else h_dat0();
		if(bit2==1) h_dat1(); else h_dat0();
		if(bit1==1) h_dat1(); else h_dat0();
		if(bit0==1) h_dat1(); else h_dat0();	
	}
}
																					
//========================延时1MS======================//
void delay_1ms(uint z)
{
	uint x,y;
	for(x=z; x>0; x--)
		for(y=2800; y>0; y--);
}
//*****************************程序结束*****************************//

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -