main.c

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

C
60
字号
#include <REG767.H>
#include <stdio.h>

void main (void)
{
unsigned char channel;

/*-----------------------------------------------
Disable P0 Output and Input.
-----------------------------------------------*/
P0M2 &= ~0x78;  /* Set P0 for Input Only */
P0M1 |=  0x78;
PT0AD =  0x78;  /* Disable Digital Inputs */

/*-----------------------------------------------
Enable the A/D Converter and use the CPU clock
as the A/D clock.
-----------------------------------------------*/
ENADC = 1;
RCCLK = 0;

/*-----------------------------------------------
Configure the serial port to run at 1200 Baud.
-----------------------------------------------*/
SCON  = 0x50;
TMOD |= 0x20;
TH1   = 0xA9;
TR1   = 1;
TI    = 1;

/*-----------------------------------------------
Perform conversions forever.
-----------------------------------------------*/
while (1)
  {
/*-----------------------------------------------
Update the channel number and store it in ADCON.
-----------------------------------------------*/
  channel = (channel + 1) % 4;

  ADCON &= ~0x03;
  ADCON |= (channel & 0x03);

/*-----------------------------------------------
Start a conversion and wait for it to complete.
-----------------------------------------------*/
  ADCI = 0;                  /* Clear conversion status */
  ADCS = 1;                  /* Start conversion */
  while (ADCI == 0);         /* Wait until conversion done */

/*-----------------------------------------------
Read A/D data and print it out.
-----------------------------------------------*/
  printf ("ADC Channel %bu = 0x%2.2bX\r\n",
          (unsigned char) channel,
          (unsigned char) DAC0);
  }
}

⌨️ 快捷键说明

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