⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 (AD9958用ADuC845控制的keil源代码包,键盘和显示部分在另一个板上
💻 C
字号:
/**********************************************
   Main.C file
   The AD9958's Main file
   Creat by Computer-lov Date: 2005.8.30
   Last edit date:2005.8.30
   Copyright(C) 2005-2015 Computer-lov
   All rights reserved
************************************************/

#include <aduc841.h>
#include "ad9958.h"
#include "isr.h"
#include "serial_port.h"
#include "adc.h"

void delayXms(unsigned int x)
{
 unsigned int j;
 while(x--)
  for(j=0;j<100;j++);
}

unsigned int current_voltage;
unsigned long int current_f;


void frequency(void)
{
 unsigned long int f;
 TR2=0;
 f=100000*(receive_buffer[1]-'0')+10000*(receive_buffer[2]-'0')+1000*(receive_buffer[3]-'0')+100*(receive_buffer[5]-'0')+10*(receive_buffer[6]-'0')+receive_buffer[7]-'0';
 if(receive_buffer[8]=='M')
  {
   f=f*1000;
  }
 current_f=f;

 write_CSR(0x80);  //CH1
 
 //write_FR1(0x14,0x00,0x00);
 // write_FR2(0x00,0x00);

 write_CFR(0x00,0x03,0x00);  //no modulation
 write_CPW0(0x00);
 write_ACR(0x1000 | current_voltage);
 set_frequency(f);
 write_CSR(0x70);
 set_frequency(0);
 run();
}
 


void voltage(void)
{
 TR2=0;
 switch(receive_buffer[1])
  {
   case 0: current_voltage+=1;
           if(current_voltage>1000)current_voltage=1000;
           break;
   case 1: current_voltage+=10;
           if(current_voltage>1000)current_voltage=1000;
           break;
   case 2: current_voltage+=100;
           if(current_voltage>1000)current_voltage=1000;
           break; 
   case 3: current_voltage-=1;
           if(current_voltage>1000)current_voltage=0;
           break;
   case 4: current_voltage-=10;
           if(current_voltage>1000)current_voltage=0;
           break;
   case 5: current_voltage-=100;
           if(current_voltage>1000)current_voltage=0;
           break;
   default: break;
  }
 write_CSR(0x80);
 write_CFR(0x00,0x03,0x00);  //no modulation
 write_ACR(0x1000 | current_voltage);
 run();
}

unsigned char code_sequence;

void PSK(void)
{
 code_sequence=100*(receive_buffer[1]-'0')+10*(receive_buffer[2]-'0')+receive_buffer[3]-'0';
 
 T2CON=0x00;              //定时器自动重装模式
 RCAP2H=0xF8;
 RCAP2L=0x30;             //定时时间为100uS

 write_CSR(0x80);
 write_CFR(0xC0,03,0x00);
 write_CPW0(0x00);
 write_CTWn(1,0x80000000);
 set_frequency(100000);
 run();
 printc(0xEF);
 ET2=1;
 TR2=1;
}


void ASK(void)
{
 code_sequence=100*(receive_buffer[1]-'0')+10*(receive_buffer[2]-'0')+receive_buffer[3]-'0';
 T2CON=0x00;              //定时器自动重装模式
 RCAP2H=0xF8;
 RCAP2L=0x30;             //定时时间为100uS

 write_CSR(0x80);
 write_CFR(0x40,03,0x00);
 write_CPW0(0x00);
 write_CTWn(1,0x00);
 set_frequency(100000);
 run();
 printc(0xEF);
 ET2=1;
 TR2=1;
}

void AM(void)
{
 unsigned char temp;
 unsigned int s_voltage;
 temp=receive_buffer[1];
 switch(temp)
  {
   case 1: s_voltage=94; break;
   case 2: s_voltage=189;break;
   case 3: s_voltage=283;break;
   case 4: s_voltage=377;break;
   case 5: s_voltage=472;break;
   case 6: s_voltage=566;break;
   case 7: s_voltage=660;break;
   case 8: s_voltage=754;break;
   case 9: s_voltage=849;break;
   case 10: s_voltage=943;break;
   default :break;
  }
 write_CSR(0x80);
 write_ACR(0x0010ff);
 
 write_CSR(0x70);
 write_ACR(0x1000 | s_voltage);
 set_frequency(1000);
 run();
}

#define SDIO_3                P2_7
#define SDIO_2                P2_6
#define SDIO_1                P2_5
#define SDIO_0                P2_4
#define SCLK                  P0_1
#define CS                    P0_2



void out_f(signed long int delta_f)
{
 unsigned long int temp;
 unsigned char x1,x2,x3,x4;
 ////////////////////////////////////////////
     P2=0x00;
     SCLK=1;
     SCLK=0;
     P2=0x40;
     SCLK=1;
     SCLK=0;


     temp=delta_f+current_f;

     x1 =((unsigned char *)&temp)[0];
     x2 =((unsigned char *)&temp)[1];
     x3 =((unsigned char *)&temp)[2];
     x4 =((unsigned char *)&temp)[3];
  

     P2=x1;
     x1<<=4;
     SCLK=1;
     SCLK=0;
     P2=x1;
     SCLK=1;
     SCLK=0;

     P2=x2;
     x2<<=4;
     SCLK=1;
     SCLK=0;
     P2=x2;
     SCLK=1;
     SCLK=0;

     P2=x3;
     x3<<=4;
     SCLK=1;
     SCLK=0;
     P2=x3;
     SCLK=1;
     SCLK=0;

     P2=x4;
     x4<<=4;
     SCLK=1;
     SCLK=0;
     P2=x4;
     SCLK=1;
     SCLK=0;

     run();
////////////////////////////////
}

void FM5K(void)
{
 write_CSR(0x86);
 run();
 CS=0;
 while(1)
  {
   out_f(	0 	);
out_f(	9528 	);
out_f(	19033 	);
out_f(	28492 	);
out_f(	37883 	);
out_f(	47182 	);
out_f(	56368 	);
out_f(	65418 	);
out_f(	74310 	);
out_f(	83023 	);
out_f(	91536 	);
out_f(	99829 	);
out_f(	107881 	);
out_f(	115673 	);
out_f(	123187 	);
out_f(	130404 	);
out_f(	137307 	);
out_f(	143878 	);
out_f(	150104 	);
out_f(	155967 	);
out_f(	161455 	);
out_f(	166554 	);
out_f(	171252 	);
out_f(	175537 	);
out_f(	179400 	);
out_f(	182830 	);
out_f(	185819 	);
out_f(	188361 	);
out_f(	190450 	);
out_f(	192079 	);
out_f(	193246 	);
out_f(	193947 	);
out_f(	194181 	);
out_f(	193947 	);
out_f(	193246 	);
out_f(	192079 	);
out_f(	190450 	);
out_f(	188361 	);
out_f(	185819 	);
out_f(	182830 	);
out_f(	179400 	);
out_f(	175537 	);
out_f(	171252 	);
out_f(	166554 	);
out_f(	161455 	);
out_f(	155967 	);
out_f(	150104 	);
out_f(	143878 	);
out_f(	137307 	);
out_f(	130404 	);
out_f(	123187 	);
out_f(	115673 	);
out_f(	107881 	);
out_f(	99829 	);
out_f(	91536 	);
out_f(	83023 	);
out_f(	74310 	);
out_f(	65418 	);
out_f(	56368 	);
out_f(	47182 	);
out_f(	37883 	);
out_f(	28492 	);
out_f(	19033 	);
out_f(	9528 	);
out_f(	0 	);
out_f(	-9528 	);
out_f(	-19033 	);
out_f(	-28492 	);
out_f(	-37883 	);
out_f(	-47182 	);
out_f(	-56368 	);
out_f(	-65418 	);
out_f(	-74310 	);
out_f(	-83023 	);
out_f(	-91536 	);
out_f(	-99829 	);
out_f(	-107881 	);
out_f(	-115673 	);
out_f(	-123187 	);
out_f(	-130404 	);
out_f(	-137307 	);
out_f(	-143878 	);
out_f(	-150104 	);
out_f(	-155967 	);
out_f(	-161455 	);
out_f(	-166554 	);
out_f(	-171252 	);
out_f(	-175537 	);
out_f(	-179400 	);
out_f(	-182830 	);
out_f(	-185819 	);
out_f(	-188361 	);
out_f(	-190450 	);
out_f(	-192079 	);
out_f(	-193246 	);
out_f(	-193947 	);
out_f(	-194181 	);
out_f(	-193947 	);
out_f(	-193246 	);
out_f(	-192079 	);
out_f(	-190450 	);
out_f(	-188361 	);
out_f(	-185819 	);
out_f(	-182830 	);
out_f(	-179400 	);
out_f(	-175537 	);
out_f(	-171252 	);
out_f(	-166554 	);
out_f(	-161455 	);
out_f(	-155967 	);
out_f(	-150104 	);
out_f(	-143878 	);
out_f(	-137307 	);
out_f(	-130404 	);
out_f(	-123187 	);
out_f(	-115673 	);
out_f(	-107881 	);
out_f(	-99829 	);
out_f(	-91536 	);
out_f(	-83023 	);
out_f(	-74310 	);
out_f(	-65418 	);
out_f(	-56368 	);
out_f(	-47182 	);
out_f(	-37883 	);
out_f(	-28492 	);
out_f(	-19033 	);
out_f(	-9528 	);
  }
}



void FM10K(void)
{
 write_CSR(0x86);
 run();
 CS=0;
 while(1)
  { 
out_f(	0 	);
out_f(	19056 	);
out_f(	38066 	);
out_f(	56984 	);
out_f(	75766 	);
out_f(	94364 	);
out_f(	112735 	);
out_f(	130835 	);
out_f(	148620 	);
out_f(	166046 	);
out_f(	183072 	);
out_f(	199658 	);
out_f(	215762 	);
out_f(	231347 	);
out_f(	246374 	);
out_f(	260808 	);
out_f(	274613 	);
out_f(	287757 	);
out_f(	300207 	);
out_f(	311935 	);
out_f(	322911 	);
out_f(	333109 	);
out_f(	342504 	);
out_f(	351075 	);
out_f(	358799 	);
out_f(	365659 	);
out_f(	371639 	);
out_f(	376723 	);
out_f(	380899 	);
out_f(	384158 	);
out_f(	386491 	);
out_f(	387894 	);
out_f(	388361 	);
out_f(	387894 	);
out_f(	386491 	);
out_f(	384158 	);
out_f(	380899 	);
out_f(	376723 	);
out_f(	371639 	);
out_f(	365659 	);
out_f(	358799 	);
out_f(	351075 	);
out_f(	342504 	);
out_f(	333109 	);
out_f(	322911 	);
out_f(	311935 	);
out_f(	300207 	);
out_f(	287757 	);
out_f(	274613 	);
out_f(	260808 	);
out_f(	246374 	);
out_f(	231347 	);
out_f(	215762 	);
out_f(	199658 	);
out_f(	183072 	);
out_f(	166046 	);
out_f(	148620 	);
out_f(	130835 	);
out_f(	112735 	);
out_f(	94364 	);
out_f(	75766 	);
out_f(	56984 	);
out_f(	38066 	);
out_f(	19056 	);
out_f(	0 	);
out_f(	-19056 	);
out_f(	-38066 	);
out_f(	-56984 	);
out_f(	-75766 	);
out_f(	-94364 	);
out_f(	-112735 	);
out_f(	-130835 	);
out_f(	-148619 	);
out_f(	-166046 	);
out_f(	-183072 	);
out_f(	-199658 	);
out_f(	-215762 	);
out_f(	-231347 	);
out_f(	-246374 	);
out_f(	-260808 	);
out_f(	-274613 	);
out_f(	-287757 	);
out_f(	-300207 	);
out_f(	-311935 	);
out_f(	-322911 	);
out_f(	-333109 	);
out_f(	-342504 	);
out_f(	-351075 	);
out_f(	-358799 	);
out_f(	-365659 	);
out_f(	-371639 	);
out_f(	-376723 	);
out_f(	-380899 	);
out_f(	-384158 	);
out_f(	-386491 	);
out_f(	-387894 	);
out_f(	-388361 	);
out_f(	-387894 	);
out_f(	-386491 	);
out_f(	-384158 	);
out_f(	-380899 	);
out_f(	-376723 	);
out_f(	-371639 	);
out_f(	-365659 	);
out_f(	-358799 	);
out_f(	-351075 	);
out_f(	-342504 	);
out_f(	-333109 	);
out_f(	-322911 	);
out_f(	-311935 	);
out_f(	-300208 	);
out_f(	-287757 	);
out_f(	-274613 	);
out_f(	-260808 	);
out_f(	-246374 	);
out_f(	-231347 	);
out_f(	-215762 	);
out_f(	-199658 	);
out_f(	-183072 	);
out_f(	-166046 	);
out_f(	-148620 	);
out_f(	-130835 	);
out_f(	-112735 	);
out_f(	-94364 	);
out_f(	-75766 	);
out_f(	-56985 	);
out_f(	-38066 	);
out_f(	-19056 	);
  }
 CS=1;
}

void FM(void)
{
 if(receive_buffer[1]==0)
  {
   FM5K();
  }
 if(receive_buffer[1]==1)
  {
   FM10K();
  }
}

void delay(unsigned int x)
{
 unsigned int i;
 while(x--)
  {
   for(i=0;i<2000;i++);
  }
}
 
void main(void)
{
 unsigned int v_out;
 unsigned char buffer[3];
 EA=0;
 AD9958_init();
 init_comport();
 ADC_init();

 delayXms(10);
 current_voltage=500;
 receive_buffer[0]=0xcc;
 receive_buffer[1]='0';
 receive_buffer[2]='0';
 receive_buffer[3]='1';
 receive_buffer[4]='.';
 receive_buffer[5]='0';
 receive_buffer[6]='0';
 receive_buffer[7]='0';
 receive_buffer[8]='K';
 frequency();
 //receive_buffer[1]=2;
 //voltage();
 //voltage();
 //current_voltage=1000;
 //voltage();
 //receive_buffer[1]=5;
// PSK();
 //AM();
 while(1)
  {
   receive_end=0;
   while(!receive_end)
    {
     delay(300);
     SET_SCONV();
     wait_end_of_ADC();
     
     v_out=ADCDATAH;
     v_out<<=8;
     v_out+=ADCDATAL;
   // printf(v_out);
     v_out&=0x0FFF;
     v_out=(2500.0/4096)*v_out;
    // printc(' ');
     v_out-=420;
     v_out*=1.732;
     v_out=v_out*2.828;
     v_out/=10;
     printc(0xee);
     //printc(' ');
     //printc(' ');
     buffer[0]=v_out%10+'0';
     v_out/=10;
     buffer[1]=v_out%10+'0';
     v_out/=10;
     buffer[2]=v_out%10+'0';
     printc(buffer[2]);
     printc('.');
     printc(buffer[1]);
     printc(buffer[0]);
    }
   switch(receive_buffer[0])
    {
      case 0xCC: frequency(); break;   //set frequency
      case 0xCD: voltage();   break;   //voltage adjust
      case 0xBB: PSK();       break;   //PSK
      case 0xBC: ASK();       break;   //ASK
      case 0xAA: AM();        break;   //AM
      case 0xAB: FM();        break;   //FM
      default:  break;
    }
   //printc(0xEF);
   printc('c');
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -