📄 main.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
- description
- See README.TXT for project description and disclaimer.
/*---------------------------------------------------------------------------*/
#include "mb90425.h"
/* Note:
The -A and -B versions include a CPU Detection Reset Circuit.
This must be cleared periodically to prevent program reset.
The conditions for clearing the counter of this circuit are given below:
1. Writing 0 to CL bit of LVRC register
2. Internal reset
3. Stopping main oscillation clock
4. Transition to sleep mode
5. Transition to time-base timer mode or timer mode
6. Starting hold
*/
void clear_CPU_operation_detection (void)
{
LVRC = 0x35; /* clears CPU operation detection */
}
/*---------------------------------------------------------------------------
#Defines
/*---------------------------------------------------------------------------*/
#define BACK 0x08 /* Backspace-Eingabe (HEX-Wert) */
#define RET 0x0d /* Return-Eingabe (HEX-Wert) */
#define ESC 0x1b /* Escape-Eingabe (HEX-Wert) */
#define HEX 0
#define DEC 1
/*---------------------------------------------------------------------------
Globals
/*---------------------------------------------------------------------------*/
unsigned int Analog_results[8];
unsigned int result;
unsigned int i = 0;
/*---------------------------------------------------------------------------
ADC-Routines
/*---------------------------------------------------------------------------*/
void Init_ADC(void)
{
DDR6 = 0x00; /* Analog-Port is input */
ADER = 0xFF; /* Analog input enable */
ADCSL = 0xC7; /* Stop conversion mode, start-channel 0, end-channel 7 */
ADCSH = 0x80; /* disable interrupt, software activation */
ADCRH = 0x78; /* 10-Bit resolution */
}
unsigned int GetADCResult(void)
{
while (ADCSH_INT == 0) /* wait for convertion end */
clear_CPU_operation_detection();
result = (ADCRH & 0x03) << 8; /* built 16 Bit */
result = result | ADCRL; /* ADCR1 (16 Bit) OR ADCR0 (8 Bit) */
ADCSH = 0x80; /* clear INT-Flag bit */
return result;
}
/*---------------------------------------------------------------------------
UART-Routines
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------
UART-Procedures
/*---------------------------------------------------------------------------*/
void InitUart1(void)
{
/* initialize UART1 e.g. mode, baud rate, stop bit, data length etc. */
CDCR1 = 0x80; /* UART1 prescaler control register */
SMR1 = 0x1B; /* serial mode register (mode 0, 9600Baud, SOE enable) */
SCR1 = 0x13; /* Serial control register */
}
void Putch (char ch) /* sends a char */
{
while (SSR1_TDRE == 0); /* wait for transmit buffer empty */
SODR1 = ch; /* put ch into buffer */
}
char Getch(void) /* waits for and returns incomming char */
{
unsigned ch;
while(SSR1_RDRF == 0) /* wait for data received... */
clear_CPU_operation_detection(); /* ... but clear CPU operation detection */
if (SSR1_ORE) /* overrun error */
{
ch = SIDR1; /* reset error flags */
return (-1);
}
else
return (SIDR1); /* return char */
}
void Puts (const char *Name2) /* Puts a String to UART */
{
unsigned int i;
for (i=0; i<strlen(Name2); i++) /* go through string */
{
if (Name2[i] == 10)
Putch(13);
Putch(Name2[i]); /* send it out */
clear_CPU_operation_detection(); /* ... but clear CPU operation detection */
}
}
void binasci4(unsigned int a, unsigned char base, char fill)
{
unsigned char val;
a &= 0x0F;
switch(base)
{
case HEX: if (a <= 9) Putch(a + '0');
else Putch(a + 'A' - 10);
break;
case DEC: val = a / 10;
if( val ) Putch( a / 10 + '0');
else Putch( fill );
Putch( a % 10 + '0');
break;
default: Puts(" <format not supported> ");
}
}
void binasci8(unsigned int a, unsigned char base, char fill)
{
unsigned int rem,val,d;
unsigned char filling=1;
switch(base)
{
case HEX: binasci4(a >> 4, HEX, fill);
binasci4(a, HEX, fill);
break;
case DEC: rem = a;
for( d=100; d >= 10; d = d / 10)
{
val = rem / d; rem = rem % d;
if( val ) filling=0;
if( filling ) Putch( fill );
else Putch( val + '0' );
}
Putch( rem + '0');
break;
default: Puts(" <format not supported> ");
}
}
void binasci16(unsigned int a, unsigned char base, char fill)
{
unsigned int rem,val,d;
unsigned char filling=1;
switch(base)
{
case HEX: binasci8(a >> 8, HEX, fill);
binasci8(a, HEX, fill);
break;
case DEC: rem = a;
for( d=10000; d >= 10; d = d / 10)
{
val = rem / d; rem = rem % d;
if( val ) filling=0;
if( filling ) Putch( fill );
else Putch( val + '0' );
}
Putch( rem + '0');
break;
default: Puts(" <format not supported> ");
}
}
/*---------------------------------------------------------------------------
MAIN.C
/*---------------------------------------------------------------------------*/
void main(void)
{
InitIrqLevels();
__set_il(7); /* allow all levels */
__EI(); /* globaly enable interrupts */
clear_CPU_operation_detection ();
PDR4 = 0xff; /* all LEDs off */
DDR4 = 0xff;
/* initialize UART1 */
InitUart1();
clear_CPU_operation_detection();
Puts("\nWelcome to the ADC-Testprogram MB90425series\n");
Puts("=> Please press any key for next conversion <=\n\n");
clear_CPU_operation_detection();
Init_ADC(); /* initialize ADC */
ADCSH = 0x82; /* start conversion */
i = 0;
clear_CPU_operation_detection();
while(1)
{
clear_CPU_operation_detection();
// Analog_results[i&0x07] = GetADCResult();
// PDR4 = ~Analog_results[0] >> 2;
Puts("Analog channel #");
binasci4(i, DEC, ' ');
binasci16(GetADCResult(), DEC, ' ');
Puts("\n");
if (i++ == 7)
{
i = 0;
Puts("\n");
}
Getch();
ADCSH = 0x82; /* next conversion */
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -