数码管电路原理图如下:
单片机源程序如下:
595驱动:
- #include "stm32f10x.h"
- #include "HC595.h"
- #include "delay.h"
- #include "key.h"
- #define uint unsigned int
- #define uchar unsigned char
- //初始化PA0、PA1和PA2为输出口,并使能这个端口的时钟
- //HC595 IO初始化
- void HC595_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能PA端口时钟
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2; //端口配置
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_SetBits(GPIOA,GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2); //PA0,1,2 输出高
- }
- void HC595_Display(unsigned char Bai, unsigned char Shi, unsigned char Ge)
- {
- uchar k;
- uchar n = Ge;
- uchar m = Shi;
- uchar x = Bai;
- for(k=0;k<8;k++)
- {
- if(n&0x80) HC595_SDI = 1;
- else HC595_SDI = 0; //判断当前发送位数据
- n <<= 1; //左移一位
- HC595_CLK = 0;
- //delay_ms(1);
- HC595_CLK = 1; //移位寄存器操作
- //delay_ms(1);
- }
- for(k=0;k<8;k++)
- {
- if(m&0x80)HC595_SDI =1;
- else HC595_SDI = 0; //判断当前发送位数据
- m <<= 1; //左移一位
- HC595_CLK = 0;
- //delay_ms(1);
- HC595_CLK = 1; //移位寄存器操作
- //delay_ms(1);
- }
-
- for(k=0;k<8;k++)
- {
- if(x&0x80)HC595_SDI =1;
- else HC595_SDI = 0; //判断当前发送位数据
- x <<= 1; //左移一位
- HC595_CLK = 0;
- //delay_ms(1);
- HC595_CLK = 1; //移位寄存器操作
- //delay_ms(1);
- }
-
- HC595_LE = 1;
- //delay_ms(1);
- HC595_LE = 0; //锁存数据
- //delay_ms(1);
- }
- //void HC595_Off(void)
- //{
- // //OE = 1;
- //}
- //void SMG(void)
- //{
- //
- // u8 key;
- // int a=0;//b=0, c=0;
- // unsigned char Bai_Wei,Shi_Wei,Ge_Wei; //千位,百位,十位,个位Qian_Wei,
- // //数码管编码
- // unsigned char table[] =
- // {0xc0,0xf9,0xa4,0xb0,0x99,
- // 0x92,0x82,0xf8,0x80,0x90,
- // 0xff,0x00,0x90,0x86,0xaf,
- // 0xc0,0x89,0xc7,0x8e,0xc1,0x7f};
- // //0,1,2,3,4,5,6,7,8,9,全暗,全亮,g,E,r,O,H,L,F,U,小数点,共阳数码管使用
- //
- //
- // key=KEY_Scan(0); //扫描按键
- // if(key==KEY_LEFT)
- // {
- // a++;
- //
- // }
- // Bai_Wei = table[a];
- // Shi_Wei = table[2];
- // Ge_Wei = table[3];
- // HC595_Display(Bai_Wei,Shi_Wei,Ge_Wei);
- //}
复制代码
- //2位数码管测试代码
- //樊川技术
- //实验现象:3位数码管轮流显示数码管编码表里面的字符
- #include "led.h"
- #include "delay.h"
- #include "sys.h"
- #include "HC595.h"
- #include "key.h"
- #include "time.h"
- unsigned char Qian_Wei,Bai_Wei,Shi_Wei,Ge_Wei; //千位,百位,十位,个位
- float d=0;
- //数码管编码
- unsigned char table[] =
- {0xc0,0xf9,0xa4,0xb0,0x99,
- 0x92,0x82,0xf8,0x80,0x90,
- 0xff,0x00,0x90,0x86,0xaf,
- 0xc0,0x89,0xc7,0x8e,0xc1,0x7f};
- //0,1,2,3,4,5,6,7,8,9,全暗,全亮,g,E,r,O,H,L,F,U,小数点,共阳数码管使用
- unsigned char table1[] =
- {0xc0,0xf9,0xa4,0xb0};
- //0,1,2,3,4,5,6,7,8,9,全暗,全亮,g,E,r,O,H,L,F,U,小数点,共阳数码管使用
- int KEY_ING=1;
- int main(void)
- {
- u8 key;
- int a=0,b = 0,c = 0;
- SystemInit(); //系统时钟初始化为72M SYSCLK_FREQ_72MHz
- delay_init(72); //延时函数初始化
- NVIC_Configuration(); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
- LED_Init(); //LED端口初始化
- HC595_Init();
- KEY_Init();
- TIM4_Init(1000,36000-1); //定时500ms
-
-
- // //清空LED数码管
- // Bai_Wei = table[10];//清空LED
- // Shi_Wei = table[10];//清空LED
- // Ge_Wei = table[10];//清空LED
- // HC595_Display(Bai_Wei, Shi_Wei,Ge_Wei);
- // delay_ms(3000);
-
-
- while(1)
- {
- key=KEY_Scan(0); //扫描按键
- switch(key)
- {
- case KEY_DOWN: KEY_ING=1;break; //按下K_DOWN按键 熄灭D2指示灯
- case KEY_LEFT: c=c+5;break; //按下K_LEFT按键 点亮D3指示灯
- case KEY_RIGHT: c=c-5;break; //按下K_RIGHT按键 熄灭D3指示灯
- }
- if(c == 10)
- {
- c = 0;
- b++;
- }
- if(c == -5)
- {
- c = 5;
- b--;
- }
- if(b == -1)
- {
- b = 9;
- a--;
- }
- if(b == 10)
- {
- b = 0;
- a++;
- }
- if(a == 3)
- {
- b= 0;
- c= 0;
- }
- if(a <0)
- {
- a=0;
- b=0;
- c= 0;
- }
-
- d=a*10+b+0.1*c;
- Bai_Wei = table1[a];
- Shi_Wei = table[b];
- Ge_Wei = table[c];
- HC595_Display(Bai_Wei,Shi_Wei,Ge_Wei);
- }
- }
复制代码
Keil代码下载:
STM32_3位数码管定时程序.rar
(295.17 KB, 下载次数: 190)
|