蓝桥杯嵌入式第六届决赛-电压测量与互补PWM输出

一、题目在这里插入图片描述
二、代码
1.初始化代码
tx.c

#include "tx.h"	

void LED_Init(void){
  GPIO_InitTypeDef   GPIO_InitStructure;
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD, ENABLE);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_Init(GPIOD, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin = 0xff00;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_Init(GPIOC, &GPIO_InitStructure);

  GPIOD->ODR|=(1<<2);
  GPIOC->ODR|=0xff00;
  GPIOD->ODR&=~(1<<2);
}


void KEY_Init(void){
  GPIO_InitTypeDef   GPIO_InitStructure;
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_8;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
}

u8 key_status[4]={1,1,1,1};
void KEY_Driver(void){
  u8 i;
  static u8 backup[4]={1,1,1,1};
  for(i=0;i<4;i++){
    if(backup[i]!=key_status[i]){
	  if(backup[i]!=0){
	  	Key_action(i+1);
	  }
	  backup[i]=key_status[i];
	}
  }
}

void KEY_Scan(void){
  u8 i;
  static u8 key_buf[4]={0xff,0xff,0xff,0xff};
  key_buf[0]=(key_buf[0]<<1)|KEY1;
  key_buf[1]=(key_buf[1]<<1)|KEY2;
  key_buf[2]=(key_buf[2]<<1)|KEY3;
  key_buf[3]=(key_buf[3]<<1)|KEY4;
  for(i=0;i<4;i++){
    if(key_buf[i]==0x00){
	  key_status[i]=0;
	}
	else if(key_buf[i]==0xff){
	  key_status[i]=1;
	}
  }
}

void Adc1_Init(void){
  ADC_InitTypeDef ADC_InitStructure;
  GPIO_InitTypeDef GPIO_InitStructure;

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOB, ENABLE);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

  ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
  ADC_InitStructure.ADC_ScanConvMode = DISABLE;
  ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
  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_Cmd(ADC1, ENABLE);

  /* Enable ADC1 reset calibration register */   
  ADC_ResetCalibration(ADC1);
  /* Check the end of ADC1 reset calibration register */
  while(ADC_GetResetCalibrationStatus(ADC1));

  /* Start ADC1 calibration */
  ADC_StartCalibration(ADC1);
  /* Check the end of ADC1 calibration */
  while(ADC_GetCalibrationStatus(ADC1));
}

u16 Get_adc(void){
  u16 temp;
  ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_239Cycles5);
  ADC_SoftwareStartConvCmd(ADC1, ENABLE);
  while(ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC)==0);
  temp=ADC_GetConversionValue(ADC1);
  ADC_SoftwareStartConvCmd(ADC1, DISABLE);
  return temp;
}

void Tim4_Init(u16 arr,u16 psc){
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  NVIC_InitTypeDef NVIC_InitStructure;

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);

  NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

  TIM_TimeBaseStructure.TIM_Period = arr-1;
  TIM_TimeBaseStructure.TIM_Prescaler = psc-1;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
  TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);

  /* TIM2 enable counter */
  TIM_Cmd(TIM4, ENABLE);
}

void Tim1_Init(u32 fre,u32 duty,u8 en){
  u16 arr;
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  TIM_OCInitTypeDef  TIM_OCInitStructure;
  GPIO_InitTypeDef GPIO_InitStructure;

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);

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

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

  arr=1000000/fre;

  TIM_TimeBaseStructure.TIM_Prescaler = 71;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseStructure.TIM_Period = arr-1;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_RepetitionCounter = 4; 
  TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);

  /* Channel 1 Configuration in PWM mode */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
  if(en){
  	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
  }
  else{
  	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable;
    TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;
  }
  TIM_OCInitStructure.TIM_Pulse = (arr-1)*duty/100;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
  TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;
  TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
  TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;

  TIM_OC2Init(TIM1, &TIM_OCInitStructure);  
  /* TIM1 counter enable */
  TIM_Cmd(TIM1, ENABLE);
  TIM_CtrlPWMOutputs(TIM1, ENABLE);
}

tx.h

/*
  程序说明: CT117E嵌入式竞赛板LCD驱动程序
  软件环境: Keil uVision 4.10 
  硬件环境: CT117E嵌入式竞赛板
  日    期: 2011-8-9
*/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __TX_H
#define __TX_H

/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"

#define KEY1 GPIO_ReadInputDataBit( GPIOA,  GPIO_Pin_0)
#define KEY2 GPIO_ReadInputDataBit( GPIOA,  GPIO_Pin_8)
#define KEY3 GPIO_ReadInputDataBit( GPIOB,  GPIO_Pin_1)
#define KEY4 GPIO_ReadInputDataBit( GPIOB,  GPIO_Pin_2)

void LED_Init(void);
void KEY_Init(void);
void KEY_Driver(void);
void KEY_Scan(void);
void Adc1_Init(void);
u16 Get_adc(void);
void Tim4_Init(u16 arr,u16 psc);
extern void Key_action(int code);  
void Tim1_Init(u32 fre,u32 duty,u8 en);

#endif /* __TX_H */

/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/

i2c.c追加

void i2c_write(u16 add,u8 data){
  I2CStart();
  I2CSendByte(0xa0);
  I2CWaitAck();
  I2CSendByte(add);
  I2CWaitAck();
  I2CSendByte(data);
  I2CWaitAck();
  I2CStop();
}

u8 i2c_read(u16 add){
  u8 temp;
  I2CStart();
  I2CSendByte(0xa0);
  I2CWaitAck();
  I2CSendByte(add);
  I2CWaitAck();

  I2CStart();
  I2CSendByte(0xa1);
  I2CWaitAck();
  temp=I2CReceiveByte();
  I2CSendAck();
  I2CStop();
  return temp;
}

2.中断函数
stm32f10x_it.h

extern u8 ms200_flag;
void TIM4_IRQHandler(void)
{
  static u16 ms200_count=0;
  if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET)
  {
    TIM_ClearITPendingBit(TIM4, TIM_IT_Update);
	KEY_Scan();
	ms200_count++;
	if(ms200_count>=100){
	  ms200_count=0;
	  ms200_flag=1;
	}
  }
}

3.主函数
main.c

#include "stm32f10x.h"
#include "lcd.h"
#include "i2c.h"
#include "tx.h"
#include "stdio.h"

u32 TimingDelay = 0;

u8 ms200_flag=1;
u8 SET_flag=0;//0-参数界面,1-设置界面
u8 Out_flag=0;//0-停止,1-开始

u16 fre=1;//以KHZ为单位
u16 duty; //占空比

float adc_value;

u16 LED_MODE=0xffff;

u8 str[20];	  

void Refrsh(void);
void Show_real(void);
void Show_set(void);
void Key_action(int code);
void Delay_Ms(u32 nTime);

//Main Body
int main(void)
{
	SysTick_Config(SystemCoreClock/1000);

	Delay_Ms(200);
	
	STM3210B_LCD_Init();
	LCD_Clear(Blue);
	LCD_SetBackColor(Blue);
	LCD_SetTextColor(White);
	
	i2c_init();
	LED_Init();
	KEY_Init();
	Adc1_Init();
	Tim4_Init(2000,72);//定时200ms
	Tim1_Init(1000,20,0);
	
	fre=i2c_read(0x00);
	Delay_Ms(5);

	while(1){
	  KEY_Driver();
	  if(ms200_flag){
	    ms200_flag=0;
		Refrsh();
		if(SET_flag==0)
		  Show_real();
		else
		  Show_set();
	  }
	}
}

void Refrsh(void){
  adc_value=Get_adc()*3.3/4096;
  duty=adc_value*100/3.3;
  
  if(Out_flag==0){ //停止
  	Tim1_Init(fre*1000,duty,0);
	LED_MODE|=(1<<8);
	GPIOC->ODR=LED_MODE;
	GPIOD->ODR|=(1<<2);
    GPIOD->ODR&=~(1<<2);
  }
  else if(Out_flag==1){ //停止
  	Tim1_Init(fre*1000,duty,1);
	LED_MODE&=~(1<<8);
	GPIOC->ODR=LED_MODE;
	GPIOD->ODR|=(1<<2);
    GPIOD->ODR&=~(1<<2);
  }
}

void Show_real(void){
  sprintf((char*)str,"       Parameter    ");
  LCD_DisplayStringLine(Line1, str);

  sprintf((char*)str," ADC: %.2fV      ",adc_value);
  LCD_DisplayStringLine(Line3, str);

  if(Out_flag==0)
    sprintf((char*)str," Status: Pause      ");
  else
    sprintf((char*)str," Status: Running      ");
  LCD_DisplayStringLine(Line5, str);

  sprintf((char*)str," Signal:  PA9:  %d%%    ",duty);
  LCD_DisplayStringLine(Line7, str);
  sprintf((char*)str,"          PB14: %d%%    ",100-duty);
  LCD_DisplayStringLine(Line8, str);
  sprintf((char*)str,"          %dKHz         ",fre);
  LCD_DisplayStringLine(Line9, str);
}

void Show_set(void){
  sprintf((char*)str,"       Setting        ");
  LCD_DisplayStringLine(Line1, str);
  sprintf((char*)str,"                         ");
  LCD_DisplayStringLine(Line3, str);
  LCD_DisplayStringLine(Line7, str);
  LCD_DisplayStringLine(Line8, str);
  LCD_DisplayStringLine(Line9, str);
  sprintf((char*)str," Signal Fre: %dKHz   ",fre);
  LCD_DisplayStringLine(Line5, str);
}

void Key_action(int code){
  if(code==1){
    Out_flag^=1;
  }
  else if(code==2){
    SET_flag^=1;
	if(SET_flag==0){
	   i2c_write(0x00,fre);	
	   Delay_Ms(5);
	}
  }
  else if(code==3){
  	if(SET_flag==1){
	  fre++;
	  if(fre>10)
	    fre=1;
	}
  }
}
void Delay_Ms(u32 nTime)
{
	TimingDelay = nTime;
	while(TimingDelay != 0);	
}

这里用到了高级定时器Tim1,PA9和PB14都是Tim1_Ch2,是互补的。设置一个,另一个自动设置好了。

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值