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

📄 f350_weigh_scale.c

📁 C8051F350单片机做的称重系统
💻 C
📖 第 1 页 / 共 2 页
字号:
//-----------------------------------------------------------------------------
//
// Copyright 2004 Silicon Laboratories
// 
// Filename:      F350_Weigh_Scale.h
// Target Device: 8051F350
// Created:       15 JAN 2004
// Created By:    DKC
// Tool chain:    KEIL Eval C51
//
// This is a stand alone weith scale design. It output units in lbs via an LCD 
// or a PC via the UART.
//
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <c8051f350.h>
#include "F350_Weigh_Scale.h"          //Weigh Scale Hearder File
#include <stdio.h>
                   
//-----------------------------------------------------------------------------
// Support Subroutines
//-----------------------------------------------------------------------------
void Config_F350(void) 
{  RSTSRC   = 0x06;                    // Enable VDD Monitor and missing clock   	
//-----------------------------------------------------------------------------
// PCA Configuration
//-----------------------------------------------------------------------------
   PCA0MD &= ~0x40;                    // WDTE = 0 (Disable watchdog timer)

//-----------------------------------------------------------------------------
// Port Configuration
//-----------------------------------------------------------------------------
   XBR0     = 0x01;                    // Enable UART to Pins P0.4, P0.5
   XBR1     = 0x40;                    // Enable Crossbar

   P0SKIP   = 0x00;                    // Skip No Port Pins
   P0MDOUT |= 0x10;                    // Enable UTX as push-pull output
   P0MDIN   = 0xFF;                    // Configure No Pins as Analog Inputs

//-----------------------------------------------------------------------------
// Oscilator Configuration
//-----------------------------------------------------------------------------
   OSCICN |= 0xC0;                     // Configure internal oscillator for
                                       // its default frequency

//-----------------------------------------------------------------------------
// UART0_Init
//-----------------------------------------------------------------------------
//
// Configure the UART0 using Timer1, for <BAUDRATE> and 8-N-1.
//
   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 |=  0x08;                  // T1M = 1; SCA1:0 = xx
   } else if (SYSCLK/BAUDRATE/2/256 < 4) {
      TH1 = -(SYSCLK/BAUDRATE/2/4);
      CKCON &= ~0x0B;                  // T1M = 0; SCA1:0 = 01                  
      CKCON |=  0x01;
   } 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
}

//-----------------------------------------------------------------------------
// FLASH_PageUpdate
//-----------------------------------------------------------------------------
// 
// This routine updates <length> characters at <dest> with those pointed to
// by <src> using a page-based read-modify-write process.
//
// It first erases <SCRATCH_PAGE> and copies the page containing the <dest> 
// data to <SCRATCH_PAGE>, excluding <length> bytes starting at <dest>.  
// It then copies <SCRATCH_PAGE> back to the page containing <dest>.  Finally, 
// it copies <length> bytes from <src> to <dest>, completing the update 
// process.  
// Note: this algorithm does not take into account memory page boundaries...
//    	It assumes each update is within one page.
void FLASH_PageUpdate (signed char *src, signed char *dest, int length) reentrant
{ 
   int i;                              // byte counter
   unsigned char xdata *pwrite;        // FLASH write pointer
   unsigned char code *pread;          // FLASH read pointer
   unsigned char code *pstop;
   unsigned char code *pstart;

   pwrite = (unsigned char xdata *) SCRATCH_PAGE;

   EA = 0;                             // disable interrupts (precautionary)
   PSCTL = 0x03;                       // MOVX writes target FLASH memory;
                                       // FLASH Erase operations enabled
   FLKEY = 0xA5;                       // FLASH key sequence #1
   FLKEY = 0xF1;                       // FLASH key sequence #2

   *pwrite = 0x00;                     // initiate erase operation

   PSCTL = 0x00;                       // Disable FLASH Writes

   // initialize <pread> to beginning of FLASH page containing <dest>
   pread = (unsigned char code *) ((unsigned int) dest & 0xFE00);
   
   // <pstop> points to <dest>, which is the beginning of the area to exclude
   // from the copy process.
   pstop = (unsigned char code *) dest;

   // <pstart> points to the byte right after the area to exclude from the
   // copy process.
   pstart = (unsigned char code *) (pstop + length);

   // Now we copy the page containing <dest> to SCRATCH_PAGE, excluding
   // the bytes that are to be changed.
   for (i = 0; i < 512; i++) {
      if ((pread < pstop) || (pread >= pstart)) {
         if (*pread != 0xFF) {         // exclude copying 0xff's for efficiency
            
            PSCTL = 0x01;              // disable FLASH erase operations;
                                       // MOVX writes target FLASH
            FLKEY = 0xA5;              // FLASH key sequence #1
            FLKEY = 0xF1;              // FLASH key sequence #2
            *pwrite = *pread;          // copy bytes
         }  PSCTL = 0x00;              // Disable FLASH Writes
      }
      pwrite++;                        // advance pointers
      pread++;
   }

   // At this point, <SCRATCH_PAGE> has a copy of the entire page containing
   // <dest>, with the exclusion of the bytes to be replaced.  We now copy
   // <SCRATCH_PAGE> back to the page containing <dest>.

   pwrite = (unsigned char xdata *) ((unsigned int) dest & 0xFE00);
   pread = (unsigned char code *) SCRATCH_PAGE;
   FLASH_PageCopy (pread, pwrite);

   // At this point, the page containing <dest> has been restored; the bytes
   // to be changed are now 0xFF's; we can proceed with copying the bytes
   // from <src> into <dest> to complete the update.

   pwrite = (unsigned char xdata *) dest;

   EA = 0;                             // disable interrupts (precautionary)
   
   for (i = 0; i < length; i++) {
      if (*src != 0xFF) {              // exclude writing 0xff's for 
         PSCTL = 0x01;                 // MOVX writes target FLASH memory
                                       // efficiency
         FLKEY = 0xA5;                 // FLASH key sequence #1
         FLKEY = 0xF1;                 // FLASH key sequence #2
         *pwrite++ = *src++;           // copy bytes
      }  PSCTL = 0x00;                 // Disable FLASH Writes
   }
}

//-----------------------------------------------------------------------------
// FLASH_PageCopy
//-----------------------------------------------------------------------------
// 
// This routine copies the FLASH page starting at <src> to <dest>.  It erases
// the <dest> page before the copy process begins.
//
void FLASH_PageCopy (unsigned char code *src, unsigned char xdata *dest) reentrant
{  
   int i;                              // byte counter

   EA = 0;                             // disable interrupts (precautionary)
   PSCTL = 0x03;                       // MOVX writes target FLASH memory;
                                       // FLASH erase operations enabled

   FLKEY = 0xA5;                       // FLASH key sequence #1
   FLKEY = 0xF1;                       // FLASH key sequence #2
   *dest = 0;                          // initiate erasure of <dest> FLASH
                                       // page
   PSCTL = 0x00;                       // Disable FLASH Writes
   
   for (i = 0; i < 512; i++) {
      if (*src != 0xFF) {              // exclude writing 0xff's for efficiency

         PSCTL = 0x01;                 // disable FLASH erase operations;
                                       // MOVX writes target FLASH memory
         FLKEY = 0xA5;                 // FLASH key sequence #1
         FLKEY = 0xF1;                 // FLASH key sequence #2
         *dest = *src;                 // copy bytes
      }  PSCTL = 0x00;                 // Disable FLASH Writes
      dest++;                          // advance pointers
      src++;
   }
}

//-----------------------------------------------------------------------------
// CalibrateADCforMeasurement
//-----------------------------------------------------------------------------
// This routine assumes memory block 0x1A00 is erased
// Then this function calibrates the voltage channel and stores the calibration
// coefficients in the parameters xxx_slope and xxx_offset.
// This calibration routine uses the F350's internal calibration functions.  
//
void CalibrateADCforMeasurement(void)
{  unsigned char xdata *idata pwrite;  // FLASH write pointer
	
   EA = 0;                             // Disable All Interrupts

                                       // Perform Self Calibration 
   ADC0MD   = 0x84;                    // Enable ADC; Internal Offset Cal.
   while(!AD0CALC);                    // Wait for calibration to complete
     
   ADC0MD   = 0x85;                    // Enable ADC; Internal Gain Cal.
   while(!AD0CALC);                    // Wait for Calibration to complete

                                       // Memory's been erased at 0x1A00
                                       // Store Gain Coefficients to FLASH
   temp_LONG_1.l = 1234;               // Prepare storage parameter
   pwrite = (char xdata *)&(DATA_PAGE[scale_slope].l);
   FLASH_PageUpdate ((unsigned char *)&temp_LONG_1.b[0], pwrite, 4);

                                       // Memory's been erased at 0x1A00
                                       // Store the Offset Coeffs to FLASH
   temp_LONG_1.l = 1234;               // Prepare storage parameter
   pwrite = (char xdata *)&(DATA_PAGE[scale_offset].l);

⌨️ 快捷键说明

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