|
我用××r单片机做过,这是代码
#include"mega8.h"
#define res PORTD.0
#define w_clk PORTD.1
#define fq PORTD.2
#define da PORTD.3
#define sd PORTC.2
#define clk PORTC.1
#define cs PORTC.0
#define ld PORTC.3
unsigned char w=0;
flash unsigned int sin[32]={2048,2396,2744,3072,3358,3624,3973,4055,
4095,4055,3973,3624,3358,3072,2744,2396,
2048,1699,1355,1024,737,471,122,41,
0,41,122,471,737,1024,1355,1699};
void delay_nus(unsigned char x) {
unsigned char y;
for(y=x;y>0;y--);
}
void delay_1us(){
;
;
}
void dac(unsigned int d){
unsigned char k;
unsigned int za;
za=d;
ld=1;
delay_1us();
cs=0;
for(k=12;k>0;k--)
{clk=0;
if(za&0x800)
sd=1;
else
sd=0;
za<<=1;
clk=1;
}
cs=1;
ld=0;
}
void TransAD9851(unsigned long F,unsigned char P)
{
unsigned long temp;unsigned char i,temp2;
//关掉全局中断
temp=F*23.861351;//计算32位频率控制字
temp2=P;
temp2=temp2%360;
temp2=4*temp2/45;
temp2=temp2<<3 | 1 ;
fq=0;
w_clk=0;
delay_1us();
for(i=0;i<32;i++)//将32位频率控制字移入DDS芯片寄存器
{
if(temp&0x000000001)
da=1;
else
da=0;
delay_1us();
w_clk=1;
delay_1us();
temp=temp>>1;
w_clk=0;
da=0;
delay_1us();
}
for(i=0;i<8;i++)
//将5位相位控制字及两个(power\reset)控制位移入DDS芯片寄存器
{
if(temp2&0x01)
da=1;
else
da=0;
delay_1us();
w_clk=1;
delay_1us();
temp2=temp2>>1;
w_clk=0;
da=0;
delay_1us();
}
fq=1;
delay_nus(3);
fq=0;
delay_nus(3);
//SEI();
}
void main(){
DDRB=0xff;
DDRD=0xff;
DDRC=0xff;
PORTD=0;
TCCR0=0X01;
//TIMSK=0X01;
TCNT0=0Xfe;
SREG=SREG|0X80;
res=1;
delay_1us();
res=0;
w_clk=0;
delay_nus(2);
w_clk=1;
delay_nus(2);
fq=1;
delay_nus(3);
fq=0;
// dac(2048);
TransAD9851(1000,0);
while(1){
//if(w>=32)
// w=0;
}
}
interrupt [TIM0_OVF] void timer0_ovf_isr(void) {
dac(sin[w]);
w++ ;
TCNT0=0Xfe; } |
|