Professional Documents
Culture Documents
Anil Project Boxes5
Anil Project Boxes5
Anil Kumar M
Banglore-560062
Email:anilkumarofficial01@gmail.com
Mobile No.: 9380042627
LinkedIn: https://www.linkedin.com/in/anilkumar98/
*/
/*
Implementation of the 5boxes project
*Microcontroller STM32F407VG is the main controller for the particular
application. Performance and low cost, availability of pins for different
communication protocols (2 × CAN interfaces (2.0B Active)),1 Mbyte of
Flash memory, and 168Mhz M4 core with DSP and FPU, enable RTOS to
Complete tasks faster.
*Display LCD I2C interface for prototyping.
\*used peripheral Programming*\
RTOS,interrupts,CAN,functions,structure,RTC,I2C Display.
*/
#include <stdint.h>
#include <stdio.h>
#include <string.h>
//user defined headers
#include "main.h"
#include "cmsis_os.h"
#include "stm32f4xx_hal.h"
#include "FreeRTOS.h"
#include "task.h"
#include "timers.h"
#include "queue.h"
#include "semphr.h"
#include "event_groups.h"
#include "stm32f4xx.h" // Device header
#include "i2c-lcd.h"
void VoltageDISP();
void UARTFUN();
void PINUPDATE();
int BOXES5();
struct Digital
{ uint8_t pack;
uint8_t open;
char DM[20];
} ;
ADC_HandleTypeDef hadc1;
DMA_HandleTypeDef hdma_adc1;
CAN_HandleTypeDef hcan1;
I2C_HandleTypeDef hi2c1;
RTC_HandleTypeDef hrtc;
RTC_TimeTypeDef sTime;
RTC_DateTypeDef sDate;
TIM_HandleTypeDef htim1;
UART_HandleTypeDef huart1;
osThreadId defaultTaskHandle;
osThreadId myTask02Handle;
osThreadId myTask03Handle;
osThreadId myTask04Handle;
osMutexId UART_MutexHandle;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_DMA_Init(void);
static void MX_ADC1_Init(void);
static void MX_CAN1_Init(void);
static void MX_I2C1_Init(void);
static void MX_RTC_Init(void);
static void MX_TIM1_Init(void);
static void MX_USART1_UART_Init(void);
void Thread0(void const * argument);
void Thread1(void const * argument);
void Thread2(void const * argument);
void Thread3(void const * argument);
uint32_t TxMailbox;
/*---------------------------------------------------------*/
uint8_t txbox1o[20] ="box1 can be opened";
uint8_t txbox1c[20] ="Box1 not closed";
uint8_t txbox1p[20] ="box1 has package";
uint8_t txbox2o[20] ="box2 can be opened";
uint8_t txbox2c[20] ="Box2 not closed";
uint8_t txbox2p[20] ="box2 has package";
uint8_t txbox3o[20] ="box3 can be opened";
uint8_t txbox3c[20] ="Box3 not closed";
uint8_t txbox3p[20] ="box3 has package";
uint8_t txbox4o[20] ="box4 can be opened";
uint8_t txbox4c[20] ="Box4 not closed";
uint8_t txbox4p[20] ="box4 has package";
uint8_t txbox5o[20] ="box5 can be opened";
uint8_t txbox5c[20] ="Box5 not closed";
uint8_t txbox5p[20] ="box5 has package";
uint8_t txpkpr[20]="Package present";
uint8_t txpknp[20]="Package not present";
uint8_t INTU[20]="Interruptpin high";
int main(void)
{ ptr=&packag;
/* BEGIN 1 */
DL packag={1,1,"V2.0"};
char *str;
strcpy(str,packag.DM);
HAL_UART_Transmit(&huart1,str,20,10);
/* END 1 */
HAL_CAN_Start(&hcan1);
//Activate the notification
HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING);
/* Start scheduler */
osKernelStart();
while (1)
{ //code in main() for test purpose...
/****
HAL_RTC_GetTime(&hrtc,&sTime,RTC_FORMAT_BIN);
HAL_RTC_GetDate(&hrtc,&sDate,RTC_FORMAT_BIN);
HAL_Delay(200);
USER CODE END WHILE ****/
/* DP1= HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_10);
DO1=HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_11);
DP2=HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_12);
DO2=HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_13);
DP3=HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_14);
DO3=HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_15);
DP4=HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_6);
DO4=HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_7);
DP5=HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_8);
DO5=HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_9);
*/
/**** PINUPDATE();
HAL_CAN_AddTxMessage(&hcan1, &TxHeader,TxData1, &TxMailbox);
switch((int)BOXES5)
{
case 1:
HAL_UART_Transmit(&huart1,txbox1o,20,10);
while(DO1)
{
PINUPDATE();
HAL_UART_Transmit(&huart1,txbox1c,20,10);
//printf("Box1 not closed");
}
PINUPDATE();
if(DP1)
{HAL_UART_Transmit(&huart1,txpkpr,20,10);
//printf("Package present");
}
else
{HAL_UART_Transmit(&huart1,txpknp,20,10);
//printf("no package");
}
break;
case 2:
HAL_UART_Transmit(&huart1,txbox2o,20,10);
//printf("box2 can be opened");
while(DO2){
PINUPDATE();
HAL_UART_Transmit(&huart1,txbox2c,20,10);
//printf("Box2 not closed");
}
PINUPDATE();
if(DP2)
{HAL_UART_Transmit(&huart1,txpkpr,20,10);
//printf("Package present");
}
else
{HAL_UART_Transmit(&huart1,txpknp,20,10);
//printf("no package");
}
break;
case 3:
HAL_UART_Transmit(&huart1,txbox3o,20,10);
//printf("box3 can be opened");
while(DO3){
PINUPDATE();
HAL_UART_Transmit(&huart1,txbox3c,20,10);
//printf("Box3 not closed");
}
PINUPDATE();
if(DP3)
{HAL_UART_Transmit(&huart1,txpkpr,20,10);
//printf("Package present");
}
else
{HAL_UART_Transmit(&huart1,txpknp,20,10);
//printf("no package");
}
break;
case 4:
HAL_UART_Transmit(&huart1,txbox4o,20,10);
//printf("box4 can be opened");
while(DO4){
PINUPDATE();
HAL_UART_Transmit(&huart1,txbox4c,20,10);
//printf("Box4 not closed");
}
PINUPDATE();
if(DP4)
{HAL_UART_Transmit(&huart1,txpkpr,20,10);
//printf("Package present");
}
else
{ HAL_UART_Transmit(&huart1,txpknp,20,10);
//printf("no package");
}
break;
case 5:
HAL_UART_Transmit(&huart1,txbox5o,20,10);
//printf("box5 can be opened");
while(DO5){
PINUPDATE();
HAL_UART_Transmit(&huart1,txbox5c,20,10);
//printf("Box5 not closed");
}
PINUPDATE();
if(DP5)
{ HAL_UART_Transmit(&huart1,txpkpr,20,10);
//printf("Package present");
}
else
{HAL_UART_Transmit(&huart1,txpknp,20,10);
//printf("no package");
}
break;
default:
PINUPDATE();
if(DP1|DP2|DP3|DP4|DP5)
{
if(DP1){HAL_UART_Transmit(&huart1,txbox1p,20,10);}if(DP2){HAL_UART_
Transmit(&huart1,txbox2p,20,10);}if(DP3){HAL_UART_Transmit(&huart1,txbox3
p,20,10);}if(DP4){
HAL_UART_Transmit(&huart1,txbox4p,20,10);}if(DP5){HAL_UART_Transmit(&huar
t1,txbox5p,20,10);}
}
break;
}****/
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
int BOXES5()
{
if(DP1&&DO1)
{
if(A[0]>=1639 && A[0]<=2459)
{
return 1;
}
}
if(DP2&&DO2)
{
if(A[1]>=1639 && A[1]<=2459)
{
return 2;
}
}
if(DP3&&DO3)
{
if(A[2]>=1639 && A[2]<=2459)
{
return 3;
}
}
if(DP4&&DO4)
{
if(A[3]>=1639 && A[3]<=2459)
{
return 4;
}
}
if(DP5&&DO5)
{
if(A[4]>=1639 && A[4]<=2459)
{
return 5;
}
}
return 0;
}
void PINUPDATE()
{
DP1= HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_10);
DO1=HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_11);
DP2=HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_12);
DO2=HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_13);
DP3=HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_14);
DO3=HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_15);
DP4=HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_6);
DO4=HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_7);
DP5=HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_8);
DO5=HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_9);
}
void VoltageDISP(){
lcd_send_cmd(0x80);//postion 00
lcd_send_string("CH0 --A1 voltage");
lcd_send_data((A[0]*0.001221));
HAL_Delay(1000);
lcd_clear();
lcd_send_string("CH1 --A2 voltage");
lcd_send_data((A[1]*0.001221));
HAL_Delay(1000);
lcd_clear();
lcd_send_string("CH2 --A3 voltage");
lcd_send_data((A[2]*0.001221));
HAL_Delay(1000);
lcd_clear();
lcd_send_string("CH0 --A4 voltage");
lcd_send_data((A[3]*0.001221));
HAL_Delay(1000);
lcd_clear();
lcd_send_string("CH0 --A5 voltage");
lcd_send_data((A[4]*0.001221));
HAL_Delay(1000);
lcd_clear();
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
/**
* @brief ADC1 Initialization Function
* @param None
* @retval None
*/
static void MX_ADC1_Init(void)
{
/* BEGIN ADC1_Init 0 */
/* END ADC1_Init 0 */
/* BEGIN ADC1_Init 1 */
/* END ADC1_Init 1 */
/** Configure the global features of the ADC (Clock, Resolution, Data
Alignment and number of conversion)
*/
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV8;
hadc1.Init.Resolution = ADC_RESOLUTION_8B;
hadc1.Init.ScanConvMode = ENABLE;
hadc1.Init.ContinuousConvMode = ENABLE;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.NbrOfConversion = 5;
hadc1.Init.DMAContinuousRequests = ENABLE;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
Error_Handler();
}
/** Configure for the selected ADC regular channel its corresponding
rank in the sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_0;
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
/** Configure for the selected ADC regular channel its corresponding
rank in the sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_1;
sConfig.Rank = 2;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
/** Configure for the selected ADC regular channel its corresponding
rank in the sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_2;
sConfig.Rank = 3;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
/** Configure for the selected ADC regular channel its corresponding
rank in the sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_0;
sConfig.Rank = 4;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
/** Configure for the selected ADC regular channel its corresponding
rank in the sequencer and its sample time.
*/
sConfig.Rank = 5;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
hcan1.Instance = CAN1;
hcan1.Init.Prescaler = 16;
hcan1.Init.Mode = CAN_MODE_NORMAL;
hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ;
hcan1.Init.TimeSeg1 = CAN_BS1_1TQ;
hcan1.Init.TimeSeg2 = CAN_BS2_1TQ;
hcan1.Init.TimeTriggeredMode = DISABLE;
hcan1.Init.AutoBusOff = DISABLE;
hcan1.Init.AutoWakeUp = DISABLE;
hcan1.Init.AutoRetransmission = DISABLE;
hcan1.Init.ReceiveFifoLocked = DISABLE;
hcan1.Init.TransmitFifoPriority = DISABLE;
if (HAL_CAN_Init(&hcan1) != HAL_OK)
{
Error_Handler();
}
hi2c1.Instance = I2C1;
hi2c1.Init.ClockSpeed = 100000;
hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
hi2c1.Init.OwnAddress1 = 0;
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c1.Init.OwnAddress2 = 0;
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_I2C_Init(&hi2c1) != HAL_OK)
{
Error_Handler();
}
huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
}
void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
{
UNUSED(hrtc);
HAL_GPIO_TogglePin(GPIOD,GPIO_PIN_12);
}
/* USER CODE BEGIN 4 */
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
/* Prevent unused argument(s) compilation warning */
UNUSED(GPIO_Pin);
HAL_UART_Transmit(&huart1,INTU,20,10);
volatile uint8_t I=0 ;
I=I^1;
if(I)
{for(;;){}
}
else
{return ;}
}
HAL_UART_Transmit(&huart1,txbox1o,20,10);
while(DO1)
{
PINUPDATE();
HAL_UART_Transmit(&huart1,txbox1c,20,10);
//printf("Box1 not closed");
}
PINUPDATE();
if(DP1)
{HAL_UART_Transmit(&huart1,txpkpr,20,10);
//printf("Package present");
}
else
{HAL_UART_Transmit(&huart1,txpknp,20,10);
//printf("no package");
}
break;
case 2:
HAL_UART_Transmit(&huart1,txbox2o,20,10);
//printf("box2 can be opened");
while(DO2){
PINUPDATE();
HAL_UART_Transmit(&huart1,txbox2c,20,10);
//printf("Box2 not closed");
}
PINUPDATE();
if(DP2)
{HAL_UART_Transmit(&huart1,txpkpr,20,10);
//printf("Package present");
}
else
{HAL_UART_Transmit(&huart1,txpknp,20,10);
//printf("no package");
}
break;
case 3:
HAL_UART_Transmit(&huart1,txbox3o,20,10);
//printf("box3 can be opened");
while(DO3){
PINUPDATE();
HAL_UART_Transmit(&huart1,txbox3c,20,10);
//printf("Box3 not closed");
}
PINUPDATE();
if(DP3)
{HAL_UART_Transmit(&huart1,txpkpr,20,10);
//printf("Package present");
}
else
{HAL_UART_Transmit(&huart1,txpknp,20,10);
//printf("no package");
}
break;
case 4:
HAL_UART_Transmit(&huart1,txbox4o,20,10);
//printf("box4 can be opened");
while(DO4){
PINUPDATE();
HAL_UART_Transmit(&huart1,txbox4c,20,10);
//printf("Box4 not closed");
}
PINUPDATE();
if(DP4)
{HAL_UART_Transmit(&huart1,txpkpr,20,10);
//printf("Package present");
}
else
{ HAL_UART_Transmit(&huart1,txpknp,20,10);
//printf("no package");
}
break;
case 5:
HAL_UART_Transmit(&huart1,txbox5o,20,10);
//printf("box5 can be opened");
while(DO5){
PINUPDATE();
HAL_UART_Transmit(&huart1,txbox5c,20,10);
//printf("Box5 not closed");
}
PINUPDATE();
if(DP5)
{ HAL_UART_Transmit(&huart1,txpkpr,20,10);
//printf("Package present");
}
else
{HAL_UART_Transmit(&huart1,txpknp,20,10);
//printf("no package");
}
break;
default:
PINUPDATE();
if(DP1|DP2|DP3|DP4|DP5)
{
if(DP1){HAL_UART_Transmit(&huart1,txbox1p,20,10);}if(DP2){HAL_UART_
Transmit(&huart1,txbox2p,20,10);}if(DP3){HAL_UART_Transmit(&huart1,txbox3
p,20,10);}if(DP4){
HAL_UART_Transmit(&huart1,txbox4p,20,10);}if(DP5){HAL_UART_Transmit(&huar
t1,txbox5p,20,10);}
}
break;
}
osDelay(1);
}
/* USER CODE END Thread1 */
}
/**
* @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