⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 c8051f31x

📁 C8051F31X 采集温度转换成摄氏温度串口发送
💻
📖 第 1 页 / 共 2 页
字号:
   SCON0 = 0x10;                       // SCON0: 8-bit variable bit rate
                                       //        level of STOP bit is ignored
                                       //        RX enabled
                                       //        ninth bits are zeros
                                       //        clear RI0 and TI0 bits
   if (SYSCLK/BAUDRATE/2/256 < 1) {
      TH1 = -(SYSCLK/BAUDRATE/2);
      CKCON &= ~0x0B;                  // T1M = 1; SCA1:0 = xx
      CKCON |=  0x08;
   } else if (SYSCLK/BAUDRATE/2/256 < 4) {
      TH1 = -(SYSCLK/BAUDRATE/2/4);
      CKCON &= ~0x0B;                  // T1M = 0; SCA1:0 = 01                  
      CKCON |=  0x09;
   } else if (SYSCLK/BAUDRATE/2/256 < 12) {
      TH1 = -(SYSCLK/BAUDRATE/2/12);
      CKCON &= ~0x0B;                  // T1M = 0; SCA1:0 = 00
   } else {
      TH1 = -(SYSCLK/BAUDRATE/2/48);
      CKCON &= ~0x0B;                  // T1M = 0; SCA1:0 = 10
      CKCON |=  0x02;
   }

   TL1 = TH1;                          // init Timer1
   TMOD &= ~0xf0;                      // TMOD: timer 1 in 8-bit autoreload
   TMOD |=  0x20;                       
   TR1 = 1;                            // START Timer1
   TI0 = 1;                            // Indicate TX0 ready
}

//-----------------------------------------------------------------------------
// Timer2_Init SYSCLK no Interrupt
//-----------------------------------------------------------------------------
//
// Configure Timer2 to auto-reload at interval specified by <counts> (no 
// interrupt generated) using SYSCLK as its time base.
//
void Timer2_Init (int counts)
{
   TMR2CN = 0x00;                      // STOP Timer2; Clear TF2H and TF2L;
                                       // disable low-byte interrupt; disable
                                       // split mode; select internal timebase
   CKCON |= 0x10;                      // Timer2 uses SYSCLK as its timebase

   TMR2RL  = -counts;                  // Init reload values
   TMR2    = TMR2RL;                   // Init Timer2 with reload value
   ET2 = 0;                            // disable Timer2 interrupts
   TR2 = 1;                            // start Timer2
}

//-----------------------------------------------------------------------------
// Support Subroutines
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// wait_soak_time
//-----------------------------------------------------------------------------
//
// This routine waits for the number of seconds indicated in the constant
// <SOAK_TIME>.
// 
void wait_soak_time (unsigned char soak_time)
{
   unsigned char i;

   for( i = soak_time; i != 0; i--) {
      wait_one_second();
      printf ("Soaking...%d\n", (int) i);
   }
}
  
//-----------------------------------------------------------------------------
// wait_one_second
//-----------------------------------------------------------------------------
//
// This routine uses timer 2 to insert a delay of approximately one second.
// Timer 2 overflows <TIMER2_RATE> times per second
//
void wait_one_second (void)
{
   unsigned int count;
   TF2H = 0;                           // Clear Timer2 overflow flag
   TR2 = 1;                            // Start Timer2
   
   for (count = TIMER2_RATE; count != 0; count--) {
      while (!TF2H);                   // wait for overflow
      TF2H = 0;                        // clear overflow indicator
   }

   TR2 = 0;                            // Stop Timer2
}

//-----------------------------------------------------------------------------
// calibrate


3楼 admin 发表于:2006-8-11 17:31:00
//-----------------------------------------------------------------------------
//
void calibrate (void)
{
   bit EA_state=EA;                    // Preserves EA state
   unsigned char xdata * codePtr;      // Used to write calibration
                                       // Value into FLASH memory
   unsigned int code* data pread;      // FLASH read pointer

   long temp_offset;                   // stores returned value from ADC
   pread = (unsigned int code *) TEMP_OFFSET;

   wait_soak_time(SOAK_TIME);          // let temperature of device stabilize
   temp_offset= (long) measure ();     // Read oversampled ADC code

   // now calculate the 0 DEG C offset value using <temp_offset>, the
   // temp sensor gain (TEMP_SENSOR_GAIN), and the ambient temperature.

   temp_offset = temp_offset - ((long) AMB_TEMP * 
                 TEMP_SENSOR_GAIN / VREF * 65536 / 1000);
   
   codePtr=(unsigned char xdata*) &TEMP_OFFSET;               
                                       // Point to TEMP_OFFSET

   EA = 0;                             // Disable interrupts


   FLKEY=0xA5;                         // Input first key code
   FLKEY=0xF1;                         // Input second key code,
                                       // FLASH is now unlocked
 
   PSCTL |= 0x01;                      // Enable FLASH Writes
   *codePtr = (temp_offset>>8);        // Write high byte of temp_gain

   PSCTL &= ~0x01;                     // disable FLASH Writes

   codePtr++;                          // Move to low byte of
                                       // TEMP_OFFSET in FLASH to
                                       // Store low byte of temp_gain
 
   FLKEY=0xA5;                         // Input first key code
   FLKEY=0xF1;                         // Input second key code,
                                       // FLASH is now unlocked
 
   PSCTL |= 0x01;                      // Enable FLASH Writes

   *codePtr =temp_offset;              // Write low byte of temp_gain
     
      
   PSCTL = 0x00;                       // Disable FLASH Writes
   EA = EA_state;                      // Restore interrupt state

}

//-----------------------------------------------------------------------------
// measure
//-----------------------------------------------------------------------------
//
// This routine averages 16383 ADC samples and returns a 16-bit unsigned 
// result.
// 
unsigned int measure (void)
{
   unsigned i;                         // Sample counter
   unsigned long accumulator=0L;       // Here's where we integrate the
                                       // ADC samples
   unsigned int currval;

   AD0INT = 0;
   AD0BUSY = 1;

   // read the ADC value and add to running total
   i = 0;
   do 
   {
      while (!AD0INT);                 // Wait for conversion to complete
      AD0INT = 0;                      // Clear end-of-conversion indicator

      currval=ADC0;                    // Store latest ADC conversion          
      AD0BUSY = 1;                     // Initiate conversion
      accumulator += currval;          // Accumulate
      i++;                             // Update counter
   } while (i != 16383);
   return (unsigned int) (accumulator >> 8);
   // shift to obtain a 16-bit result (14 + 10 = 24 - 8 = 16) bits
}


int get_temp (void)
{
   unsigned int ADC_code;
   long result;
   
   ADC_code = measure();
   
   result = ADC_code - TEMP_OFFSET;

   // result = result * (VREF / 65536) * (1000 / TEMP_SENSOR_GAIN) * ( 100 )
   // the equation above is re-arranged for fixed-point math.

   result = result * (long) VREF / 256 * 1000 / TEMP_SENSOR_GAIN * 100 / 256;
   
   return (int) result;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -