威望0
积分7946
贡献0
在线时间763 小时
UID1
注册时间2021-4-14
最后登录2024-11-21
管理员
- UID
- 1
- 威望
- 0
- 积分
- 7946
- 贡献
- 0
- 注册时间
- 2021-4-14
- 最后登录
- 2024-11-21
- 在线时间
- 763 小时
|
[mw_shl_code=cpp,true]///S波段单片机控制2820MHZ,出S波段DDS
//不设置校验,本振单片机对程序slip 数据进行解包,将接报数据处理,并且往下级单片机传递。
#include "C8051F120.h"
#include <intrins.h>
unsigned char SPI_Data = 0xA5;
typedef unsigned char uchar;
typedef unsigned int uint;
typedef unsigned short ushort;
unsigned char xdata Com0_Rxd_Buff [5] ; //串口0接收缓冲区
unsigned char SPI_Data_Array[2] = {0,0};
uchar xdata comb0_txd_buff[5]; //串口0发送缓冲区
uint xdata Com0_Count_Rxd ; // 串行0接口接收计数器
uint xdata Com0_Delay_Count_Rxd=0 ; //串口0接收延时记数器
uchar xdata Com0_Flag_Rxd =0 ; //UART0接收延时标志
///uchar xdata pinlv_choose =0 ; //通过SPI传入的1byte数据
unsigned long suanshu1(unsigned char rxd1[4]);// SEND 03 04
void send_frequency(void );//频点预置
unsigned char xdata rypinlv[4];
void TEMPERATURE_INIT( );
//端口定义
sbit CLK1=P2^0;
sbit DAT1=P2^1;
sbit LEE1=P3^7;///本振1:2430
////////////////
//DDS s波段dds管脚 260-460MHZ
sbit SCLK=P0^4; // pin21
sbit SDIO=P0^5; // pin20
sbit IOUPDATE=P0^6; //pin86
sbit DDSRESET=P0^7; //pin85
/////////////////中频衰减器
sbit ATTEN1_B1=P2^4;//1
sbit ATTEN1_B2=P2^5;//
sbit ATTEN1_B3=P2^6;//
sbit ATTEN1_B4=P2^7;//8
sbit ATTEN1_B5=P1^0;//16
/////////////////射频衰减器
sbit ATTEN2_B1=P1^5;//1
sbit ATTEN2_B2=P1^4;//2
sbit ATTEN2_B3=P1^3;//4
sbit ATTEN2_B4=P1^2;//8
sbit ATTEN2_B5=P1^1;//16
///////////////////////
sbit TRIGS=P3^4;// 发射触发给703
unsigned long ValueToWrite1=0;
uchar flag_counts = 0;
uchar spi_countflag = 0;
uchar spi_pllflag =0;
//uchar flag_uart0 = 0;//进串口0标志位
uint delay_count = 0;//三秒计时,三秒未收到,主动上报
uchar delay_3second_flag=0;//三秒标志位1表示已经过了三秒
//子函数声明
void WDT_Init (void); //禁止看门狗
void Oscillator_Init(void); //系统时钟初始化
void PORT_Init (void);
void timer0_init (void); //初始化定时器0
void UART_Init (void);
void UART0_TX(uchar leng);
void SPI0_Init (void); //SPI初始化
void Init_Device (void);
void freq_jump (void);//跳频程序算法
void freq_dingzhi (void);
void ddssongshu(void);
void send08regsitor(unsigned long ValueToWrite2);
//检波通道Ain0.7 AD8313 检波通道Ain0.6 ADL6010
void SPI_Rxd_Buff(void);
void Check_Com0_Rxd_Buff_0X01( void ) ;
unsigned char xdata plchoose[64];
void ATTEN1(unsigned char dat3); //中频1数控衰减器
void ATTEN2(unsigned char dat3); //射频数控衰减器
/*============================================================
*
* 函 数 名:time_t0()
*
* 参 数:
*
* 功能描述: 定时中断T0
============================================================*/
void time_t0(void) interrupt 1 using 1
{
TH0 = 0xee ;
TL0 = 0x00 ; // 定时5ms
if( ( Com0_Flag_Rxd == 1)&& (Com0_Delay_Count_Rxd != 0) ) Com0_Delay_Count_Rxd--; //UART0接收延时
delay_count++;
}
void delay (unsigned long length) //延时子函数
{
while (length >0)
length--;
}
/*============================================================
*
* 函 数 名:UART0_ISR
*
* 参 数:
*
* 功能描述:串口0中断服务程序
*
============================================================*/
void UART0_ISR (void) interrupt 4 using 1
{
if(RI0!=0){
RI0 = 0;
if ( Com0_Count_Rxd <5)
{
Com0_Rxd_Buff [Com0_Count_Rxd] = SBUF0;
Com0_Count_Rxd++;
Com0_Flag_Rxd = 1;
}
}
}
/////////////////////////////////////
void Oscillator_Init()
{
//24.5MHZ内部时钟 内部时钟频率倍增
int i = 0;
SFRPAGE = CONFIG_PAGE;
OSCICN = 0x83;
CCH0CN &= ~0x20;
SFRPAGE = LEGACY_PAGE;
FLSCL = 0x90;
SFRPAGE = CONFIG_PAGE;
CCH0CN |= 0x20;
PLL0CN |= 0x01;
PLL0DIV = 0x01;
PLL0FLT = 0x21;
PLL0MUL = 0x02;
for (i = 0; i < 15; i++); // Wait 5us for initialization
PLL0CN |= 0x02;
while ((PLL0CN & 0x10) == 0);
CLKSEL = 0x12;
}
/*============================================================
*
* 函 数 名:Timer0_Init
*
* 参 数:
*
* 功能描述:定时中断0初始化
============================================================*/
void Timer0_Init (void)
{
TMOD |= 0x01 ; //定时器工作在方式1,16位定时器\定时器0采用系统时钟12分频时基
TH0 = 0xee ; TL0 = 0x00 ; //定时器0初始值 \5ms溢出
TR0 = 1 ; //启动定时器0
ET0 = 1 ; //开定时器0中断
}
/*============================================================
*
* 函 数 名:PORT_Init
*
*
* 功能描述:端口初始化
============================================================*/
void PORT_Init (void)
{
SFRPAGE = CONFIG_PAGE;
P0MDOUT = 0xF1;
P1MDOUT = 0xFF;
P2MDOUT = 0xFF;
P3MDOUT = 0xF7;
XBR0 = 0x04;
XBR1 = 0x0A;
XBR2 = 0x40;
}
/*============================================================
*
* 函 数 名:UART_Init
*
* 参 数:
*
* 功能描述:UART端口初始化115200
============================================================*/
void UART_Init (void)
{
SFRPAGE = TIMER01_PAGE;
TCON = 0x51;
TMOD = 0x22;
CKCON = 0x18;
TH0 = 0xEE;
TH1 = 0xF3;
SFRPAGE = UART0_PAGE;
SCON0 = 0x50; //
// SSTA0 = 0x10;
IE = 0x93;//总中断中断
}
////////////////////////////////////////703 2430MHZ
void sent31(unsigned char dat0)
{
unsigned char i=0;
unsigned char a=0x80;
dat0=dat0<<1;
for(;i<7;i++)
{
CLK1=0;
if((dat0&a)!=0) DAT1=1;
else DAT1=0;
CLK1=1;
a/=2;
}
}
void sent01(unsigned char dat0)
{
unsigned char i=0;
unsigned char a=0x80;
for(;i<8;i++)
{
CLK1=0;
if((dat0&a)!=0)DAT1=1;
else DAT1=0;
CLK1=1;
a/=2;
}
}
void sent11(unsigned char dat1[4])
{
unsigned char i=0;
LEE1=1;
sent31(dat1);
for(i=1;i<4;i++) sent01(dat1);
CLK1=0;
CLK1=1;
CLK1=0;
LEE1=0;
}
/////////////////////////
void Check_Com0_Rxd_Buff( void )
{
uchar atten1=0;
uchar sum1=0;
uchar i=0;
mess_low8=Com0_Rxd_Buff [4];
mess_high8=Com0_Rxd_Buff [3];
messagesource_low8=Com0_Rxd_Buff [2];
messagesource_high8=Com0_Rxd_Buff [1];
atten1 =Com0_Rxd_Buff [6];
////////////////首先对串口命令进行处理
if(Com0_Rxd_Buff [7]==0X01) // RECE
{
ATT_SWITCH=0;
ATTEN1(20);
ATTEN2(5);
if((atten1>0)&&(atten1<=11))
{
ATTEN1(20+atten1);
ATTEN2(5);
}
if((atten1>11)&&(atten1<=37))
{
ATTEN1(31);
ATTEN2(atten1-6);
}
if((atten1>37)&&(atten1<=60))
{
ATTEN1(31);
ATTEN2(31);
}
}
////////////////////////
if(Com0_Rxd_Buff [7]==0X10) // TRAN
{
ATT_SWITCH=1;
ATTEN1(31);
ATTEN2(31);
}
///////////////////串口回复
comb0_txd_buff[0]=0X5A;
comb0_txd_buff[2]=mess_low8;
comb0_txd_buff[1]=mess_high8;
comb0_txd_buff[4]=messagesource_low8;
comb0_txd_buff[3]=messagesource_high8;
comb0_txd_buff[5]=Com0_Rxd_Buff [5];
comb0_txd_buff[6]=Com0_Rxd_Buff [6];
comb0_txd_buff[7]=Com0_Rxd_Buff [7];
comb0_txd_buff[10]=0X7B;
TEMPERATURE_INIT( );
for(i=1;i<9;i++)
{
sum1+=comb0_txd_buff;
}
comb0_txd_buff[9]=sum1;
UART0_TX(11);
}
//////////////
/*============================================================
*
* 函 数 名:main
*
* 参 数:
*
* 功能描述:主函数
*
*
* 返 回 值:
*
* 抛出异常:
*
============================================================*/
main()
{
char ii; uchar jj=0; uchar sum=0;
uint uart_real_length =0;
unsigned int crc_16 =0; uchar crc16_reallongth =0;
//默认关闭功放28V和接收5V
// LD1=0;
SP = 0x60;///////////////////////////////////////////////////////////////////////////////////////////////////////////////
Init_Device() ;
Com0_Delay_Count_Rxd =20 ; //Com0_Delay_Count_Rxd =100 ; //UART0接收总延时50*5ms/200字节
Com0_Flag_Rxd =0 ; //UART0接收延时标志
Com0_Count_Rxd =0;
UART_Init(); //串口初始化
//LELOPOWER=1;
//POWER_PROTECK=1;
delay(6000);
comb0_txd_buff[0]=0x55;
comb0_txd_buff[1]=0x55;
comb0_txd_buff[2]=0x31;
comb0_txd_buff[3]=0x56;
ii=4;
UART0_TX(ii);
SFRPAGE =0;
timer0_init (); //初始化定时器0
delay(1000);
/////////////////////////////////////////////////////////////////////////
send_frequency( );
///////////
DDSRESET=1;
delay(20);
DDSRESET=0;
delay(1000);
ddssongshu();
////////////
SFRPAGE =0;
EA=1;
//ES0 = 1 ;
// IP = 0x10;
EX0 = 1; // Enable /INT0 interrupts
// EX1 = 1;
for(;;)
{
IE = 0x93;
// Enable /INT0 interrupts
if (Com0_Delay_Count_Rxd == 0 )
{
EA=0;
ES0 = 0 ;
// Enable /INT0 interrupts
sum=0;
delay_count = 0;
Check_Com0_Rxd_Buff(); //串口0缓冲区处理函数
comb0_txd_buff[0]= ;
comb0_txd_buff[1]= ;
jj=2;
UART0_TX(jj);
Com0_Delay_Count_Rxd =20 ; //Com0_Delay_Count_Rxd = 100 ; //UART0接收总延时70*5ms/200字节
Com0_Flag_Rxd = 0 ; //UART0延时标志清0
Com0_Count_Rxd = 0 ; //串口接收计数器清0
}
}
}
/*============================================================
* 函 数 名:WDT_Init
* 功能描述:禁止看门狗功能
============================================================*/
void WDT_Init (void)
{
EA = 0 ; //禁止总中断
WDTCN = 0xde ; //禁止软件看门狗定时器
WDTCN = 0xad ;
//EA = 1 ; //开总中断
}
//////////////////////////////////////////////////////
void UART0_TX(uchar leng) //串口发送函数
{
uchar data ii;
for(ii=0 ;ii<leng; ii++)
{
SBUF0=comb0_txd_buff[ii];
while (TI0==0);
TI0=0;
}
}
/*
void SPI_Init()
{
SFRPAGE = SPI0_PAGE;
SPI0CN = 0x05;
SPI0CKR = 0x05;
SPI0CFG =0x20;
}
*/
void Init_Device(void)
{
WDT_Init ();
// SPI_Init();
PORT_Init ();
Oscillator_Init();
// Interrupts_Init();
}
void send_frequency(void )
{
unsigned char sent[4];
/****************************固定本振**************************************/
///
////////////////////////////////////////////////////////////////////////2.43GHz
LEE1=0;
CLK1=0;
DAT1=0;
sent[0]=0x02;
sent[1]=0x00;
sent[2]=0x00;
sent[3]=0x02;
sent11(sent);
sent[0]=0x03;
sent[1]=0x00;
sent[2]=0x00;
sent[3]=0x30;
sent11(sent);
sent[0]=0x04;
sent[1]=0x99;
sent[2]=0x99;
sent[3]=0x99;
sent11(sent);
sent[0]=0x05;
sent[1]=0x00;
sent[2]=0x00;
sent[3]=0x00;
sent11(sent);
sent[0]=0x06;
sent[1]=0x40;
sent[2]=0x1C;
sent[3]=0x1F;//1F-小数模式,3F-整数模式
sent11(sent);
sent[0]=0x07;
sent[1]=0x20;
sent[2]=0x48;
sent[3]=0x64;
sent11(sent);
sent[0]=0x08;
sent[1]=0x01;//倍频
sent[2]=0x6F;
sent[3]=0xFF;
sent11(sent);
sent[0]=0x09;
sent[1]=0x40;
sent[2]=0x3F;
sent[3]=0xFF;
sent11(sent);
sent[0]=0x0A;
sent[1]=0x00;
sent[2]=0x00;
sent[3]=0x00;
sent11(sent);
sent[0]=0x0B;
sent[1]=0x00;
sent[2]=0xE0;
sent[3]=0x61;
sent11(sent);
sent[0]=0x0C;
sent[1]=0x00;
sent[2]=0x00;
sent[3]=0x19;
sent11(sent);
sent[0]=0x0D;
sent[1]=0x00;
sent[2]=0x00;
sent[3]=0x00;
sent11(sent);
sent[0]=0x0E;
sent[1]=0x00;
sent[2]=0x00;
sent[3]=0x00;
sent11(sent);
sent[0]=0x0F;
sent[1]=0x00;
sent[2]=0x00;
sent[3]=0x01;
sent11(sent);
}
//CRC16算法
void ddssongshu(void)
{
unsigned char ControlValue,i;
unsigned long ValueToWrite;
IOUPDATE=0;
ControlValue=0x01;
for(i=0; i<8; i++)
{
SCLK=0;
if(0x80 == (ControlValue & 0x80))
{
SDIO=1; //Send one to SDIO pin
}
else
{
SDIO=0; //Send zero to SDIO pin
}
SCLK=1;
ControlValue <<= 1; //Rotate data
}
delay(2);
//And then the data
ValueToWrite =0x00810900;
for (i=0; i<32; i++)
{
SCLK=0;
if(0x80000000 == (ValueToWrite & 0x80000000))
{
SDIO=1; //Send one to SDIO pin
}
else
{
SDIO=0; //Send zero to SDIO pin
}
// delay(10);
SCLK=1;
ValueToWrite <<= 1; //Rotate data
}
delay(1);
//SET_CS(); //bring CS high again
IOUPDATE=1;
for (i=0; i<10; i++);
IOUPDATE=0;
ControlValue=0x03;
for(i=0; i<8; i++)
{
SCLK=0;
if(0x80 == (ControlValue & 0x80))
{
SDIO=1; //Send one to SDIO pin
}
else
{
SDIO=0; //Send zero to SDIO pin
}
SCLK=1;
ControlValue <<= 1; //Rotate data
}
delay(2);
//And then the data
ValueToWrite =0x01053120;
for (i=0; i<32; i++)
{
SCLK=0;
if(0x80000000 == (ValueToWrite & 0x80000000))
{
SDIO=1; //Send one to SDIO pin
}
else
{
SDIO=0; //Send zero to SDIO pin
}
// delay(10);
SCLK=1;
ValueToWrite <<= 1; //Rotate data
}
delay(1);
//SET_CS(); //bring CS high again
IOUPDATE=1;
delay(10000);
for (i=0; i<10; i++);
IOUPDATE=0;
ControlValue=0x03;
for(i=0; i<8; i++)
{
SCLK=0;
if(0x80 == (ControlValue & 0x80))
{
SDIO=1; //Send one to SDIO pin
}
else
{
SDIO=0; //Send zero to SDIO pin
}
SCLK=1;
ControlValue <<= 1; //Rotate data
}
delay(2);
//And then the data
ValueToWrite =0x00053120;
for (i=0; i<32; i++)
{
SCLK=0;
if(0x80000000 == (ValueToWrite & 0x80000000))
{
SDIO=1; //Send one to SDIO pin
}
else
{
SDIO=0; //Send zero to SDIO pin
}
// delay(10);
SCLK=1;
ValueToWrite <<= 1; //Rotate data
}
delay(1);
//SET_CS(); //bring CS high again
IOUPDATE=1;
for (i=0; i<10; i++);
IOUPDATE=0;
//Write out the control word
ControlValue=0x0B;
for(i=0; i<8; i++)
{
SCLK=0;
if(0x80 == (ControlValue & 0x80))
{
SDIO=1; //Send one to SDIO pin
}
else
{
SDIO=0; //Send zero to SDIO pin
}
SCLK=1;
ControlValue <<= 1; //Rotate data
}
delay(2);
//And then the data
ValueToWrite =0x1B6DB941;//0x24901E57+0x444444*(DATA/16*10+DATA%16);//////////////////////////////0x2044D014
for (i=0; i<32; i++)
{
SCLK=0;
if(0x80000000 == (ValueToWrite & 0x80000000))
{
SDIO=1; //Send one to SDIO pin
}
else
{
SDIO=0; //Send zero to SDIO pin
}
// delay(10);
SCLK=1;
ValueToWrite <<= 1; //Rotate data
}
delay(1);
//SET_CS(); //bring CS high again
IOUPDATE=1;
delay(200);
IOUPDATE=0;
}
//////////////////////////////////////
//////////////
void ATTEN1(unsigned char dat3) //数控衰减器LER
{
unsigned char i=0;
unsigned char a=0x20; //比较 后六位
if((dat3&0x10)!=0)
ATTEN1_B5=1;
else
ATTEN1_B5=0;
if((dat3&0x08)!=0)
ATTEN1_B4=1;
else
ATTEN1_B4=0;
if((dat3&0x04)!=0)
ATTEN1_B3=1;
else
ATTEN1_B3=0;
if((dat3&0x02)!=0)
ATTEN1_B2=1;
else
ATTEN1_B2=0;
if((dat3&0x01)!=0)
ATTEN1_B1=1;
else
ATTEN1_B1=0;
}
void ATTEN2(unsigned char dat3) //数控衰减器LER
{
unsigned char i=0;
unsigned char a=0x20; //比较 后六位
if((dat3&0x10)!=0)
ATTEN2_B5=1;
else
ATTEN2_B5=0;
if((dat3&0x08)!=0)
ATTEN2_B4=1;
else
ATTEN2_B4=0;
if((dat3&0x04)!=0)
ATTEN2_B3=1;
else
ATTEN2_B3=0;
if((dat3&0x02)!=0)
ATTEN2_B2=1;
else
ATTEN2_B2=0;
if((dat3&0x01)!=0)
ATTEN2_B1=1;
else
ATTEN2_B1=0;
}
////////////////////////////////////用作DDS跳频
void send08regsitor(unsigned long ValueToWrite2)
{
unsigned char ControlValue,i;
unsigned long ii,ValueToWrite;
/////////////////////////////////////////////////////////////////////////
for (i=0; i<10; i++);
IOUPDATE=0;
ControlValue=0x08;
for(i=0; i<8; i++)
{
SCLK=0;
if(0x80 == (ControlValue & 0x80))
{
SDIO=1; //Send one to SDIO pin
}
else
{
SDIO=0; //Send zero to SDIO pin
}
SCLK=1;
ControlValue <<= 1; //Rotate data
}
delay(2);
//And then the data
ValueToWrite =ValueToWrite2; //送数,寄存器6 设置频率间隔,现在设置为最小,可修改
for (i=0; i<32; i++)
{
SCLK=0;
if(0x80000000 == (ValueToWrite & 0x80000000))
{
SDIO=1; //Send one to SDIO pin
}
else
{
SDIO=0; //Send zero to SDIO pin
}
// delay(10);
SCLK=1;
ValueToWrite <<= 1; //Rotate data
}
delay(1);
//SET_CS(); //bring CS high again
IOUPDATE=1;
delay(200);
IOUPDATE=0;
/////////////////////////////////////////////////////////////////////////
}
///////////////////
void TEMPERATURE_INIT( ) //“33” 温度上报
{
unsigned char TEMPH;
unsigned int w,temph;
SFRPAGE = ADC0_PAGE;
AMX0SL = 0x08;
delay(1000);
ADC0CF = 0xF8;
delay(2000);
ADC0CN = 0x81;
delay(1000);
SFRPAGE = ADC0_PAGE;
REF0CN = 0x07;
delay(2000);
for(w=0;w<10;w++)
{
AD0BUSY=1;
while(AD0BUSY==1) ;
delay(100);
temph+=ADC0H;
}
TEMPH=temph/10;
//delay(8000);
temph=0;
if(TEMPH>=84)//理论计算得应该是88为0度 ,变换一个二进数数,温度变换3。3度
{
TEMPH=(TEMPH-84)*10/3;
comb0_txd_buff[9]=TEMPH;
}
else
{
TEMPH=(84-TEMPH)*10/3;
comb0_txd_buff[9]=TEMPH+0x80;
}
//comb0_txd_buff[3]=TEMPH;
delay(100);
}[/mw_shl_code] |
|