找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1365|回复: 1
收起左侧

51单片机74HC595驱动

[复制链接]
ID:346927 发表于 2019-5-28 10:38 | 显示全部楼层 |阅读模式
  1. #include <reg51.h>
  2. #include <intrins.h>
  3. #define uchar unsigned char
  4. #define uint unsigned int
  5. #define nop() _nop_()
  6. uchar t0;
  7. uchar code SEG_DAT[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
  8. uchar code BIT_CS[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf};  

  9. sbit SH_CP = P0^0;  //移位串行时钟脉冲
  10. sbit DS    = P0^1;  //串行数据输入
  11. sbit ST_CP = P0^2;  //数据并行输出锁存器控制脉冲  
  12. //串行输入子程序  
  13. void Serial_Input_595(uchar dat)
  14. {  
  15. uchar i;
  16.                  
  17. for (i = 0; i < 8; i++)
  18.     {
  19.       if (dat&0x80) //发送高位
  20.   {
  21.    DS = 1;
  22.   }
  23.   else
  24.   {
  25.    DS = 0;
  26.   }
  27.   dat <<= 1; //次高位左移到高位
  28.   SH_CP = 0; //移位时钟线拉低
  29.   nop();
  30.   nop();   
  31.   SH_CP = 1; //DS位上的0或1在移位脉冲上升沿被移入595
  32.   nop();
  33.   nop();        
  34. }
  35. }
  36. //并行输出子程序  
  37. void Parallel_Output_595()
  38. {  
  39. ST_CP = 0;
  40. nop();
  41. nop();   
  42. ST_CP = 1; //上升沿将数据送到输出锁存器
  43. nop();
  44. nop();
  45. }
  46. void display(uint count)
  47. {
  48. uchar i=0;
  49. uchar a[6];
  50. uchar temp;
  51. a[0]=0;
  52. a[1]=count/10000;
  53. a[2]=(count%10000)/1000;
  54. a[3]=(count%10000%1000)/100;
  55. a[4]=(count%10000%1000%100)/10;
  56. a[5]=count%10;
  57.   
  58. for(i = 0; i <= 5; i++)
  59. {
  60.   Serial_Input_595(BIT_CS[i]);
  61.   temp=a[i];
  62.   Serial_Input_595(SEG_DAT[temp]);  
  63.   Parallel_Output_595();
  64. }
  65. }
  66. void timer0_init(void)
  67. {
  68. TMOD=0x01;
  69. TH0=(65536-50000)/256;
  70. TL0=(65536-50000)%256;
  71. EA=1;
  72. ET0=1;
  73. TR0=1;
  74. }
  75. void timer0(void) interrupt 1
  76. {
  77. TH0=(65536-50000)/256;
  78. TL0=(65536-50000)%256;
  79. t0++;
  80. }
  81. void main(void)
  82. {
  83. uint i=0;
  84. timer0_init();
  85. while(1)
  86. {
  87.      
  88.   if(t0==5)
  89.   {
  90.     t0=0;
  91.     i++;
  92.    }
  93.   display(i);
  94.   if(i==65535) i=0;
  95. }
  96. }
复制代码


proteus仿真图

proteus仿真图

74HC595.rar

61.79 KB, 下载次数: 15, 下载积分: 黑币 -5

回复

使用道具 举报

ID:1 发表于 2019-5-28 19:41 | 显示全部楼层
本帖需要重新编辑补全电路原理图,源码,详细说明与图片即可获得100+黑币(帖子下方有编辑按钮)
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表