📄 sin-genereter.c
字号:
//-----------------------------------------------------------------------------
// F020正弦波
//-----------------------------------------------------------------------------
// Copyright 2005 Silicon Laboratories, Inc.
// http://www.silabs.com
//
// Program Description:
//
// This program flashes the P2.2 green LED on the C8051F34x target board
// five times a second using the interrupt handler for Timer2.
//
//
// 通过ad7541ad转化器,7204缓冲器,共12位数据
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <c8051f020.h> // SFR declarations
#include <math.h>
//-----------------------------------------------------------------------------
// Global Constants
//-----------------------------------------------------------------------------
#define dport1 P7
#define dport2 P2
#define PI 3.1425926
#define displace 0x07ff
sbit LED1 = P2^2; // LED='1' means ON
sbit W_ =P3^0;
sbit FF_ =P3^1;
sbit EF_ =P3^2;
sbit HF_ =P3^3;
sbit R_ =P3^4;
sbit RS_ =P3^5;
sbit FL_ =P3^6;
sbit b7=P2^7;
sbit b6=P2^6;
sbit b5=P2^5;
sbit b4=P2^4;
unsigned char dataport1;
unsigned char dataport2;
int code TABLE[256] = {0x0,0x1,0x2,0x3,0x4,0x5,0x6,0x7,0x8,0x9,0xa,0xb,0xc,0xd,0xe,0xf};
int xdata SINE_TABLE2[2048];
double y;
//-----------------------------------------------------------------------------
// Function Prototypes
//-----------------------------------------------------------------------------
void OSCILLATOR_Init (void);
void PORT_Init (void);
void reset7204(void);
void write7204(void);
void read7204(void);
void retran(void);
tran16(int num);
//-----------------------------------------------------------------------------
// main() Routine
//-----------------------------------------------------------------------------
void main (void)
{int i;
WDTCN = 0xde;
WDTCN = 0xad;
OSCILLATOR_Init (); // Initialize system clock to
// 24.5MHz
PORT_Init (); // Initialize crossbar and GPIO
reset7204();
//------------------------------
for(i=0;i<2048;i++)
{
y=5.0*sin(2*PI*i/2048)+5.0;
SINE_TABLE2[i]=tran16((int)(0x0fff*y/10));
//SINE_TABLE2[20]=tran16(4000);
}
//------------------------------
for(i=0;i<2048;i++)
{
dataport1=SINE_TABLE2[i];
dataport2=SINE_TABLE2[i]>>4;
write7204();}
while (1)
{
while(EF_) //7204未空则向外读
{
for (i=0; i < 20; i++) ;//延时,调节频率
read7204();}
retran();
}
}
//-----------------------------------------------------------------------------
// Initialization Subroutines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// OSCILLATOR_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// This function initializes the system clock to (24.5 Mhz / 3)
//
//-----------------------------------------------------------------------------
void OSCILLATOR_Init (void)
{
int i; // delay counter
OSCXCN = 0x67; // start external oscillator with
// 22.1184MHz crystal
for (i=0; i < 2560; i++) ; // XTLVLD blanking interval (>1ms)
//while (!(OSCXCN & 0x80)) ; // Wait for crystal osc. to settle
OSCICN = 0x88; // select external oscillator as SYSCLK
// source and enable missing clock
// detector
}
//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// This function configures the crossbar and GPIO ports.
//
// P2.2 digital push-pull LED1
//-----------------------------------------------------------------------------
void PORT_Init (void)
{
XBR0 = 0x04; // Enable UART0
XBR1 = 0x00;
XBR2 = 0x40; // Enable crossbar and weak pull-ups
P0MDOUT |= 0x01; // enable TX0 as a push-pull output
P2MDOUT |= 0xf0; // enable P1.6 (LED) as push-pull output
P3MDOUT |= 0xfF;
P74OUT|=0X80;
}
//----------------------------------------------------------------------------
//7204RESET
//----------------------------------------------------------------------------
void reset7204(void)
{
int i;
RS_=1;
for(i=0;i<1;i++);
RS_=0;
W_=1;
R_=1;
for(i=0;i<5;i++);
RS_=1;
W_=0;
}
//----------------------------------------------------------------------------
//7204RITE
//----------------------------------------------------------------------------
void write7204(void)
{
int i;
W_=1;
W_=0;
dport1=dataport1;
dport2=dataport2;
for(i=0;i<1;i++);
W_=1;
}
//----------------------------------------------------------------------------
//7204read
//----------------------------------------------------------------------------
void read7204(void)
{
R_=1;
R_=0;
R_=1;
}
//----------------------------------------------------------------------------
//7204retran
//----------------------------------------------------------------------------
void retran(void)
{
FL_=1;
FL_=0;
R_=1;
FL_=1;
}
//-----------------------------------------------------------------------------
// 10-16十进制转化十六进制 (三位)
//-----------------------------------------------------------------------------
tran16(int num)
{
unsigned char re1,re2,re3;
unsigned int num1,num2;
num1=num/16;
re1=num%16;
re1=TABLE[re1];
num2=num1/16;
re2=num1%16;
re2=TABLE[re2];
re3=num2;
re3=TABLE[re3];
return re3*16*16+re2*16+re1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -