浏览代码

dont work :-(

Vladimir N. Shilov 7 年之前
父节点
当前提交
08bcebc9dc
共有 1 个文件被更改,包括 21 次插入41 次删除
  1. 21 41
      main.c

+ 21 - 41
main.c

@@ -33,6 +33,8 @@ __C_task void main(void)
 
   /* Timer1 ticks with 1 MHz frequency for DS18B20 delays */
   TCCR1B = TIM1_PRESCALER;
+//  TCNT1 = 64536;
+//  TIMSK |= 1<<TOIE1; // enable TIM1_OVF interrupt
 
   /* ADC init */
   resultADC = 0;
@@ -174,6 +176,7 @@ static void setTime(void) {
   TWI_SendData(twi_buf, 5);
 }
 
+/* DS18B20 functions */
 static uint8_t ds18b20_Reset(void) {
   uint8_t i;
 
@@ -182,37 +185,24 @@ static uint8_t ds18b20_Reset(void) {
   DS18B20_OUTPUT_MODE;
 
   //ds18b20_delay(us(480));
-  Flag.T1OC = 0;
   TCNT1 = 0;
-  OCR1A = 480;
-  TIMSK |= (1<<OCF1A);
-  while (0 == Flag.T1OC) {
-    MCUCR = 1<<SE;
-    __sleep();
+  while (TCNT1 <= 500) {
   }
-  Flag.T1OC = 0;
 
   /* Release line and wait for 60uS */
   DS18B20_INPUT_MODE;
   //ds18b20_delay(us(60));
   TCNT1 = 0;
-  OCR1A = 60;
-  OCR1B = 420;
-  TIMSK |= (1<<OCF1B);
-  while (0 == Flag.T1OC) {
-    MCUCR = 1<<SE;
-    __sleep();
+  while (TCNT1 <= 70) {
   }
-  Flag.T1OC = 0;
 
   /* Store line value and wait until the completion of 480uS period */
   i = DS18B20_VALUE;
   //ds18b20_delay(us(420));
-  while (0 == Flag.T1OC) {
-    MCUCR = 1<<SE;
-    __sleep();
+  //OCR1B = 420;
+  TCNT1 = 0;
+  while (TCNT1 <= 430) {
   }
-  TIMSK &= ~((1<<OCF1A) | (1<<OCF1B));
 
   /* Return the value read from the presence pulse (0=OK, else=WRONG) */
   return i;
@@ -223,7 +213,8 @@ static void ds18b20_WriteBit(uint8_t bit) {
   DS18B20_LOW;
   DS18B20_OUTPUT_MODE;
   //ds18b20_delay(us(1));
-  __delay_cycles(8);
+  TCNT1 = 0;
+  while (TCNT1 <= 1);
 
   /* If we want to write 1, release the line (if not will keep low) */
   if(1 == bit) {
@@ -232,15 +223,9 @@ static void ds18b20_WriteBit(uint8_t bit) {
 
   /* Wait for 60uS and release the line */
   //ds18b20_delay(us(60));
-  Flag.T1OC = 0;
   TCNT1 = 0;
-  OCR1A = 60;
-  TIMSK |= (1<<OCF1A);
-  while (0 == Flag.T1OC) {
-    MCUCR = 1<<SE;
-    __sleep();
+  while (TCNT1 <= 60) {
   }
-  TIMSK &= ~(1<<OCF1A);
 
   DS18B20_INPUT_MODE;
 }
@@ -252,21 +237,15 @@ static uint8_t ds18b20_ReadBit(void) {
   DS18B20_LOW;
   DS18B20_OUTPUT_MODE;
   //ds18b20_delay(us(1));
-  __delay_cycles(8);
+  TCNT1 = 0;
+  while (TCNT1 <= 1);
 
   /* Release line and wait for 14uS */
   DS18B20_INPUT_MODE;
   //ds18b20_delay(us(14));
-  Flag.T1OC = 0;
   TCNT1 = 0;
-  OCR1A = 14;
-  OCR1B = 45;
-  TIMSK |= (1<<OCF1A) | (1<<OCF1B);
-  while (0 == Flag.T1OC) {
-    MCUCR = 1<<SE;
-    __sleep();
+  while (TCNT1 <= 14) {
   }
-  Flag.T1OC = 0;
 
   /* Read line value */
   if (DS18B20_VALUE != 0) {
@@ -275,11 +254,9 @@ static uint8_t ds18b20_ReadBit(void) {
 
   /* Wait for 45uS to end and return read value */
   //ds18b20_delay(us(45));
-  while (0 == Flag.T1OC) {
-    MCUCR = 1<<SE;
-    __sleep();
+  TCNT1 = 0;
+  while (TCNT1 <= 45) {
   }
-  TIMSK &= ~((1<<OCF1A) | (1<<OCF1B));
 
   return bit;
 }
@@ -313,7 +290,7 @@ static void ds18b20_StartMeasure(void) {
   } else {
     ds18b20_WriteByte(DS18B20_CMD_SKIPROM);
     ds18b20_WriteByte(DS18B20_CMD_CONVERTTEMP);
-    RTOS_SetTask(ds18b20_ReadTemperature, 750, 0);
+    RTOS_SetTask(ds18b20_ReadTemperature, 800, 0);
   }
 }
 
@@ -393,11 +370,14 @@ __interrupt void TIMER0_OVF_ISR(void) {
 
 #pragma vector=TIMER1_COMPA_vect
 __interrupt void TIMER1_COMPA_ISR(void) {
-  Flag.T1OC = 1;
 }
 
 #pragma vector=TIMER1_COMPB_vect
 __interrupt void TIMER1_COMPB_ISR(void) {
+}
+
+#pragma vector=TIMER1_OVF_vect
+__interrupt void TIMER1_OVF_ISR(void) {
   Flag.T1OC = 1;
 }