• 【龙芯1B】:74HC595数码管或74HC138数码管程序开发



    学习目标:解决龙芯1B:74HC595或74HC138数码管显示问题

            首先我们要知道数码管的原理;以74HC595为例,74HC595是具有三态输出功能(即具有高电平、低电平和高阻抗三种输出状态)的门电路。输出寄存器可以直接清除,具有100MHz的移位频率。

            详细看数据手册:

                    (1条消息) 单片机数码管74HC595数据手册-单片机文档类资源-CSDN文库

            龙芯1B开发板     集成74HC595电路图:

                    手册图:

    开发流程:

            第一步:目的:将要准备输入的位数据移入74HC595数据输入端上。方法:送位数据到595。

            第二步:目的:将位数据逐位移入74HC595,即数据串入方法:SH_CP产生一上升沿,将DS上的数据移入74HC595移位寄存器中,先送高位,再送低位。

            第三步:目的:并行输出数据。即数据并出方法:ST_CP产生一上升沿,将由DS上已移入数据寄存器中的数据送入到输出锁存器。


    STM32数码管显示代码:

            解决方法我可以根据STM32的代码思想来解决,当然这就是一名高级CV工程师必会的东西了,不过大部分也是因为STM32的代码生态比较好,要不然也用不上这种方法,所以我们更要支持国产,愿祖国越来越好。

            main.c:

    1. #include "SysTick.h"
    2. #include "smg.h"
    3. char smgduan[16]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};//0~F 数码管段选数据
    4. int main()
    5. {
    6. int i;
    7. SysTick_Init(72);
    8. LED_Init();
    9. SMG_Init();
    10. while(1)
    11. {
    12. for(i=0;i<16;i++)
    13. {
    14. GPIO_Write(SMG_PORT,(int)~smgduan[i]);
    15. delay_ms(1000);
    16. }
    17. }
    18. }

            smg.h:

    1. #ifndef _SMG_H
    2. #define _SMG_H
    3. #include "system.h"
    4. #define SMG_PORT_RCC RCC_APB2Periph_GPIOC
    5. #define SMG_PIN GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7
    6. #define SMG_PORT GPIOC
    7. void SMG_Init(void);
    8. #endif

            smg.c:

    1. #include "smg.h"
    2. void SMG_Init()
    3. {
    4. GPIO_InitTypeDef GPIO_InitStructure;//结构体定义
    5. RCC_APB2PeriphClockCmd(SMG_PORT_RCC,ENABLE);//打开GPIOC的时钟
    6. GPIO_InitStructure.GPIO_Pin=SMG_PIN;
    7. GPIO_InitStructure.GPIO_Mode =GPIO_Mode_Out_PP;
    8. GPIO_InitStructure.GPIO_Speed =GPIO_Speed_50MHz;
    9. GPIO_Init(SMG_PORT,&GPIO_InitStructure);
    10. GPIO_SetBits(GPIOC,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7);
    11. }

    龙芯1B数码管代码:

            smg_drv.c:

    1. /*
    2. * smg_drv.c
    3. *
    4. * created:2022.11.18
    5. * author:LHB 数码管程序
    6. */
    7. #include "smg_drv.h"
    8. #include
    9. #include "ls1b.h"
    10. #include "mips.h"
    11. #include "ls1b_gpio.h"
    12. //74HC138
    13. //#define HC138_A(val) GPIO_WriteBit(GPIOD, GPIO_Pin_12, (BitAction)val)
    14. //#define HC138_B(val) GPIO_WriteBit(GPIOD, GPIO_Pin_13, (BitAction)val)
    15. //#define HC138_C(val) GPIO_WriteBit(GPIOD, GPIO_Pin_14, (BitAction)val)
    16. //74HC595
    17. #define HC595_SI(val) gpio_write(39,val)// GPIO_WriteBit(GPIOC, GPIO_Pin_6, (BitAction)val)
    18. #define HC595_RCK(val) gpio_write(48,val)// GPIO_WriteBit(GPIOC, GPIO_Pin_7, (BitAction)val)
    19. #define HC595_SCK(val) gpio_write(49,val)//GPIO_WriteBit(GPIOC, GPIO_Pin_8, (BitAction)val)
    20. /*******************************
    21. 功 能:数码管端口初始化
    22. 参 数:无
    23. 返回值:无
    24. *******************************/
    25. void SMG_Init(void)
    26. {
    27. gpio_enable(39,DIR_OUT);
    28. gpio_enable(48,DIR_OUT);
    29. gpio_enable(49,DIR_OUT);
    30. gpio_enable(45,DIR_OUT);
    31. gpio_enable(44,DIR_OUT);
    32. gpio_enable(43,DIR_OUT);
    33. gpio_enable(42,DIR_OUT);
    34. HC595_Send(0xff);
    35. }
    36. /*******************************
    37. 功 能:HC595发送数据
    38. 参 数:dat 数据
    39. 返回值:无
    40. *******************************/
    41. void HC595_Send(unsigned char dat)
    42. {
    43. unsigned char dat_buf = 0, i;
    44. for(i=0; i<8; i++)
    45. {
    46. dat_buf = dat & 0x80;
    47. if (dat_buf) //输出1bit数据
    48. {
    49. HC595_SI(1); //将74HC595串行数据输入引脚设置为高电平
    50. }
    51. else
    52. {
    53. HC595_SI(0); //将74HC595串行数据输入引脚设置为低电平
    54. }
    55. HC595_SCK(0);
    56. delay_us(1);
    57. HC595_SCK(1);
    58. delay_us(1);
    59. dat <<= 1;
    60. }
    61. HC595_RCK(0);
    62. delay_us(3);
    63. HC595_RCK(1);
    64. }
    65. //显示的数字数组, 依次为0, 1, 2 3 4 5 6 7
    66. unsigned char digivalue[] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07};
    67. unsigned char Display[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; // 不带小数点
    68. unsigned char Display_1[] = {0xbf,0x86,0xdb,0xcf,0xef,0xed,0xfd,0x87,0xff,0xef,0xff,0x00}; // 带小数点
    69. /*******************************
    70. 功 能:数码管位段控制
    71. *******************************/
    72. void SMG_Sele(unsigned char index)
    73. {
    74. switch(index)
    75. {
    76. case 0:
    77. gpio_write(45,1);
    78. gpio_write(44,0);
    79. gpio_write(43,0);
    80. gpio_write(42,0);
    81. break;
    82. case 1:
    83. gpio_write(45,0);
    84. gpio_write(44,1);
    85. gpio_write(43,0);
    86. gpio_write(42,0);
    87. break;
    88. case 2:
    89. gpio_write(45,0);
    90. gpio_write(44,0);
    91. gpio_write(43,1);
    92. gpio_write(42,0);
    93. break;
    94. case 3:
    95. gpio_write(45,0);
    96. gpio_write(44,0);
    97. gpio_write(43,0);
    98. gpio_write(42,1);
    99. break;
    100. default:
    101. gpio_write(45,0);
    102. gpio_write(44,0);
    103. gpio_write(43,0);
    104. gpio_write(42,0);
    105. break;
    106. }
    107. }
    108. /**************************************************************
    109. *功 能:动态数码管模拟
    110. **************************************************************/
    111. void hc595_Test(unsigned short num)
    112. {
    113. unsigned char temp = 0;
    114. unsigned char j = 0;
    115. if(num >= 9999)
    116. num = 0000;
    117. for(j = 0; j < 10; j++)
    118. {
    119. // 数据选择
    120. temp = Display[num / 1000];
    121. HC595_Send(temp);
    122. SMG_Sele(0); //数码管显示数据
    123. delay_ms(1);
    124. // 选择数据
    125. temp = Display[num / 100%10];
    126. HC595_Send(temp);
    127. SMG_Sele(1); //数码管显示数据
    128. delay_ms(1);
    129. // 选择数据
    130. temp = Display[num / 10%10];
    131. HC595_Send(temp);
    132. SMG_Sele(2); //数码管显示数据
    133. delay_ms(1);
    134. // 选择数据
    135. temp = Display[num % 10];
    136. HC595_Send(temp);
    137. SMG_Sele(3); //数码管显示数据
    138. delay_ms(1);
    139. }
    140. }

    运行结果:

            


    总结

            需要详细工程请联系

    1. 关注微信公众号(嵌入式up)
    2. CSDN私信
    3. 工作微信:bulidupup

    努力到无能为力,拼搏到感动自己。

  • 相关阅读:
    Electron 18-19 值得关注的变化
    初识Spring(一)IOC
    ubuntu安装nvm
    对于koa中间件的理解
    Dubbo入门
    “Vue进阶:深入理解插值、指令、过滤器、计算属性和监听器“
    C中结构体和C++类各自对象的大小——C++
    龙蜥开发者说:海纳百川,有容乃大,我在龙蜥社区的升级之旅 | 第 11 期
    [Power Query] 数据类型转换
    .NET Reactor简单使用教程
  • 原文地址:https://blog.csdn.net/oxygen23333/article/details/127933740