GPIO.c

#include "STM32Lib\\stm32f10x.h"

#include "hal.h"

/*******************************************************************************

* Function Name  : GPIO_Configuration
* 设置PD3,PD4,PD5,PD6为键盘输入
* 设置PB0,5,8,9; PC5,7; PD7 ;PA8 为输出LED灯
*******************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;

/*允许总线CLOCK,在使用GPIO之前必须允许相应端的时钟.
从STM32的设计角度上说,没被允许的端将不接入时钟,也就不会耗能,
这是STM32节能的一种技巧,*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);

/* PC8按键输入*/

GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;  //上拉输入
GPIO_Init(GPIOC, &GPIO_InitStructure);

/* PC9按键输入*/

GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;  //上拉输入
GPIO_Init(GPIOC, &GPIO_InitStructure);
}

hal.h

#ifndef HAL_H

#define HAL_H

//硬件初始化

extern void  ChipHalInit(void);
extern void  ChipOutHalInit(void);

//输入宏定义
#define GET_LEFT() (GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_8))
#define GET_RIGHT() (GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_9))
extern void USART1_Putc(u8 c);
extern void USART_Configuration(void);
extern void USART1_Puts(char * str);

#endif

TIM.c

#include "STM32Lib\\stm32f10x.h"

void Tim1_Configuration(void)

{
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
TIM_OCInitTypeDef  TIM_OCInitStructure;

GPIO_InitTypeDef GPIO_InitStructure;

/* PA8设置为功能脚(PWM) */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);

TIM_DeInit(TIM1);

/*TIM1时钟配置*/

TIM_TimeBaseStructure.TIM_Prescaler = 72;      //预分频(时钟分频)72M/72=1000K
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //向上计数
TIM_TimeBaseStructure.TIM_Period = 2000;       //装载值 1000k/2000=500hz
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x0;
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure);

/* Channel 1 Configuration in PWM mode */

TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;     //PWM模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;  //正向通道有效
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;//反向通道无效
TIM_OCInitStructure.TIM_Pulse = 300;        //占空时间
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;   //输出极性
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;     //互补端的极性  
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;  

TIM_OC1Init(TIM1,&TIM_OCInitStructure);       //通道1

/* TIM1 counter enable */

TIM_Cmd(TIM1,ENABLE);
/* TIM1 Main Output Enable */
TIM_CtrlPWMOutputs(TIM1,ENABLE);
}

//设置捕获寄存器1

void SetT1Pwm1(u16 pulse)
{
TIM1->CCR1=pulse;
}

main.c

/************************************************************

**实验名称:PWM
**功能:是PA8产生PWM输出,按键调节占空比
*************************************************************/

#include "STM32Lib\\stm32f10x.h"

#include "hal.h"
#define SIZE 0
u8    table[11]={"0123456789 "};
char buffer[10]={"0000000000"};
void Delay(u16 n);

void d_2_char(u32 x)

{
buffer[SIZE+0]=table[x%10000000000/100000];
buffer[SIZE+1]=table[x%1000000000/100000];
buffer[SIZE+2]=table[x%100000000/100000];
buffer[SIZE+3]=table[x%10000000/10000];
buffer[SIZE+4]=table[x%1000000/10000];
buffer[SIZE+5]=table[x%100000/10000];
buffer[SIZE+6]=table[x%10000/1000];
buffer[SIZE+7]=table[x%1000/100];
buffer[SIZE+8]=table[x%100/10];
buffer[SIZE+9]=table[x%10];
}

//延迟函数
void Delay(u16 speed)
{
u16 i;
while(speed!=0)
{
 speed--;
 for(i=0;i<400;i++);
}
}
extern void SetT1Pwm1(u16 pulse);

int main(void)

{
u16 pulse=300;
ChipHalInit();   //片内硬件初始化
ChipOutHalInit();  //片外硬件初始化
for(;;)
{
 if(GET_LEFT()==0)
 {
  while(GET_LEFT()==0);
  if(pulse<=2000)
  {
   pulse+=30;
  // SetT1Pwm1(pulse);
  }
  else
  {
   pulse=300;
  }
       d_2_char(pulse);
 USART1_Puts(buffer);
 USART1_Puts("\r\n");

 }

 if(GET_RIGHT()==0)

 {
  while(GET_RIGHT()==0);
  if(pulse<=2000)
  {
   pulse-=60;
  }
  else
  {
   pulse=1800;
  }

 d_2_char(pulse);

 USART1_Puts(buffer);
 USART1_Puts("\r\n");

 }

 SetT1Pwm1(pulse);
}
}