標(biāo)題:
51單片機驅(qū)動TM1809源程序
[打印本頁]
作者:
趣致克林
時間:
2023-6-3 12:17
標(biāo)題:
51單片機驅(qū)動TM1809源程序
//========================================================================//
//-----------MCU型號為STC15F104E(程序下載時輸入內(nèi)置30MHz振蕩)-----------//
// 程序功能:向TM1809低速模式發(fā)送3個像素點數(shù)據(jù)并檢測交流過零信號以實現(xiàn) //
// 七彩同步漸變循環(huán)。 ISP程序下載:P3.0(RXD) P3.1(TXD) //
// 交流過零檢測輸入:P3.2(采用外部0中斷) 數(shù)據(jù)輸出:P3.3 //
//========================================================================//
#include<reg52.h> //MCU資源頭文件
#include<intrins.h> //移位函數(shù)
#define nop _nop_();
#define uchar unsigned char //宏替換,方便書寫
#define uint unsigned int //宏替換,方便書寫
sbit DIO=P3^3; //數(shù)據(jù)輸出引腳聲明
uchar bdata LED_DAT; //可位操作的數(shù)據(jù)發(fā)送暫存變量聲明
sbit bit0=LED_DAT^0; //被發(fā)送的數(shù)據(jù)各位定義
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(); //數(shù)碼BIT0
void h_dat1(); //數(shù)碼BIT1
void fs_rgbdat(); //發(fā)送RGB灰度數(shù)據(jù)
void red_jl(); //紅色漸亮
void red_jm(); //紅色漸滅
void green_jl(); //綠色漸亮
void green_jm(); //綠色漸滅
void blue_jl(); //藍(lán)色漸亮
void blue_jm(); //藍(lán)色漸滅
void white_jl(); //白色漸亮
void white_jm(); //白色漸滅
void delay_1ms(uint z); //延時函數(shù)聲明
//*****************************主程序開始*****************************//
void main()
{
while(1)
{
RR=0; GG=0; BB=0;
fs_rgbdat(); //發(fā)送RGB灰度數(shù)據(jù)
DIO=0; //數(shù)據(jù)IO口置0
delay_1ms(50); //延時100毫秒等待所有MCU復(fù)位
red_jl(); //紅色漸亮
red_jm(); //紅色漸滅
green_jl(); //綠色漸亮
green_jm(); //綠色漸滅
blue_jl(); //藍(lán)色漸亮
blue_jm(); //藍(lán)色漸滅
white_jl(); //白色漸亮
white_jm(); //白色漸滅
}
}
//*****************************主程序結(jié)束*****************************//
//*****************************子程序開始*****************************//
//=======================紅色漸亮=======================//
void red_jl()
{
uint i;
RR=0; GG=0; BB=0;
for(i=0; i<256; i++) //白色漸滅
{
fs_rgbdat(); //發(fā)送RGB灰度數(shù)據(jù)
DIO=0; //數(shù)據(jù)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(); //發(fā)送RGB灰度數(shù)據(jù)
DIO=0; //數(shù)據(jù)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(); //發(fā)送RGB灰度數(shù)據(jù)
DIO=0; //數(shù)據(jù)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(); //發(fā)送RGB灰度數(shù)據(jù)
DIO=0; //數(shù)據(jù)IO口置0
delay_1ms(5);
GG--;
}
RR=0; GG=0; BB=0;
}
//=======================藍(lán)色漸亮=======================//
void blue_jl()
{
uint i;
RR=0; GG=0; BB=0;
for(i=0; i<256; i++) //白色漸滅
{
fs_rgbdat(); //發(fā)送RGB灰度數(shù)據(jù)
DIO=0; //數(shù)據(jù)IO口置0
delay_1ms(5);
BB++;
}
RR=0; GG=0; BB=255;
}
//=======================藍(lán)色漸滅=======================//
void blue_jm()
{
uint i;
RR=0; GG=0; BB=255;
for(i=0; i<256; i++) //白色漸滅
{
fs_rgbdat(); //發(fā)送RGB灰度數(shù)據(jù)
DIO=0; //數(shù)據(jù)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(); //發(fā)送RGB灰度數(shù)據(jù)
DIO=0; //數(shù)據(jù)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(); //發(fā)送RGB灰度數(shù)據(jù)
DIO=0; //數(shù)據(jù)IO口置0
delay_1ms(5);
RR--;
GG--;
BB--;
}
RR=0; GG=0; BB=0;
}
//=============低速模式數(shù)碼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;
}
//=============低速模式數(shù)碼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;
}
//===================發(fā)送RGB灰度數(shù)據(jù)===================//
void fs_rgbdat() //發(fā)送RGB灰度數(shù)據(jù)
{
uint k;
for(k=0; k<1024; k++) //發(fā)送1024個像素點的數(shù)據(jù) (燈的封裝與PCB封裝不一致,須按RBG的順序發(fā)送數(shù)據(jù)。。。
{
LED_DAT=RR; //紅燈數(shù)據(jù)賦值給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; //藍(lán)燈數(shù)據(jù)賦值給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; //綠燈數(shù)據(jù)賦值給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--);
}
//*****************************程序結(jié)束*****************************//
復(fù)制代碼
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1