임베디드/고추건조기

코드 분해

twoweeks-within 2024. 12. 14. 16:47

코드 분해
최대한 간단히 > 시간축소
+
write 함수가 있으면 그동안 방해받지 않도록 보호해주어야함

@Ds18b20_Init();@


uint8_t isTemperSensorInit(){
return m_init;
}
//  init이 잘됐는지 확인용

bool Ds18b20_Init_simple (){
m_init = 0; // 처음에는 0

OneWire_Init(&OneWire,_DS18B20_GPIO ,_DS18B20_PIN);

//OneWireDevices = OneWire_First(&OneWire);
OneWire.ROM_NO[0] = 0x00;
OneWire.ROM_NO[1] = 0x00;
OneWire.ROM_NO[2] = 0x00;
OneWire.ROM_NO[3] = 0x00;
OneWire.ROM_NO[4] = 0x00;
OneWire.ROM_NO[5] = 0x00;
OneWire.ROM_NO[6] = 0x00;
OneWire.ROM_NO[7] = 0x00;
OneWire_GetFullROM(&OneWire, temperSensor.Address);
// 온도센서가 하나이므로 주소 직접할당
// 디버그로 주소 확인
  > 실행중이면 일시정지후 expressions 에서 확인 할 수 있음 > format 도 hex 로 변경


Ds18b20Delay(50);
DS18B20_SetResolution(&OneWire,temperSensor.Address, DS18B20_Resolution_12bits);
Ds18b20Delay(50);
DS18B20_DisableAlarmTemperature(&OneWire,temperSensor.Address);
// 구조체 배열을 굳이 사용할필요 X > 단일구조체

m_init =1 ;  //잘됐으면 1

return true;

}

@ Ds18b20_ManualConvert(); @

void StartConverting(){

 m_busy=1;
 DS18B20_StartAll(& OneWire);
 m_isConverting =1;
 m_busy=0;

}
void checkConverting(){
m_busy=1;
m_isConverting = !DS18B20_AllDone(&OneWire);
m_busy=0;
// DS18B20_AllDone : 읽음완료 > 1, 0: 비완료
// !0 이면 아직 converting 중
}

float getTemper(){
Ds18b20Delay(100);
m_busy=1;
temperSensor.DataIsValid = DS18B20_Read(&OneWire, temperSensor.Address, &temperSensor.Temperature);
m_busy=0;
return temperSensor.Temperature;


}

main.c 에서 구현
  if(!isConverting()){
  StartConverting();
  }
  checkConverting();

  if(!isConverting()){
temper = getTemper();
  }


>>> 온도는 나옴, 깜빡거림 그대로
onwire.c ( ds18b20에서 사용 라이브러리) 까지 들어가서 더 쪼갬

 

interrupt에서

void TIM3_IRQHandler(void)

{

   if(isTemperSensorInit()&& ! isBusy() ){  

        digit4_temper((int)(getCurrentTemper()*10));

}

 

isBusy : 1 , 즉 일하는중이면 temper 온도 표시 못하게 막음

    > 일종의 분리 역할

 



1. StartConverting()
DS18B20_StartAll
{
OneWire_Reset(OneWire);
OneWire_WriteByte(OneWire, ONEWIRE_CMD_SKIPROM);
OneWire_WriteByte(OneWire, DS18B20_CMD_CONVERTTEMP);
}

>>>>> onewire 분해

INIT


inline uint8_t OneWire_Reset(OneWire_t* OneWireStruct)
{
uint8_t i;
ONEWIRE_LOW(OneWireStruct);
ONEWIRE_OUTPUT(OneWireStruct);   // 아웃풋모드
ONEWIRE_DELAY(480);     //480 최소한
ONEWIRE_DELAY(20);   // DS18B20 연결시간 

/* Release line and wait for 70us */

 

m_busy_line =1; //바빠


ONEWIRE_INPUT(OneWireStruct);     // 인풋모드
ONEWIRE_DELAY(70);   // 값입력대기
/* Check bit value */
i = HAL_GPIO_ReadPin(OneWireStruct->GPIOx, OneWireStruct->GPIO_Pin); // 값입력
m_busy_line = 0; // 끝났어
/* Delay for 410 us */
ONEWIRE_DELAY(410); // 로딩
/* Return value of presence pulse, 0 = OK, 1 = ERROR */
return i;

 

+ OneWire_WriteByte 이때도 해주고.. StartConverting() 마무리

 

 

R/W

 

2.   checkConverting();

 

DS18B20_AllDone > OneWire_ReadBit 

 

uint8_t bit = 0;

m_busy_line =1

/* Line low */

ONEWIRE_LOW(OneWireStruct);

ONEWIRE_OUTPUT(OneWireStruct);

ONEWIRE_DELAY(2);  2 +

 

/* Release line */

ONEWIRE_INPUT(OneWireStruct);

ONEWIRE_DELAY(10); 2+ 10

 

/* Read line value */

if (HAL_GPIO_ReadPin(OneWireStruct->GPIOx, OneWireStruct->GPIO_Pin)) {

/* Bit is HIGH */

bit = 1;

}

 

/* Wait 50us to complete 60us period */

ONEWIRE_DELAY(50); 2+ 10 + 50 =62 

// 그림에서 read 는 60us 라서 얼추맞음

m_busy_line =0;

/* Return bit value */

return bit;

 

 

그랬지만,,, 달라진게없음

 >  더 더 더 쪼개야함

 

 

딜레이때는 0으로 풀어줌

+ 짧은딜레이는 그냥 같이 묶어버림 > 완료

 

 

 

 

'임베디드 > 고추건조기' 카테고리의 다른 글

I2C hello world :)  (0) 2024.12.17
I2C 구조  (0) 2024.12.15
문제원인  (0) 2024.12.14
장치 통합  (0) 2024.12.12
난방실 만들기  (0) 2024.12.11