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

📄 bb.c

📁 自己写的930单片机的1602显示和tlc549串行ad程序
💻 C
字号:
												  #include <REG931.H>
#include <INTRINS.H>

typedef  unsigned char	uchar;
typedef  unsigned int	uint;

#define ClkDown		OCC = 0  	//P1.7
#define ClkUp   	OCC = 1 
	
#define DataLow   	KB0 = 0 	//P0.0
#define DataHigh	KB0 = 1 	

#define LEDown    	OCD = 0 	//P2.1
#define LEUp		OCD = 1


sbit LED1 	  = P2^2;
sbit LED2 	  = P2^3;
sbit KB 	  = P1^6;
sbit adcs		= P2^4;	 //out
sbit dout		= P2^3;	 //in

sbit adclk		= P1^4;	 //out

// 间接访问方式地址定义
// 控制引脚定义
sbit	RS	=P1^7;
sbit 	RW	=P2^1;
sbit 	Elcm=P2^0; 



////////////////////////////////////////////////////////////////////////////
#define DataPort P0// 数据端口
//instruction
#define Busy 		0x80
#define Work_M		0x38//n=1,f=0
#define Cls			0x01
#define Input_M 	0x06//i/d=1  ac++  s=0 screen move=off
#define Screen_Off 	0x08
#define Disp_M		0x0c//d=1 disp=on c=0 cursor=off
#define Ddram_Al1	0x80;
#define Ddram_Al2	0xc0;//0x80+0x40
	  #define    Wait1us        _nop_();
#define    Wait2us        {_nop_();_nop_();}
#define    Wait4us        {Wait2us;Wait2us;}
#define    Wait8us        {Wait4us;Wait4us;}
#define    Wait10us      {Wait8us;Wait2us;}



/* 设为全局变量 */
unsigned char com,dat;



///////////////////////////////////////////////
uchar dispc[2][16]=
	{
	' ',' ','O','U','T','P','U','T',' ','P','O','W','E','R',' ',' ',
	' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','d','B','m',' ',' ',' '
	};

uchar low[2][16]=
	{
	' ',' ','O','U','T','P','U','T',' ','P','O','W','E','R',' ',' ',
	' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','d','B','m',' ',' ',' '
	};
uchar pllfreq[6]={2,0,8,0,0,0};
uchar pllstate=1;
/////////////////////////////////////////
code  unsigned char ctab[16]={'0','1','2','3','4','5','6','7','8','9','a','b','c','<','o','w'};
 
/////////////////////////////////////////

	

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
uchar rflag=0; //定义读标志
uchar sflag=0; //定义发送标志

///////////////////
#define	SCOUNT 10
#define	RCOUNT 10	//note :   cause the invalid ip --> 0000
//idata <256 data <128 bdata bit pdata movx @ri xdata movx @dptr
unsigned char data   countsms;

unsigned char data   rpoint;//point the next address
unsigned char data   spoint;
//**********************
unsigned char idata  ssbuf[SCOUNT+1];
//**********************
unsigned char  data  rbuf[RCOUNT+1];
uchar rtemp;

//======================================================================
//串口接收中断
void UART_ISR(void) interrupt 4 using 3//当接收完一个字节RI置1进入Recieve中断
{
rtemp=SBUF; //保存所读取的数据
RI=0; //RI清0以等待下次发送	// set flag for polling in main loop

//rflag--1 已接收到AA,2--命令字节已接收到
if (rflag==1)
{
	rbuf[rpoint]=rtemp;
	if (rpoint<RCOUNT)
 	{
		rpoint++;
	}
}
if (rtemp==0xaa)
{
	rpoint=0;
	rflag=1;
}
}

//=======================================================================
//串口发送
void sendm(unsigned char sl)
{
EA=0;//dellete????????????????????????????duplex
for(spoint=0;spoint<sl;spoint++)			// send string
{
    // clear transmit interrupt flag
	SBUF=ssbuf[spoint];      //start sending one byte
	while (!TI);
	TI=0;	
  // wait until sent
}
EA=1;
}

//=======================================================================
//串口初始化
void UART_init(void)
{	
	SCON =0x50;				//SCON: serail mode 1, 8-bit UART, enable ucvr
	SSTAT=0x60;				// separate Rx / Tx interrupts

	BRGR0=0xF0;				// setup BRG for 9600 baud @ 7.373MHz internal RC oscillator
	BRGR1=0x02;


	BRGCON = 0x03;     		// enable BRG

//header of sending string
	rpoint=0;
	spoint=0;
	rbuf[RCOUNT]=0;
	ssbuf[SCOUNT]=0;
	ssbuf[0]=0xaa;

	rflag=0;


	ESR=1; //ESR=Enable Serial Recieve
	EST=1; //EST=Enable Serial Transmit
//	EA=1; //使能中断

}

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

void initio(void)
{

	P0M1 = 0x00;
	P0M2 = 0x00;//P0			

	P1M1 = 0x2c;     			
	P1M2 = 0x0c; //P1.2 P1.3  0C OUTPUT

	P2M1 = 0x00;     		
	P2M2 = 0x00;

	DataLow;
	ClkDown;
	LEDown;

	KB=1;

	LED1=1;
	LED2=1;
	
}   


/***********************************/
/* 间接访问方式的驱动函数      */
/***********************************/
/* 直接驱动函数由4个子程序组成:
  1.读BF和AC值函数WaitForEnable
  2.写指令代码函数LcdWriteCommand
  3.写显示数据函数LcdWriteData
  4.读显示数据函数LcdReadData
*/   

/*---------读BF和AC值函数---------*/ 
/*
=======================================================
正常读写操作之前必须检测LCD控制器状态: CS=1 RS=0 RW=1
DB7: 0 LCD控制器空闲; 1 LCD控制器忙
========================================================
*/
void WaitForEnable( void )
{
	DataPort = 0xff;
	RS =0;RW = 1;
	Elcm = 1;
	/* 判BF=0? */
	_nop_();
	while( DataPort & Busy );
	Elcm = 0;
	//ACC=P1; while(ACC_7==1); com=cradd;
}


/*
=======================================================
写控制字符子程序: E=1 RS=0 RW=0
=======================================================
*/
/*-----------写指令函数-----------*/
void LcdWriteCommand( void ) 
{
	WaitForEnable(); // 检测忙信号?
	RS = 0;RW = 0;
	DataPort = com;
	// 送控制字子程序
	_nop_();
	Elcm = 1;
	_nop_();
	_nop_();
	Elcm = 0; // 操作允许脉冲信号
}


/*
=======================================================
当前位置写字符子程序: E =1 RS=1 RW=0
=======================================================
*/
/*-----------写数据函数-----------*/
//void LcdWriteData( char dataW ) {
void LcdWriteData( void )
{
	WaitForEnable();
	// 检测忙信号
	RS = 1;RW = 0;
	DataPort = dat;
	_nop_();
	Elcm = 1;
	_nop_();
	_nop_(); 
	Elcm = 0; // 操作允许脉冲信号

}


/*
=======================================================
initialization of LCD   
=======================================================
*/
void initLCD(void)
{


unsigned char i,j;

    for(j=0;j<3;j++)
   		{
	  		RS=0;
			RW=0;
			DataPort=Work_M; 
        	Elcm=1;
		 	_nop_();
        	Elcm=0;
			for(i=0;i<180;i++);
         	Elcm=1;
		 	_nop_();
        	Elcm=0;
		}

	com=Work_M;
    LcdWriteCommand();

    com=Screen_Off;
    LcdWriteCommand();

    com=Cls;
    LcdWriteCommand();

    com=Input_M; 
    LcdWriteCommand();

   	com=Disp_M;
    LcdWriteCommand(); 
}


/*
=======================================================
disp the string
=======================================================
*/
void Dispall(void)
{
	uchar i;
	com = Ddram_Al1;
	LcdWriteCommand();
	
	for (i=0;i<16;i++)
		{
		dat=dispc[0][i];
		LcdWriteData(); // 写字符
		}
	
	com = Ddram_Al2;
	LcdWriteCommand();
	
	for (i=0;i<16;i++)
	{
		dat=dispc[1][i];
		LcdWriteData(); 
	}
}

	 //////
void Dispall1(void)
{
	uchar i;
	com = Ddram_Al1;
	LcdWriteCommand();
	
	for (i=0;i<16;i++)
		{
		dat=low[0][i];
		LcdWriteData(); // 写字符
		}
	
	com = Ddram_Al2;
	LcdWriteCommand();
	
	for (i=0;i<16;i++)
	{
		dat=low[1][i];
		LcdWriteData(); 
	}
}
//============================================================================================== 
/****************************************************************
;说明CPU配置WDTE=1WDSE=0
;***************************************************************/
void init_wdt()
{
ACC = WDCON; //读取WDT控制寄存器
ACC = ACC|0xc5; //置位ACC.2准备启动WDT	 PRE2PRE1PRE0=110_D765	WDCLK_D0当该位为1时选择看门狗振荡器作为时钟源
WDL = 0xff; //设置8位倒计时器初值  c5/1.31s	  e5/2.62s
WDCON = ACC; //启动WDT
WFEED1 = 0xA5; //清零第一部分
WFEED2 = 0x5A; //清零第二部分
}

/*****************************
;喂狗子程序
;注意程序中开启中断
*****************************/
void clrwdt()
{
EA = 0; //关闭中断
WFEED1 = 0xA5; //执行清零第一部分
WFEED2 = 0x5A; //执行清零第二部分
EA = 1; //开中断
}
//************************************************************************************
			 void  DelayNS()		
{ uchar i,j;
  
  { for(i=0; i<200; i++)
     for(j=0; j<250; j++);
  }
}
//ad 
unsigned char ADCSelChannel(void)
{
      unsigned char ConvertValue = 0;
      unsigned char i;
	  unsigned char tmp=0;
      
      adcs = 1;
      adclk = 0;
      Wait4us;
      adcs = 0; //芯片起始
      Wait4us; //等待延时
      for (i = 0; i < 8; i ++) //输入采样转换时钟
      {
            adclk = 1;
			tmp=tmp<<1;
			if (dout)
            {
			tmp++; }
			
                  
            adclk = 0;
      }
			
      adcs=1;
		ConvertValue=tmp;
		for(i=0;i<3;i++)
		_nop_();
      return (ConvertValue); //返回转换结果
}

void main()
{
   uchar A;
  uchar B;
  uchar C;
  uchar D;

  
uint c,i;
uchar cc;
unsigned char ss[8];
uchar addata=0;
		 uchar temp;
//_______________________________________________________________________
initio();
initLCD();
//_______________________________________________________________________
Dispall();
/////////////////////////////////////////////////////////
UART_init();
/////////////////////////////////////////////////////////////////////////
c=0;
cc=0;
init_wdt(); //调用看门狗初始化子程序
EA=1; //使能中断

while(1)
{		  
clrwdt(); 
addata=ADCSelChannel();

  
	A = addata/1000;
 	 B = addata%1000/100;
 	 C = addata%1000%100/10;
 	 D = addata%1000%100%10;
	 c=A*1000+B*100+C*10+D;
	 		 
  for (i=0;i<16;i++)
	{
		
	  dispc[0][i]=low[0][i];  }
	  for (i=0;i<16;i++)
	{
		
	  dispc[1][i]=low[1][i];  }

if(c<4){		
		dispc[1][2]	=ctab[13]; 
		dispc[1][3]	=ctab[13];
		dispc[1][4]	=ctab[1];
		dispc[1][5]	=ctab[5];	
 
 }	

 if(c>=4&&c<5){			dispc[1][4]	=ctab[1];
		dispc[1][5]	=ctab[5];	}

 if(c>=5&&c<6){			dispc[1][4]	=ctab[1];
		dispc[1][5]	=ctab[6];	}

  if(c>=6&&c<8){			dispc[1][4]	=ctab[1];
		dispc[1][5]	=ctab[7];	}
	if(c>=8&&c<10){			dispc[1][4]	=ctab[1];
		dispc[1][5]	=ctab[8];	}
	if(c>=10&&c<12){			dispc[1][4]	=ctab[1];
		dispc[1][5]	=ctab[9];	}
	if(c>=12&&c<14){			dispc[1][4]	=ctab[2];
		dispc[1][5]	=ctab[0];	}
if(c>=14&&c<16){			dispc[1][4]	=ctab[2];
		dispc[1][5]	=ctab[1]; }
	if(c>=16&&c<18){			dispc[1][4]	=ctab[2];
		dispc[1][5]	=ctab[2];	}
if(c>=18&&c<20){			dispc[1][4]	=ctab[2];
		dispc[1][5]	=ctab[3];	}
if(c>=20&&c<23){			dispc[1][4]	=ctab[2];
		dispc[1][5]	=ctab[4];	}
	if(c>=23&&c<26){			dispc[1][4]	=ctab[2];
		dispc[1][5]	=ctab[5];	}				
  
	 if(c>=26&&c<30){			dispc[1][4]	=ctab[2];
		dispc[1][5]	=ctab[6];	}
			if(c>=30&&c<34){			dispc[1][4]	=ctab[2];
		dispc[1][5]	=ctab[7];	}		
			if(c>=34&&c<38){			dispc[1][4]	=ctab[2];
		dispc[1][5]	=ctab[8];	}		
			if(c>=38&&c<43){			dispc[1][4]	=ctab[2];
		dispc[1][5]	=ctab[9];	}		
			if(c>=43&&c<49){			dispc[1][4]	=ctab[3];
		dispc[1][5]	=ctab[0];	}		
			if(c>=49&&c<56){			dispc[1][4]	=ctab[3];
		dispc[1][5]	=ctab[1];	}		
			if(c>=56&&c<64){			dispc[1][4]	=ctab[3];
		dispc[1][5]	=ctab[2];	}		
			if(c>=64&&c<73){			dispc[1][4]	=ctab[3];
		dispc[1][5]	=ctab[3];	}		
			if(c>=73&&c<82){			dispc[1][4]	=ctab[3];
		dispc[1][5]	=ctab[4];	}		
			if(c>=82&&c<92){			dispc[1][4]	=ctab[3];
		dispc[1][5]	=ctab[5];	}		
			if(c>=92&&c<103){			dispc[1][4]	=ctab[3];
		dispc[1][5]	=ctab[6];	}		
			if(c>=103&&c<114){			dispc[1][4]	=ctab[3];
		dispc[1][5]	=ctab[7];	}		
			if(c>=114&&c<125){			dispc[1][4]	=ctab[3];
		dispc[1][5]	=ctab[8];	}				
		if(c>=125&&c<137){			dispc[1][4]	=ctab[3];
		dispc[1][5]	=ctab[9];	}		
				if(c>=137&&c<149){			dispc[1][4]	=ctab[4];
		dispc[1][5]	=ctab[0];	}		
if(c>=149)	{ 	 
 
 			 
			   
			  
			 

			//ss[3]=(addata>>=4)-(ss[0]<<1);
	//	dispc[1][2]	=ctab[15];
	//	dispc[1][3]	=ctab[15];
		dispc[1][4]	=ctab[4];
		dispc[1][5]	=ctab[1]; }
		 
 Dispall();  
	  		  
				  
		   
		
				  
			DelayNS();			 
	}}//

/*if(0){	Dispall1();	

 }	 
 else	{ 	 A = addata/1000;
  B = addata%1000/100;
  C = addata%1000%100/10;
  D = addata%1000%100%10;
 
 			 ss[0]=A;
			 
			 ss[1]=B;
			 
			  ss[2]=C;
			  
			  ss[3]=D;
			   
			  
			 

			//ss[3]=(addata>>=4)-(ss[0]<<1);
	
		dispc[1][2]	=ctab[ss[1]];
		dispc[1][3]	=ctab[ss[2]];
		dispc[1][4]	=ctab[ss[3]];
		 
 Dispall();  
	  		  }
				  
		   
		
				  
			DelayNS();			 
	}

}


 
	/*	
		cc++;
			
			if(cc>9){
			cc=0;
			c++;	   }
				if(c>15){
			c=0;
				   }		
	c++;
	cc++;
	if (c>65000)
	{
		c=0;
		initLCD();//every 72 cycles
		Dispall();
		LED1=~LED1;
	}
	if (cc>250) 
	{
		cc=0;
		clrwdt();
	}

    if(KB==0)
	{
		LED1 = 0; 
		LED2 = 0;
		while(1); 
	}

	if((rflag==1)&&(rpoint>6))
	{
	  	pllstate	=rbuf[0];
		pllfreq[0]	=rbuf[1];
		pllfreq[1]	=rbuf[2];
		pllfreq[2]	=rbuf[3];
		pllfreq[3]	=rbuf[4];
		pllfreq[4]	=rbuf[5];
		pllfreq[5]	=rbuf[6];

		rflag=0;
		rpoint=0;

		ssbuf[0]=0xaa;
		ssbuf[1]=pllstate;
		ssbuf[2]=pllfreq[0];
		ssbuf[3]=pllfreq[1];
		ssbuf[4]=pllfreq[2];
		ssbuf[5]=pllfreq[3];
		ssbuf[6]=pllfreq[4];
		ssbuf[7]=pllfreq[5];
		ssbuf[8]='O';

		sendm(9);
		LED2=~LED2;

//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
		if (pllstate==1)
			{
				dispc[1][7]=' ';dispc[1][8]=' ';
			}
		else
			{
				dispc[1][7]='U';dispc[1][8]='n';
			}
	
		dispc[0][6]	=ctab[pllfreq[0]];
		dispc[0][7]	=ctab[pllfreq[1]];
		dispc[0][8]	=ctab[pllfreq[2]];
		dispc[0][9]	=ctab[pllfreq[3]];
		//.
		dispc[0][11]=ctab[pllfreq[4]];
		dispc[0][12]=ctab[pllfreq[5]];
		*/

⌨️ 快捷键说明

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