- #include "stm32f10x.h"
- #include"sys.h"
- u8 KeyValue=0;
- void Delay_Ms( u16 time );
- u8 anjian(void);
- u8 table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //Õý³£¹²Ñô¼«´úÂë
- void GPIO_Configuration(void);
- int main()
- {
-
- GPIO_Configuration();
-
- while(1)
- {
- anjian();
- GPIO_ResetBits (GPIOC, GPIO_Pin_0);
- GPIOA->ODR=table[KeyValue%10];
- Delay_Ms(5);
- GPIO_SetBits(GPIOC, GPIO_Pin_0);
- GPIO_ResetBits (GPIOC, GPIO_Pin_1);
- GPIOA->ODR=table[KeyValue/10];
- Delay_Ms(5);
- GPIO_SetBits(GPIOC, GPIO_Pin_1);
- }
- }
- void GPIO_Configuration(void) //GPIO¿ÚÅäÖÃ
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
- //LED
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
- GPIO_InitStructure.GPIO_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;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_Init(GPIOA,&GPIO_InitStructure);
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_SetBits(GPIOB, GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3);
- GPIO_ResetBits(GPIOB, GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7);
-
- }
- void delay_ms(u16 time) //ÑÓʱº¯Êý½èÓñðÈ˵ĸÄÌì×ÐϸÀí½â´ó¸Å1000Ϊ1s
- {
- u16 i=0;
- while(time--)
- {
- i=12000;
- while(i--);
- }
- }
- u8 anjian(void)
- {
-
- if((GPIO_ReadInputData(GPIOB)&0xff)!=0x0f)
- {
- Delay_Ms(10);
- if((GPIO_ReadInputData(GPIOB)&0xff)!=0x0f)
- {
- GPIO_SetBits(GPIOB, GPIO_Pin_0);
- GPIO_ResetBits(GPIOB, GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3);
- switch(GPIO_ReadInputData(GPIOB)&0xff)
- {
- case 0x11: KeyValue = 1; break;
- case 0x21: KeyValue = 5; break;
- case 0x41: KeyValue = 9; break;
- case 0x81: KeyValue = 13;break;
- }
- GPIO_SetBits(GPIOB, GPIO_Pin_1);
- GPIO_ResetBits(GPIOB, GPIO_Pin_0 | GPIO_Pin_2 | GPIO_Pin_3);
- switch(GPIO_ReadInputData(GPIOB)&0xff)
- {
- case 0x12: KeyValue = 2; break;
- case 0x22: KeyValue = 6; break;
- case 0x42: KeyValue = 10;break;
- case 0x82: KeyValue = 14;break;
- }
- GPIO_SetBits(GPIOB, GPIO_Pin_2);
- GPIO_ResetBits(GPIOB, GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_3);
- switch(GPIO_ReadInputData(GPIOB)&0xff)
- {
- case 0x14: KeyValue = 3; break;
- case 0x24: KeyValue = 7; break;
- case 0x44: KeyValue = 11;break;
- case 0x84: KeyValue = 15;break;
- }
- GPIO_SetBits(GPIOB, GPIO_Pin_3);
- GPIO_ResetBits(GPIOB, GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2);
- switch(GPIO_ReadInputData(GPIOB)&0xff)
- {
- case 0x18: KeyValue = 4; break;
- case 0x28: KeyValue = 8; break;
- case 0x48: KeyValue = 12;break;
- case 0x88: KeyValue = 16;break;
- }
- GPIO_SetBits(GPIOB, GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3);
- GPIO_ResetBits(GPIOB, GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 |GPIO_Pin_7);
- while((GPIO_ReadInputData(GPIOB)&0xff)!=0x0f)
- {
- GPIO_ResetBits (GPIOC, GPIO_Pin_0);
- GPIOA->ODR=table[KeyValue%10];
- Delay_Ms(5);
- GPIO_SetBits(GPIOC, GPIO_Pin_0);
- GPIO_ResetBits (GPIOC, GPIO_Pin_1);
- GPIOA->ODR=table[KeyValue/10];
- Delay_Ms(5);
- GPIO_SetBits(GPIOC, GPIO_Pin_1);
- };
- return KeyValue;
- }
- }
- return 0;
- }
- void Delay_Ms( u16 time )
- {
- u16 i,j;
- for( i = 0; i < time; i++ )
- for( j = 10000; j > 0; j-- );
- }
|