본문 바로가기
# Semiconductor/[Semicon Academy]

[Harman 세미콘 아카데미] 79일차 - STM32/ARM(PWM, I2C, 자율주행 프로젝트)

by Graffitio 2023. 10. 23.
[Harman 세미콘 아카데미] 79일차 - STM32/ARM(PWM, I2C, 자율주행 프로젝트)
728x90
반응형
[PWM]

 

 

/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.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 */
  uint8_t rxData[1];
  uint8_t duty[3];
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
	HAL_UART_Receive_IT(&huart2, rxData, sizeof(rxData)); // input from uart2
	HAL_UART_Receive_IT(&huart1, rxData, sizeof(rxData)); // input from uart2
	HAL_UART_Transmit_IT(&huart2, rxData, sizeof(rxData)); // output to uart2
	HAL_UART_Transmit_IT(&huart1, rxData, sizeof(rxData)); // output to uart2
}
/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* 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 */
//  uint16_t dutyValue;

  /* 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_TIM3_Init();
  MX_USART2_UART_Init();
  MX_USART1_UART_Init();
  /* USER CODE BEGIN 2 */
  HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1);
  HAL_UART_Receive_IT(&huart1, rxData, sizeof(rxData));
  HAL_UART_Receive_IT(&huart2, rxData, sizeof(rxData));

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
    for(int i = 0 ; i < 10000 ; i++)
	{
		TIM3->CCR1 = i;
		HAL_Delay(1);
	}
  }
  /* USER CODE END 3 */
}

 

UART로 값을 입력해서 duty cycle을 조정해보자.

  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
	  if(rxData[0] == 'a' || rxData[0] == 'b' || rxData[0] == 'c' || rxData[0] == 'd')
	  {
		  dutyValue = rxData[0];

		  switch(dutyValue)
		  {
		  case 'a' :
			  TIM3->CCR1 = 250; // 25% duty
			  break;
		  case 'b' :
			  TIM3->CCR1 = 500; // 50% duty
			  break;
		  case 'c' :
			  TIM3->CCR1 = 750; // 75% duty
			  break;
		  case 'd' :
			  TIM3->CCR1 = 0; // 0% duty
			  break;
		  }
	  }
  }

 


 

[I2C]

 

 

#ifndef INC_I2C_LCD_H_
#define INC_I2C_LCD_H_

#include "stm32f4xx_hal.h" // 얘가 있어야 HAL 함수를 쓸 수 있다.

void lcd_init();
void lcd_send_cmd(char cmd);
void lcd_send_data(char data);
void lcd_send_string(char *str);
void lcd_put_cur(int row, int col);
void lcd_clear();

#endif /* INC_I2C_LCD_H_ */

 

#include "I2C_LCD.h"

extern I2C_HandleTypeDef hi2c1; // hi2c1이 외부에 선언되었다는 뜻

#define SLAVE_ADDRES_LCD	0x27<<1

void lcd_init()
{
	// cmd init
	HAL_Delay(50);
	lcd_send_cmd(0x30);
	HAL_Delay(5);
	lcd_send_cmd(0x30);
	HAL_Delay(1);
	lcd_send_cmd(0x30);
	HAL_Delay(10);
	lcd_send_cmd(0x20); // 4bit mode
	HAL_Delay(10);

	// display init
	lcd_send_cmd(0x28); // function set
	HAL_Delay(1);
	lcd_send_cmd(0x08); // display off
	HAL_Delay(1);
	lcd_send_cmd(0x01); // clear display
	HAL_Delay(1);
	lcd_send_cmd(0x06); // entry mode
	HAL_Delay(1);
	lcd_send_cmd(0x0C); // display on
}

void lcd_send_cmd(char cmd)
{
	char data_u, data_l;
	uint8_t data_t[4];
	data_u = (cmd & 0xf0); // msb쪽 4bit만 남기고 나머지 다 버림.
	data_l = ((cmd << 4) & 0xf0); // lsb쪽 4bit만 님기고 나머지 다 버림.
	data_t[0] = data_u | 0x0C;	// en = 1, rs = 0
	data_t[1] = data_u | 0x08;	// en = 0, rs = 0
	data_t[2] = data_l | 0x0C;	// en = 1, rs = 0
	data_t[3] = data_l | 0x08;	// en = 0, rs = 0

	HAL_I2C_Master_Transmit(&hi2c1, SLAVE_ADDRES_LCD, (uint8_t *)data_t, 4, 100);
}

void lcd_send_data(char data)
{
	char data_u, data_l;
	uint8_t data_t[4];
	data_u = (data & 0xf0); // msb쪽 4bit만 남기고 나머지 다 버림.
	data_l = ((data << 4) & 0xf0); // lsb쪽 4bit만 님기고 나머지 다 버림.
	data_t[0] = data_u | 0x0D;	// en = 1, rs = 0
	data_t[1] = data_u | 0x09;	// en = 0, rs = 0
	data_t[2] = data_l | 0x0D;	// en = 1, rs = 0
	data_t[3] = data_l | 0x09;	// en = 0, rs = 0

	HAL_I2C_Master_Transmit(&hi2c1, SLAVE_ADDRES_LCD, (uint8_t *)data_t, 4, 100);
}

void lcd_send_string(char *str)
{
	while(*str) lcd_send_data(*str++); // *str이 참이면, lcd_send_data(*str++) 실행
}

void lcd_put_cur(int row, int col)
{
    switch (row)
    {
        case 0:
            col |= 0x80;
            break;
        case 1:
            col |= 0xC0;
            break;
    }

    lcd_send_cmd(col);
}

void lcd_clear()
{
	lcd_send_cmd (0x80);
	for (int i=0 ; i<70 ; i++)
	{
		lcd_send_data(' ');
	}
}

 

/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "i2c.h"
#include "usart.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "I2C_LCD.h"
#include "stdio.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);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* 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 */
  uint16_t count = 0;
  uint8_t buff[30];
  /* 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_I2C1_Init();
  MX_USART2_UART_Init();
  /* USER CODE BEGIN 2 */
  HAL_Delay(1000); // 통신 되게 느리니까 적당히 주자.
  lcd_init();
  lcd_send_string("Hello HARMAN"); // 12글자
  HAL_Delay(500);
  lcd_put_cur(1, 0); // 다음 행으로 커서 이동
  lcd_send_string("TEST LCD COM!"); // 12글자
  HAL_Delay(1000);
  lcd_clear();
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
	  sprintf(buff, "count %-5d", count++);
	  lcd_put_cur(1, 0);
	  lcd_send_string(buff);
	  HAL_Delay(100);
  }
  /* USER CODE END 3 */
}

 


 

자율주행 프로젝트 진행 중...
728x90
반응형