공부방/ARM_STM32 노진호교수님_필기

240424 LCD & ULTRASONIC & INTERRUPTS

맘스터치보단파파이스 2024. 4. 24. 16:42

E : 동작이 느려서 약간의 delay를 준다.

 

lcd.h에 함수 선언 안해줬을 때

 

 

문자열의 특징

제일 마지막 문자에는 binary 0

문자열의 이름은 주소

rm p.201

중첩되어있는 벡터 인터럽트들

인터럽트 되고있는 도중에 또 인터럽트를 걸 수 있다.

우선순위에 따라 실행 할 수 있다.

rm p.202

물리적인 Address

외부 인터럽트가 걸리면 Address로 간다는 뜻 

stratup.s 에 들어가 있는 EXTI1

함수의 이름은 주소를 의미한다.

인터럽트가 걸리면 함수 실행.

1. PC1 rising edge 발생하여 EXTI1 실행

2. Vector Table의 0x0000 005C주소로 강제 jump

3. Vector Tavle 주소인 0x0000 005C에 입력되어 있는 주소로 jump

4. 해당 인터럽트 서비스 루틴에서 코드 진행.

외부 인터럽트가 걸리면 모이는 곳

 

TIMER/COUNTER를 이용하여 측정.

prescaler를 통해 너무 빠른 100MHz CLK 제어한다.

1MHz--> 1us

1kHz --> 1ms

1us 에 0.034cm 씩 간다.

#include "UltraSonic.h"


uint16_t tim_counter;
uint16_t echoFlag = 0; //flag
extern TIM_HandleTypeDef htim1;
/* Micro sec 사용가능하게 하는 code 시작*/
void DelayInit()
{
   CoreDebug->DEMCR &= ~CoreDebug_DEMCR_TRCENA_Msk;
   CoreDebug->DEMCR |=  CoreDebug_DEMCR_TRCENA_Msk;

   DWT->CTRL &= ~DWT_CTRL_CYCCNTENA_Msk; //~0x00000001;
   DWT->CTRL |=  DWT_CTRL_CYCCNTENA_Msk; //0x00000001;

   DWT->CYCCNT = 0;

   /* 3 NO OPERATION instructions */
   __ASM volatile ("NOP");
   __ASM volatile ("NOP");
   __ASM volatile ("NOP");
}

void DelayUS(uint32_t us) {
   uint32_t cycles = (SystemCoreClock/1000000L)*us;
   uint32_t start = DWT->CYCCNT;
   volatile uint32_t cnt;

   do
   {
      cnt = DWT->CYCCNT - start;
   } while(cnt < cycles);
}
/* Micro sec 사용가능하게 하는 code 끝 */
int UltraSonic_getEchopinState()
{
   return (HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_1)); //rising edge(상승)
}

void UltraSonic_clearTimer()
{
   __HAL_TIM_SET_COUNTER(&htim1,0); //처음에 0으로 세팅
}
void UltraSonic_startTimer()
{
   HAL_TIM_Base_Start(&htim1);
}
void UltraSonic_stopTimer()
{
   HAL_TIM_Base_Stop(&htim1);
}

uint16_t UltraSonic_getTimerCounter()
{
   return __HAL_TIM_GET_COUNTER(&htim1);
}


void UltraSonic_ISR_Process(uint16_t GPIO_Pin)
{
   if (GPIO_Pin == GPIO_PIN_1) {//인터럽트 루틴
         //echo pin high 유지시간 측정

         if(UltraSonic_getEchopinState()){
            UltraSonic_clearTimer();
            UltraSonic_startTimer();
            UltraSonic_clearEchoFlag();
         }
         else { //falling edge (하강)
            UltraSonic_stopTimer();
            tim_counter = UltraSonic_getTimerCounter();
            UltraSonic_setEchoFlag();
         }
      }
}

void UltraSonic_startTrig()
{
   HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0, SET);
   DelayUS(15);
   HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0, RESET);
}

void UltraSonic_clearEchoFlag()
{
   echoFlag = 0;
}

void UltraSonic_setEchoFlag()
{
   echoFlag = 1;
}


int UltraSonic_isCmpltRecvEcho()//flag return
{
   return echoFlag;
}

int UltraSonic_getDistance()
{
   int timeout = 0;
   UltraSonic_startTrig();
   while(!UltraSonic_isCmpltRecvEcho()){//1이 되는것을 기다리기
      timeout++;
      if(timeout > 20)return 0;
      HAL_Delay(1);
   }
   UltraSonic_clearEchoFlag();
   return tim_counter * 0.017; //cm distance (0.034/2)갔다오는거 나누기
}

'공부방 > ARM_STM32 노진호교수님_필기' 카테고리의 다른 글

240425_PWM, MOTOR  (0) 2024.04.26
240425_dirver_UltraSonic  (0) 2024.04.25
240424 과제  (0) 2024.04.24
240423 UART2_LCD  (1) 2024.04.23
240423 LOWLEVEL  (0) 2024.04.23