你要模拟出SPI来,因为595是串入并出的芯片
你主要的就是模拟出SPI就会控制它了,还有就是你的硬件接线图不要错了
现在我的电脑装不了仿真,你要的话,就M我
我根据你的硬件图给你写个就好了
QQ 247519442
你用一个字节就可以搞定了,你要知道你现在的问题是把它送出去就好了
我有个程序,里面有模拟显示数码管的
你在代码里面提取你要的
#include
#include
#include
#include
#include
/*****************模拟SPI控制数码管**************************/
sbit SPI1_SH_CLK = P1^4;//模拟SPI移位时钟
sbit SPI1_ST_CLK = P1^5;//模拟SPI锁存时钟
sbit SPI1_OUT_OE = P1^6;//模拟SPI输出控制
sbit SPI1_DATA = P1^7;//模拟SPI数据输出
/*************************************************************/
/*****************系统操作用数据定义**************************/
unsigned char ADC_date[4] = {0};//保存四个电位器AD转换值
unsigned char dispram[8] = {10,10,10,10,10,10,10,10};//数码管显示缓存
sbit DOWN_BUTTON = P3^3;//下降的按键
sbit UP_BUTTON = P3^4; //提升的按键
bit flash ;
sbit beef= P0^7;
bit Control_Coordinate=0;
unsigned char kkk;
unsigned char Hbeef,Lbeef;
unsigned int m = 0; //m主要用来作为附加计数用的控制变量
unsigned char code Letter[]=
{
0x32,0x48,0x7a,0x17,0x37,0x48,0x12,0x32,0x7e,0x7a,0x37,0x6b,0x7e,0x7a
};
unsigned char code seg[]={0x7e,0x48,0x3d,0x6d,0x4b,0x67,0x77,0x4c,0x7f,0x6f,0x00,0x37,0x1f};
//数码管段码0,1,2,3,4,5,6,7,8,9,E,P
unsigned char code seg_only[]={0x04,0x08,0x40,0x20,0x10,0x02,0x01,0x80,0x00};//a.b.c.d.e.f.g.dp
unsigned int code TABLE[15]={64260,64400,64524,64580,
64684,64777,64820,64898,
64968,65030,65058,65110,
65157,65178,65217};
/************************************************************/
unsigned char code SONG1[]=
{
0x35,0x31,0x34,0x34,0x24,0x35,0x32,0x32,0x24,0x38,
0x44,0x58,0x48,0x34,0x31,0x34,0x34,0x24,0x38,0x34,
0x7F,0x32,0x34,0x32,0x34,0x24,0x38,0x34,0x24,0x38,
0x43,0x58,0x48,0x34,0x32,0x34,0x38,0x24,0x38,0x34,
0x7F,0x16,0x4C,0x74,0x78,0x64,0x54,0x48,0x54,0x64,
0x58,0x44,0x34,0x24,0x38,0x24,0x14,0x12,0x21,0x14,
0x78,0x68,0x3F,0x4C,0x74,0x78,0x64,0x52,0x42,0x48,
0x53,0x64,0x58,0x44,0x34,0x24,0x38,0x24,0x24,0x38,
0x44,0x58,0x48,0x3C,0x00
};
unsigned char code SONG0[]={ //生日快乐
0X82,0X01,0X81,0X94,0X84,0XB4,0XA4,0X04,0X82,0X01,
0X81,0X94,0X84,0XC4,0XB4,0X04,0X82,0X01,0X81,0XF4,
0XD4,0XB4,0XA4,0X94,0XE2,0X01,0XE1,0XD4,0XB4,0XC4,
0XB4,0X04,0X00
};
/*****************************************************
** 函数声明部分 **
*****************************************************/
/*******************系统配置函数声明********************/
void sysclk(void); //系统时钟初始化
void PIO_Init(void); //I/O口初始化
void SPI_Init(void); //SPI初始化
//void PWM(void); //PWM初始化
void t01_init(void); //定时器0、1初始化
void Interrupt_Init(void); //中断初始化
void ADC_INIT(void); //AD转换初始化
void delay(unsigned int time); //延时程序
/*****************显示操作函数声明********************/
void Spel_count(unsigned int Putin_Count,unsigned char LR); //Putin_Count为10000以内整形数据
//LR为左右数码管控制,LR=0左边
void led_flash1(unsigned char speed);
void led_flash2(unsigned char speed); //数码管的跑动显示
void SPI_Write_Byte(unsigned char write_byte);
//模拟SPI传送一个字节给595显示
void Coordinate(unsigned char X_radiation,unsigned char Y_radiation);
//显示某个坐标
void Sing_Song(unsigned char *P_song);
//音乐演奏函数
/****************************************************/
/****************************************************/
/****************************************************/
/*****************************************************
** 主函数 **
*****************************************************/
void main(void) //主程序
{
PCA0MD &= ~0x40;// 关闭看门狗
PIO_Init(); //I/O口初始化配置
sysclk(); //系统时钟初始化配置
t01_init(); //定时器初始化配置
//PWM();
SPI_Init(); //SPI0DAT是SPI的数据寄存器
Interrupt_Init(); //中断初始化配置
ADC_INIT(); //AD转换初始化
SPI1_OUT_OE = 0; //允许595输出
AD0BUSY=1; //初始化完后,先启动一次AD转换
delay(1);
EA=1; //然后再开中断
delay(1);
beef = 0;
//Spel_count(2010,0);
//Spel_count(121,1); //启动后左右数码管都显示0
/*
ADC_date[0]表示手柄右边左右方向的AD转换值 对应P2.0输入 中间值为0x80c0
ADC_date[1]表示手柄左边左右方向的AD转换值 对应P2.1输入 中间值为0x8040
ADC_date[2]表示手柄右边前后方向的AD转换值 对应P2.2输入 中间值为0x8340
ADC_date[3]表示手柄左边前后方向的AD转换值 对应P2.3输入 中间值为0x7f04
*/
//Sing_Song(SONG0);
while(1)
{
for(Hbeef=0;Hbeef<8;Hbeef++)
dispram[Hbeef] = 10;
while(1)
{
led_flash1(120);
led_flash2(120);
led_flash1(60);
led_flash1(60);
led_flash1(60);
led_flash1(60);
led_flash1(40);
delay(200);
led_flash2(60);
led_flash2(60);
led_flash2(60);
led_flash2(60);
led_flash2(40);
break;
}
kkk=0;m=0;
while(1)
{
if(kkk==50)break;
Coordinate(0,kkk%6);
}
kkk=0;m=7000;
while(1)
{
Spel_count(m,0);
if(m==13000)
{m=0;kkk++;break;}
//Spel_count(kkk,0);
Spel_count(13000-m,1);
}
while(1)
{Sing_Song(SONG0);
break;
}
}
}
/****************************************************/
/****************************************************/
/****************************************************/
/*****************************************************
** 系统初始化函数 **
*****************************************************/
void sysclk(void) //内部晶振
{
OSCICL=0xa3; // 0x83
OSCICN=0xc3; //二分频
CLKSEL=0x00;
}
void PIO_Init(void) // 端口配置
{
P0MDIN=0xff; //禁止模拟输入,0为模拟,1为数字
P0MDOUT=0xff; //0为开漏,1为推挽(ff)
P0SKIP=0xf0;
P1MDIN=0xff;
P1MDOUT=0xf0; //低四位用于138
P1SKIP=0xff;
P2MDIN=0xff; //禁止模拟输入,0为模拟,1为数字
P2MDOUT=0xff; //0为开漏,1为推挽(ff)
P2SKIP=0xff;
P3MDIN=0xff;
P3MDOUT=0xff; //低四位用于138
XBR0=0x02;
XBR1=0x40;
}
void SPI_Init(void) //SPI初始化
{
SPI0CFG=0x40;
SPI0CN=0x01; //0000 0001最后一位是SPI使能位 SPI工作在三线主方式
SPI0CKR=0x2f; //SPI 时钟频率设置为150kHz 0x6f 0x2f
}
void t01_init(void)
{
TCON=0x10; //0101 0000 定时器0/1允许工作
TMOD=0x11; //定时器0/1工作在16位计数方式
CKCON=0x00; //系统时钟12分频
TH0=0xf8; //PCA 50Hz 时钟
TL0=0x00;
TH1 = 0x00;
TL1 = 0x00;
}
void Interrupt_Init(void) //中断设定
{
IE=0x0a; //允许定时器0/1中断请求
IP=0x08; //SPI为低优先级
}
void ADC_INIT(void)
{
AMX0P=0x08; //P2.0
AMX0N=0x1f; //单端方式GND为负输入
ADC0CF=0xfc; //1111 1100 数据左对齐
ADC0CN=0x80; //使能AD转换
REF0CN=0x0a;
}
/****************************************************/
/****************************************************/
/****************************************************/
/*****************************************************
** 自定义的用户函数 **
*****************************************************/
//将数据进行分离
void Spel_count(unsigned int Putin_Count,unsigned char LR)
{
Control_Coordinate = 0;//以正常形式显示
if(Putin_Count>=10000)
{TR1= 0;
if(!flash)
{
dispram[0+4*LR] = 11;//字母E
dispram[1+4*LR] = 12;//字母P
dispram[2+4*LR] = 12;
dispram[3+4*LR] = 12;
beef = 1;
}
else
{
dispram[0+4*LR] = 10;//字母E
dispram[1+4*LR] = 10;//字母P
dispram[2+4*LR] = 10;
dispram[3+4*LR] = 10;
beef = 0;
}
}
else
{
dispram[3+4*LR] = Putin_Count%10;
if(Putin_Count>=10)
{dispram[2+4*LR] = (Putin_Count/10)%10;}
else dispram[2+4*LR] = 10;
if(Putin_Count>=100)
{dispram[1+4*LR] = (Putin_Count/100)%10;}
else dispram[1+4*LR] = 10;
if(Putin_Count>=1000)
{dispram[0+4*LR] = (Putin_Count/1000)%10;}
else dispram[0+4*LR] = 10;
}
}
void led_flash1(unsigned char speed)
{
unsigned char led_i,led_j;
Control_Coordinate = 0;
for(led_i=0;led_i<20;led_i++)
{
for(led_j=0;led_j<7;led_j++)
dispram[led_j] = dispram[led_j+1];
if(led_i<10)
dispram[7] = led_i;
else
dispram[7] = 10;
delay(speed);
}
}
void led_flash2(unsigned char speed)
{
unsigned char led_i,led_j;
Control_Coordinate = 0;
for(led_i=0;led_i<20;led_i++)
{
for(led_j=7;led_j>0;led_j--)
dispram[led_j] = dispram[led_j-1];
if(led_i<10)
dispram[0] = led_i;
else
dispram[0] = 10;
delay(speed);
}
}
void SPI_Write_Byte(unsigned char write_byte)
{
unsigned char k;
for(k=0;k<8;k++)
{
SPI1_DATA = (write_byte>>(7-k))&0x01;
SPI1_SH_CLK = 1;
SPI1_SH_CLK = 0;
}
}
void Coordinate(unsigned char X_radiation,unsigned char Y_radiation)
{unsigned char radiation;
Control_Coordinate = 1; //以坐标形式显示
for(radiation=0;radiation<8;radiation++)
if(X_radiation==0)
dispram[radiation] = Y_radiation;
else
{
if(radiation !=X_radiation-1)
dispram[radiation] = 8;
dispram[X_radiation-1] = Y_radiation;
}
}
void Sing_Song(unsigned char *P_song) //播放音乐
{
unsigned char i=0;
unsigned char yfm,jpm;
while(*(P_song+i))
{
jpm=(*(P_song+i))&0x0f; //节拍值
yfm=((*(P_song+i))>>0x04)&0x0f;//简谱值
if(yfm) //简谱为1,取计数值
{
yfm --;
Hbeef=TABLE[yfm]/256; //取计数值高字节
TH1 = Hbeef;
Lbeef=TABLE[yfm]%256; //取计数值低字节
TL1 = Lbeef;
TR1 = 1; //启动TIMER0
}
else {TR1 = 0;beef = 0;} //简谱为0,不发音
Spel_count(yfm,0);
Spel_count(jpm,1);
delay(jpm*100); //节拍延时
i++;
}
}
void delay(unsigned int time) //延时程序
{unsigned int i,j;
for(i=0;i<2000;i++)
{
for(j=0;j
用STC89C52控制595我们没用过,一般是用245
符号 引脚 描述
Q0…Q7 15, 1, 7 并行数据输出
GND 8 地
Q7’ 9 串行数据输出
MR 10 主复位(低电平)
SHCP 11 移位寄存器时钟输入
STCP 12 存储寄存器时钟输入
OE 13 输出有效(低电平)
DS 14 串行数据输入
VCC 16 电源