|
![](static/image/common/ico_lz.png)
楼主 |
发表于 2008-6-16 11:44:06
|
显示全部楼层
用arduino驱动的,程序如下:
/*
显示流程:
1:传送595数据
2:关闭EN 1,关闭显示
3:切换行信息
4:刷新595,STB
5:开EN 0
6:延时 (刷新率调整)
*/
#define RowA 2 //行信号,驱动138
#define RowB 3
#define RowC 4
#define RowD 5
int hc138en=6; //138 使能
//使用了硬件SPI,以下脚不能更改
#define R1 11 //数据出 MOSI
#define CLK 13 //时钟 SCK
#define STB 10 //595 刷新显示 SS
#define DATAIN 12 //数据入,读FLASH时有用 MISO
byte row=0;
byte hz[] =
{//智[16*16]横排
0x20,0x00,0x22,0x04,0x3F,0x7E,0x48,0x44,
0x08,0x44,0xFF,0xC4,0x10,0x44,0x14,0x7C,
0x22,0x44,0x42,0x10,0x9F,0xF8,0x10,0x10,
0x1F,0xF0,0x10,0x10,0x1F,0xF0,0x10,0x10,
//和[16*16]横排
0x02,0x00,0x07,0x00,0x78,0x00,0x08,0x04,
0x0A,0xFE,0xFF,0x84,0x08,0x84,0x18,0x84,
0x1C,0x84,0x2A,0x84,0x2A,0x84,0x48,0x84,
0x88,0xFC,0x08,0x84,0x08,0x00,0x08,0x00,
//科[16*16]横排
0x04,0x10,0x0E,0x10,0xF8,0x90,0x08,0x50,
0x08,0x10,0xFE,0x90,0x08,0x50,0x1C,0x14,
0x1A,0x1E,0x29,0xF0,0x28,0x10,0x48,0x10,
0x88,0x10,0x08,0x10,0x08,0x10,0x08,0x10,
//技[16*16]横排
0x10,0x40,0x10,0x40,0x10,0x48,0x13,0xFC,
0xFC,0x40,0x10,0x40,0x10,0x40,0x13,0xF8,
0x1A,0x08,0x31,0x10,0xD1,0x10,0x10,0xA0,
0x10,0x40,0x10,0xB0,0x51,0x0E,0x26,0x04};
void spi_transfer(volatile char data)
{
SPDR = data; // Start the transmission
while (!(SPSR & (1<<SPIF))) // Wait the end of the transmission
{
};
//return SPDR; // return the received byte
}
void hc138sacn(byte r){ //输出行线状态ABCD (A低,D高)
digitalWrite(RowA,(r & 0x01));
digitalWrite(RowB,(r & 0x02));
digitalWrite(RowC,(r & 0x04));
digitalWrite(RowD,(r & 0x08));
}
void hc595senddata(byte data){// 高位在前 反相(1亮0灭)
for (byte i=0; i<8;i++) {
digitalWrite(CLK,0);
if (data & 0x80) {
digitalWrite(R1, 0);
} else {
digitalWrite(R1, 1);
}
data=data<<1;
digitalWrite(CLK,1);
}
}
void setup () {
pinMode(RowA, OUTPUT);
pinMode(RowB, OUTPUT);
pinMode(RowC, OUTPUT);
pinMode(RowD, OUTPUT); //138片选
pinMode(hc138en, OUTPUT); //138 使能
pinMode(R1, OUTPUT);//595 数据
pinMode(CLK, OUTPUT); //595 时钟
pinMode(STB, OUTPUT); //595 使能
pinMode(DATAIN, INPUT); //595 使能
//digitalWrite(hc138d, HIGH);
Serial.begin(19200);
//SPI硬件设置
// SPCR = 01010000
//interrupt disabled,spi enabled,msb 1st,master,clk low when idle,
//sample on leading edge of clk,system clock/4 rate (fastest)
SPCR = (1<<SPE)|(1<<MSTR);
delay(10);
}
void loop () {
if (Serial.available() > 0) {
// read the incoming byte:
row = Serial.read();
// say what you got:
Serial.print("I received: ");
Serial.println(row, DEC);
}
for(row=0;row<16;row++){
for (int i=0;i<4;i++){//8片595
//硬件SPI 发送8字节耗时:22US
spi_transfer(~(hz[i*32+row*2])); //硬件SPI
spi_transfer(~(hz[i*32+row*2+1])); //硬件SPI
//软件SPI 发送8字节耗时:770US
// hc595senddata(hz[i*32+row*2]);
// hc595senddata(hz[i*32+row*2+1]);
}
digitalWrite(hc138en, 1); //关闭显示
hc138sacn(row); //换行
digitalWrite(STB, 0); //595刷新
digitalWrite(STB, 1);
delayMicroseconds(500) ; //节电用,
digitalWrite(hc138en, 0); //开启显示
delayMicroseconds(500) ; //刷新频率调,差不多60HZ,1/16间隔
}
} |
|