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

📄 selftest.c

📁 我自己做的给予ST公司的upsd3234a单片机的频率计。可以测量10K以下的频率。这是此频率计的自检程序
💻 C
字号:
			  
#pragma optimize(8,size)
#include "upsd3200.h"
#include "upsd_hardware.h"
#include "LCD_IO.h"
#include "upsd_timer.h"

unsigned int frequence;
#define TIMER_COUNT 0x7dcb 
static unsigned int timer0_tick;
unsigned char idata dispbuf[30];
xdata PSD_REGS PSD8xx_reg _at_ PSD_REG_ADDR;  // Define PSD registers at address "csiop" space
	  



/*------------------------------------------------------------------------------
void timer_init(void);

This function enables TIMER 0.  TIMER 0 will generate a synchronous interrupt
once every 100Hz.
------------------------------------------------------------------------------*/
void timer_init (void)
{
  EA = 0;			/* disable interrupts */

  TR0 = 0;			/* stop timer 0 */
  
  timer0_tick = 0;
  
  TMOD = 0x11; //clear timer 0 mode bits - bottom 4 bits */
  TL0=(TIMER_COUNT&&0x00ff);
  TH0=(TIMER_COUNT >> 8);
  TH1=0x00;	
  TL1=0x00;
  PT0=0;
  ET1=1;
  ET0=1;
  EA=1;
  TR0=1;
  TR1=1;
}



/*------------------------------------------------------------------------------
unsigned int timer0_count (void);

This function returns the current timer0 tick count.
------------------------------------------------------------------------------*/
unsigned int timer0_count (void)
{
  unsigned int t;

  EA = 0;				// disable ints so we cna read steady value
  t = timer0_tick;
  EA = 1;				// enable
  return(t);
}


/*------------------------------------------------------------------------------
static void timer0_isr(void);

This function is an interrupt service routine for TIMER 0.  It should never
be called by a C or assembly function.  It will be executed automatically
when TIMER 0 overflows.
------------------------------------------------------------------------------*/




/*------------------------------------------------------------------------------
static void timer1_isr (void);

This function is an interrupt service routine for TIMER 0.  It should never
be called by a C or assembly function.  It will be executed automatically
when TIMER 0 overflows.
------------------------------------------------------------------------------*/


void timer1_isr(void)interrupt 3
{
	TH1=0x00;
	TL1=0x00;
}
/*-----------------------------------------------------------------------
                十六进制数转换为BCD码
				HEX_TO_BCD(unsigned int n)
-------------------------------------------------------------------------*/

HEX_TO_BCD(unsigned int n)
{
      dispbuf[3]=((n/1000)+48);
	  dispbuf[2]=((n/100)%10+48);
	  dispbuf[1]=((n/10)%10+48);
	  dispbuf[0]=((n%10)+48);
}
/*-----------------------------------------------------------


------------------------------------------------------------*/
void timer0_isr(void)interrupt 1 
{
/*------------------------------------------------
Stop timer, adjust the timer 0 counter so that we get another
interrupt in 10ms, and restart the timer.
------------------------------------------------*/
  TR0 = 0;				/* stop timer 0 */

  TL0 = (TIMER_COUNT & 0x00FF);
  TH0 = (TIMER_COUNT >> 8);

  TR0 = 1;	/* start timer 0 */

  timer0_tick++;// Increment global var timer_tick (number of 10ms ticks)
  if(timer0_count() >= 100)
	  {
		   timer0_tick=0;
		   frequence=TH1*256+TL1;
		   TH1=0;
		   TL1=0;
		   HEX_TO_BCD(frequence);
		   }
}

/*--------------------------------------------------------------------------
                      LCD显示码子程序
					  display();
---------------------------------------------------------------------------*/
void display(void)
{
	printfLCD("THE FREQUENCE IS :\n");
    printfLCD("      ");
	//delay_1sec();
	/*putch_LCD(dispbuf[3]);
	putch_LCD(dispbuf[2]);
	putch_LCD(dispbuf[1]);
	putch_LCD(dispbuf[0]);*/
    printfLCD(dispbuf);
	printfLCD(" Hz\n");
}


/*-------------------------------------------------------------------------
                 和上位机通讯程序
				 void trans(void);
---------------------------------------------------------------------------*/
  /*void trans(void)
{
	unsigned char i;
	EA = 0;
	T2MOD = 0;
	T2CON = 0x30;
	RCAP2L = 0xDC;
	RCAP2H = 0xFF;
	TL2 = 0xDC;
	TH2 = 0xFF;	 //设置波特率为9600bps

	SCON |= 0x50;  //方式1,允许接收
	PCON = 0x00;
	IE |= 0x90; 
	TR2 = 1;  //开启计数控制位
	
	while(1){
	    i=0;
		while(dispbuf[i] != 0x00){
		SBUF = "THE FREQUENCE IS\N";
		SBUF = dispbuf[i];
			while(TI=0);
			TI=0;
			i++;
			}
	}
}





/*------------------------------------------------------------------------
 每隔一秒,读频率计数器
void main(void)
--------------------------------------------------------------------------*/

void main(void)
{
	WDKEY = 0x55;
	PSD8xx_reg.VM |= 0x80;      // enable peripheral I/O mode for LCD display
	timer_init();
	lcd_init();
	//trans();
while(1)
	{
	display();
	   }
}






⌨️ 快捷键说明

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