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

📄 main.c

📁 这是单片机仿真模数转换的一个源码实验
💻 C
字号:
#include <reg51.h>
#include <intrins.h>
#define uint unsigned int 
#define uchar unsigned char

sbit DIN  = P2^0;
sbit LOAD = P2^1;
sbit CLK  = P2^2;

sbit dout=P1^4;	
sbit sclk=P1^5;	
sbit cs = P1^6;
sbit shdn=P1^7;	
/*sbit DOUT=P1^4;	
sbit SCLK=P1^5;	
sbit CS = P1^6;
sbit SHDN=P1^7;*/

sbit LED  = P1^0;

void Write7129( uchar Add, uchar Data );
void Init7219( void );		// initialize 7219	
void DelayMs( uchar n );
uint Rec_data;
void max1240(void);
uchar DispBuf[8]={1,2,3,4,5,6,7,8};		// display buffer

/*
void max1240(void)
{
  uchar i; 
  float temp;
  uint rice;
  //CS = 1; 
   
  
   //DOUT = 1;
  // _nop_(); _nop_();  _nop_(); _nop_();
   CS = 0;
   SCLK = 0;
        _nop_();
   //DOUT = 0; _nop_();
 if(DOUT==0)
  {
     _nop_();_nop_(); _nop_(); _nop_(); _nop_(); _nop_();_nop_();
  if(DOUT==1)
   {
     for(i=0;i<12;i++)
     {
	    SCLK = 1; _nop_();
        SCLK = 0;
	   // Rec_data <<=1;	 
		 if(DOUT==1)
		   Rec_data = (Rec_data<<=1) | 0x0001;   
   	     else Rec_data <<=1; 
	 }
        CS = 1;
   }
  }

temp = (float)Rec_data*0.6105006;
  //Pdata = (int)temp;
rice=(uint)temp;

DispBuf[0]=(rice%10000)/1000;

DispBuf[1]=(rice%1000)/100;

DispBuf[2]=(rice%100)/10;

DispBuf[3]=rice%10;
}*/
void adcover(void)
{ uint rice=0;
  uchar i;
float rice1=0;
 sclk=0;
 cs=0;
_nop_();
if(dout==0)
{_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();

if(dout==1)
{ 
  for(i=0;i<12;i++)
  { sclk=1;
  _nop_();
  sclk=0;
  
 if(dout==1)
   {rice=(rice<<1)|0x0001;}
 else   
 rice=rice<<1;
   }
   cs=1;} 
}

rice1=(float)rice*0.6105006;//(2.5*rice*1000)/0xfff;


rice=(uint)rice1;

DispBuf[0]=(rice%10000)/1000;

DispBuf[1]=(rice%1000)/100;

DispBuf[2]=(rice%100)/10;

DispBuf[3]=rice%10;

}

/*void Process()
{
  float temp;
  uint rice;
  max1240();
 // temp =  (Rec_data*2.5)/0xfff;
  //temp = temp*1000;
  temp = (float)Rec_data*0.6105006;
  //Pdata = (int)temp;
rice=(uint)temp;

DispBuf[0]=(rice%10000)/1000;

DispBuf[1]=(rice%1000)/100;

DispBuf[2]=(rice%100)/10;

DispBuf[3]=rice%10;
}*/

void main()
{
	DelayMs( 1000 );
	Init7219( );
	Write7129( 0x01 , DispBuf[0] );
	Write7129( 0x02 , DispBuf[1] );
	Write7129( 0x03 , DispBuf[2] );
	Write7129( 0x04 , DispBuf[3] );
	Write7129( 0x05 , DispBuf[4] );
	Write7129( 0x06 , DispBuf[5] );
	Write7129( 0x07 , DispBuf[6] );
	Write7129( 0x08 , DispBuf[7] );
		
	LED  = 0 ;
	while(1)
	{
   // max1240();
    adcover();
    Write7129( 0x01 , DispBuf[0] );
	Write7129( 0x02 , DispBuf[1] );
	Write7129( 0x03 , DispBuf[2] );
	Write7129( 0x04 , DispBuf[3] );
	Write7129( 0x05 , DispBuf[4] );
	Write7129( 0x06 , DispBuf[5] );
	Write7129( 0x07 , DispBuf[6] );
	Write7129( 0x08 , DispBuf[7] );

	}

}
void Init7219( void )
{
	Write7129( 0x0c , 0x01);		//开启
	Write7129( 0x09 , 0xff );		//译码方式 B译码
	Write7129( 0x0a , 0x04 );		//亮度
	Write7129( 0x0b , 0x03 );		//扫描界限	
}

void Write7129( uchar Add, uchar Data )
{
	uint Send;
	uchar i;
	Send = Add*0x100 + Data;
      LOAD = 1;
      _nop_();
      _nop_();
	  LOAD = 0;
	  _nop_();
	  _nop_();
    for(i=0;i<16;i++)
     {
       CLK = 0;
        _nop_();
       DIN = 0;
          if((Send & 0x8000) == 0x8000)
             DIN = 1;
         _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
       CLK = 1;
       _nop_();
       _nop_();
       Send <<= 1;
     }
	 LOAD = 1;
	  _nop_();
     CLK = 0;
	 _nop_();
	 _nop_();
	 _nop_();
	 _nop_();
//	 LOAD = 0;
	 // wirte your code here
}

void DelayMs( uchar n )
{
	uchar i;
	while( n-- )
	{
		for( i = 0 ; i < 125 ; i++ )
			;
	}
}

⌨️ 快捷键说明

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