蓝桥杯嵌入式SS_main.c_V0.1

#include "main.h"
#include "stdio.h"
#include "stdbool.h"
#include "string.h"
#include "rcc\bsp_rcc.h"
#include "key_led\bsp_key_led.h"
#include "lcd\bsp_lcd.h"
#include "UART\bsp_uart.h"//需要#define HAL_UART_MODULE_ENABLED,并添加uart.h和uart_ex.h
#include "i2c\bsp_i2c.h"
#include "adc\bsp_adc.h"//需要#define HAL_ADC_MODULE_ENABLED,并添加adc.h和adc_ex.h
#include "tim\bsp_tim.h"//需要#define HAL_UART_TIM_ENABLED,不用添加tim.h和timex.h
#include "rtc\bsp_rtc.h"//需要#define HAL_RTC_MODULE_ENABLED,并添加rtc.h和rtc_ex.h
#include "dma\bsp_dma.h"//不需要#define HAL_DMA_MODULE_ENABLED,不需要添加dma.h和dma_ex.h
#include "DAC\bsp_dac.h"//需要#define HAL_DMA_MODULE_ENABLED,并添加dma.h和dma_ex.h
__IO uint32_t uwTick_Key_Set_Point=0;//uwTick为24位
__IO uint32_t uwTick_Led_Set_Point=0;
__IO uint32_t uwTick_LCD_Set_Point=0;
__IO uint32_t uwTick_Uart1_Set_Point=0;
uint8_t ucKey_Val=0, ucKey_Down=0, ucKey_Up=0, ucKey_Old=0;
uint8_t ucLed=0;
uint8_t Lcd_Disp_String[21]={0};//最多显示20个字符
uint8_t Uart_String[21]={0};
uint8_t Uart_Rxbuf=0;
uint8_t EEPROM_String_1[5]={0x11,0x22,0x33,0x44,0x55};
uint8_t EEPROM_String_2[5]={0};
uint8_t RES_4017=0;
uint16_t InputPWM1_T=0;uint16_t InputPWM1_High=0;float InputPWM1_Duty=0;//TIM3 CH1 输入捕获PWM占空比和周期
uint16_t InputPWM2_T=0;uint16_t InputPWM2_High=0;float InputPWM2_Duty=0;//TIM2 CH1 输入捕获PWM占空比和周期
RTC_TimeTypeDef H_M_S_Time={0};//RTC时钟-时间
RTC_DateTypeDef Y_M_D_Date={0};//RTC时钟-日期
//DMA测试
//内存到内存测试
//在normal模式下数量寄存器的值CNDTR减到0后不会自动恢复
uint8_t DataA[6]={0x01,0x02,0x03,0x04,0X05,0X06};
uint8_t DataB[6]={0};
//内存到外设测试
uint8_t SendBuff[202]={0};			
//外设到内存测试
//在normal模式下数量寄存器的值CNDTR减到0后不会自动恢复
bool flag_finish=0;
uint16_t ADC1_Value[50];
uint32_t sum=0;
float adc1=0;
//在DMA_CIRCULAR模式下数量寄存器的值CNDTR减到0后会自动恢复,在主函数中处理读取的数组
const uint16_t Sine12bit[100]={	0x0800,0x0881,0x0901,0x0980,0x09FD,0x0A79,0x0AF2,0x0B68,0x0BDA,0x0C49,
																0x0CB3,0x0D19,0x0D79,0x0DD4,0x0E29,0x0E78,0x0EC0,0x0F02,0x0F3C,0x0F6F,	
																0x0F9B,0x0FBF,0x0FDB,0x0FEF,0x0FFB,0x0FFF,0x0FFB,0x0FEF,0x0FDB,0x0FBF,
																0x0F9B,0x0F6F,0x0F3C,0x0F02,0x0EC0,0x0E78,0x0E29,0x0DD4,0x0D79,0x0D19,
																0x0CB3,0x0C49,0x0BDA,0x0B68,0x0AF2,0x0A79,0x09FD,0x0980,0x0901,0x0881,
																0x0800,0x077F,0x06FF,0x0680,0x0603,0x0587,0x050E,0x0498,0x0426,0x03B7,	
																0x034D,0x02E7,0x0287,0x022C,0x01D7,0x0188,0x0140,0x00FE,0x00C4,0x0091,
																0x0065,0x0041,0x0025,0x0011,0x0005,0x0001,0x0005,0x0011,0x0025,0x0041,
																0x0065,0x0091,0x00C4,0x00FE,0x0140,0x0188,0x01D7,0x022C,0x0287,0x02E7,
																0x034D,0x03B7,0x0426,0x0498,0x050E,0x0587,0x0603,0x0680,0x06FF,0x077F,
};

void Key_Proc(void){
	if((uwTick -  uwTick_Key_Set_Point)<50)	return;
	uwTick_Key_Set_Point = uwTick;
	ucKey_Val = Key_Scan();
	ucKey_Down = ucKey_Val & (ucKey_Old ^ ucKey_Val); 
	ucKey_Up = ~ucKey_Val & (ucKey_Old ^ ucKey_Val);	
	ucKey_Old = ucKey_Val;
	if(ucKey_Down == 1)ucLed = 0x55;
	if(ucKey_Down == 2)ucLed = 0xaa;
	if(ucKey_Down == 3){//传输多次
		memset(DataB, 0, 6);
		__HAL_DMA_DISABLE(&hdma_memtomem_dma1_channel1);
		hdma_memtomem_dma1_channel1.Instance->CNDTR = 6;
		__HAL_DMA_ENABLE(&hdma_memtomem_dma1_channel1);
		HAL_DMA_PollForTransfer(&hdma_memtomem_dma1_channel1, HAL_DMA_FULL_TRANSFER, HAL_MAX_DELAY);
		DataA[0]++;DataA[0]&=0x0f;
		DataA[1]++;DataA[1]&=0x0f;
		DataA[2]++;DataA[2]&=0x0f;
		DataA[3]++;DataA[3]&=0x0f;
		DataA[4]++;DataA[4]&=0x0f;
		DataA[5]++;DataA[5]&=0x0f;
	}
	if(ucKey_Down==4){
		HAL_UART_Transmit_DMA(&huart1,(uint8_t *)SendBuff,202);
		//法一:直接重新开启DMA和恢复数量寄存器的值CNDTR
		//HAL_ADC_Start_DMA(&hadc1,(uint32_t*)&ADC1_Value,50);
		//法二:更改数量寄存器的值CNDTR,中断使能
		//DMA 中断完成后,单片机会设置中断标志位,取消中断使能,同时将 DMA 传输长度清零。
//		HAL_ADC_Stop(&hadc1);
//    __HAL_DMA_DISABLE(&hdma_adc1);
//    DMA1_Channel2->CNDTR = 50;
//    __HAL_DMA_ENABLE(&hdma_adc1);
//		HAL_ADC_Start(&hadc1);
//		__HAL_DMA_ENABLE_IT(&hdma_adc1, DMA_IT_HT | DMA_IT_TC | DMA_IT_TE);	
		
	}
}

void Led_Proc(void){
	LED_Disp(ucLed);
}

void LCD_Proc(void){
	uint8_t i=0;
	if((uwTick -  uwTick_LCD_Set_Point)<50)	return;
	uwTick_LCD_Set_Point = uwTick;
	sprintf((char *)Lcd_Disp_String,"UartC:%03d ADC1C:%02d",DMA2_Channel4->CNDTR,DMA1_Channel2->CNDTR);
	LCD_DisplayStringLine(Line7, Lcd_Disp_String);
	
	sprintf((char *)Lcd_Disp_String,"DACVal:%04X Cnt:%03d",HAL_DAC_GetValue(&hdac1,DAC_CHANNEL_1),DMA1_Channel3->CNDTR);
	LCD_DisplayStringLine(Line8, Lcd_Disp_String);
	
	sprintf((char *)Lcd_Disp_String,"M:%X%X%X%X%X R:%X",EEPROM_String_2[0],EEPROM_String_2[1],EEPROM_String_2[2],EEPROM_String_2[3],EEPROM_String_2[4],RES_4017);
	LCD_DisplayStringLine(Line0, Lcd_Disp_String);
	//main函数中只需要执行一次转换即可
	for(i=0;i<50;i++){
		sum+=ADC1_Value[i];
	}
	adc1=sum*3.3/50/4096;
	sum=0;
	sprintf((char *)Lcd_Disp_String,"R37:%4.2fV R38:%4.2fV",GetADC(&hadc2)*3.3/4096,adc1);
	LCD_DisplayStringLine(Line1, Lcd_Disp_String);
	
	sprintf((char *)Lcd_Disp_String, "R39:%4d %4.1f%%",1000000/InputPWM1_T,InputPWM1_Duty*100);
	LCD_DisplayStringLine(Line2, Lcd_Disp_String);	
	sprintf((char *)Lcd_Disp_String, "R40:%4d %4.1f%%",1000000/InputPWM2_T,InputPWM2_Duty*100);
	LCD_DisplayStringLine(Line3, Lcd_Disp_String);	
	
	HAL_RTC_GetTime(&hrtc, &H_M_S_Time, RTC_FORMAT_BIN);//读取日期和时间必须同时使用
	HAL_RTC_GetDate(&hrtc, &Y_M_D_Date, RTC_FORMAT_BIN);
	sprintf((char *)Lcd_Disp_String,"%02d-%02d-%02d %d %02d:%02d:%02d",Y_M_D_Date.Year,Y_M_D_Date.Month,Y_M_D_Date.Date,Y_M_D_Date.WeekDay,H_M_S_Time.Hours,H_M_S_Time.Minutes,H_M_S_Time.Seconds);
	LCD_DisplayStringLine(Line4,Lcd_Disp_String);

	sprintf((char *)Lcd_Disp_String,"%08X A:%X%X%X%X%X%X",(uint32_t)&DataA,DataA[0],DataA[1],DataA[2],DataA[3],DataA[4],DataA[5]);
	LCD_DisplayStringLine(Line5, Lcd_Disp_String);
	
	sprintf((char *)Lcd_Disp_String,"%08X B:%X%X%X%X%X%X",(uint32_t)&DataB,DataB[0],DataB[1],DataB[2],DataB[3],DataB[4],DataB[5]);
	LCD_DisplayStringLine(Line6, Lcd_Disp_String);
	
}
void UART_Proc(void){
//	static uint16_t counter=0;
//	if((uwTick -  uwTick_Uart1_Set_Point)<1000)	return;
//	uwTick_Uart1_Set_Point = uwTick;
//	sprintf((char *)Uart_String, "%04d:Hello,world.\r\n", counter);
//	HAL_UART_Transmit(&huart1,(unsigned char *)Uart_String, strlen((char *)Uart_String), 50);
//	if(++counter==10000)counter=0;
}

int main(void){
	uint8_t i=0;
  HAL_Init();           /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  SystemClock_Config(); /* Configure the system clock */
	
	for (i=0;i<200;i++)SendBuff[i]='a';
	SendBuff[200]='\n';SendBuff[201]='\r';
	KEY_LED_Init();
	I2CInit();i2c_24c02_write(EEPROM_String_1,0,5);HAL_Delay(1);i2c_24c02_read(EEPROM_String_2,0,5);write_resistor(0X66&0X7F);RES_4017=read_resistor();
	
	MX_DMA_Init();HAL_DMA_Start(&hdma_memtomem_dma1_channel1,(uint32_t)&DataA,(uint32_t)&DataB, 0);

	UART1_Init();HAL_UART_Receive_IT(&huart1,&Uart_Rxbuf,1);
	
	MX_ADC1_Init();HAL_ADC_Start_DMA(&hadc1,(uint32_t*)&ADC1_Value,50);MX_ADC2_Init();
	
	MX_DAC1_Init();MX_TIM7_Init();
	HAL_TIM_Base_Start(&htim7);
	//1.产生三角波,未使用到DMA
	HAL_DAC_SetValue(&hdac1,DAC_CHANNEL_1,DAC_ALIGN_12B_R,0X100);HAL_DAC_Start(&hdac1, DAC_CHANNEL_1);
	//2.产生正弦波,使用到DMA
//	HAL_DAC_Start_DMA(&hdac1, DAC_CHANNEL_1, (uint32_t*)Sine12bit,100, DAC_ALIGN_12B_R);
  

	Basic_TIM6_Init();
	HAL_TIM_Base_Start_IT(&htim6);
	InputPWM_TIM2_Init();HAL_TIM_Base_Start(&htim2);HAL_TIM_IC_Start_IT(&htim2,TIM_CHANNEL_1);HAL_TIM_IC_Start_IT(&htim2,TIM_CHANNEL_2);
	InputPWM_TIM3_Init();HAL_TIM_Base_Start(&htim3);HAL_TIM_IC_Start_IT(&htim3,TIM_CHANNEL_1);HAL_TIM_IC_Start_IT(&htim3,TIM_CHANNEL_2);
	OutputSQU_TIM15_Init(); HAL_TIM_OC_Start_IT(&htim15,TIM_CHANNEL_1);HAL_TIM_OC_Start_IT(&htim15,TIM_CHANNEL_2);
	OutputPWM_TIM16_Init();HAL_TIM_OC_Start(&htim16,TIM_CHANNEL_1);
	OutputPWM_TIM17_Init();HAL_TIM_OC_Start(&htim17,TIM_CHANNEL_1);
	MX_RTC_Init();
	LCD_Init();	LCD_Clear(White);	LCD_SetTextColor(Blue);	LCD_SetBackColor(White);	
  while(1){
		Key_Proc();
		Led_Proc();
		LCD_Proc();
		UART_Proc();
  }
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart){
	if(huart->Instance == USART1){
		ucLed=0Xaa;LED_Disp(ucLed);HAL_Delay(500);
		ucLed=0X55;LED_Disp(ucLed);HAL_Delay(500);
		ucLed=0X00;
		HAL_UART_Receive_IT(&huart1,&Uart_Rxbuf,1);
	}
}
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart){
	uint8_t i=0;
	static uint8_t count=0;
	if(huart->Instance == USART1){
		ucLed=count;
		for (i=0;i<200;i++)SendBuff[i]='a'+count;
		count++;
		count&=0x1f;
	}
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim){
	static uint16_t counter=0;
	if(htim->Instance==TIM6){
//		sprintf((char *)Uart_String, "%04d:Hello,world.\r\n", counter);
//		HAL_UART_Transmit(&huart1,(unsigned char *)Uart_String, strlen((char *)Uart_String), 50);
		if(++counter==10000)counter=0;
	}
}
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim){
	if(htim->Instance == TIM3){
		if(htim->Channel ==HAL_TIM_ACTIVE_CHANNEL_1){
			InputPWM1_T=HAL_TIM_ReadCapturedValue(&htim3,TIM_CHANNEL_1)+1;
			InputPWM1_Duty=1.0*InputPWM1_High/InputPWM1_T;
		}else if(htim->Channel ==HAL_TIM_ACTIVE_CHANNEL_2){
			InputPWM1_High=HAL_TIM_ReadCapturedValue(&htim3,TIM_CHANNEL_2)+1;
		}
	}
	if(htim->Instance == TIM2){
		if(htim->Channel ==HAL_TIM_ACTIVE_CHANNEL_1){
			InputPWM2_T=HAL_TIM_ReadCapturedValue(&htim2,TIM_CHANNEL_1)+1;
			InputPWM2_Duty=1.0*InputPWM2_High/InputPWM2_T;
		}else if(htim->Channel ==HAL_TIM_ACTIVE_CHANNEL_2){
			InputPWM2_High=HAL_TIM_ReadCapturedValue(&htim2,TIM_CHANNEL_2)+1;
		}
	}
}
void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim){
	if(htim->Instance ==TIM15){
		if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1){
			__HAL_TIM_SetCompare(&htim15,TIM_CHANNEL_1,(__HAL_TIM_GetCounter(&htim15)+499));//1K
		}else if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2){
			__HAL_TIM_SetCompare(&htim15,TIM_CHANNEL_2,(__HAL_TIM_GetCounter(&htim15)+99));//5K
		}
	}
}
void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc){
		ucLed|=0XfF;LED_Disp(ucLed);HAL_Delay(500);
		ucLed&=0X00;LED_Disp(ucLed);HAL_Delay(500);
}

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc){
		uint8_t i=0;
		for(i=0;i<50;i++){
			sum+=ADC1_Value[i];
		}
		adc1=sum*3.3/50/4096;
		sum=0;
}
//****************************************************************//
void Error_Handler(void){/* @brief  This function is executed in case of error occurrence. */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while(1);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值