/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include "stm32_eval.h"
#include <stdio.h>
//#include "ARMJISHU_TouchScreen_ADS7843.h"
#include "sys.h"
void USART_Configuration(void);
void RCC_Configuration(void);
void GPIO_Configuration(void);
void Delay(vu32 nCount);
void Delay1(vu32 nCount);
/* Values magic to the Board keys */
#define NOKEY 0
#define KEY1 1
#define KEY2 2
#define KEY3 3
#define KEY4 4
#define KEY5 5
/*按键相关定义*/
#define RCC_KEY1 RCC_APB2Periph_GPIOB
#define GPIO_KEY1_PORT GPIOB
#define GPIO_KEY1 GPIO_Pin_0
#define RCC_KEY2 RCC_APB2Periph_GPIOB
#define GPIO_KEY2_PORT GPIOB
#define GPIO_KEY2 GPIO_Pin_1
#define RCC_KEY3 RCC_APB2Periph_GPIOB
#define GPIO_KEY3_PORT GPIOB
#define GPIO_KEY3 GPIO_Pin_2
#define RCC_KEY4 RCC_APB2Periph_GPIOB
#define GPIO_KEY4_PORT GPIOB
#define GPIO_KEY4 GPIO_Pin_3
#define RCC_KEY5 RCC_APB2Periph_GPIOB
#define GPIO_KEY5_PORT GPIOB
#define GPIO_KEY5 GPIO_Pin_4
/* Private function prototypes -----------------------------------------------*/
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
/*static void Delay_ARMJISHU(__IO uint32_t nCount)
{
for (; nCount != 0; nCount--)
{
if(GPIO_ADS7843_INT_VALID)
{
ARMJISHU_TouchScreen_ADS7843();
}
}
} */
void delay(void)
{
unsigned long ik ;
for(ik=0;ik<0x0baf8;ik++) ;
}
void delay1(void)
{
unsigned long ik ;
for(ik=0;ik<0x00af8;ik++) ;
}
/* Private functions ---------------------------------------------------------*/
/*******************************************************************************
* Function Name : GPIO_KEY_Config.
* Description : Configures the Extension Button.
* Input : None.
* Output : None.
* Return : None.
*******************************************************************************/
void GPIO_KEY_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure KEY1 Button */
RCC_APB2PeriphClockCmd(RCC_KEY1, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin = GPIO_KEY1;
GPIO_Init(GPIO_KEY1_PORT, &GPIO_InitStructure);
/* Configure KEY2 Button */
RCC_APB2PeriphClockCmd(RCC_KEY2, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin = GPIO_KEY2;
GPIO_Init(GPIO_KEY2_PORT, &GPIO_InitStructure);
/* Configure KEY3 Button */
RCC_APB2PeriphClockCmd(RCC_KEY3, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin = GPIO_KEY3;
GPIO_Init(GPIO_KEY3_PORT, &GPIO_InitStructure);
/* Configure KEY4 Button */
RCC_APB2PeriphClockCmd(RCC_KEY4, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin = GPIO_KEY4;
GPIO_Init(GPIO_KEY4_PORT, &GPIO_InitStructure);
/* Configure KEY5 Button */
RCC_APB2PeriphClockCmd(RCC_KEY5, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin = GPIO_KEY5;
GPIO_Init(GPIO_KEY5_PORT, &GPIO_InitStructure);
}
/*******************************************************************************
* Function Name : ReadKey
* Description : Reads key from demoboard.
* Input : None
* Output : None
* Return : Return RIGHT, LEFT, SEL, UP, DOWN or NOKEY
*******************************************************************************/
u8 ReadKeyDown(void)
{
/* 1 key is pressed */
if(!GPIO_ReadInputDataBit(GPIO_KEY1_PORT, GPIO_KEY1)==0)
{
return KEY1;
}
/* 2 key is pressed */
if(!GPIO_ReadInputDataBit(GPIO_KEY2_PORT, GPIO_KEY2)==0)
{
return KEY2;
}
/* 3 key is pressed */
if(!GPIO_ReadInputDataBit(GPIO_KEY3_PORT, GPIO_KEY3)==0)
{
return KEY3;
}
/* 4 key is pressed */
if(!GPIO_ReadInputDataBit(GPIO_KEY4_PORT, GPIO_KEY4)==0)
{
return KEY4;
}
/* 5 key is pressed */
if(!GPIO_ReadInputDataBit(GPIO_KEY5_PORT, GPIO_KEY5)==0)
{
return KEY5;
}
/* No key is pressed */
else
{
return NOKEY;
}
}
void motor_Turn_on_1(void) //正转
{
GPIO_Write(GPIOA, 0x08);
delay();
GPIO_Write(GPIOA,0x0C);
delay();
GPIO_Write(GPIOA,0x04);
delay();
GPIO_Write(GPIOA,0x06);
delay();
GPIO_Write(GPIOA, 0x02);
delay();
GPIO_Write(GPIOA, 0x03);
delay();
GPIO_Write(GPIOA, 0x01);
delay();
GPIO_Write(GPIOA, 0x09);
delay();
}
void motor_Turn_on_2(void) //反转
{
GPIO_Write(GPIOA,0x09);
delay();
GPIO_Write(GPIOA,0x01);
delay();
GPIO_Write(GPIOA,0x03);
delay();
GPIO_Write(GPIOA,0x02);
delay();
GPIO_Write(GPIOA, 0x06);
delay();
GPIO_Write(GPIOA, 0x04);
delay();
GPIO_Write(GPIOA, 0x0C);
delay();
GPIO_Write(GPIOA, 0x08);
delay();
}
void motor_Turn_on_3(void) // 正转两圈
{
u8 i,j;
u8 r;
u8 N=64;
for(r=0;r<N;r++)
{
i=ReadKeyDown();
if(i==5)
{
break; //如果K5按下,退出此循环
}
for(j=0;j<64;j++) //电机外轴旋转一周,不是里里面面所看到的一周
{
GPIO_Write(GPIOA, 0x08);
delay();
GPIO_Write(GPIOA,0x0C);
delay();
GPIO_Write(GPIOA,0x04);
delay();
GPIO_Write(GPIOA,0x06);
delay();
GPIO_Write(GPIOA, 0x02);
delay();
GPIO_Write(GPIOA, 0x03);
delay();
GPIO_Write(GPIOA, 0x01);
delay();
GPIO_Write(GPIOA, 0x09);
delay();
}
}
}
void motor_Turn_on_4(void) // 反转两圈
{
u8 i,j;
u8 r;
u8 N=64;
for(r=0;r<N;r++)
{
i=ReadKeyDown();
if(i==5)
{
break; //如果K5按下,退出此循环
}
for(j=0;j<64;j++) //电机外轴旋转一周,不是里里面面所看到的一周
{
GPIO_Write(GPIOA,0x09);
delay();
GPIO_Write(GPIOA,0x01);
delay();
GPIO_Write(GPIOA,0x03);
delay();
GPIO_Write(GPIOA,0x02);
delay();
GPIO_Write(GPIOA, 0x06);
delay();
GPIO_Write(GPIOA, 0x04);
delay();
GPIO_Write(GPIOA, 0x0C);
delay();
GPIO_Write(GPIOA, 0x08);
delay();
}
}
}
void motor_Turn_on_5(void) // 停止
{
GPIO_ResetBits(GPIOA, GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3);
}
void motor_ccw(u8 key)
{
/* Turn Off Select LED */
switch(key)
{
case 0:
motor_Turn_on_1(); //正转
break;
case 1:
motor_Turn_on_2(); //反转
break;
case 2: //正转两圈
motor_Turn_on_3();
break;
case 3:
motor_Turn_on_4(); //反转两圈
break;
case 4:
motor_Turn_on_5(); //停止
break;
default:
motor_Turn_on_5();
break;
}
}
int main(void)
{
// int m=2, n=1;
u8 KeyNum = 0;
#ifdef DEBUG
debug();
#endif
USART_Configuration(); //必须放在前面
RCC_Configuration();
GPIO_Configuration();
// ADS7843_Init();
while (1)
{
KeyNum=ReadKeyDown();
motor_ccw(KeyNum-1);
}
}
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 115200;
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;
STM_EVAL_COMInit(COM1, &USART_InitStructure);
}
void RCC_Configuration()
{
//定义错误状态变量
ErrorStatus HSEStartUpStatus;
//将RCC寄存器重新设置为默认值
RCC_DeInit();
//打开外部高速时钟晶振
RCC_HSEConfig(RCC_HSE_ON);
//等待外部高速时钟晶振工作
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS)
{
//设置AHB时钟(HCLK)为系统时钟
RCC_HCLKConfig(RCC_SYSCLK_Div1);
//设置高速AHB时钟(APB2)为HCLK时钟
RCC_PCLK2Config(RCC_HCLK_Div1);
//设置低速AHB时钟(APB1)为HCLK的2分频
RCC_PCLK1Config(RCC_HCLK_Div2);
//设置FLASH代码延时
FLASH_SetLatency(FLASH_Latency_2);
//使能预取指缓存
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
//设置PLL时钟,为HSE的9倍频 8MHz * 9 = 72MHz
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
//使能PLL
RCC_PLLCmd(ENABLE);
//等待PLL准备就绪
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
//设置PLL为系统时钟源
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
//判断PLL是否是系统时钟
while(RCC_GetSYSCLKSource() != 0x08);
}
//开启TIM3的时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
//开启GPIOB的时钟和复用功能
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO,ENABLE);
}
void GPIO_Configuration()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//打开外设A,的时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//IO口的方向
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化外设0,1,2,3,6的端口
}
/**
* [url=home.php?mod=space&uid=247401]@brief[/url] Retargets the C library printf function to the USART.
* @param None
* @retval None
*/
PUTCHAR_PROTOTYPE
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
USART_SendData(EVAL_COM1, (uint8_t) ch);
/* Loop until the end of transmission */
while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TC) == RESET)
{
}
return ch;
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
/**
* @}
*/
/**
* @}
*/
/******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
,程序编译没问题,但实际运行按键根本不起作用,通电后电机就按一个方向转。。仿真发现再没按按键情况下,程序会直接return KEY4; 前面的key1,key2,key3不返回。按键本身都没问题,求大神赐教是设么原因。。帮忙找找错,