<MAIN.C>
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "FreeRTOS.h"
#include "task.h"
#include <stdio.h>
#include "semphr.h"
#include "MyQueue.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
uint32_t myTick;
int isPress, start;
/* 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 ---------------------------------------------------------*/
ADC_HandleTypeDef hadc1;
I2C_HandleTypeDef hi2c1;
UART_HandleTypeDef huart3;
/* USER CODE BEGIN PV */
QueueHandle_t xQueue;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART3_UART_Init(void);
static void MX_ADC1_Init(void);
static void MX_I2C1_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
extern void vTaskA(void * param);
extern void vTaskB(void * param);
extern void TaskA(void * param);
extern void TaskB(void * param);
extern void TaskC(void * param);
extern void TaskD(void * param);
extern uint32_t gTickCount;
int fputc(int c, FILE * fp)
{
//HAL_UART_Transmit(&huart3, (uint8_t *)&c, 1, (uint32_t)-1);
//ITM_SendChar(c);
while((USART3->SR & (0X1 << 7)) == 0 );
USART3 -> DR = (uint8_t)c;
return c;
}
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if (GPIO_Pin == GPIO_PIN_0)
{
gTickCount = 0;
printf("EXTI-0 interrupt. \r\n");
}
}
void queue_init()
{
xQueue = xQueueCreate(8, sizeof(message_t));
}
extern SemaphoreHandle_t xSemaphoreUart;
/* 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_USART3_UART_Init();
MX_ADC1_Init();
MX_I2C1_Init();
/* USER CODE BEGIN 2 */
uint8_t data = 0;
HAL_ADC_Start(&hadc1);
HAL_I2C_Mem_Write(&hi2c1, 0x68<<1, 0x6B, 1, &data, 1, (uint32_t)-1);
HAL_ADC_Start(&hadc1);
// xTaskCreate(
// vTaskA, /* Function that implements the task. */
// "vTask_A", /* Text name for the task. */
// 2*configMINIMAL_STACK_SIZE, /* Stack size in words, not bytes. */
// NULL, /* Parameter passed into the task. */
// tskIDLE_PRIORITY,/* Priority at which the task is created. */
// NULL );
//
// xTaskCreate(
// vTaskB, /* Function that implements the task. */
// "vTask_B", /* Text name for the task. */
// 2*configMINIMAL_STACK_SIZE, /* Stack size in words, not bytes. */
// NULL, /* Parameter passed into the task. */
// tskIDLE_PRIORITY,/* Priority at which the task is created. */
// NULL );
//
xTaskCreate(
TaskA, /* Function that implements the task. */
"Task_A", /* Text name for the task. */
2*configMINIMAL_STACK_SIZE, /* Stack size in words, not bytes. */
NULL, /* Parameter passed into the task. */
tskIDLE_PRIORITY,/* Priority at which the task is created. */
NULL );
xTaskCreate(
TaskB, /* Function that implements the task. */
"Task_B", /* Text name for the task. */
2*configMINIMAL_STACK_SIZE, /* Stack size in words, not bytes. */
NULL, /* Parameter passed into the task. */
tskIDLE_PRIORITY,/* Priority at which the task is created. */
NULL );
xTaskCreate(
TaskC, /* Function that implements the task. */
"Task_C", /* Text name for the task. */
2*configMINIMAL_STACK_SIZE, /* Stack size in words, not bytes. */
NULL, /* Parameter passed into the task. */
tskIDLE_PRIORITY,/* Priority at which the task is created. */
NULL );
xTaskCreate(
TaskD, /* Function that implements the task. */
"Task_D", /* Text name for the task. */
2*configMINIMAL_STACK_SIZE, /* Stack size in words, not bytes. */
NULL, /* Parameter passed into the task. */
tskIDLE_PRIORITY,/* Priority at which the task is created. */
NULL );
xSemaphoreUart = xSemaphoreCreateBinary();
xSemaphoreGive(xSemaphoreUart);
queue_init();
vTaskStartScheduler();
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
<mytask.c>
#include "main.h"
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#include "MyQueue.h"
#include "FND.C"
#include <stdio.h>
uint32_t gTickCount;
extern int isPress;
extern int start;
extern ADC_HandleTypeDef hadc1;
extern QueueHandle_t xQueue;
extern I2C_HandleTypeDef hi2c1;
SemaphoreHandle_t xSemaphoreUart;
// 0819 fnd-> rtos quiz
void TaskA(void * param)
{
while(1)
{
if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == GPIO_PIN_SET)
{
xSemaphoreTake(xSemaphoreUart, portMAX_DELAY);
printf("pressed \r\n");
xSemaphoreGive(xSemaphoreUart);
}
}
}
int four, three, two, one;
void TaskB(void * param)
{
while(1)
{
one = (xTaskGetTickCount() / 100 ) % 10;
two = (xTaskGetTickCount() / 1000 ) % 10;
three = (xTaskGetTickCount() / 10000 ) % 10;
four = (xTaskGetTickCount() / 100000 ) % 10;
display_fnd(4, one);
display_fnd(3, two);
display_fnd(2, three);
display_fnd(1, four);
}
// clock
}
uint8_t i2c_buf[8];
void TaskC(void * param)
{
while(1)
{
HAL_I2C_Mem_Read(&hi2c1, 0x68 << 1, 0x3B, 1, i2c_buf, sizeof(i2c_buf), (uint32_t)-1);
int16_t temp=(i2c_buf[6]<<8)|i2c_buf[7];
xSemaphoreTake(xSemaphoreUart, portMAX_DELAY);
printf("x : %d\r\n", (int16_t)(i2c_buf[0]<<8)|i2c_buf[1]);
printf("y : %d\r\n", (int16_t)(i2c_buf[2]<<8)|i2c_buf[3]);
printf("z : %d\r\n", (int16_t)(i2c_buf[4]<<8)|i2c_buf[5]);
printf("temp : %.2f\r\n", ((float)temp/340+36.53));
xSemaphoreGive(xSemaphoreUart);
vTaskDelay(2000);
}
}
void TaskD(void * param)
{
float a =0.9, b=0.1, newValue;
newValue = (int)HAL_ADC_GetValue(&hadc1);
while(1)
{
newValue = newValue * a + HAL_ADC_GetValue(&hadc1) * b;
xSemaphoreTake(xSemaphoreUart, portMAX_DELAY);
printf("cds : %f \r\n", newValue);
xSemaphoreGive(xSemaphoreUart);
vTaskDelay(1000);
}
}
<FND.C>
# define D0 GPIO_PIN_13
# define D1 GPIO_PIN_4
# define D2 GPIO_PIN_5
# define D3 GPIO_PIN_6
# define D4 GPIO_PIN_11
# define D5 GPIO_PIN_12
# define D6 GPIO_PIN_12
# define D7 GPIO_PIN_11 // DOT
void display_fnd(int pos, int value) // value : 0~9
{
// initialize
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13 |GPIO_PIN_4 |GPIO_PIN_5 |GPIO_PIN_6|GPIO_PIN_11 |GPIO_PIN_12, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_11| GPIO_PIN_12 , GPIO_PIN_SET); //GPIO_PIN_11
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14|GPIO_PIN_15|GPIO_PIN_0|GPIO_PIN_1, GPIO_PIN_RESET);
// center dot OFF!
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_7, GPIO_PIN_SET);
if (value == 0) // -> G=1 H=1
{
HAL_GPIO_WritePin(GPIOD, D0|D1|D2|D3|D4|D5, GPIO_PIN_RESET);
}
else if (value == 1)
{
HAL_GPIO_WritePin(GPIOD, D1|D2, GPIO_PIN_RESET);
}
else if (value == 2)
{
HAL_GPIO_WritePin(GPIOD, D0|D1|D3|D4, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOE, D6, GPIO_PIN_RESET);
}
else if (value == 3)
{
HAL_GPIO_WritePin(GPIOD, D0|D1|D2|D3, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOE, D6, GPIO_PIN_RESET);
}
else if (value == 4)
{
HAL_GPIO_WritePin(GPIOD, D1|D2|D5, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOE, D6, GPIO_PIN_RESET);
}
else if (value == 5)
{
HAL_GPIO_WritePin(GPIOD, D0|D2|D3|D5, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOE, D6, GPIO_PIN_RESET);
}
else if (value == 6)
{
HAL_GPIO_WritePin(GPIOD, D2|D3|D4|D5, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOE, D6, GPIO_PIN_RESET);
}
else if (value == 7)
{
HAL_GPIO_WritePin(GPIOD, D0|D1|D2, GPIO_PIN_RESET);
}
else if (value == 8)
{
HAL_GPIO_WritePin(GPIOD, D0|D1|D2|D3|D4|D5, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOE, D6, GPIO_PIN_RESET);
}
else if (value == 9)
{
HAL_GPIO_WritePin(GPIOD, D0|D1|D2|D5, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOE, D6, GPIO_PIN_RESET);
}
if (pos == 1) {
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_SET);
}
else if (pos == 2)
{
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, GPIO_PIN_SET);
}
else if (pos == 3)
{HAL_GPIO_WritePin(GPIOD, GPIO_PIN_0, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_11 , GPIO_PIN_RESET);}
else if (pos == 4)
{
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_1, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, D7, GPIO_PIN_SET);
}
vTaskDelay(5);
}
<MYQUEUE.H>
#ifndef __MYQUEUE_H__
#define __MYQUEUE_H__
typedef struct Message
{
uint32_t tick;
uint32_t value;
}message_t;
#endif
-----------------
until 함수 사용한 MYTASK.C
until 사용하면 print되는 시간이 정확해짐
#include "main.h"
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#include "MyQueue.h"
#include "FND.C"
#include <stdio.h>
uint32_t gTickCount;
extern int isPress;
extern int start;
extern ADC_HandleTypeDef hadc1;
extern QueueHandle_t xQueue;
extern I2C_HandleTypeDef hi2c1;
SemaphoreHandle_t xSemaphoreUart;
// 0819 fnd-> rtos quiz
void TaskA(void * param)
{
portTickType xLastWakeTime;
xLastWakeTime = xTaskGetTickCount();
while(1)
{
if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == GPIO_PIN_SET)
{
xSemaphoreTake(xSemaphoreUart, portMAX_DELAY);
printf("pressed \r\n");
xSemaphoreGive(xSemaphoreUart);
}
}
}
int four, three, two, one;
void TaskB(void * param)
{
while(1)
{
one = (xTaskGetTickCount() / 100 ) % 10;
two = (xTaskGetTickCount() / 1000 ) % 10;
three = (xTaskGetTickCount() / 10000 ) % 10;
four = (xTaskGetTickCount() / 100000 ) % 10;
display_fnd(4, one);
display_fnd(3, two);
display_fnd(2, three);
display_fnd(1, four);
}
// clock
}
uint8_t i2c_buf[8];
void TaskC(void * param)
{
portTickType xLastWakeTime;
xLastWakeTime = xTaskGetTickCount();
while(1)
{
HAL_I2C_Mem_Read(&hi2c1, 0x68 << 1, 0x3B, 1, i2c_buf, sizeof(i2c_buf), (uint32_t)-1);
int16_t temp=(i2c_buf[6]<<8)|i2c_buf[7];
xSemaphoreTake(xSemaphoreUart, portMAX_DELAY);
printf("x : %d\r\n", (int16_t)(i2c_buf[0]<<8)|i2c_buf[1]);
printf("y : %d\r\n", (int16_t)(i2c_buf[2]<<8)|i2c_buf[3]);
printf("z : %d\r\n", (int16_t)(i2c_buf[4]<<8)|i2c_buf[5]);
printf("temp : %.2f\r\n", ((float)temp/340+36.53));
xSemaphoreGive(xSemaphoreUart);
vTaskDelayUntil(&xLastWakeTime, 2000);
//vTaskDelay(2000);
}
}
void TaskD(void * param)
{
float a =0.9, b=0.1, newValue;
newValue = (int)HAL_ADC_GetValue(&hadc1);
portTickType xLastWakeTime;
xLastWakeTime = xTaskGetTickCount();
while(1)
{
newValue = newValue * a + HAL_ADC_GetValue(&hadc1) * b;
xSemaphoreTake(xSemaphoreUart, portMAX_DELAY);
printf("cds : %f \r\n", newValue);
xSemaphoreGive(xSemaphoreUart);
vTaskDelayUntil(&xLastWakeTime, 1000);
//vTaskDelay(1000);
}
}
// 0819
//void vTaskA(void * pvParameters)
//{
// message_t message;
// for(;;)
// {
// message.tick=xTaskGetTickCount();
// message.value = HAL_ADC_GetValue(&hadc1);
// xQueueSend(xQueue, (void *)&message, portMAX_DELAY);
// vTaskDelay(1000);
// }
//}
//void vTaskB(void * pvParameters)
//{
// message_t message;
// for(;;)
// {
// xQueueReceive(xQueue, (void *)&message, portMAX_DELAY);
// printf("tick : %d \r\n", message.tick);
// printf("value : %d \r\n", message.value);
//
// }
//}
// 0818 p. 165 quiz
//void TaskA(void * param)
//{
// while(1)
// {
// if (HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_7) == GPIO_PIN_RESET)
// {
// xSemaphoreGive(xSemaphoreUart);
// }
// vTaskDelay(100);
// }
//}
//void TaskB(void * param)
//{
// while(1)
// {
// xSemaphoreTake(xSemaphoreUart, portMAX_DELAY);
//
//
// HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_12);
// }
//}
// semapphore
//SemaphoreHandle_t xSemaphoreUart;
//void TaskA()
//{
// while(1)
// {
// xSemaphoreTake(xSemaphoreUart, portMAX_DELAY);
// printf("abcdef \r\n");
// xSemaphoreGive(xSemaphoreUart);
//
// vTaskDelay(0); // taskYIELD(); // *(INT *)0XE000ED04 = (0X1 << 28);
// }
//}
//void TaskB()
//{
// while(1)
// {
// xSemaphoreTake(xSemaphoreUart, portMAX_DELAY);
// printf("1234567890 \r\n");
// xSemaphoreGive(xSemaphoreUart);
//
// vTaskDelay(0);
// }
//}
//void TaskA(void * pvParameters)
//{
// while(1)
// {
// taskENTER_CRITICAL();
// gTickCount = gTickCount + 1;
// taskEXIT_CRITICAL();
// if ((gTickCount % 100000) == 0)
// {
// printf("%d \r\n", gTickCount/100000);
// }
// //vTaskDelay(1);
//
// }
//}
//void TaskA(void * param)
//{
// __NOP();
//
// while(1)
// {
// HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_12);
// vTaskDelay(10000);
// //for(int k=0; k<0x100000; k++);
// }
//}
//void TaskB(void * param)
//{
// while(1)
// {
// HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_13);
// vTaskDelay(50);
// //for(int k=0; k<0x100000; k++);
// }
//}
//void TaskC(void * param)
//{
// while(1)
// {
// HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_14);
// vTaskDelay(100);
// //for(int k=0; k<0x100000; k++);
// }
//}
//void TaskD(void * param)
//{
// while(1)
// {
// HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_15);
// //vTaskDelay(200);
// for(int k=0; k<0x100000; k++);
// }
//}
//void Task_A(void *pvParameters)
//{
// for(;;)
// {
// for(int k=0; k<2000; k++);
// }
//}
//void Task_B(void *pvParameters)
//{
// vTaskDelay(100);
//}
'FW 심화 과정 > [2] STM32심화실습' 카테고리의 다른 글
FreeRTOS 코드 백업 (0) | 2022.08.19 |
---|---|
0816 조도센서 / 가속도온도 센서 실습 (MPU-6000) (0) | 2022.08.16 |
0811 PWM / FND (0) | 2022.08.11 |
0809 CubeMX 시작 (0) | 2022.08.09 |
0808 까지 main 코드 백업 (0) | 2022.08.09 |
0809 함수 및 라이브러리 생성 (0) | 2022.08.09 |