我今天搞了一下红外,把PC10接到STM8开发板的红外腿上。
原理图:
STM32U5:
STM8:
主程序代码:
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
#define TXBUFFERSIZE (COUNTOF(aTxBuffer)+4)
/* USER CODE END Includes */
uint8_t aTxBuffer[] = "Key value:0x \r\n ";
/* Private variables ---------------------------------------------------------*/
TIM_HandleTypeDef htim1;
UART_HandleTypeDef huart2;
u16 Interval_tim1=0;
uint8_t Data[8];
/* USER CODE BEGIN PV */
/* USER CODE END PV */
typedef void (* BSP_EXTI_LineCallback)(void);
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void SystemPower_Config(void);
static void MX_GPIO_Init(void);
static void MX_TIM1_Init(void);
static void MX_USART2_UART_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
static void MX_ICACHE_Init(void)
{
/* USER CODE BEGIN ICACHE_Init 0 */
/* USER CODE END ICACHE_Init 0 */
/* USER CODE BEGIN ICACHE_Init 1 */
/* USER CODE END ICACHE_Init 1 */
/** Enable instruction cache in 1-way (direct mapped cache)
*/
if (HAL_ICACHE_ConfigAssociativityMode(ICACHE_1WAY) != HAL_OK)
{
Error_Handler();
}
if (HAL_ICACHE_Enable() != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN ICACHE_Init 2 */
/* USER CODE END ICACHE_Init 2 */
}
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
void HAL_GPIO_EXTI_Falling_Callback(uint16_t GPIO_Pin)
{
if (GPIO_Pin == GPIO_PIN_10)
{
VS838_Receive_Handle();
}
//HAL_UART_Transmit(&huart2, (uint8_t *)aTxBuffer8, 5, 5000);
}
static void MX_GPIO_EXTI_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOC_CLK_ENABLE();
/*Configure GPIO pin : PC10 */
GPIO_InitStruct.Pin = GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
/* EXTI interrupt init*/
HAL_NVIC_SetPriority(EXTI10_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(EXTI10_IRQn);
/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
}
void HAL_GPIO_EXTI_IRQHandler1(uint16_t GPIO_Pin)
{
/* EXTI line interrupt detected */
if (__HAL_GPIO_EXTI_GET_RISING_IT(GPIO_Pin) != 0U)
{
__HAL_GPIO_EXTI_CLEAR_RISING_IT(GPIO_Pin);
HAL_GPIO_EXTI_Rising_Callback(GPIO_Pin);
}
if (__HAL_GPIO_EXTI_GET_FALLING_IT(GPIO_Pin) != 0U)
{
__HAL_GPIO_EXTI_CLEAR_FALLING_IT(GPIO_Pin);
HAL_GPIO_EXTI_Falling_Callback(GPIO_Pin);
}
}
/**
* [url=home.php?mod=space&uid=247401]@brief[/url] The application entry point.
* @retval int
*/
int main(void)
{
u8 wan,qian,bai,shi,ge;
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* Configure the System Power */
SystemPower_Config();
/* USER CODE BEGIN SysInit */
MX_ICACHE_Init();
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_GPIO_EXTI_Init();
MX_TIM1_Init();
MX_USART2_UART_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
HAL_TIM_Base_Start(&htim1);
while (1)
{
/* USER CODE END WHILE */
if( VS838_receive_ok == 1 ) //Ò»Ö¡ºìÍâÊý¾Ý½ÓÊÕÍê³É
{
VS838_receive_ok=0;
aTxBuffer[13]=(uint8_t)(VS838_Receive_Data>>28);
if(aTxBuffer[13]>=0 && aTxBuffer[13]<=9)
aTxBuffer[13]+=0x30;
else if(aTxBuffer[13]>=10 && aTxBuffer[13]<=15)
aTxBuffer[13]+=55;
aTxBuffer[14]=(uint8_t)((VS838_Receive_Data>>24)&0x0f);
if(aTxBuffer[14]>=0 && aTxBuffer[14]<=9)
aTxBuffer[14]+=0x30;
else if(aTxBuffer[14]>=10 && aTxBuffer[14]<=15)
aTxBuffer[14]+=55;
aTxBuffer[15]=(uint8_t)((VS838_Receive_Data>>20)&0x0f);
if(aTxBuffer[15]>=0 && aTxBuffer[15]<=9)
aTxBuffer[15]+=0x30;
else if(aTxBuffer[15]>=10 && aTxBuffer[15]<=15)
aTxBuffer[15]+=55;
aTxBuffer[16]=(uint8_t)((VS838_Receive_Data>>16)&0x0f);
if(aTxBuffer[16]>=0 && aTxBuffer[16]<=9)
aTxBuffer[16]+=0x30;
else if(aTxBuffer[16]>=10 && aTxBuffer[16]<=15)
aTxBuffer[16]+=55;
aTxBuffer[17]=(uint8_t)((VS838_Receive_Data>>12)&0x0f);
if(aTxBuffer[17]>=0 && aTxBuffer[17]<=9)
aTxBuffer[17]+=0x30;
else if(aTxBuffer[17]>=10 && aTxBuffer[17]<=15)
aTxBuffer[17]+=55;
aTxBuffer[18]=(uint8_t)((VS838_Receive_Data>>8)&0x0f);
if(aTxBuffer[18]>=0 && aTxBuffer[18]<=9)
aTxBuffer[18]+=0x30;
else if(aTxBuffer[18]>=10 && aTxBuffer[18]<=15)
aTxBuffer[18]+=55;
aTxBuffer[19]=(uint8_t)((VS838_Receive_Data>>4)&0x0f);
if(aTxBuffer[19]>=0 && aTxBuffer[19]<=9)
aTxBuffer[19]+=0x30;
else if(aTxBuffer[19]>=10 && aTxBuffer[19]<=15)
aTxBuffer[19]+=55;
aTxBuffer[20]=(uint8_t)(VS838_Receive_Data&0x0f);
if(aTxBuffer[20]>=0 && aTxBuffer[20]<=9)
aTxBuffer[20]+=0x30;
else if(aTxBuffer[20]>=10 && aTxBuffer[20]<=15)
aTxBuffer[20]+=55;
if (HAL_UART_Transmit(&huart2, (uint8_t *)aTxBuffer, TXBUFFERSIZE, 5000) != HAL_OK)
{
Error_Handler();
}
VS838_Receive_Data=0;
}
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE4) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
RCC_OscInitStruct.MSIState = RCC_MSI_ON;
RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT;
RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_4;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
|RCC_CLOCKTYPE_PCLK3;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB3CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief Power Configuration
* @retval None
*/
static void SystemPower_Config(void)
{
HAL_PWREx_EnableVddIO2();
/*
* Disable the internal Pull-Up in Dead Battery pins of UCPD peripheral
*/
HAL_PWREx_DisableUCPDDeadBattery();
/*
* Switch to SMPS regulator instead of LDO
*/
if (HAL_PWREx_ConfigSupply(PWR_SMPS_SUPPLY) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN PWR */
/* USER CODE END PWR */
}
/**
* @brief TIM1 Initialization Function
* @param None
* @retval None
*/
static void MX_TIM1_Init(void)
{
/* USER CODE BEGIN TIM1_Init 0 */
/* USER CODE END TIM1_Init 0 */
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
/* USER CODE BEGIN TIM1_Init 1 */
/* USER CODE END TIM1_Init 1 */
htim1.Instance = TIM1;
htim1.Init.Prescaler = 3;
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
htim1.Init.Period = 60000;
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim1.Init.RepetitionCounter = 0;
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM1_Init 2 */
/* USER CODE END TIM1_Init 2 */
}
/**
* @brief USART2 Initialization Function
* @param None
* @retval None
*/
static void MX_USART2_UART_Init(void)
{
/* USER CODE BEGIN USART2_Init 0 */
/* USER CODE END USART2_Init 0 */
/* USER CODE BEGIN USART2_Init 1 */
/* USER CODE END USART2_Init 1 */
huart2.Instance = USART2;
huart2.Init.BaudRate = 115200;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart2.Init.ClockPrescaler = UART_PRESCALER_DIV1;
huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_SetTxFifoThreshold(&huart2, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_SetRxFifoThreshold(&huart2, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_DisableFifoMode(&huart2) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART2_Init 2 */
/* USER CODE END USART2_Init 2 */
}
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOF_CLK_ENABLE();
__HAL_RCC_GPIOG_CLK_ENABLE();
/*Configure GPIO pin Output Level */
/*Configure GPIO pin : USER_BUTTON_Pin */
GPIO_InitStruct.Pin = GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
/*Configure GPIO pin : VBUS_SENSE_Pin */
/*Configure GPIO pin : PB15 */
/*Configure GPIO pins : PA9 PA10 */
/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief Period elapsed callback in non blocking mode
* [url=home.php?mod=space&uid=536309]@NOTE[/url] This function is called when TIM17 interrupt took place, inside
* HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
* a global variable "uwTick" used as application time base.
* @param htim : TIM handle
* @retval None
*/
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
/* USER CODE BEGIN Callback 0 */
/* USER CODE END Callback 0 */
if (htim->Instance == TIM17) {
HAL_IncTick();
}
/* USER CODE BEGIN Callback 1 */
/* USER CODE END Callback 1 */
}
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#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 CODE BEGIN 6 */
/* 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) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
vs838.c:
#include "main.h"
u8 VS838_Status=0; //ºìÍâ½ÓÊÕ´¦Àí״̬
u8 VS838_Receive_Count=0; //ºìÍâ½ÓÊÕÊý¾Ýλ¼ÆÊý
u32 VS838_Receive_Data=0; //32λµÄºìÍâ½ÓÊÕÊý¾Ý
u8 VS838_receive_ok=0; //ºìÍâ½ÓÊÕÍê³É±êÖ¾
extern TIM_HandleTypeDef htim1;
extern UART_HandleTypeDef huart2;
/*
********************************************************************************
GPIO³õʼ»¯
********************************************************************************
*/
void VS838_GPIO(void)
{
}
/*
********************************************************************************
¶¨Ê±Æ÷³õʼ»¯
********************************************************************************
*/
void TIM2_Config(void)//¶¨Ê±Æ÷³õʼ»¯
{
}
/*
********************************************************************************
ºìÍâ½âÂë³õʼ»¯º¯Êý
********************************************************************************
*/
void VS838_Init(void)//ºìÍâ½âÂë³õʼ»¯º¯Êý
{
VS838_GPIO();
TIM2_Config();
}
/*
********************************************************************************
ºìÍâ½ÓÊÕÊý¾Ý´¦Àíº¯Êý
********************************************************************************
*/
uint8_t VS838_Process(void)//ºìÍâ½ÓÊÕÊý¾Ý´¦Àíº¯
{
uint8_t VS838_num=0; //×îÖÕ´¦ÀíºóµÄ¼üÖµ·µ»ØÖµ
uint8_t Address_H,Address_L; //µØÖ·Âë,µØÖ··´Âë
uint8_t Data_H,Data_L; //Êý¾ÝÂë,Êý¾Ý·´Âë
if(VS838_receive_ok==1) //½ÓÊÕÍê³É
{
Address_H=VS838_Receive_Data>>24; //µÃµ½µØÖ·Âë
Address_L=(VS838_Receive_Data>>16)&0xff; //µÃµ½µØÖ··´Âë
if((Address_H==(uint8_t)~Address_L))//¼ìÑéÒ£¿Øʶ±ðÂë(ID)¼°µØÖ·
{
Data_H=VS838_Receive_Data>>8; //µÃµ½Êý¾ÝÂë
Data_L=VS838_Receive_Data; //µÃµ½Êý¾Ý·´Âë
if(Data_H==(uint8_t)~Data_L) //½ÓÊÕÊý¾ÝÂëÕýÈ·
{
VS838_num=Data_H; //ÕýÈ·¼üÖµ
VS838_Receive_Data=0;
}
}
}
return VS838_num; //·µ»Ø¼üÖµ
}
/*
********************************************************************************
ºìÍâ½ÓÊÕÖжϴ¦Àíº¯Êý
********************************************************************************
*/
void VS838_Receive_Handle(void)//ºìÍâ½ÓÊÕÖжϴ¦Àíº¯Êý
{
u16 Interval_tim=0;//Á½¸öϽµÑؼä¸ôʱ¼ä
switch(VS838_Status)
{
case 0://µÚÒ»¸öϽµÑØ£¬¶¨Ê±Æ÷¿ªÊ¼¼ÆÊý
VS838_Status=1;
HAL_TIM_Base_Start(&htim1); //Enable TIM2
//¶¨Ê±Æ÷¼ÆÊýÖµÇåÁã
TIM1->CNT=0;
break;
case 1://µÚ¶þ¸öϽµÑØ£¬¶¨Ê±Æ÷¹Ø±Õ£¬¶ÁÈ¡¶¨Ê±Æ÷¼ÆÊýÖµ
HAL_TIM_Base_Stop(&htim1);
Interval_tim=0;
Interval_tim=TIM1->CNT; //¶ÁÈ¡¶¨Ê±Æ÷¼ÆÊýÖµ
TIM1->CNT = 0; //¶¨Ê±Æ÷¼ÆÊýÖµÇåÁã
HAL_TIM_Base_Start(&htim1); //Enable TIM2
if( (Interval_tim>=12500)&&(Interval_tim<=14500) )//ÅжÏÒýµ¼ÂëÊÇ·ñÕýÈ·9+4.5ms
{
VS838_Status=2; //½øÈëÏÂһ״̬
}
else //Òýµ¼Âë´íÎ󣬴ÓнÓÊÕ
{
VS838_Status=0;
VS838_Receive_Count=0;
}
break;
case 2://¿ªÊ¼32λÊý¾ÝµÄ½ÓÊÕ
HAL_TIM_Base_Stop(&htim1);
Interval_tim=0;
Interval_tim=TIM1->CNT;
TIM1->CNT=0;
HAL_TIM_Base_Start(&htim1); //Enable TIM2
if( (Interval_tim>=1000)&&(Interval_tim<=1300) ) //¼ä¸ô1.12ms ->0
{
VS838_Receive_Data=VS838_Receive_Data<<1;
VS838_Receive_Count++;
}
else if( (Interval_tim>=2000)&&(Interval_tim<=2600) ) //¼ä¸ô2.25ms ->1
{
VS838_Receive_Data=VS838_Receive_Data<<1;
VS838_Receive_Data=VS838_Receive_Data|0x0001;
VS838_Receive_Count++;
}
else//²»ÊÇ0,1 ½ÓÊÕ´íÎ󣬴ÓнÓÊÕ
{
VS838_Status=0;
VS838_Receive_Data=0;
VS838_Receive_Count=0;
}
//³¬³ö½ÓÊÕÊý¾ÝλÊý£¬½ÓÊÕÏÂÒ»¸ö
while(VS838_Receive_Count==32)
{
VS838_receive_ok=1;//ºìÍâ½ÓÊÕÍê³É
VS838_Status=0;
VS838_Receive_Count=0;
break;
}
break;
default :
{
VS838_Status=0;
}
break;
}
}
void VS838_Demo(void) //ºìÍâÊý¾Ý½ÓÊÕÊý¾Ý´¦ÀíÀý³Ì
{
if( VS838_receive_ok == 1 ) //Ò»Ö¡ºìÍâÊý¾Ý½ÓÊÕÍê³É
{
VS838_receive_ok=0;
VS838_Receive_Data=0;
}
}
中断函数:
void EXTI10_IRQHandler(void)
{
/* USER CODE BEGIN EXTI10_IRQn 0 */
/* USER CODE END EXTI10_IRQn 0 */
HAL_GPIO_EXTI_IRQHandler1(GPIO_PIN_10);
/* USER CODE BEGIN EXTI10_IRQn 1 */
/* USER CODE END EXTI10_IRQn 1 */
}
效果图:
|