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

📄 source.c

📁 一个直流信号源的全部开发资料,包括原理图,PCB,单片机程序
💻 C
字号:
#include < stdio.h>
#include < stdlib.h>
#include <reg52.h>
#include <math.h>
sbit	DSP_LOAD=P1^2;
sbit	DSP_DIN=P1^3;
sbit	DSP_CLK=P1^1;

sbit	X_CS=P0^0;
sbit	X_SCK=P0^3;
sbit	X_SI=P0^4;
sbit	X_SO=P0^1;
sbit	X_WP=P0^2;

sbit	AD_BUSY=P3^3;

sbit	DA_CS=P0^6;
sbit	DA_CLK=P0^5;
sbit	DA_D=P0^7;

sbit	S_UP=P1^5;
sbit	S_VI=P1^4;
sbit	S_DOWN=P1^6;
sbit	S_FAST=P1^7;

sbit	VI_S=P2^0;

#define ADI 0x80;
#define ADU 0x40;
#define ADH 0x20;
#define ADL 0x10;
#define SET_AD 0x08;




unsigned char ad_cnt;
unsigned int ad_vol;
unsigned int set_vol;
unsigned int adj_vol;
unsigned int check_adj;

bit ad_u;
bit enable_ad;

void x_write ( unsigned int x_address,unsigned char *psavedata,unsigned char amount) ;
unsigned char  * x_read( unsigned int address ,unsigned char  amount) ;
void x_wren_cmd (void);
void init_watchdog(unsigned char ch);
void x_outbyte( unsigned char ch);
unsigned char x_inbyte( void );
void wait(unsigned int time);
void init_cpu(void);
void disp_init(void);
void disp_outbyte(int outbytes);
void display(unsigned int pos,unsigned int disp_code);
void disp_ad(unsigned int ad,unsigned char type);
void da_outbyte(unsigned int outbytes);
//void set_da(unsigned int vol);
unsigned char send_buf[7];
unsigned char rcv_buf[6];
unsigned char rcv_cnt;
unsigned char send_cnt;
void main(void)
{
	unsigned char type,loop;
	unsigned long is_power;
    init_cpu();
    disp_init();
    da_outbyte(0);
	init_watchdog(0);
	if( is_power != 0x12345678)
	{
		enable_ad=1;
		check_adj=10125;
		ad_u=1;
	    set_vol=0;
		adj_vol=0;
		is_power = 0x12345678 ;
	}
	send_cnt=0;
	rcv_cnt=0;
    while(1)
    {
		if(S_VI == 0)
		{
			wait(100);
			if(S_VI == 0)
			{
				ad_u=~ad_u;
				if(ad_u) VI_S =1;
				else VI_S=0;
                set_vol=0;
                adj_vol=0;
                da_outbyte(0);
				wait(400);
			}
		}
		if((S_UP == 0) | (S_DOWN == 0))
		{
			wait(50);
			if((S_UP == 0) | (S_DOWN == 0))
			{
				enable_ad=0;
				type=SET_AD;
				if(ad_u) { type=type | ADU; }
				else { type=type | ADI; }
				disp_ad(set_vol,type);
				wait(400);
				wait(400);
				wait(200);

				loop=0;
				while(1)
				{
					type=SET_AD;
					if(ad_u) { type=type | ADU; }
					else { type=type | ADI; }
					disp_ad(set_vol,type);
					if(S_UP == 0)
					{
						wait(50);
						if(S_UP== 0)
						{
							loop=0;
							if(S_FAST== 0) { set_vol=set_vol+1000; }
							else { set_vol=set_vol+100; }
							if (( set_vol > 60000) & (ad_u)) set_vol = 60000;
							if (( set_vol > 24000) & (!ad_u)) set_vol = 24000;
						}
					}
					if(S_DOWN == 0)
					{
						wait(50);
						if(S_DOWN == 0)
						{
							loop=0;
							if(S_FAST== 0)
							{
								if(set_vol>1000) { set_vol=set_vol-1000;}
								else { set_vol=0; }
							}
							else
							{
								if(set_vol>100) { set_vol=set_vol-100;}
								else { set_vol=0; }
							}
						}
					}
					wait(50);
					loop++;
					if(loop > 50) break;
					X_CS=0;
					X_CS=1;
				}
				enable_ad=1;
			}
		}
		X_CS=0;
		X_CS=1;
  	}
}
//////////////////////////////
//
//NAME: init_com
//DESCRIPTION: UART INTERRUPT SERVICE PROGRAM
//CALL:
//INPUT:
//OUTPUT:NONE
//
//
//send:H[L = ]U[I]XXXXX  7bytes  ad volue
//receive:U[I]XXXXX 6bytes  set ad volue
///////////////////////////////
void init_com() interrupt 4
{
	unsigned int i;
	ES=0;
	if(TI)
	{
		if(send_cnt<7)
		{
			SBUF=send_buf[send_cnt];
			send_cnt++;
		}
		TI=0;
	}
	if(RI)
	{
		rcv_buf[rcv_cnt]=SBUF;
                if((rcv_buf[rcv_cnt] == 'U') | ( rcv_buf[rcv_cnt] == 'I'))
                {
                 rcv_buf[0]=rcv_buf[rcv_cnt];
                 rcv_cnt=0;
                 }
                
		rcv_cnt++;
		if(rcv_cnt >= 6)
		{
			if((rcv_buf[0] == 'U' ) | (rcv_buf[0] == 'I' ))
			{
				if(rcv_buf[0] == 'U') ad_u=1;
				else ad_u=0;
				i=atoi(&rcv_buf[1]);
				if((i > 60000) & (rcv_buf[0] == 'U')) i=60000;
				if((i > 24000) & (rcv_buf[0] == 'I')) i=24000;
				set_vol=i;
				if(ad_u) VI_S =1;
				else VI_S=0;
			}
			rcv_cnt=0;
		}
		RI=0;
	}
	ES=1;
}

//////////////////////////////
//
//NAME: da_outbyte(unsigned int outbytes)
//DESCRIPTION: output a word to da max5541
//CALL:
//INPUT:  NONE
//OUTPUT:NONE
//
///////////////////////////////
void da_outbyte(unsigned int outbytes)
{
	unsigned char loop;
	unsigned int itemp;
	DA_CS=0;
	for(loop = 0 ; loop < 16 ; loop++)
	{
		DA_CLK=0;
		itemp=outbytes & 0x8000;
		if ( itemp == 0 ) DA_D = 0 ;
		else DA_D = 1 ;
		DA_CLK=1;
		outbytes=outbytes<<1;
	}
	DA_CLK=0;
	DA_CS=1;
}
//////////////////////////////
//
//NAME: init_cpu
//DESCRIPTION: init cpu
//CALL:
//INPUT:  NONE
//OUTPUT:NONE
//
///////////////////////////////
void init_cpu(void)
{
	X_CS=1;
	X_SO=1;
	X_SCK=0;
	X_SI=0;
	X_WP=0;
    DSP_LOAD=1;
    DSP_CLK=0;
    TL1=0;
    TH1=0;
    ad_cnt=0;
    TR1=1;
    TMOD=0x92;
	ET1=1;
	EX1=1;
	IT1=1;
	EA=1;

	DA_CS=1;
	DA_CLK=0;
	DA_D=1;

	SCON=0x50;
	T2CON=0x30;//SET TIMER2 MODE FOR COM0
	TH2=0xff;  // ffdc for 9600 fffd for 115200 for 11.0592MHz
	TL2=0xdc;
	RCAP2H=0xff;
	RCAP2L=0xdc;
	TR2=1;
	ES=1;





}
///////////////////////////////
//
//NAME: init_1
//DESCRIPTION:  INTERRUPT 1 SERVICE PROGRAM FOR AD COUNTER
//CALL:
//INPUT:
//OUTPUT:NONE
//
///////////////////////////////
void init_1() interrupt 2
{
	unsigned long vol;
	unsigned char type;
	unsigned long adj;
	TR1=0;

	vol=ad_cnt;
	vol=vol<<8;
	vol=vol | TH1 ;
	vol=vol << 8;
	vol=vol | TL1;
	vol=vol>>3;
	if (vol > 10001)
	{
		vol=vol-10000;
		if( ad_u )
		{
            vol=vol * 60000;
            vol=vol/20001;
		}
		else
		{
			vol=vol*48000;
			vol=vol/20001;
			vol=vol*check_adj;
			vol=vol/10000;
		}
        ad_vol=vol;
	}
	else
	{
		ad_vol=0;
	}

	if ( ad_u)
	{
		type = ADU;
		send_buf[1]='U';
	}
	else
	{
		type = ADI;
		send_buf[1]='I';
	}
	send_buf[0]='E';
	if( abs(ad_vol - set_vol) > 0 )
	{

		if(ad_u)
		{
	        if( ad_vol > set_vol )
			{
				type=type | ADH ;
				send_buf[0]='H';
				adj=ad_vol-set_vol;
				vol=adj*65535;
				vol=vol/120000;
				if ( adj_vol > vol) adj=adj_vol-(unsigned int)vol;
				else adj=0;
			}
	        if( ad_vol < set_vol )
			{
				type=type | ADL ;
				send_buf[0]='L';
				adj=set_vol-ad_vol;
				vol=adj*65535;
				vol=vol/120000;
				vol=adj_vol+ vol;
				if( vol > 65535 )
				    adj = 65535 ;
				else adj=vol;
			}
		}
		else
		{
			if( ad_vol > set_vol )
			{
				type=type | ADH ;
				send_buf[0]='H';
				adj=ad_vol-set_vol;
				vol=adj*13107;  //vol=adj*125*65535/12000000
				
				vol=vol/19200;
               // vol=vol*check_adj;
			//	vol=vol/10000;
				if ( adj_vol > vol) adj=adj_vol-(unsigned int)vol;
				else adj=0;
			}
	        if( ad_vol < set_vol )
			{
				type=type | ADL ;
				send_buf[0]='L';
				adj=set_vol-ad_vol;
				vol=adj*13107;
                                vol=vol/19200;
			//	vol=vol*check_adj;
			//	vol=vol/10000;
				vol=adj_vol+ vol;
				if( vol > 65535 )
				    adj = 65535 ;
				else adj=vol;
			}
		}
		adj_vol=adj;
		da_outbyte(adj);
	}
	if(enable_ad)
	{
		disp_ad(ad_vol,type);
		sprintf(&send_buf[2],"%05u",ad_vol);
		send_cnt=0;
		TI=1;
	}
	ad_cnt=0;
	TH1=0;
	TL1=0;
	TR1=1;
}
///////////////////////////////
//
//NAME: init_timer1
//DESCRIPTION: TIMER1 INTERRUPT SERVICE PROGRAM FOR AD COUNTER
//CALL:
//INPUT:
//OUTPUT:NONE
//
///////////////////////////////
void init_timer1() interrupt 3
{
    ad_cnt++;
}

/////////////////////////////
//
//NAME: disp_ad
//DESCRIPTION: disp_ad
//CALL:
//INPUT:  ad:ad value  type:display type
//OUTPUT:NONE
//
///////////////////////////////
void disp_ad(unsigned int ad,unsigned char type)
{
    unsigned char ch,ch1;
    unsigned int itemp;
    itemp=ad;
    ch=ad/10000;
    ch1=ADU;
    if( (type & ch1)  == ch1 ) display(4,ch | 0x80);
    else display(4,ch);
    itemp=itemp-ch*10000;
    ch=itemp/1000;
    ch1=ADI;
    if(( type & ch1 ) == ch1 ) display(5,ch | 0x80);
    else display(5,ch);
    itemp=itemp-ch*1000;
    ch=itemp/100;
    display(6,ch);
    itemp=itemp-ch*100;
    ch=itemp/10;
    display(7,ch);
    itemp=itemp-ch*10;
    ch=itemp;
    display(8,ch);
    ch1=ADH;
    if(( type & ch1 ) == ch1 )
	{
		display(1,0x0c);
		display(2,0x0f);
	}
    else
    {
        ch1=ADL;
        if (( type & ch1 ) == ch1 ) display(1,0x0d);
		else display(1,0x0f);
		display(2,0x0f);
    }
    ch1= SET_AD;
    if(( type & ch1 ) == ch1 )
    {
        display(1,0x05);
        display(2,0x0b);
    }
	display(3,0x0f);
}

//////////////////////////////
//
//NAME: display
//DESCRIPTION: display
//CALL:
//INPUT:  pos:place  disp_code:display code
//OUTPUT:NONE
//
///////////////////////////////
void display(unsigned int pos,unsigned int disp_code)
{
    int k=0;
    k=pos*256+disp_code;
    disp_outbyte(k);
}
//////////////////////////////
//
//NAME: disp_init
//DESCRIPTION: init 7219
//CALL:
//INPUT:  NONE
//OUTPUT:NONE
//
///////////////////////////////
void disp_init(void)
{
    int k=0x010f;
    unsigned int i;
    disp_outbyte(0x09ff);           //DECODE MODE:1-8
    disp_outbyte(0x0a01);           //INTENSITY:31/32
    disp_outbyte(0x0b07);           //SCAN LIMIT:1-8
    disp_outbyte(0x0c01);           //SHUT DOWN:OFF
    disp_outbyte(0x0f00);           //DISPLAY TEST:OFF
    for ( i = 0 ;i < 8 ; i++)
    {
        disp_outbyte(k);
        k=k+0x0100;
    }
}
//////////////////////////////
//
//NAME: disp_outbyte(int outbytes)
//DESCRIPTION: output a word to 7219
//CALL:
//INPUT:  NONE
//OUTPUT:NONE
//
///////////////////////////////
void disp_outbyte(int outbytes)
{
    int i ,j;
    DSP_LOAD=0;

    for( i = 0 ; i < 16 ;i++)
    {
		DSP_CLK=0;
        j=outbytes &  0x8000;
        if ( j == 0 ) DSP_DIN = 0 ;
        else DSP_DIN=1;
        outbytes=outbytes<<1;
        DSP_CLK=1;
     }
     DSP_LOAD=1;
     DSP_CLK=0;
	 DSP_CLK=1;
	 DSP_LOAD=0;
}


//below functions are for x5045
/*
/////////////////////////////////////////
//
//NAME: x_write
//DESCRIPTION: WRITE MUL-BYTES TO X5045
//CALL: x_outbyte,wait,x_wren_cmd
//INPUT: x_address: address of x5045 000h-01ffh
//       *psavedata :data pointer to be saved
//		 amount: counter of data <0x10
//RETURN:NONE
////////////////////////////////////////
void x_write ( unsigned int x_address,unsigned char *psavedata,unsigned char amount)
{
	unsigned char  sendaddress,i,ch;
	x_wren_cmd();
   // X_WP=1;
	X_SCK=0;
	wait(5);
	X_CS=0;
	if ( x_address >=0x100 )
	{	x_outbyte(0x0a);
		x_address = x_address-0x100 ;
	}
	else
	{
		x_outbyte(0x02);
	}
	sendaddress=(unsigned char ) x_address;
	x_outbyte(sendaddress);
	for (i=0 ; i<amount ;i++)
	{       ch=*psavedata;
		x_outbyte( ch );
                psavedata++;
                }
	X_SCK=0;
	X_CS=1;
   // X_WP=1;
	wait(6);
}
/////////////////////////////////////////
//
//NAME: x_read
//DESCRIPTION: WRITE MUL-BYTES TO X5045
//CALL: x_outbyte,x_inbyte;wait,x_wren_cmd
//INPUT: address: address of x5045
//		 amount: counter of data
//RETURN:data pointer
////////////////////////////////////////

unsigned char  * x_read( unsigned int address ,unsigned char  amount)
{
	unsigned char  * preaddata;
	unsigned char  sendaddress,i;
	unsigned char  readdata[10];
	X_SCK=0;
	wait(6);
	X_CS=0;
	if ( address >=0x100 )
	{	x_outbyte(0x0b);
		address=address-0x100;
	}
	else
	{
		x_outbyte(0x03);
	}
	sendaddress=(unsigned char ) address;
	x_outbyte(sendaddress);
	for ( i=0 ; i<amount ;i++)
	{	readdata[i]=x_inbyte();
         }

	X_SCK=0;
	X_CS=1;
	preaddata=&readdata;
	return(&readdata);
}
*/
/////////////////////////////////////////
//
//NAME: x_wren_cmd
//DESCRIPTION: WRITE ENABLE
//CALL: x_outbyte
//INPUT:NONE
//RETURN:NONE
////////////////////////////////////////
void x_wren_cmd (void)
{
	X_SCK=0;
	wait(3);
	X_CS=0;
	x_outbyte(0x06);
	X_SCK=0;
	X_CS=1;
}
/////////////////////////////////////////
//
//NAME: init_watchdog
//DESCRIPTION: INIT WATCHDOG TO X5045
//CALL: x_outbyte,x_wren_cmd
//INPUT: ch=0:1.4S,ch=1: 600mS,ch=2 :200mS ch=3 : disablewatchdog
//RETURN:NONE
////////////////////////////////////////
void init_watchdog(unsigned char ch)
{	if (!( ch > 0x03))
	{
   // X_WP=1;
	x_wren_cmd();
	X_SCK=0;
	X_CS=0;
	x_outbyte(0x01);
	ch=ch<<4;
	x_outbyte(ch);
	X_SCK=0;
	X_CS=1;
 //   X_WP=1;
	}
}
/*
unsigned char x_inbyte( void )
{
	unsigned char  i;
	unsigned char  ch=0x00;
	for ( i=0 ; i<8 ;i++ )
	{	X_SCK=1;
		X_SCK=0;
		ch=ch*2;
		if (X_SO)
        {  ch=ch+1;
        }
 	}
	return ( ch );
}
*/
void x_outbyte( unsigned char ch) 	//ch:data to be sent to x5045
{
	unsigned char  i;
	for ( i=0 ; i<8 ; i++ )
	{	X_SCK=0;
		X_SI=(bit)(ch & 0x80);
		ch=ch*2;
		X_SCK=1;
	 }
	 X_SI=0;
}
//X5045 PROG END ///////////////////////
/////////////////////////////////////////
//
//NAME: wait
//DESCRIPTION: WAIT
//CALL: NONE
//INPUT: time:mS <356
//RETURN:NONE
////////////////////////////////////////
void wait(unsigned int time)
{
	X_CS=0;
	X_CS=1;
    time=time*183;
	for ( time; time>=1 ; time--)
	{
    }
	X_CS=0;
	X_CS=1;
}

⌨️ 快捷键说明

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