74HC595数码管使用笔记

该程序主要涉及共阳数码管的显示控制,通过SPI接口进行数据传输,初始化GPIO并设置延时函数。在主函数中进行了全亮显示和关闭显示的测试,之后在主循环中根据CAN_RX_FINISH标志读取并显示电压值。
摘要由CSDN通过智能技术生成
  1. 硬件

  1. display.c程序

#include "display.h"

unsigned char   Tab[] = {0xc0,0xF9,0xa4,0xb0,0x99,0x92,0x82, 0xf8,0x80,
                            0x90, 0x88,0x83,0xc6,0xa1,0x86,0x8e,0x8c,0xc1,0x91,0xC7,0xff,0x7f,0x00};
                       //0 1 2 3 4 5 6 7 8 9 A b C d F P U y L 灭 . 亮//  共阳数码管  20220804liu矫正
unsigned char  Tab3[] = {0xc0,0xcf,0xa4,0xb0,0x99,0x92,0x82, 0xf8,0x80,
                            0x90, 0x88,0x83,0xc6,0xa1,0x86,0x8e,0x8c,0xE3,0x91,0x7c,0xff,0x7f,0x00};
                       //0 1 2 3 4 5 6 7 8 9 A B C D F P u Y L 灭 . 亮//  共阳数码管,与Tab[]仅修改了u;
unsigned char  Tab2[] = {0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10,0x08,0x03,0x46,0x21,0x06,0x0e}; 
                       //带小数点0~F   //共阳数码管
unsigned char  Tab7[] = {0xA3,0x8E,0xFF,0xC0}; // 共阳极数码管 o,F,不显示

void delay10ms(uint16_t temp)
{
    uint16_t  i,t;
    t=temp;
    while(t!=0)
    {
        for(i=200;i>0;i--)
           ;
        t--;
    }
}

void Display_Init(void)
{         
  GPIO_InitTypeDef  GPIO_InitStructure;

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);//使能GPIOF时钟

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13| GPIO_Pin_14;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//普通输出模式
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
  GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化
    
  GPIO_ResetBits(GPIOB,GPIO_Pin_12 | GPIO_Pin_13| GPIO_Pin_14);//GPIOF9,F10设置高,灯灭
}

void Spi_Display(unsigned char value)
{
    unsigned char i;
    for(i = 0; i < 8; i++)
    {        
        GPIO_ResetBits(GPIOB, GPIO_Pin_13); 
        
        delay_us(1);
        if(0x80 & value)
            GPIO_SetBits(GPIOB, GPIO_Pin_14);
        else
            GPIO_ResetBits(GPIOB, GPIO_Pin_14);
        value<<= 1;
        
        delay_us(1);
        GPIO_SetBits(GPIOB, GPIO_Pin_13);        
        delay_us(1);        
    }

    GPIO_ResetBits(GPIOB, GPIO_Pin_13);
    GPIO_ResetBits(GPIOB, GPIO_Pin_14);    
}

void Load(void)
{
    GPIO_SetBits(GPIOB, GPIO_Pin_12);    
    delay_us(1);
    GPIO_ResetBits(GPIOB, GPIO_Pin_12);
}
  1. main函数中的初始化及自检程序

    Display_Init();
    
    Spi_Display(Tab[22]);  //全亮
    Spi_Display(Tab[22]);
    Spi_Display(Tab[22]);
    Spi_Display(Tab[22]);
    Load();    
    
    Spi_Display(Tab[22]);  //全亮
    Spi_Display(Tab[22]);
    Spi_Display(Tab[22]);
    Spi_Display(Tab[22]);
    Load();    
    delay_ms(2000);
    
//数码管显示OFF
    Spi_Display(Tab7[1]);
    Spi_Display(Tab7[1]);
    Spi_Display(Tab7[3]);
    Spi_Display(Tab7[2]);
    Load();    
    
    Spi_Display(Tab7[1]);
    Spi_Display(Tab7[1]);
    Spi_Display(Tab7[3]);
    Spi_Display(Tab7[2]);
    Load();    
  1. 主循环中的刷新程序

void can_display(void)
{
        if(CAN_RX_FINISH == 1)
        {
            CAN_RX_FINISH = 0;


            AC_V =  RX_BUF[0][3] + (RX_BUF[0][2] << 8);   
            
            if(AC_V < 500)
            {
                    Spi_Display(Tab7[1]);      //第一位不显示 + OFF;
                    Spi_Display(Tab7[1]);
                    Spi_Display(Tab7[3]);
                    Spi_Display(Tab7[2]);  
                    
                    Spi_Display(Tab7[1]);
                    Spi_Display(Tab7[1]);
                    Spi_Display(Tab7[3]);
                    Spi_Display(Tab7[2]);
                    Load();    
            }
            else
            {
                    DC_V =  RX_BUF[2][3] + (RX_BUF[2][2] << 8);
                
                    Spi_Display(Tab7[2]);  
                    Spi_Display(Tab7[2]);
                    Spi_Display(Tab7[2]);
                    Spi_Display(Tab7[2]);
//                    Load();    
                
                    Spi_Display(Tab7[2]);
                    Spi_Display(Tab7[2]);
                    Spi_Display(Tab7[2]);
                    Spi_Display(Tab7[2]);
                    Load();    
                                    
                    Spi_Display(Tab[AC_V/10%10]);
                    Spi_Display(Tab[AC_V/100%10]);
                    Spi_Display(Tab[AC_V/1000%10]);
                    Spi_Display(Tab[17]);
//                    Load();    
                    
                    Spi_Display(Tab[DC_V%10]);
                    Spi_Display(Tab2[DC_V/10%10]);
                    Spi_Display(Tab[DC_V/100%10]);
                    Spi_Display(Tab3[17]);
                    Load();    
                    
                    if((DC_V >490)&& (DC_V< 500)) // 2022年11月1日修改为下main的条件;
//                    if(DC_V< 500)
                    {
                        DC_V_OK = 1;
                    }
                    else
                    {
                        DC_V_OK = 0;
                    }
            }                                            
        }    
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值