51单片机之串行输出【关于74HC595芯片使用】
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char//定义无符号字符型;uchar 具有unsigned char 的作用
#define uint unsigned int//定义无符号整数型;uint 具有 unsigned int 的作用
sbit shcp=P0^0;/*74HC595的第11引脚;上升沿时ds(14)数据存储器数据移位;
串行输入口输入一个字符;eg:ds=1001011;shcp一次上升沿;
QA(15引脚)=1;循环8次;QA<QB...<QH;数据依此为1101001;
*/
sbit ds=P0^1;//串行数据输入端14引脚
sbit stcp=P0^2;//上升沿时以移位寄存器的数据进入存储寄存器12
sbit mr=P0^3;//复危(低电平有效)13
sbit oe=P0^4;//使能10引脚;高电平有效
uchar code shuzi[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//595(0-9)不带小数点
uchar num;//带有秒表闪烁
uint i,x,y;
//延时单位约10ms;具体延长时间看实际情况
void delay( uint d){
uint a,b,c;
for(c=0;c<125;c++){
for(a=0;a<40;a++){
for(b=0;b<d;b++);
}
}
}
void nop(){//延时一个指令周期此处为(1um)
_nop_();
}
void chushihua595(){//初始化595,不可缺少;延时可以省略
oe=0;
mr=1;
shcp=0;
nop();
stcp=0;
nop();
}
void main(){//_nop_()可以省略;
chushihua595(); //595锁存器初始化
for(i=0;i<9;i++){
num = shuzi[i];//此处数组可以使用i;
for(x=0;x<10;x++){//此处for函数中的x不能用i代替;否则电路运行故障或者显示错误;而程序编译不会提示错误;
ds=num&0x80;
num=_crol_(num,1);
shcp=1;
nop();
shcp=0;
};
stcp=1;
nop();
stcp=0;
delay(10);
} ;
while(1);/*keill中的main具有类似循环的作用;不
过,main的效果是跳变(由低电平跳到高电平,时效非常短);
如果不加while(1);语句;main将有while(1){}的现象*/
}
//595需要先初始化;定义变量类型:如果在主函数类进行可能出现错误。
//串行输出是由移位寄存器溢出补位