四位抢答器VHDL
四位抢答器
#include "stm32f10x_lib.h"
#include "stdio.h"
void RCC_Configuration(void);
void GPIO_Configuration(void);
void Key_Scan(void);
void delay(vu32 Count);
u8 Key_Flag=0;
u8 Key_Reset=0;
u8 Key_Start=0;
main(void)
{
#ifdef DEBUG
debug();
#endif
RCC_Configuration();
GPIO_Configuration();
GPIO_ResetBits(GPIOC, GPIO_Pin_6);
GPIO_ResetBits(GPIOC, GPIO_Pin_7);
GPIO_ResetBits(GPIOC, GPIO_Pin_8);
GPIO_ResetBits(GPIOC, GPIO_Pin_9);
while(1)
{
Key_Scan();
if(Key_Reset==1)
{
GPIO_ResetBits(GPIOC, GPIO_Pin_6);
GPIO_ResetBits(GPIOC, GPIO_Pin_7);
GPIO_ResetBits(GPIOC, GPIO_Pin_8);
GPIO_ResetBits(GPIOC, GPIO_Pin_9);
GPIO_ResetBits(GPIOD, GPIO_Pin_8);
GPIO_ResetBits(GPIOD, GPIO_Pin_9);
GPIO_ResetBits(GPIOD, GPIO_Pin_10);
GPIO_ResetBits(GPIOD, GPIO_Pin_11);
Key_Flag=0;
}
if(Key_Start==1)
{
switch(Key_Flag)
{
case 1:
Key_Flag=0;
Key_Start=0;
GPIO_SetBits(GPIOC, GPIO_Pin_6);
GPIO_Write(GPIOD,0x01
break;
case 2:
Key_Flag=0;
Key_Start=0;
GPIO_SetBits(GPIOC, GPIO_Pin_7);
GPIO_Write(GPIOD,0x02
break;
case 3:
Key_Flag=0;
Key_Start=0;
GPIO_SetBits(GPIOC, GPIO_Pin_8);
GPIO_Write(GPIOD,0x03
break;
case 4:
Key_Flag=0;
Key_Start=0;
GPIO_SetBits(GPIOC, GPIO_Pin_9);
GPIO_Write(GPIOD,0x04
break;
}
}
}
}
void Key_Scan(void)
{
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_10)==0)
{
Key_Reset=1;
Key_Start=0;
}
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11)==0)
{
Key_Start=1;
Key_Reset=0;
}
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_12)==0)
{
Key_Flag=1;
}
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13)==0)
{
Key_Flag=2;
}
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_14)==0)
{
Key_Flag=3;
}
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15)==0)
{
Key_Flag=4;
}
}
void RCC_Configuration(void)
{
ErrorStatus HSEStartUpStatus;
u8 SYSCLKSourceValue;
RCC_DeInit();
RCC_HSEConfig(RCC_HSE_ON);
HSEStartUpStatus=RCC_WaitForHSEStartUp();
if(HSEStartUpStatus==SUCCESS)
{
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* Flash 2 wait state */
FLASH_SetLatency(FLASH_Latency_2);
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLK1Config(RCC_HCLK_Div2);
RCC_PCLK2Config(RCC_HCLK_Div2);
RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);
RCC_PLLCmd(ENABLE);
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
SYSCLKSourceValue=RCC_GetSYSCLKSource();
while(SYSCLKSourceValue != 0x08)
{ }
}
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|
RCC_APB2Periph_GPIOD|
RCC_APB2Periph_GPIOB, ENABLE); }
void GPIO_Configuration(void) {
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure GPIOC pin0, pin1, pin2 and pin3 as Output push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13| GPIO_Pin_14| GPIO_Pin_15; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode =GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode =GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
}
void delay(vu32 Count)
{
for(Count;Count!=0;Count--);
}