標(biāo)題:
一個(gè)點(diǎn)陣屏顯示 和 流水燈 程序 怎么整合為只有一個(gè)main函數(shù)的程序?
[打印本頁(yè)]
作者:
Q_p
時(shí)間:
2017-3-16 15:46
標(biāo)題:
一個(gè)點(diǎn)陣屏顯示 和 流水燈 程序 怎么整合為只有一個(gè)main函數(shù)的程序?
//點(diǎn)陣#include<reg52.h>
sbit T_STR = P2^6; //鎖存引腳,屏上標(biāo)識(shí)位STR
sbit R1 = P0^4; //數(shù)據(jù)引腳,屏上標(biāo)識(shí)R1
sbit G1 = P2^4; //數(shù)據(jù)引腳,屏上標(biāo)識(shí)G1
sbit T_CLK = P2^5; //時(shí)鐘引腳,屏上標(biāo)識(shí)位CLK
sbit OE = P2^7; //使能引腳,屏上標(biāo)識(shí)EN/OE
unsigned char MoveBitNum,MoveBitNumtemp,IncNum;//移動(dòng)位數(shù),臨時(shí)移動(dòng)位數(shù),大于一個(gè)字節(jié)增加字節(jié)數(shù)
unsigned int HzNum;//漢字個(gè)數(shù)
unsigned char buff[10];
/*-----------------------------------------------
16x16漢字取模數(shù)據(jù)
------------------------------------------------*/
unsigned char code hztest[][32]= //取模選擇字節(jié)倒序 ,正序得出的字是反的,可以在軟件中設(shè)置自動(dòng)倒敘
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, //前面一屏大小的空字符,不顯示,這里根據(jù)屏的大小修改,這個(gè)是1個(gè)漢字屏
//所以寫(xiě)入1個(gè)漢字
0x80,0x00,0x84,0x00,0x88,0x3F,0x48,0x00,0xA1,0x1F,0x82,0x10,0x92,0x12,0x90,0x14,
0xE8,0x7F,0x88,0x10,0x47,0x12,0x44,0x14,0xC4,0x3F,0x04,0x10,0x04,0x0A,0x00,0x04,
0x80,0x00,0x80,0x00,0xFF,0x7F,0x80,0x00,0x80,0x00,0xFE,0x3F,0x12,0x24,0x22,0x22,
0xF2,0x27,0x82,0x20,0x82,0x20,0xFA,0x2F,0x82,0x20,0x82,0x20,0x82,0x28,0x02,0x10,
0x10,0x00,0xD0,0x7F,0x12,0x04,0x12,0x04,0x12,0x04,0x92,0x3F,0x92,0x24,0x92,0x24,
0x92,0x24,0x92,0x24,0x92,0x24,0x90,0x2C,0x88,0x14,0x08,0x04,0x04,0x04,0x02,0x04,
0x20,0x02,0x20,0x02,0xFF,0x7F,0x20,0x02,0x08,0x00,0x90,0x1F,0x92,0x10,0x84,0x10,
0xA4,0x10,0xA0,0x14,0x90,0x08,0x8E,0x40,0x88,0x40,0x88,0x40,0x08,0x7F,0x00,0x00,
0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0xFF,0x7F,0x80,0x00,0x80,0x00,
0x40,0x01,0x40,0x01,0x20,0x02,0x20,0x02,0x10,0x04,0x08,0x08,0x04,0x10,0x03,0x60,
0x44,0x10,0x88,0x10,0x88,0x08,0x00,0x04,0xFE,0x7F,0x02,0x40,0x01,0x20,0xF8,0x07,
0x00,0x02,0x80,0x01,0xFF,0x7F,0x80,0x00,0x80,0x00,0x80,0x00,0xA0,0x00,0x40,0x00,
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, //尾部的一屏的空字符,用于清屏,參數(shù)和屏大小有關(guān)。
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
/*-----------------------------------------------
向595寫(xiě)入一個(gè)字節(jié) 單紅色
------------------------------------------------*/
void InputByte( unsigned char DataR1) //寫(xiě)一個(gè)字節(jié)
{
unsigned char i;
for(i=8; i>0; i--)
{
R1 = !(DataR1&0x01);
T_CLK = 0;
T_CLK = 1;
DataR1 = DataR1 >> 1;
}
}
/*-----------------------------------------------
向595寫(xiě)入兩個(gè)字節(jié) 雙色
------------------------------------------------*/
void Input2Byte( unsigned char DataR1,unsigned char DataG1) //寫(xiě)一個(gè)字節(jié)
{
unsigned char i;
for(i=8; i>0; i--)
{
R1 = !(DataR1&0x01);
G1 = !(DataG1&0x01);
T_CLK = 0;
T_CLK = 1;
DataR1 = DataR1 >> 1;
DataG1 = DataG1 >> 1;
}
}
/*-----------------------------------------------
延時(shí)程序
------------------------------------------------*/
void Delay(unsigned int t)
{
while(--t);
}
/*-----------------------------------------------
主程序
------------------------------------------------*/
main()
{
unsigned char count;//16行掃描數(shù)據(jù),范圍0-15
unsigned int i, j;
unsigned char temp;
while(1)
{
i++;
if(i==100)//更改流動(dòng)速度,1T單片機(jī)和12T單片機(jī)速度大約5-8倍,注意更改參數(shù)
{
i=0;
MoveBitNum++;
if(MoveBitNum==16)//每次移動(dòng)完一個(gè)漢字大小后循環(huán)
{
MoveBitNum=0;
HzNum+=1; //調(diào)用下一個(gè)漢字
if(HzNum>=50)//需要顯示的漢字個(gè)數(shù),包括前面的一屏空字符的個(gè)數(shù),后面清屏的空字符不包含在內(nèi),這里是(漢字個(gè)數(shù)+1)
HzNum=0; //完全顯示完后循環(huán)調(diào)用
}
}
Delay(1);//控制掃描頻率
for(j=0;j<2;j++) //取每個(gè)漢字的前2個(gè)字節(jié),
{ //漢字個(gè)數(shù)+1
buff[j+j]=hztest[HzNum+j][count+count]; //每次移動(dòng)完一個(gè)漢字后,選擇下一個(gè)漢字
buff[j+j+1]=hztest[HzNum+j][count+count+1];
}
if(MoveBitNum<8) // 判讀移動(dòng)距離是大于一個(gè)字節(jié)還是小于一個(gè)字節(jié),因?yàn)橐粋(gè)字節(jié)左移右移最大只能8位
{ IncNum=0; MoveBitNumtemp=MoveBitNum; }
else
{ IncNum=1; MoveBitNumtemp=MoveBitNum-8;}//大于8就減去8得到的數(shù)值還是小于8
T_STR=0; //鎖存無(wú)效
for(j=0;j<2;j++) //按bit的方式移動(dòng)緩沖區(qū)的內(nèi)容,然后輸出到595,即取出的數(shù)值每個(gè)字節(jié)左移一定的位數(shù),
{ //后面左移出的數(shù)據(jù)整合到前面的字節(jié)中,保持?jǐn)?shù)據(jù)的連續(xù)性
temp=(buff[j+IncNum]>>MoveBitNumtemp)|(buff[j+1+IncNum]<<(8-MoveBitNumtemp));//這句比較重要,需要自行拿出2個(gè)字節(jié)的數(shù)據(jù)模擬分析
InputByte(temp);//輸出到595
}//8個(gè)字節(jié)傳輸完鎖存輸出
OE = 1;
P0=15-count;//用P0口的前4位控制16行,屏內(nèi)部通過(guò)4-16譯碼器工作,循環(huán)掃描16行
T_STR=1; //鎖存有效,此時(shí)一行的數(shù)據(jù)顯示到屏上
OE = 0;
count++;
if(count==16)
count=0;
}
}
//流水燈
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
sbit shcp=P2^0; // 移位寄存時(shí)鐘 1有效
sbit ds=P2^1; // 輸入
sbit stcp=P2^2; // 輸出時(shí)鐘 1有效
sbit k1=P3^2;
sbit k2=P3^3;
uchar n=100;
//彩燈循環(huán)
uchar data_s[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // 逆時(shí)針流水燈
0xff,0x00,0xff,0x00,0xff,0x00,0xff,0x00, // 閃爍
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01, // 順時(shí)針流水燈
0xff,0x00,0xff,0x00,0xff,0x00,0xff,0x00};
uchar data_s1[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01,
0xff,0x00,0xff,0x00,0xff,0x00,0xff,0x00,
0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0xff,0x00,0xff,0x00,0xff,0x00,0xff,0x00};
void delay(int x)
{
unsigned a,b;
for(a=x;a>0;a--)
for(b=250;b>0;b--) ;
}
void send(uchar aa) // 74hc595控制
{
uchar c;
for(c=0;c<8;c++) // 掃描每一個(gè)字節(jié) 0x01為1個(gè) 在內(nèi)部左移
{
ds=aa>>7;
aa<<=1;
shcp=0; // 高電平有效 先賦0 再取1
shcp=1;
}
stcp=0; // 高電平有效 先賦0 再取1
stcp=1;
}
void main()
{ uchar z;
EA=1;
EX0=1;EX1=1;
IT0=1;IT1=1;
while(1)
{ for(z=0;z<48;z++){
send(data_s[z]);
send(data_s1[z]);
delay(n);
}}
}
void dis_1() interrupt 0
{
if(n<240&&k1==0)
n=n+20;
}
void dis_2() interrupt 2
{
if(n>0&&k2==0)
n=n-10;
}
作者:
angmall
時(shí)間:
2017-3-16 19:00
//點(diǎn)陣
//#include<reg52.h>
//流水燈
#include <reg51.h>
#include <intrins.h>
#define uchar unsigned char
sbit T_STR = P2^6; //鎖存引腳,屏上標(biāo)識(shí)位STR
sbit R1 = P0^4; //數(shù)據(jù)引腳,屏上標(biāo)識(shí)R1
sbit G1 = P2^4; //數(shù)據(jù)引腳,屏上標(biāo)識(shí)G1
sbit T_CLK = P2^5; //時(shí)鐘引腳,屏上標(biāo)識(shí)位CLK
sbit OE = P2^7; //使能引腳,屏上標(biāo)識(shí)EN/OE
sbit shcp=P2^0; // 移位寄存時(shí)鐘 1有效
sbit ds=P2^1; // 輸入
sbit stcp=P2^2; // 輸出時(shí)鐘 1有效
sbit k1=P3^2;
sbit k2=P3^3;
uchar n=100;
unsigned char MoveBitNum,MoveBitNumtemp,IncNum;//移動(dòng)位數(shù),臨時(shí)移動(dòng)位數(shù),大于一個(gè)字節(jié)增加字節(jié)數(shù)
unsigned int HzNum;//漢字個(gè)數(shù)
unsigned char buff[10];
/*-----------------------------------------------
16x16漢字取模數(shù)據(jù)
------------------------------------------------*/
unsigned char code hztest[][32]= //取模選擇字節(jié)倒序 ,正序得出的字是反的,可以在軟件中設(shè)置自動(dòng)倒敘
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, //前面一屏大小的空字符,不顯示,這里根據(jù)屏的大小修改,這個(gè)是1個(gè)漢字屏
//所以寫(xiě)入1個(gè)漢字
0x80,0x00,0x84,0x00,0x88,0x3F,0x48,0x00,0xA1,0x1F,0x82,0x10,0x92,0x12,0x90,0x14,
0xE8,0x7F,0x88,0x10,0x47,0x12,0x44,0x14,0xC4,0x3F,0x04,0x10,0x04,0x0A,0x00,0x04,
0x80,0x00,0x80,0x00,0xFF,0x7F,0x80,0x00,0x80,0x00,0xFE,0x3F,0x12,0x24,0x22,0x22,
0xF2,0x27,0x82,0x20,0x82,0x20,0xFA,0x2F,0x82,0x20,0x82,0x20,0x82,0x28,0x02,0x10,
0x10,0x00,0xD0,0x7F,0x12,0x04,0x12,0x04,0x12,0x04,0x92,0x3F,0x92,0x24,0x92,0x24,
0x92,0x24,0x92,0x24,0x92,0x24,0x90,0x2C,0x88,0x14,0x08,0x04,0x04,0x04,0x02,0x04,
0x20,0x02,0x20,0x02,0xFF,0x7F,0x20,0x02,0x08,0x00,0x90,0x1F,0x92,0x10,0x84,0x10,
0xA4,0x10,0xA0,0x14,0x90,0x08,0x8E,0x40,0x88,0x40,0x88,0x40,0x08,0x7F,0x00,0x00,
0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0xFF,0x7F,0x80,0x00,0x80,0x00,
0x40,0x01,0x40,0x01,0x20,0x02,0x20,0x02,0x10,0x04,0x08,0x08,0x04,0x10,0x03,0x60,
0x44,0x10,0x88,0x10,0x88,0x08,0x00,0x04,0xFE,0x7F,0x02,0x40,0x01,0x20,0xF8,0x07,
0x00,0x02,0x80,0x01,0xFF,0x7F,0x80,0x00,0x80,0x00,0x80,0x00,0xA0,0x00,0x40,0x00,
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, //尾部的一屏的空字符,用于清屏,參數(shù)和屏大小有關(guān)。
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
//彩燈循環(huán)
uchar data_s[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // 逆時(shí)針流水燈
0xff,0x00,0xff,0x00,0xff,0x00,0xff,0x00, // 閃爍
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01, // 順時(shí)針流水燈
0xff,0x00,0xff,0x00,0xff,0x00,0xff,0x00};
uchar data_s1[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01,
0xff,0x00,0xff,0x00,0xff,0x00,0xff,0x00,
0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0xff,0x00,0xff,0x00,0xff,0x00,0xff,0x00};
/*-----------------------------------------------
向595寫(xiě)入一個(gè)字節(jié) 單紅色
------------------------------------------------*/
void InputByte( unsigned char DataR1) //寫(xiě)一個(gè)字節(jié)
{
unsigned char i;
for(i=8; i>0; i--)
{
R1 = !(DataR1&0x01);
T_CLK = 0;
T_CLK = 1;
DataR1 = DataR1 >> 1;
}
}
/*-----------------------------------------------
向595寫(xiě)入兩個(gè)字節(jié) 雙色
------------------------------------------------*/
void Input2Byte( unsigned char DataR1,unsigned char DataG1) //寫(xiě)一個(gè)字節(jié)
{
unsigned char i;
for(i=8; i>0; i--)
{
R1 = !(DataR1&0x01);
G1 = !(DataG1&0x01);
T_CLK = 0;
T_CLK = 1;
DataR1 = DataR1 >> 1;
DataG1 = DataG1 >> 1;
}
}
/*-----------------------------------------------
延時(shí)程序
------------------------------------------------*/
void Delay(unsigned int t)
{
while(--t);
}
void delayLED(int x)
{
unsigned a,b;
for(a=x;a>0;a--)
for(b=250;b>0;b--) ;
}
void send(uchar aa) // 74hc595控制
{
uchar c;
for(c=0;c<8;c++) // 掃描每一個(gè)字節(jié) 0x01為1個(gè) 在內(nèi)部左移
{
ds=aa>>7;
aa<<=1;
shcp=0; // 高電平有效 先賦0 再取1
shcp=1;
}
stcp=0; // 高電平有效 先賦0 再取1
stcp=1;
}
/*-----------------------------------------------
主程序
------------------------------------------------*/
main()
{
unsigned char count;//16行掃描數(shù)據(jù),范圍0-15
unsigned int i, j;
unsigned char temp;
uchar z;
EA=1;
EX0=1;EX1=1;
IT0=1;IT1=1;
while(1)
{
i++;
if(i==100)//更改流動(dòng)速度,1T單片機(jī)和12T單片機(jī)速度大約5-8倍,注意更改參數(shù)
{
i=0;
MoveBitNum++;
if(MoveBitNum==16)//每次移動(dòng)完一個(gè)漢字大小后循環(huán)
{
MoveBitNum=0;
HzNum+=1; //調(diào)用下一個(gè)漢字
if(HzNum>=50)//需要顯示的漢字個(gè)數(shù),包括前面的一屏空字符的個(gè)數(shù),后面清屏的空字符不包含在內(nèi),這里是(漢字個(gè)數(shù)+1)
HzNum=0; //完全顯示完后循環(huán)調(diào)用
}
}
Delay(1);//控制掃描頻率
for(j=0;j<2;j++) //取每個(gè)漢字的前2個(gè)字節(jié),
{ //漢字個(gè)數(shù)+1
buff[j+j]=hztest[HzNum+j][count+count]; //每次移動(dòng)完一個(gè)漢字后,選擇下一個(gè)漢字
buff[j+j+1]=hztest[HzNum+j][count+count+1];
}
if(MoveBitNum<8) // 判讀移動(dòng)距離是大于一個(gè)字節(jié)還是小于一個(gè)字節(jié),因?yàn)橐粋(gè)字節(jié)左移右移最大只能8位
{ IncNum=0; MoveBitNumtemp=MoveBitNum; }
else
{ IncNum=1; MoveBitNumtemp=MoveBitNum-8;}//大于8就減去8得到的數(shù)值還是小于8
T_STR=0; //鎖存無(wú)效
for(j=0;j<2;j++) //按bit的方式移動(dòng)緩沖區(qū)的內(nèi)容,然后輸出到595,即取出的數(shù)值每個(gè)字節(jié)左移一定的位數(shù),
{ //后面左移出的數(shù)據(jù)整合到前面的字節(jié)中,保持?jǐn)?shù)據(jù)的連續(xù)性
temp=(buff[j+IncNum]>>MoveBitNumtemp)|(buff[j+1+IncNum]<<(8-MoveBitNumtemp));//這句比較重要,需要自行拿出2個(gè)字節(jié)的數(shù)據(jù)模擬分析
InputByte(temp);//輸出到595
}//8個(gè)字節(jié)傳輸完鎖存輸出
OE = 1;
P0=15-count;//用P0口的前4位控制16行,屏內(nèi)部通過(guò)4-16譯碼器工作,循環(huán)掃描16行
T_STR=1; //鎖存有效,此時(shí)一行的數(shù)據(jù)顯示到屏上
OE = 0;
count++;
if(count==16)
count=0;
// 流水燈
for(z=0;z<48;z++){
send(data_s[z]);
send(data_s1[z]);
delayLED(n);
}
}
}
/*
void main()
{
while(1)
{
}
}
*/
void dis_1() interrupt 0
{
if(n<240&&k1==0)
n=n+20;
}
void dis_2() interrupt 2
{
if(n>0&&k2==0)
n=n-10;
}
復(fù)制代碼
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1