数码管

–Numeric.h

#ifndef __Numeric_h
#define __Numeric_h

#define  BITBANDADDR(addr,n)   ((addr & 0xF0000000) + 0x2000000 + ((addr & 0xFFFFF) << 5) + (n << 2))
#define  MEMACCESS(addr)         (*((volatile unsigned long *)addr))
#define  BIT_ADDR(addr,n)      (MEMACCESS(BITBANDADDR(addr,n)))

#define  GPIOD_ODR_ADDR        (GPIOD_BASE + 0x0C)
#define  GPIOD_IDR_ADDR        (GPIOD_BASE + 0x08)
#define  PDout(n)               BIT_ADDR(GPIOD_ODR_ADDR,n)
#define  PDin(n)                BIT_ADDR(GPIOD_IDR_ADDR,n)

#define  PD2         PDout(2)
#define  PD3         PDout(3)

#define  LED0_7_0    (GPIOE->ODR = 0xFFFE)
#define  LED0_7_1    (GPIOE->ODR = 0xFFFD)
#define  LED0_7_2    (GPIOE->ODR = 0xFFFB)
#define  LED0_7_3    (GPIOE->ODR = 0xFFF7)
#define  LED0_7_4    (GPIOE->ODR = 0xFFEF)
#define  LED0_7_5    (GPIOE->ODR = 0xFFDF)
#define  LED0_7_6    (GPIOE->ODR = 0xFFBF)
#define  LED0_7_7    (GPIOE->ODR = 0xFF7F)
//第几个数码管显示
#define  PE0_7_0     (GPIOE->ODR = 0xFF3F)
#define  PE0_7_1     (GPIOE->ODR = 0xFF06)
#define  PE0_7_2     (GPIOE->ODR = 0xFF5B)
#define  PE0_7_3     (GPIOE->ODR = 0xFF4F)
#define  PE0_7_4     (GPIOE->ODR = 0xFF66)
#define  PE0_7_5     (GPIOE->ODR = 0xFF6D)
#define  PE0_7_6     (GPIOE->ODR = 0xFF7D)
#define  PE0_7_7     (GPIOE->ODR = 0xFF07)
#define  PE0_7_8     (GPIOE->ODR = 0xFF7F)
#define  PE0_7_9     (GPIOE->ODR = 0xFF6F)
#define  PE0_7_Point (GPIOE->ODR = 0xFF80)
//数码管显示的内容
void Numeric_Init(void);
void Numeric_Show(int,int);
//第一个参数显示的位置,第二个参数为显示的数
void Numeric_Num_Print(int);
void Numeric_Led_Print(int);
void Numeric_delay(int);
void Numeric_Double_Show(int led_num ,int num);
void Numeric_Show_Double(int *double_number ,int size);

#endif

–Numeric.c

#include <stdio.h>
#include "stm32f10x.h"
#include "Numeric.h"
int num;
int led_num;
int i;
int j;
int double_i;
int dtoi_num;
int double_point_locat;
int double_num_locat;
void Numeric_Init(void)
{
    RCC->APB2ENR |= 0x0060;  
    GPIOD->CRL = (GPIOD->CRL&0xFFFF00FF)|0x00003300;
    //PD2~3 用来设置位、段
    GPIOE->CRL = (GPIOE->CRL&0x00000000)|0x33333333;
    //数码管用到了PE0~7端口
}

void Numeric_Show(int led_num ,int num)
{
    Numeric_Led_Print(led_num);//位码
    PD2 = 1;//关闭锁存
    PD2 = 0;

    Numeric_Num_Print(num);//断码
    PD3 = 1;
    PD3 = 0;
    Numeric_delay(100);
}
void Numeric_Double_Show(int led_num ,int num) //为了显示多个数,需要用到视觉暂留所以延时短
{
    Numeric_Led_Print(led_num);
    PD2 = 1;
    PD2 = 0;

    Numeric_Num_Print(num);
    PD3 = 1;
    PD3 = 0;
    Numeric_delay(1);
}
void Numeric_Num_Print(int num)
{
     if(num == 0)
     {
         PE0_7_0; // 显示数字0
     }else if (num == 1)
     {
         PE0_7_1;
     }else if (num == 2)
     {
         PE0_7_2;
     }else if (num == 3)
     {
         PE0_7_3;
     }else if (num == 4)
     {
         PE0_7_4;
     }else if (num == 5)
     {
         PE0_7_5;
     }else if (num == 6)
     {
         PE0_7_6;
     }else if (num == 7)
     {
         PE0_7_7;
     }else if (num == 8)
     {
         PE0_7_8;
     }else if (num == 9)
     {
         PE0_7_9;
     }else
     {
         PE0_7_Point; //显示符号.
     }
}
void Numeric_Led_Print(int led_num)
{
    if(led_num == 0)
     {
         LED0_7_0;//第 0 个位置上的数码管显示
     }else if (led_num == 1)
     {
         LED0_7_1;
     }else if (led_num == 2)
     {
         LED0_7_2;
     }else if (led_num == 3)
     {
         LED0_7_3;
     }else if (led_num == 4)
     {
         LED0_7_4;
     }else if (led_num == 5)
     {
         LED0_7_5;
     }else if (led_num == 6)
     {
         LED0_7_6;
     }else if (led_num == 7)
     {
         LED0_7_7;
     }
}
void Numeric_delay(int ms)
{
    for( i = 0 ; i < 2000 ; i++)
        for(j = 0 ; j < ms ; j++);
}

void Numeric_Show_Double(int *double_number ,int size)//完成显示小数
{
        double_num_locat = 0;
        double_point_locat = 0;

      for(double_i = 0 ; double_i < size ; double_i++)
        {
            dtoi_num = double_number[double_i];

            if(dtoi_num < 10)
            {
                Numeric_Double_Show(double_num_locat,dtoi_num);
                double_num_locat++;

            }else if (dtoi_num >= 10){

                double_point_locat = double_num_locat - 1;
                Numeric_Double_Show(double_point_locat,dtoi_num);
            }

        }
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值