main.c

来自「这是单片机C51典型应用设计代码」· C语言 代码 · 共 33 行

C
33
字号
#include <REG515.h>
#include <stdio.h>

#define V_REF 5
#define V_SCALE (0xFF/V_REF)
#define R_SCALE (99/V_REF)

unsigned char volt;

void main(void)
{
  //setup serial 0
  SCON   = 0x50;    // SCON0: mode 1, 8-bit UART, enable rcvr
  TMOD  |= 0x20;    // TMOD:  timer 1, mode 2, 8-bit reload
  TH1    = 221;     // TH1:   reload value for 9600 baud @ 16MHz
  TR1    = 1;       // TR1:   timer 1 run
  TI     = 1;       // TI0:   set TI to send first char of UART

  ADCON |= 0xF8;    //Set MUX to pin 0 and mode to single-shot
  DAPR   = 0x00;    //Set voltage range 0-5v and start first sample

  while(1)
  {
    if(!BSY)
    {
      volt = ADDAT;     //Get the result from the ADC
      printf("AIN0 Volts = %u.%.3u\n", (int)(volt/V_SCALE), (int)(volt%V_SCALE)*R_SCALE);
      IADC = 0;         //Clear the ADC interrupt flag
      DAPR = 0x00;      //Start next sample
    }
  }
}

⌨️ 快捷键说明

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