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

📄 adc_samplesession.c.bak

📁 nec demo source code
💻 BAK
字号:
//============================================================================
// PROJECT      = 78K0S/KA1+ Low Pin Count - DemoKit-KA1
// MODULE       = adc_samplesession.c
// SHORT DESC.  = -
// DEVICE       = uPD78F9222
// VERSION      = 1.0
// DATE         = 22.12.2004
// LAST CHANGE  = -
// ===========================================================================
// Description:  This sample program simulates a simple voltage meter.
//               By using the integrated ADC, the voltage supplied to ADC on
//               input channel 0, port P20/ANI0, is measured. The input voltage
//               is adjusted by potentiometer R24. The board shows the measured
//               voltage by flashing LED's D1 to D4. 
//               (D1 = ON = 3.75 - 5.00 V,
//                D2 = ON = 2.50 - 3.74 V,
//                D3 = ON = 1.25 - 2.49 V,
//                D4 = ON = 0.00 - 1.24 V)
// ===========================================================================

#pragma sfr
#pragma DI
#pragma EI
#pragma interrupt INTTM80 isr_INTTM80

#pragma section @@CNST  OPT AT 80H
const char OPTION=0x98;
//const char OPTION0=0x98;
//const char OPTION1=0x98;

//-----------------------------------------------------------------------------
// Global Defines
//-----------------------------------------------------------------------------

#define LED1   P2.3         // LED D1
#define LED2   P13.0	    // LED D2
#define LED3   P4.5         // LED D3
#define LED4   P12.3	    // LED D4

//-----------------------------------------------------------------------------
// Function prototyps
//-----------------------------------------------------------------------------
void drive_LED(unsigned char value);

//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------
unsigned char LED_active;
unsigned char LED_duty;
unsigned char toggle;

//-----------------------------------------------------------------------------
// Module:   init_CPU
// Function: Initialization of CPU
//-----------------------------------------------------------------------------
#pragma section @@CODE  OPT1 AT 1000H
void init_CPU (void)
{
// stop watchdog timer
  WDTM = 0x70;

//clock gererator settings
  PCC = 0x00;			// set CPU clock to fx
  PPCC = 0x00;

  LSRCM = 0x01;			// low speed ring oscillator stops

  OSTS  = 0x00;			//shortest stabilisation time 2^10/fx

// interrupt setting
   IF0  = 0x00;
   IF1  = 0x00;
   MK0  = 0xFF;
   MK1  = 0xFF;
}

//-----------------------------------------------------------------------------
// Module:   init_LED
// Function: Initialization of LED ports
//-----------------------------------------------------------------------------
void init_LED (void)
{
  PMC2.3=0;           // set port mode control of P23 to port mode
  PM2.3=0;            // set port P23 to output mode -> LED1
  PM4.5=0;            // set port P45 to output mode -> LED3
  PM12.3=0;           // set port P123 to output mode -> LED4

  LED1=0;     	    // drive LED1
  LED2=0;	    // drive LED2
  LED3=0;           // drive LED3
  LED4=0;	    // drive LED4

  LED1=1;	    // switch LED1 off
  LED2=1;	    // switch LED2 off
  LED3=1;	    // switch LED3 off
  LED4=1;	    // switch LED4 off
  
}

//-----------------------------------------------------------------------------
// Module:   init_ADC
// Function: Initialization of AD converter
//-----------------------------------------------------------------------------
void init_ADC (void)
{
  PMC2.0=1;               //set alternate function mode for Port20
  PM2.0=1;                //set Port20 to input mode
}


//-----------------------------------------------------------------------------
// Module:   init_TM80
// Function: Initialization of Timer80
//-----------------------------------------------------------------------------
void init_TM80 (unsigned char time)
{
   TCE80 = 0;                   //disable timer80
   TMC80 = 0x06;		//set input clock to fxp / 2^10 7.81Khz @ 8MHz
   CR80  = time;		//set interval time	
   TCE80 = 1;                   //start timer80
   TMIF80 = 0;                  //clear interrupt request flag
   TMMK80=0;                    //enable timer80 interrupt
}

//-----------------------------------------------------------------------------
// Module:   adc_conversion
// Function: starts AD measurement on channel n
//-----------------------------------------------------------------------------
unsigned char adc_conversion(unsigned char channel)
{
  unsigned char result;
  ADS  = channel;               //select AD channel
  ADCE = 1;                     //enable voltage generator
  ADCS = 1;                     //start conversion
  while(!ADIF);                 //wait for conversion finished
  result = ADCRH;               //load AD conversion result
  ADCS = 0;                     //stop conversion
  ADIF = 0;                     //clear request flag
  return result;		//return AD result
}


//-----------------------------------------------------------------------------
// Module:   wait
// Function: generates a wait loop
//-----------------------------------------------------------------------------
__callt void wait(void)
{
  char a=0;
  while(a!=0xff)
  {
    a++;
  }
}

//-----------------------------------------------------------------------------
// Module:   main
// Function: main program
//-----------------------------------------------------------------------------

void main(void)
{
unsigned char adc_result;

  DI();        			// global interrupt disable
  init_CPU();           // CPU initialization
  init_LED();			// LED port initialization
  init_ADC();			// ADC initialization
  init_TM80(10);		// initialization of timer80
  EI(); 				// global interrupt enable


  while(1)
  {
    adc_result = adc_conversion(0x00); // start AD conversion

    LED_active = adc_result/64;        // convert result
    LED_duty = 64 - (adc_result%64);   // convert result
    wait();
  }
}

//-----------------------------------------------------------------------------
// ISR: 	  isr_INTTM80
// Function: Interrupt service routine of Timer80
//-----------------------------------------------------------------------------
__interrupt void isr_INTTM80(void)
{
   if(!toggle)
   {
      toggle=1;
      switch(LED_active)
      {
        case (0): drive_LED(0x1);
                  break;
        case (1): drive_LED(0x3);
                  break;
        case (2): drive_LED(0x7);
                 break;
        case (3): drive_LED(0xf);
                 break;
      }
    }
    else
    {
      toggle=0;
      switch(LED_active)
      {
        case (0): drive_LED(0x0);
                  break;
        case (1): drive_LED(0x1);
                  break;
        case (2): drive_LED(0x3);
                 break;
        case (3): drive_LED(0x7);
                 break;
      }
    }
    init_TM80(LED_duty);		// change timer interval
}


//-----------------------------------------------------------------------------
// Module:   drive_LED
// Function: Output of 4-bit value to LED port
//-----------------------------------------------------------------------------
void drive_LED(unsigned char value)
{
  LED1 =~(value>>3);
  LED2 =~(value>>2);
  LED3 =~(value>>1);
  LED4 =~(value);
}

⌨️ 快捷键说明

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