STM32
直播中

秋风宜人

14年用户 923经验值
擅长:基础元器件
私信 关注
[问答]

如何使用stm32f103rct6控制adf4351实现30M~~4G的正弦波?

如何使用STM32f103rct6控制adf4351实现30M~~4G的正弦波?

回帖(1)

王强

2021-12-17 14:03:29
刚开始接触adf4351的时候,发现不知道如何去下手去编写程序,
怎么通过按键去实现控制产生不同的正弦波,
同时通过OLED显示。代码如下:
#include “delay.h”
#include “sys.h”
#include “lcd.h”
#include “touch.h”
#include “gui.h”
#include “test.h”
#include “MS5611.h”
#include “24l01.h”
#include “spi.h”
#include “string.h”
#include “usart.h”
#include “key.h”
#include “ADF4351.h”
void KEY_Init(void);
u8 KEY_Scan(void);
u8 keycount=1;
int sweeptime=0;
int timebai=9;
int timeshi=0;
int timege=0;
int timeyi=0;
extern uint32_t Pressure;
extern uint64_t dT,TEMP;
void LCD_MS5611_DataShow(void);
u8 T[9],P[13];
int key1_flag,key0_flg,keyup_flag;
void ADF4351Init(void);
void ReadToADF4351(u8 count, u8 *buf);
void WriteToADF4351(u8 count, u8 *buf);
void WriteOneRegToADF4351(u32 Regster);
void ADF4351_Init_some(void);
void ADF4351WriteFreq(float Fre);
uint8_t data_buf[9]={0};
u8 tmp_buf[33];
float fre=80;
float frequence=80;
int main(void)
{
SystemInit();
key1_flag=0;key0_flg=0;keyup_flag=0;
delay_init(72);
//delay_ms(1000);
IIC_Init();
delay_ms(100);
KEY_Init();
delay_ms(100);
LCD_Init();
LCD_Clear(WHITE);
LCD_Clear(WHITE);
ADF4351Init();
ADF4351_Init_some();
ADF4351WriteFreq(fre) ;
while(1){
keycount=KEY_Scan();
switch(keycount)
{
case KEY0_PRES:{
LCD_Clear(WHITE);
Gui_StrCenter(0,40,BLUE,YELLOW,“sweep frequence”,16,1);


                        key0_flg=key0_flg+1;
                if(key0_flg==2)
                {
                        LCD_Clear(WHITE);       
                        Gui_StrCenter(0,40,BLUE,YELLOW,"start sweep",16,1);
                        while(key0_flg==2)
                        {
                                frequence=frequence+0.1;
                                keycount=KEY_Scan();  
                                delay_us(sweeptime);
                                if(keycount==1)
                                {
                                       
                                        key0_flg=key0_flg+1;
                                       
                                }
                                if(frequence>110)
                                {
                                        frequence=80;
                                }
                                ADF4351WriteFreq(frequence)        ;
                                       
                        }
                       
                key0_flg=0;       
                       
                }
               
               
               
                break;}


                case  KEY1_PRES:{
            LCD_Clear(WHITE);               
                        Gui_StrCenter(0,60,BLUE,YELLOW,"dop frequence",16,1);
                  key1_flag=key1_flag+1;
                if(key1_flag==2){
                        LCD_Clear(WHITE);       
    Gui_StrCenter(0,60,BLUE,YELLOW,"dop dop",16,1);                               
                        while((key1_flag==2))
                        {
                               
                                //Gui_StrCenter(0,60,BLUE,YELLOW,"dop dop",16,1);
                                keycount=KEY_Scan();  
                                if(keycount==1)
                                {
                                        LCD_Clear(WHITE);
                                        fre=fre+1;
                                       
                                        ADF4351WriteFreq(fre)        ;
                                        Gui_StrCenter(0,20,BLUE,YELLOW,"frequence",16,1);                       
                                        LCD_ShowNum(30,50,fre,5,16) ;
                                }
                                else if(keycount==2)
                                {
                                         key1_flag=key1_flag+1;
                                                LCD_Clear(WHITE);       
                                       
                                }
                                else if(keycount==3)
                                {
                                         LCD_Clear(WHITE);
                                        fre=fre-1;
                                        if(fre<35){
                                                fre=35;
                                                ADF4351WriteFreq(fre)        ;
                                        Gui_StrCenter(0,20,BLUE,YELLOW,"frequence",16,1);                       
                                        LCD_ShowNum(30,50,fre,5,16) ;
                                        }
                                        ADF4351WriteFreq(fre)        ;
                                        Gui_StrCenter(0,20,BLUE,YELLOW,"frequence",16,1);                       
                                        LCD_ShowNum(30,50,fre,5,16) ;
                                }
                               
                        }
                        key1_flag=0;
                }
               
                break;}
               
                case  WKUP_PRES:{
                        LCD_Clear(WHITE);               
                        Gui_StrCenter(0,60,BLUE,YELLOW,"step in",16,1);
                  keyup_flag=keyup_flag+1;
                        if(keyup_flag==2)
                        {
                                LCD_Clear(WHITE);               
                        Gui_StrCenter(0,60,BLUE,YELLOW,"step step",16,1);
                                while(keyup_flag==2)
                                {
                                       
                                        keycount=KEY_Scan();  
                                        if(keycount==2)
                                        {
                                                LCD_Clear(WHITE);
                                                                                                        timeyi=timeyi+1;
                                                if(timeyi>2)
                                                {
                                                timeyi=0;
                                                LCD_Clear(WHITE);               
                                                        Gui_StrCenter(0,20,BLUE,YELLOW,"bai wei",16,1);                       
                                                        LCD_ShowNum(30,50,timebai,5,16) ;       
                                                }
                                               
                                                else if(timeyi==0)
                                                {
                                                        LCD_Clear(WHITE);               
                                                        Gui_StrCenter(0,20,BLUE,YELLOW,"bai wei",16,1);                       
                                                        LCD_ShowNum(30,50,timebai,5,16) ;
                                                }
                                                else if(timeyi==1)
                                                {
                                                        LCD_Clear(WHITE);               
                                                        Gui_StrCenter(0,20,BLUE,YELLOW,"shi wei",16,1);                       
                                                        LCD_ShowNum(30,50,timeshi,5,16) ;
                                                }
                                                else if(timeyi==2)
                                                {
                                                        LCD_Clear(WHITE);               
                                                        Gui_StrCenter(0,20,BLUE,YELLOW,"ge wei",16,1);                       
                                                        LCD_ShowNum(30,50,timege,5,16) ;
                                                }
                                                sweeptime=sweeptime+timebai*100+timeshi*10+timege;
                                        }
                                else if(keycount==1)
                                {
                                        LCD_Clear(WHITE);
                                        if(timeyi==0){
                                               
                                                timebai=timebai+1;
                                                if(timebai>9)
                                                {
                                                        timebai=0;
                                                }
                                                                                                                LCD_Clear(WHITE);               
                                                        Gui_StrCenter(0,20,BLUE,YELLOW,"bai wei",16,1);                       
                                                        LCD_ShowNum(30,50,timebai,5,16) ;       
                                        }
                                                else if(timeyi==1){
                                                       
                                        timeshi=timeshi+1;
                                                        if(timeshi>9)
                                                        {
                                                                timeshi=0;
                                                        }
                                                        LCD_Clear(WHITE);               
                                                        Gui_StrCenter(0,20,BLUE,YELLOW,"shi wei",16,1);                       
                                                        LCD_ShowNum(30,50,timeshi,5,16) ;
                                                }
                                               
                                                else if(timeyi==2)
                                                {
                                                        timege=timege+1;
                                                        if(timege>9)
                                                        {
                                                                timege=0;
                                                               
                                                        }
                                                                                                                        LCD_Clear(WHITE);               
                                                        Gui_StrCenter(0,20,BLUE,YELLOW,"ge wei",16,1);                       
                                                        LCD_ShowNum(30,50,timege,5,16) ;
                                                }
                                       
                                        sweeptime=sweeptime+timebai*100+timeshi*10+timege;
                                }
                                else if(keycount==3)
                                {
                                        keyup_flag=keyup_flag+1;
                                }
                        }
                        keyup_flag=0;       
                        }
                break;       
                }                 
        }
}
}


void LCD_MS5611_DataShow(void)
{
//unsigned char T[9],P[13];


  T[0] = 'T';
  T[1] = ':';
        T[2] = TEMP/1000 + 0x30;
        T[3] = TEMP%1000/100 + 0x30;
        T[4] = '.';
        T[5] = TEMP%100/10 + 0x30;
        T[6] = TEMP%10 + 0x30;
  T[7]='C';
        T[8] = '';


       
  P[0] = 'P';
P[1] = ':';
        P[2] = Pressure/100000 + 0x30;
        P[3] = Pressure%100000/10000 + 0x30;
        P[4] = Pressure%10000/1000 + 0x30;
        P[5] = Pressure%1000/100 + 0x30;
        P[6] = '.';
        P[7] = Pressure%100/10 + 0x30;
        P[8] = Pressure%10 + 0x30;
        P[10] = 'p';
        P[11] = 'a';
        P[12] = '';

        //LCD_ShowString(100,190,200,16,16, P);       


  delay_ms(500);
        LCD_Clear(WHITE);
        Gui_StrCenter(0,28,BLUE,YELLOW,P,16,1);
        Gui_StrCenter(0,60,BLUE,YELLOW,T,16,1);
       


        //printf("        Pressure : %urnrnrn",Pressure); //´®¿ÚÊä³öԭʼÊý¾Ý


}
u8 KEY_Scan(void)
{
if((KEY00||KEY10||WK_UP1))
{
delay_ms(10);//È¥¶¶¶¯
if(KEY00){
delay_ms(100);
while(!KEY0){;}
return KEY0_PRES;
}
else if(KEY1==0){
                if(KEY1==0){
                delay_ms(100);
                while(!KEY1){;}
               
                return KEY1_PRES;
       
}        }
               
        else if(WK_UP==1){
               
                if(WK_UP==1){
                delay_ms(100);
                while(WK_UP){;}
               
                return WKUP_PRES;
       
}             
//return 0;// ÎÞ°´¼ü°´ÏÂ


}
}
}
void KEY_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;


RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC,ENABLE);


GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_1;//PA15
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_13;//PC5
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_0;//PA0
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;  
GPIO_Init(GPIOA, &GPIO_InitStructure);


}
主要adf4351初始化代码如下:
void ADF4351Init(void)
{
u8 buf[4] = {0,0,0,0};


ADF_Output_GPIOInit();


buf[3] = 0x00;
buf[2] = 0x58;
buf[1] = 0x00;                                //write communication register 0x00580005 to control the progress
buf[0] = 0x05;                                //to write Register 5 to set digital lock detector
WriteToADF4351(4,buf);               


buf[3] = 0x00;
buf[2] = 0xec;                                //(DB23=1)The signal is taken from the VCO directly;(DB22-20:4H)the RF divider is 16;(DB19-12:50H)R is 80
buf[1] = 0x80;                                //(DB11=0)VCO powerd up;
buf[0] = 0x3C;                                //(DB5=1)RF output is enabled;(DB4-3=3H)Output power level is 5
WriteToADF4351(4,buf);               


buf[3] = 0x00;
buf[2] = 0x00;
buf[1] = 0x04;                                //(DB14-3:96H)clock divider value is 150.
buf[0] = 0xB3;
WriteToADF4351(4,buf);       


buf[3] = 0x00;
buf[2] = 0x01;                                //(DB6=1)set PD polarity is positive;(DB7=1)LDP is 6nS;
buf[1] = 0x0E;                                //(DB8=0)enable fractional-N digital lock detect;
buf[0] = 0x42;                                //(DB12-9:7H)set Icp 2.50 mA;
WriteToADF4351(4,buf);                //(DB23-14:1H)R counter is 1


buf[3] = 0x00;
buf[2] = 0x00;
buf[1] = 0x80;                           //(DB14-3:6H)MOD counter is 6;
buf[0] = 0x29;                           //(DB26-15:6H)PHASE word is 1,neither the phase resync
WriteToADF4351(4,buf);           //nor the spurious optimization functions are being used
                                                   //(DB27=1)prescaler value is 8/9


buf[3] = 0x00;
buf[2] = 0x2c;
buf[1] = 0x80;
buf[0] = 0x18;                                //(DB14-3:0H)FRAC value is 0;
WriteToADF4351(4,buf);                //(DB30-15:140H)INT value is 320;       


}
void ADF4351_Init_some(void)
{
WriteOneRegToADF4351(ADF4351_R2);
WriteOneRegToADF4351(ADF4351_R3);
WriteOneRegToADF4351(ADF4351_R5);
}
adf4351写入频率函数:
void ADF4351WriteFreq(float Fre) // (xx.x) M Hz
{
u16 Fre_temp, N_Mul = 1, Mul_Core = 0;
u16 INT_Fre, Frac_temp, Mod_temp, i;
u32 W_ADF4351_R0 = 0, W_ADF4351_R1 = 0, W_ADF4351_R4 = 0;
float multiple;


if(Fre < 35.0)
        Fre = 35.0;
if(Fre > 4400.0)
        Fre = 4400.0;
Mod_temp = 1000;
Fre = ((float)((u32)(Fre*10)))/10;


Fre_temp = Fre;
for(i = 0; i < 10; i++)
{
        if(((Fre_temp*N_Mul) >= 2199.9) && ((Fre_temp*N_Mul) <= 4400.1))
                break;
        Mul_Core++;
        N_Mul = N_Mul*2;
}


multiple = (Fre*N_Mul)/25;               
INT_Fre = (u16)multiple;
Frac_temp = ((u32)(multiple*1000))%1000;
while(((Frac_temp%5) == 0) && ((Mod_temp%5) == 0))
{
        Frac_temp = Frac_temp/5;
        Mod_temp = Mod_temp/5;
}
while(((Frac_temp%2) == 0)&&((Mod_temp%2) == 0))
{
        Frac_temp = Frac_temp/2;
        Mod_temp = Mod_temp/2;
}
Mul_Core %= 7;
W_ADF4351_R0 = (INT_Fre<<15)+(Frac_temp<<3);
W_ADF4351_R1 = ADF4351_R1_Base + (Mod_temp<<3);
W_ADF4351_R4 = ADF4351_R4_ON + (Mul_Core<<20);
WriteOneRegToADF4351(ADF4351_RF_OFF);
WriteOneRegToADF4351(W_ADF4351_R1);
WriteOneRegToADF4351(W_ADF4351_R0);
WriteOneRegToADF4351(W_ADF4351_R4);


// WriteOneRegToADF4351((u32)(ADF4351_R4_ON + (Mul_Core<<20)));
}
举报

更多回帖

发帖
×
20
完善资料,
赚取积分