📄 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 "mb95100.h"
#define SEG_A 0xFE
#define SEG_B 0xFD
#define SEG_C 0xFB
#define SEG_D 0xF7
#define SEG_E 0xEF
#define SEG_F 0xDF
#define SEG_G 0xBF
#define SEG_DP 0x7F
#define SEG_0 SEG_A & SEG_B & SEG_C & SEG_D & SEG_E & SEG_F
#define SEG_1 SEG_B & SEG_C
#define SEG_2 SEG_A & SEG_B & SEG_D & SEG_E & SEG_G
#define SEG_3 SEG_A & SEG_B & SEG_C & SEG_D & SEG_G
#define SEG_4 SEG_B & SEG_C & SEG_F & SEG_G
#define SEG_5 SEG_A & SEG_C & SEG_D & SEG_F & SEG_G
#define SEG_6 SEG_A & SEG_C & SEG_D & SEG_E & SEG_F & SEG_G
#define SEG_7 SEG_A & SEG_B & SEG_C
#define SEG_8 SEG_A & SEG_B & SEG_C & SEG_D & SEG_E & SEG_F & SEG_G
#define SEG_9 SEG_A & SEG_B & SEG_C & SEG_D & SEG_F & SEG_G
const unsigned char seg_display[12] = { SEG_0, SEG_1, SEG_2, SEG_3, SEG_4,
SEG_5, SEG_6, SEG_7, SEG_8, SEG_9,
0xff, 0x00 };
/*****************************************************************************/
/* UART */
/*****************************************************************************/
const char welcome[100]="Welcome at FUJITSU \n\n"; // welcome text
const char volt[50]="the Voltage on Pin AN1 ist : ";
unsigned char ch; // to store a char in
void UART_init (void)
{
PSSR0 = 0x05; BRSR0 = 130; // Baudrate Generator: 9600 Baud
SMC10 = 0x0C; // 8N1
SMC20 = 0x58; // enable UART, reset UART, no IRQ
SSR0 = 0x00; // clear flags
}
void UART_sendbyte (char ch)
{
while (!SSR0_TDRE);
TDR0 = ch;
}
char UART_readbyte_wait (void)
{
while(!SSR0_RDRF); // wait, until byte is received
return (RDR0); // return received byte
}
void UART_sendstring (const char *string)
{
unsigned int i;
for (i=0; i<strlen(string); i++)
{
if (string[i] == 10)
UART_sendbyte(13);
UART_sendbyte(string[i]);
}
}
/*****************************************************************************/
/* ADC */
/*****************************************************************************/
float faktor = 0.019608, calco; // faktor = 5V / 8 bit (255)
int ziff, x, memo, tmp;
void InitADC(void)
{
ADC2 = 0x89; // 8-bit resolution, enable interrupts
ADC1 = 0x01; // AN0 pin as input
}
// subroutine to send the calculated voltage to the terminal and show on 7-Seg
void calc(void)
{
tmp = ADDL >> 2; // clear lowest two bits ( always toggeling )
if (tmp != memo >> 2) // refresh terminal only if voltage changes
{
UART_sendstring(volt); // text to terminal
calco = faktor * ADDL; // calculate voltage
ziff = calco; // 1. number to terminal
ch = ("%c" , '0' + ziff);
UART_sendbyte(ch);
PDR0 = seg_display[ziff]; // show full voltage value on 7-Seg display
ch = ("%c" , ',');
UART_sendbyte(ch);
for (x=0;x<3;x++) // send another x numbers to terminal
{
calco =( calco * 10 )-( ziff * 10 );
ziff = calco;
ch = ("%c" , '0' + ziff);
UART_sendbyte(ch);
}
UART_sendstring(" V\r"); // unit "V" to terminal
}
memo = ADDL; // keep in mind last voltage
}
/*****************************************************************************/
/* Main Routine */
/*****************************************************************************/
void main(void)
{
// initialize I/O-ports
PDR0 = 0xff; // Port 0:
DDR0 = 0xff; // 7-Segment display (all segments off)
PDR1 = 0x00; // Port 1:
DDR1 = 0xfe; // P10 = UI0 = Input; other pins are set to output 'L'
PDR2 = 0x00; // Port 2:
DDR2 = 0xff; // unused - set all pins to output 'L'
PDR3 = 0x00; // Port 3:
AIDRL = 0xfe; // used as I/O-port (no analog inputs), P30 as AN-input
DDR3 = 0xfe; // set all pins to output 'L' , P30 as input
PDR4 = 0x00; // Port 4:
AIDRH = 0xff; // used as I/O-port (no analog inputs)
DDR4 = 0xff; // unused - set all pins to output 'L'
PDR5 = 0x00; // Port 5:
DDR5 = 0xff; // unused - set all pins to output 'L'
PDR6 = 0xff; // Port 6:
DDR6 = 0xff; // unused - set all pins to output 'L'
PDR7 = 0x00; // Port 7:
DDR7 = 0xff; // unused - set all pins to output 'L'
PDR8 = 0x00; // Port 8:
DDR8 = 0xff; // unused - set all pins to output 'L'
PDRE = 0x00; // Port E:
DDRE = 0xfc; // PE0=INT10, PE1=INT11 keybutton inputs; other pins set to output 'L'
InitIrqLevels(); // initialise Interrupt level register and IRQ vector table
__EI(); // global interrupt enable
__set_il(3); // set global interrupt mask to allow all IRQ levels
// initialize UART
UART_init(); // init UART
UART_sendstring("\n");
UART_sendstring (welcome); // welcome text
// initialize ADC
InitADC(); // init AD - converter
while(1) // waiting for interrupt ( no operation )
__asm("\tnop");
}
__interrupt void ISR_ADC (void)
{
calc(); // voltage calculating
ADC1 = 0x01; // clear interrupt flag
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -