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

📄 ad9850.c

📁 MCU+AD9850+KEYPAD+LCD的信号发生器
💻 C
📖 第 1 页 / 共 2 页
字号:


//ad9850源程序
#include <reg51.h>
#include <stdio.h>
#include <absacc.h>
#include <intrins.h>
#include <math.h>

#define uchar unsigned char
#define uint  unsigned int


#define Zero      0x30
#define One       0x31
#define Two       0x32
#define Three     0x33
#define Four      0x34
#define Five      0x35
#define Six       0x36
#define Seven     0x37
#define Eight     0x38
#define Nine      0x39

//键盘毽值
#define Key_0     24
#define Key_1     25
#define Key_2     17
#define Key_3     9
#define Key_4     26
#define Key_5     18
#define Key_6     10
#define Key_7     27
#define Key_8     19
#define Key_9     11
#define Freq_Step_Up     0
#define Freq_Step_Down   1
#define Set_Freq         2
#define Key_Sure         3




#define Para      35.7913941333         //

// 光标参数
#define NoDisp        0
#define NoCur         1
#define CurNoFlash    2
#define CurFlash      3

//键盘控制字
#define  HD7279_READ        0x15 //读 
#define  HD7279_RESET       0xa4 //复位


/*--------------------------------------------
调用方式:自行I/O 口定义 
函数说明:各接口定义 
--------------------------------------------*/

sbit  HD7279_CS   = P1^5; // HD7279_CS  -- P3.5
sbit  HD7279_CLK  = P1^4; // HD7279_CLK -- P3.4
sbit  HD7279_DATA = P1^3; // HD7279_DATA-- P3.7
sbit  HD7279_KEY  = P3^2; // HD7279_KEY -- P3.2




//ad9851的控制线
sbit Data_Bus = P1^0;
sbit W_CLK    = P1^1;
sbit FQ_UD    = P1^2;
//lcd的控制线
sbit RS = P2^7;
sbit RW = P2^6;
sbit E  = P2^5;
uchar Xpos; //列方向地址指针
uchar Ypos; //行方向地址指针


//ad9851的变量
long Fout = 100;
float Frequancy_Com  = 0;
uchar  Frequancy_Command[5] = {0x44,0x44,0x44,0x04,0x01};  //2Mhz的配置字


//键盘的变量
uchar Key;
bit Key_Interupt_Flag = 0;
bit Error_Flag = 0;
bit Set_Key = 0;
uchar Input_Data_Count = 0;

//液晶的变量
uchar  Freq_Disp_Buf[8] = {0x4f,0xa5,0x4f,0x4f,0x4f,0x4f,0x4f,0x4f};  //初始化显示的字符
uchar code blank_disp[]={"                          "};//将错误信息清除

//函数声明
void WaitreadBF();
void LcdWcn(uchar);
void LcdWc(uchar);
void WriteChar(uchar);
void LcdPos();
void LcdWd(uchar);
void LcdWdn(uchar);
void   HD7279_LongDelay();
void   HD7279_ShortDelay();
void   Delay1ms(uchar);
void   HD7279_SendByte(uchar);
void   Write7279(uchar,uchar);
uchar  Read7279(uchar);
uchar  HD7279_ReceiveByte();
uchar  HD7279_GetKey();

void LCD_Display(uchar Xpos1,uchar Ypos1,uchar str[]);
/*--------------------------------------------------
函数名称:
函数功能:
传递参数:
----------------------------------------------------*/
void Delay(int count)
{
  uchar i;
  for(;count > 0;count--)
     for(i=50;i>0;i--)
        {;}
}

/*--------------------------------------------------
函数名称:
函数功能:
传递参数:
----------------------------------------------------*/


void Ad9850_SendData(uchar Data)
{
  uchar j,Dat;
  Dat = Data;
  Delay(10);
  for(j=0;j<8;j++)
     {
       if (Dat&1)
          {
            Data_Bus = 1;
 
          }
       else 
          {
            Data_Bus = 0;

          }

        W_CLK = 0;
        W_CLK = 1;  
        Dat = Dat >> 1;  
        Data_Bus = 0;
        W_CLK = 0;
        FQ_UD    = 0;
     }
    
     
     
}

/*--------------------------------------------------
函数名称:
函数功能:
传递参数:
----------------------------------------------------*/


void Reset_Ad9851()
{

  Data_Bus = 0;
  W_CLK    = 0; 
  FQ_UD    = 0;

  W_CLK = 1;
  W_CLK = 0; 
  
  Delay(50);
  
  FQ_UD = 1;
  Delay(10);
  FQ_UD = 0;
 
  Ad9850_SendData(0x00);
  

  FQ_UD = 1;
  Delay(10);
  FQ_UD = 0;
  Delay(10);
}


/*--------------------------------------------------
函数名称:
函数功能:
传递参数:
----------------------------------------------------*/



void Write_Data_Ad9850(uchar Number,uchar *Write_Data)
{
     Reset_Ad9851();
     
     for(;Number > 0;Number--)
	    {
          Ad9850_SendData(*Write_Data);
		  Write_Data++;
        }

  
     
     FQ_UD = 1;
     Delay(50);
     FQ_UD = 0;
}

/*--------------------------------------------------
函数名称:
函数功能#海海? 实现100hz的步进
传递参数:
----------------------------------------------------*/
void Freq_Step_Increase()
{
  Fout = Fout + 100;
}

/*--------------------------------------------------
函数名称:
函数功能:
传递参数:
----------------------------------------------------*/
void Freq_Step_Decrease()
{
  uchar Message_Box[]={"Error!Frequancy below 0hz!"};
  Fout = Fout - 100;
  if(Fout < 10)
    {
	  LCD_Display(0,0,Message_Box);
      Error_Flag = 1;
	  Fout = 1000000;
	}
}


/*--------------------------------------------------
函数名称:
函数功能:
传递参数:
----------------------------------------------------*/

void Frequancy_Control_Convert(long Frequancy)
{
  
   long  Freq_Conv;

   Frequancy_Com   = Frequancy * Para;
   Freq_Conv = (long)Frequancy_Com;
   Frequancy_Command[0] = (uchar)(Freq_Conv&0x000000ff);
   Freq_Conv = Freq_Conv>>8;
   Frequancy_Command[1] = (uchar)(Freq_Conv&0x000000ff);
   Freq_Conv = Freq_Conv>>8;
   Frequancy_Command[2] = (uchar)(Freq_Conv&0x000000ff);
   Freq_Conv = Freq_Conv>>8;
   Frequancy_Command[3] = (uchar)(Freq_Conv&0x000000ff);
   
   Frequancy_Command[4] = 0x81;
}


/*--------------------------------------------
函数名称?函数说明:例函数
--------------------------------------------*/
/*
void Key_Scan()
{
  if(Function_Up == 0)
    {
	  Fout = Fout << 4;
	  Fout = Fout | 
	}
}

*/

/******************************************************************** 
延时程序

由Delay参数确定延迟时间

********************************************************************/ 

void mDelay(unsigned int Delay) 
{ 
   unsigned int i;
    for(;Delay>0;Delay--)
       {
         for(i=0;i<124;i++)
            {;}
       }
}

/******************************************************************** 
送控制字子程序

********************************************************************/ 

void LcdWcn(uchar c)
{ 
    RS = 0;
    RW = 0;
    P0=c;
    E = 1;
    _nop_();
    E = 0;
} 




/******************************************************************** 
检测忙信号的送控制字子程序

********************************************************************/ 
void LcdWc(uchar c) 
{
    WaitreadBF();
    LcdWcn(c);
}




/******************************************************************** 
光标设置命令

Cur 为设定光标参数

********************************************************************/ 
void SetCur(uchar Cur)
{ 
    switch(Cur)
      { case 0x0:
             { 
                LcdWc(0x08); //关显示
                      break;
             } 
        case 0x1:
             { 
                LcdWc(0x0c); //开显示但无光标
                      break;
             }
        case 0x2:
             { 
                LcdWc(0x0e); //开显示有光标但不闪烁
                      break;
             }
        case 0x3:
             { 
                LcdWc(0x0f); //开显示有光标且闪烁
                      break;
             }
        default: break;
       }
}

/******************************************************************** 
清屏命令

********************************************************************/ 
void ClrLcd()
{ 
    LcdWc(0x01); 
}

/******************************************************************** 
在指定的行与列显示

********************************************************************/ 
void WriteChar(uchar c)
{ 
    LcdPos();
    LcdWd(c);
}

/******************************************************************** 
正常读写操作之前检测LCD控制器

********************************************************************/ 
void WaitreadBF()
{ 
    uchar tmp;
    P0=0xff;
    RS = 0;
    RW = 1;
    E  = 1;
    _nop_();
    for(;;)
       { 
           tmp = P0;
           tmp&=0x80;
            if(tmp==0)
                 break;
       }
    E = 0;
}


/******************************************************************** 
写字符子程序


********************************************************************/ 
void LcdWdn(uchar c)
{ 
    RS = 1;
    RW = 0;
    P0=c; //写入待写字符
    E  = 1;
    _nop_();
    E  = 0;
}

/******************************************************************** 
带忙检测的写字符子程序

********************************************************************/ 
void LcdWd(uchar c)
{
    WaitreadBF();
    LcdWdn(c);
}



/******************************************************************** 


********************************************************************/ 
void LcdPos()
{ 
    uchar tmp;
    Xpos&=0x3f; //402型液晶的范围是0~39
    Ypos&=0x01; //Y的范围是0~1
    tmp=Xpos;
    if(Ypos==1)
      {   
           tmp+=0x40;
      }
    tmp|=0x80;
    LcdWc(tmp);
}

/******************************************************************** 
LCD的复位程序

********************************************************************/ 

void RstLcd() 
{ 
    mDelay(15); //延时15ms
    LcdWcn(0x38);
    mDelay(5);
    LcdWcn(0x38);
    mDelay(5);
    LcdWcn(0x38);
    LcdWc(0x38);
    LcdWc(0x08);
    LcdWc(0x01);
    LcdWc(0x06);
    LcdWc(0x0c);
}

/******************************************************************** 


********************************************************************/ 
void WriteString(char s[])
{ 
    uchar pS = 0;
     for(;;)
        {
            WriteChar(s[pS]);
            pS++;
            if(s[pS]==0)
                 break;
            if(++Xpos>=39) //每行最多显示40个字符
                 break;
        }
}

/******************************************************************** 
显示字符
参数  Xpos1  Ypos1  str[]
********************************************************************/
void LCD_Display(uchar Xpos1,uchar Ypos1,uchar str[])
{
    Xpos = Xpos1;
    Ypos = Ypos1;
    WriteString(str);
   
}


/*--------------------------------------------------
函数名称:
函数功能:
传递参数:
----------------------------------------------------*/

void Lcd_Any_Disp(uchar xsite,uchar ysite,uchar Data)
{
  	Xpos = xsite;

⌨️ 快捷键说明

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