Re: PIC18F26K20 搭配A9850模組使用

作者 ahao1022 於 2019年08月05日 22:34:25
非常抱歉個位版大再發文有移
肉最重要的code及圖片



#include "mcc_generated_files/mcc.h"

/*
Main application
*/
//unsigned char freq[8];
unsigned char freq[8]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};


//-------------------------------------------------------
// AD9850 serial mode rest
//-------------------------------------------------------
void AD9850_rest_serial(void)
{
SCK_SetLow();//AD9850 WCLK
FQUP_SetLow(); //AD9850 FQUD
//rest single
AD9850_Rest_SetLow();
AD9850_Rest_SetHigh();
AD9850_Rest_SetLow();
//WCLK single
SCK_SetLow();//AD9850 WCLK
SCK_SetHigh();
SCK_SetLow();
//fQUD single
FQUP_SetLow();//AD9850 FQUD
FQUP_SetHigh();
FQUP_SetLow();

}

//-------------------------------------------------------
// Write commands and data to the AD985(serial port)
//-------------------------------------------------------
void AD9850_wr_serial(unsigned int y)
{
char i,w;
long int a,b,c,x;
//Calculate the HEX value of the frequency
a=4294967296/125000000;//2^32/125M
b=y/1000000;
c=b*a;

//write w4 data 0~7
w=(c>>0);
for(i=0;i<8;i++)
{
x=SPI_Exchange8bit('w');
x>>i&0x01;
SCK_SetHigh();//AD9850 WCLK
SCK_SetLow();//AD9850 WCLK
}
//write w3 data 8~15

w=(c>>8);
for(i=0;i<8;i++)
{
x=SPI_Exchange8bit('w');
x>>i&0x01;
SCK_SetHigh();//AD9850 WCLK
SCK_SetLow();//AD9850 WCLK

}
//write w2 data

w=(c>>16);
for(i=0;i<8;i++)
{
x=SPI_Exchange8bit('w');
x>>i&0x01;
SCK_SetHigh();//AD9850 WCLK
SCK_SetLow();//AD9850 WCLK

}
//write w1 data
w=(c>>24);
for(i=0;i<8;i++)
{
x=SPI_Exchange8bit('w');
x>>i&0x01;
SCK_SetHigh();//AD9850 WCLK
SCK_SetLow();//AD9850 WCLK

}
//write w0 data 注意
w=c;
for(i=0;i<8;i++)
{
x=SPI_Exchange8bit('w');
x>>i&0x01;
SCK_SetHigh();//AD9850 WCLK
SCK_SetLow();//AD9850 WCLK
}
//Move in enable
FQUP_SetHigh();//AD9850 FQUD
FQUP_SetLow();

}


void main(void)
{
// Initialize the device
SYSTEM_Initialize();
AD9850_rest_serial();
AD9850_wr_serial(1000);
//Serial write 1000Hz program

// If using interrupts in PIC18 High/Low Priority
Mode you need to enable the Global High and Low
Interrupts

// If using interrupts in PIC Mid-Range Compatibility
Mode you need to enable the Global and Peripheral
Interrupts
// Use the following macros to:

// Enable the Global Interrupts
//INTERRUPT_GlobalInterruptEnable();

// Disable the Global Interrupts
//INTERRUPT_GlobalInterruptDisable();

// Enable the Peripheral Interrupts
//INTERRUPT_PeripheralInterruptEnable();

// Disable the Peripheral Interrupts
//INTERRUPT_PeripheralInterruptDisable();

while (1)
{
// Add your application code
}
}
/**
End of File
*/

來自: http://www.microchip.com.tw/newbb/viewtopic.php?forum=1&topic_id=22699&post_id=79857