#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "delay.h"
/********************************硬件接口定义*********************************/
#define LS138A GPIO_Pin_7 //=P2^0; //138译码器管脚定义
#define LS138B GPIO_Pin_6 //=P2^1;
#define LS138C GPIO_Pin_5 //=P2^2;
/********************************宏定义*********************************/
#define LS138a(x) x ? GPIO_SetBits(GPIOB, LS138A): GPIO_ResetBits(GPIOB, LS138A)
#define LS138b(x) x ? GPIO_SetBits(GPIOB, LS138B): GPIO_ResetBits(GPIOB, LS138B)
#define LS138c(x) x ? GPIO_SetBits(GPIOB, LS138C): GPIO_ResetBits(GPIOB, LS138C)
/*
x ? a:b 是一个三目运算符,表达的意思就是判断x的值,如果为真,执行a(这里可以代表一串代码),
如果为假,则执行b,然后#define LS138a(x)属于带参数的宏定义,这里宏定义的意思就是在程序中,
如果执行LS138a(1); 那么就会执行GPIO_SetBits(GPIOB, LS138A);
如果执行LS138a(0); 则执行GPIO_ResetBits(GPIOB, LS138A);
*/
u16 Count0;
u8 Count;
unsigned long D[16], LedOut[8];
// 此表为 LED 的字模
//此表为 LED 的字模, 共“阴”极数码管的码表 0~9 段选
unsigned char Disp_Tab[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};
int main()
{
unsigned char i;
//初始化,输入输出模式设定
GPIO_InitTypeDef GPIO_InitStruct;//初始化 IO 参数
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//使能IO口时钟 GPIOB
//端口配置 38译码器:GPIOB.5~7 8位动态数码管:GPIOB.8~15
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7| //译码器
GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11|
GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15
;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;//速度 50MHz
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;//通用推挽输出
GPIO_Init(GPIOB,&GPIO_InitStruct);//根据设定参数配置 GPIO
delay_init();
while (1)
{
Count++;
if(Count==20)
{
Count0++;
Count=0;
}
D[0] =Count0;
D[1] =Count0;
/********以下将数据的值送到LED数码管显示*************/
LedOut[0]=Disp_Tab[D[1]%100000000/10000000]; //千位
LedOut[1]=Disp_Tab[D[1]%10000000/1000000]; //百位
LedOut[2]=Disp_Tab[D[1]%1000000/100000]; //十位
LedOut[3]=Disp_Tab[D[1]%100000/10000]; //个位
LedOut[4]=Disp_Tab[D[0]%10000/1000]; //;//千位
LedOut[5]=Disp_Tab[D[0]%1000/100];// ;//百位
LedOut[6]=Disp_Tab[D[0]%100/10];//十位
LedOut[7]=Disp_Tab[D[0]%10]; //个位
for(i=0; i<8; i++)
{
//LedOut=0x4f;
//GPIOB->BSRR = LedOut & 0x00FF; //屏蔽高位操作
//GPIOB->BRR = (~LedOut) & 0x00FF; //屏蔽高位操作
GPIO_Write(GPIOB , LedOut);
switch(i)//138译码位选
{ //138译码 A B C
case 0: //0 0 0
LS138a(0); LS138b(0); LS138c(0);//显示第0位
break;
case 1: //1 0 0
LS138a(1); LS138b(0); LS138c(0);//显示第1位
break;
case 2: //0 1 0
LS138a(0); LS138b(1); LS138c(0);//显示第2位
break;
case 3: //1 1 0
LS138a(1); LS138b(1); LS138c(0);//显示第3位
break;
case 4: //0 0 1
LS138a(0); LS138b(0); LS138c(1);//显示第4位
break;
case 5: //1 0 1
LS138a(1); LS138b(0); LS138c(1);//显示第5位
break;
case 6: //0 1 1
LS138a(0); LS138b(1); LS138c(1);//显示第6位
break;
case 7: //1 1 1
LS138a(1); LS138b(1); LS138c(1);//显示第7位
break;
}
delay_ms(300); //延时函数
}
}
}
这个是主程序,谱中的板子stm32f103c8,迷你版,程序下载进去之后,只有第一位亮,乱码 |