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

📄 main.c

📁 mb90340的AD转换小小 程序
💻 C
字号:
/* THIS SAMPLE CODE IS PROVIDED AS IS AND IS SUBJECT TO ALTERATIONS. FUJITSU */
/* MICROELECTRONICS ACCEPTS NO RESPONSIBILITY OR LIABILITY FOR ANY ERRORS OR */
/* ELIGIBILITY FOR ANY PURPOSES.                                             */
/*                 (C) Fujitsu Microelectronics Europe GmbH                  */
/*---------------------------------------------------------------------------
  MAIN.C
  - This sample uses an Analogue - Digital conversion and UART0 
	The AD converter samples an analogue value from signal on Pin 37 (AN1).
	The value from the AD converter is shown on Port 0 (LED磗).
	With the value a subroutine calculates the impressed voltage on Pin 37 (AN1)
	and sends it to the terminal.
	
  - See README.TXT for project description and disclaimer.

/*---------------------------------------------------------------------------*/


#include "mb90340.h"

const char welcome[100]="Welcome at FUJITSU \n\n";	    /* welcome text */
const char volt[50]="the Voltage on Pin AN1 is : ";
unsigned char ch;                                       /* to store a char in */

void InitUart0(void)
{
  // Initialize UART asynchronous mode
  // BGR0 = 1666; //  9600 Baud @ 16MHz
  // BGR0 = 832;  // 19200 Baud @ 16MHz
  // BGR0 = 416;  // 38400 Baud @ 16MHz

  // BGR0 = 2083; //  9600 Baud @ 20MHz
  // BGR0 = 1041; // 19200 Baud @ 20MHz
  // BGR0 = 520;  // 38400 Baud @ 20MHz

  BGR0 = 2499; //  9600 Baud @ 24MHz
  // BGR0 = 1249; // 19200 Baud @ 24MHz
  // BGR0 = 624;  // 38400 Baud @ 24MHz
   
  SCR0 = 0x17; // 8N1
  SMR0 = 0x0d; // enable SOT3, Reset, normal mode
  SSR0 = 0x00; // LSB first
  DDR0 = 0xFF; // Data Direction Register -> outputs
}

void Putch0(char ch)        	 	/* sends a char                     */
{
  while (SSR0_TDRE == 0);    		/* wait for transmit buffer empty 	*/
  TDR0 = ch;                 		/* put ch into buffer			    */
}

void Puts0(const char *Name2)  		/* Puts a String to UART            */
{
  int i;
 
  for (i=0; i<strlen(Name2); i++)   /* go through string                */
  {
    if (Name2[i] == 10)
      Putch0(13);
    Putch0(Name2[i]);              	/* send it out                      */
  }
}

void InitAdc(void)
{
  ADCS0 = 0xA0;                     /* AD set to 8 bit continous mode   */
  ADSR = 0xFC21;                    /* ADSR0 + ADSR1 read only AN1      */
  ADCS1 = 0xA2;                     /* interrupt enabled, start conversion */
}

/************************************************************************/
float factor = 0.019608, calco;	    /* factor = 5 / 8 bit (255)         */
int ziff, x, memo, tmp;				

/* subroutine to send the calculated voltage to the terminal */
void calc(void)			
{
  tmp = ADCR0 >> 1;                 /* clear lowest bit (always toggeling) */
  if (tmp != memo >> 1)             /* refresh terminal only if voltage changes */
  {
    Puts0 (volt);                   /* text to terminal                 */
    calco = factor * ADCR0;         /* calculate voltage                */ 
    ziff = calco;                   /* 1. number to terminal            */
    ch = '0' + ziff;
    Putch0(ch);
    ch = ',';
    Putch0(ch);
    for (x=0;x<3;x++)               /* send 3 decimals to terminal      */
    {
      calco =( calco * 10 )-( ziff * 10 );
      ziff = calco;
      ch = '0' + ziff;
      Putch0(ch);
    }
    Puts0(" V\r");                  /* unit "V" to terminal             */
  }
  memo = ADCR0;                     /* keep in mind last voltage        */
}

void main(void)
{
  DDR0 = 0xFF;                      /* Data direction Port 0 = Output   */		
  InitIrqLevels();
  __set_il(7);                      /* allow all levels                 */
  __EI();                           /* globaly enable interrupts        */
 	
  /* initialize UART */
  InitUart0();                      /* init UART                        */
	
  Puts0("\n");			
  Puts0 (welcome);                  /* welcome text                     */
	
  /* initialize AD */
  InitAdc();                        /* init AD - converter              */
 
  while(1)                          /* waiting for interrupt ( no operation ) */
  {
    __asm("\tnop");
  }
}

__interrupt void ADWandler (void)
{
  PDR0 = ADCR0;                     /* shows voltage on Port 0 ( LED磗 ) */
  	
  calc();                           /* voltage calculating              */
    
  ADCS1 = 0xA2;                     /* clear interrupt flag             */
}




  	





⌨️ 快捷键说明

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