最近在做一个智能物料搬运小车,用到了颜色识别,最开始用OpenMv实现功能,由于实际需求有变,改用颜色识别模块实现响应功能。环境:keil5、STM32CubeMX 5.0、STM32F103ZET6开发板
本例程仅用于粗略的分辨红绿蓝颜色
首先根据TCS3200模块使用手册配置管脚
在STM32CubeMX中根据需求设置管脚
PE4--- OUT
PE5--- OE
PE6--- S3
PE7--- S2
PE8--- S1
PE9--- S0
PE4为外部中断模式,打开EXTI line4外部中断
其余管脚配置为输出模式
生成代码
根据模块的工作方式配置函数:
白平衡函数
红色通道检测函数
绿色通道检测函数
蓝色通道检测函数
即可对颜色进行识别,代码如附件所示:
单片机源程序如下:
- /* USER CODE END Header */
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- #include "tim.h"
- #include "usart.h"
- #include "gpio.h"
- /* Private includes ----------------------------------------------------------*/
- /* USER CODE BEGIN Includes */
- #include "TCS3200.h"
- /* 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 */
- /* Private variables ---------------------------------------------------------*/
- /* USER CODE BEGIN PV */
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- static void MX_NVIC_Init(void);
- /* USER CODE BEGIN PFP */
- // printf()重定向
- #ifdef __GNUC__
- #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
- #else
- #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
- #endif
- PUTCHAR_PROTOTYPE
- {
- HAL_UART_Transmit(&huart1 , (uint8_t *)&ch, 1, 0xFFFF);
- return ch;
- }
- /* USER CODE END PFP */
- /* Private user code ---------------------------------------------------------*/
- /* USER CODE BEGIN 0 */
- extern uint16_t Rgena,Ggena,Bgena;
- extern uint16_t count,dir;
- /* USER CODE END 0 */
- /**
- * @brief The application entry point.
- * @retval int
- */
- int main(void)
- {
- /* 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();
- /* USER CODE BEGIN SysInit */
- /* USER CODE END SysInit */
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_USART1_UART_Init();
- MX_TIM6_Init();
- /* Initialize interrupts */
- MX_NVIC_Init();
- /* USER CODE BEGIN 2 */
- HAL_TIM_Base_Start_IT(&htim6);
- TCS3200_Init();
- WhiteBalance();
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- if(Color_test(1) == 1)
- printf("Red\r\n");
- else
- printf("None\r\n");
- // printf("%f\t%f\t%f\r\n",255.0/Rgena,255.0/Ggena,255.0/Bgena);
- // printf("%d\t%d\t%d\r\n\r\n",Rgena,Ggena,Bgena);
- // printf("%d\t%d\t%d\r\n",TCS3200_RED(),TCS3200_GREEN(),TCS3200_BLUE());
- HAL_Delay(500);
- // printf("%d",Color_test());
- }
- /* USER CODE END 3 */
- }
- /**
- * @brief System Clock Configuration
- * @retval None
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct = {0};
- RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
- /**Initializes the CPU, AHB and APB busses clocks
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
- RCC_OscInitStruct.HSEState = RCC_HSE_ON;
- RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV2;
- RCC_OscInitStruct.HSIState = RCC_HSI_ON;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
- RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL15;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- Error_Handler();
- }
- /**Initializes the CPU, AHB and APB busses clocks
- */
- RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
- |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
- {
- Error_Handler();
- }
- }
- /**
- * @brief NVIC Configuration.
- * @retval None
- */
- static void MX_NVIC_Init(void)
- {
- /* EXTI4_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(EXTI4_IRQn, 0, 0);
- HAL_NVIC_EnableIRQ(EXTI4_IRQn);
- /* USART1_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
- HAL_NVIC_EnableIRQ(USART1_IRQn);
- /* TIM6_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(TIM6_IRQn, 0, 0);
- HAL_NVIC_EnableIRQ(TIM6_IRQn);
- }
- /* USER CODE BEGIN 4 */
- void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
- {
- UNUSED(htim);
- if(htim == &htim6)
- {
- if(dir == 200)
- {
- HAL_GPIO_TogglePin(GPIOB,LED_Pin);
- dir = 0;
- }
- }
- }
- /* USER CODE END 4 */
- /**
- * @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 */
- /* 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,
- tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* USER CODE END 6 */
- }
- #endif /* USE_FULL_ASSERT */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码
所有资料51hei提供下载:
TCS3200-TEST.7z
(254.87 KB, 下载次数: 128)
|