keyscan.h文件
//-----------------------------
// 自制8按键键盘驱动程序
//1:用AC1_14通道(I/O PC4),其中ADC1和PC时钟在
// Keyscan_init(void)已经打开,主函数中不另用PC4即可
//2:在用到的的程序中声明 #include"keyscan.h"
// 即可用,注:先要Keyscan_init(void),再用
// Keyscan(void)读取键值
//作者:hjw199089
//时间:11.8.17
//------------------------------------*/
#ifndef __KEYSCAN_H
#define __KEYSCAN_H
#include "stm32f10x.h"
extern void Keyscan_init(void);
//-----键值返回 0无键按下,1--8键值正常---//
extern u8 Keyscan(void);
#endif
keyscan.c文件
#include "stm32f10x.h"
#include "keyscan.h"
GPIO_InitTypeDef GPIO_InitStructure;
void ADC_Configuration(void);
void Keyscan_init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1|RCC_APB2Periph_GPIOC , ENPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //模拟输入引脚
GPIO_Init(GPIOC, &GPIO_InitStructure);
//------------------//
ADC_Configuration();
}
/
void ADC_Configuration(void)
{
ADC_InitTypeDef ADC_InitStructure;
/
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
/
ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1, ADC_SampleTime_13Cycles5);
/
ADC_Cmd(ADC1, ENABLE);
/
ADC_ResetCalibration(ADC1);
/
while(ADC_GetResetCalibrationStatus(ADC1));
/
ADC_StartCalibration(ADC1);
/
while(ADC_GetCalibrationStatus(ADC1));
}
//-----键值返回 0无键按下,1--8键值正常---//
u8 Keyscan(void)
{
u8 keynum;
u16 AD_value;
u16 temp;
temp=0x4ff;
keynum=0;
while(temp--)
{
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
AD_value=ADC_GetConversionValue(ADC1);
if((AD_value)*3.3/4096==0)
keynum=1;
if( (((AD_value)*3.3/4096) > 0.1)&&(((AD_value)*3.3/4096) < 0.2))
keynum=2;
if( (((AD_value)*3.3/4096) > 0.2)&&(((AD_value)*3.3/4096) < 1))
keynum=3;
if( (((AD_value)*3.3/4096) > 1)&&(((AD_value)*3.3/4096) < 1.68))
keynum=4;
if( (((AD_value)*3.3/4096) > 1.68)&&(((AD_value)*3.3/4096) < 2.34))
keynum=5;
if( (((AD_value)*3.3/4096) > 2.35)&&(((AD_value)*3.3/4096) < 2.55))
keynum=6;
if( (((AD_value)*3.3/4096) > 2.55)&&(((AD_value)*3.3/4096) < 2.85))
keynum=7;
if( (((AD_value)*3.3/4096) > 2.85)&&(((AD_value)*3.3/4096) < 3.05))
keynum=8;
if( (((AD_value)*3.3/4096) > 3.05)&&(((AD_value)*3.3/4096) <=3.3))
keynum=0;
ADC_SoftwareStartConvCmd(ADC1, DISABLE);
}
return(keynum);
}
main.c
#include "stm32f10x.h"
#include "string.h"
#include "stdio.h"
#include "keyscan.h"
/
void RCC_Configuration(void)
{
/
/
//RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1| RCC_APB2Periph_AFIO |
RCC_APB2Periph_GPIOA , ENABLE); //RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE| RCC_APB2Periph_GPIOB
//RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 , ENABLE);
}
/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
/
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/
void SysClock_Init(void)
{
ErrorStatus HSEStartUpStatus;
RCC_DeInit();
RCC_HSEConfig(RCC_HSE_ON);
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS){
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
FLASH_SetLatency(FLASH_Latency_2);
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLK2Config(RCC_HCLK_Div1);
RCC_PCLK1Config(RCC_HCLK_Div2);
RCC_ADCCLKConfig(RCC_PCLK2_Div4);//36M
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_7);//56M
RCC_PLLCmd(ENABLE);
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
while(RCC_GetSYSCLKSource() != 0x08)
{
;
}
}
}
/
void USART1_Init(unsigned int baud)
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = baud;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE);
}
//
void USART1_SendByte(unsigned char temp)
{
USART_SendData(USART1, temp);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
//
void USART1_Printf(char *pch)
{
while(*pch != '\0'){
USART1_SendByte(*(unsigned char *)pch);
pch++;
}
}
//
void Delay(unsigned short time)
{
unsigned short i, j;
for(; time > 0; time--){
for(j = 0; j < 10; j++){
for(i = 0; i < 1000; i++);
}
}
}
/
int main(void)
{
u8 key;
char string[50] = "";
SysClock_Init(); // 初始化系统时钟 72MHZ
RCC_Configuration(); // 使能外设
GPIO_Configuration(); // 配置引脚
NVIC_Configuration(); // 配置中断
USART1_Init(9600); // 配置串口1,波特率9600
Keyscan_init();
while(1)
{
key=Keyscan();
if(key)
{
USART_Cmd(USART1, ENABLE);
sprintf(string, "ADC:%d\r\n",key );
USART1_Printf(string);
USART_Cmd(USART1, DISABLE);
}
Delay(30);
}
}
下载程序,打开串口调试软件,波特率9600,按按键观察打印值即可
文章评论(0条评论)
登录后参与讨论