main.c

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

C
98
字号
/*-----------------------------------------------------------------------------
The following program generates sine and sawtooth waves on the DAC outputs
of the Philips LPC Devices.  This code was compiled and tested using the
Keil Software PK51 Professional Developer's Kit.
-----------------------------------------------------------------------------*/
#include <REG769.H>

/*-----------------------------------------------
 Sine Wave Table 
-----------------------------------------------*/
unsigned char xdata sintab [] = {
0x00, 0x01, 0x03, 0x04, 0x06, 0x07, 0x09, 0x0A, 
0x0C, 0x0E, 0x0F, 0x11, 0x12, 0x14, 0x15, 0x17, 
0x18, 0x1A, 0x1C, 0x1D, 0x1F, 0x20, 0x22, 0x23, 
0x25, 0x26, 0x28, 0x29, 0x2B, 0x2C, 0x2E, 0x2F, 
0x30, 0x32, 0x33, 0x35, 0x36, 0x38, 0x39, 0x3A, 
0x3C, 0x3D, 0x3F, 0x40, 0x41, 0x43, 0x44, 0x45, 
0x47, 0x48, 0x49, 0x4A, 0x4C, 0x4D, 0x4E, 0x4F, 
0x51, 0x52, 0x53, 0x54, 0x55, 0x57, 0x58, 0x59, 
0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60, 0x61, 
0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 
0x6A, 0x6B, 0x6C, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 
0x70, 0x71, 0x72, 0x73, 0x73, 0x74, 0x75, 0x75, 
0x76, 0x76, 0x77, 0x77, 0x78, 0x79, 0x79, 0x7A, 
0x7A, 0x7A, 0x7B, 0x7B, 0x7C, 0x7C, 0x7C, 0x7D, 
0x7D, 0x7D, 0x7E, 0x7E, 0x7E, 0x7E, 0x7F, 0x7F, 
0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F };

static unsigned char DA0_next_out;
static unsigned char DA1_next_out;
static unsigned int i;

void timer0_isr (void) interrupt 1
{
/*-----------------------------------------------
Output D/A Value 
-----------------------------------------------*/
DAC0 = DA0_next_out;       /* previous Sine Wave value */
DAC1 = DA1_next_out;       /* previous Sawtooth value  */

/*-----------------------------------------------
Calculate next D/A Value
-----------------------------------------------*/

if (i++ >= 511) i = 0;

if (i >= 384)
  DA0_next_out = 127 - sintab[127 - (i % 128)];  /* 180 - 270 quadrant   */
else if (i >= 256)
  DA0_next_out = 127 - sintab[i % 128];          /* 90 - 180 quadrant    */
else if (i >= 128)
  DA0_next_out = 128 + sintab[127 - (i % 128)];  /* 0 - 90 quadrant      */
else
  DA0_next_out = 128 + sintab[i];                /* 270 - 0 quadrant     */

DA1_next_out = i % 256;                          /* For Sawtooth pattern */
}

void main (void)
{
/*-----------------------------------------------
Disable the A/D Converter (this is required for
DAC0)
-----------------------------------------------*/
ADCI = 0;  /* Clear A/D conversion complete flag */
ADCS = 0;  /* Clear A/D conversion start flag */
ENADC = 0; /* Disable the A/D Converter */

/*-----------------------------------------------
Set P1.6 and P1.7 to Input Only (Hi Z).
-----------------------------------------------*/
P1M2 &= ~0xC0;
P1M1 |=  0xC0;

/*-----------------------------------------------
Enable the D/A Converter
-----------------------------------------------*/
ENDAC0 = 1;
ENDAC1 = 1;

/*-----------------------------------------------
 Initialize Timer 0 Interrupt 
-----------------------------------------------*/
TMOD  = 0x02;               /* TMOD: Timer 0, mode 2, 8-bit reload        */
TCON  = 0x10;               /* TCON:                                      */
TH0   = 240;                /* TH0:  Reload value                         */
TL0   = 251;                /* TL0:  Reload value                         */
ET0   = 1;                  /* ET0:  Enable timer 0                       */
TR0   = 1;                  /* TR0:  Timer 0 run                          */
EA    = 1;                  /* EA:   Enable interrupts                    */

while (1) {
  WDRST = 0x1E;			        /* Reset Watchdog Timer                       */
  WDRST = 0xE1;	    		    /* Reset Watchdog Timer                       */																	   
  }
}

⌨️ 快捷键说明

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