以mb95100 的10位ADC为例,主要使用了UART 模块和AD转换模块,AD转换用来转换输入口的模拟信号,转换的数据传给UART 然后传输出去。
#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 };
LED 初始化
/*****************************************************************************/
/* UART UART 初始化及输入输出控制 */
/*****************************************************************************/
const char welcome[100]="Welcome at FUJITSU \n\n"; // 欢迎文本
const char volt[50]="the Voltage on Pin AN1 ist : ";
unsigned char ch;
void UART_init (void)
{
PSSR0 = 0x05; BRSR0 = 130; //波特率寄存器: 9600 Baud
SMC10 = 0x0C; // 8N1
SMC20 = 0x58; // 使UART, 复位UART, 没有IRQ 中断请求
SSR0 = 0x00; // 标志位清零
}
void UART_sendbyte (char ch)
{
while (!SSR0_TDRE);
TDR0 = ch;
} // UART 传输字
char UART_readbyte_wait (void)
{
while(!SSR0_RDRF); /等待直到数据接收
return (RDR0); // 返回接受的数据
} // UART 读取
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]);
}
} //UART 传输字符串
/*****************************************************************************/
/* ADC 初始化,将结果由16 进制转换为10 进制*/
/*****************************************************************************/
float faktor = 0.019608, calco; // faktor = 5V / 8 bit (255)
int ziff, x, memo, tmp;
void InitADC(void)
{
ADC2 = 0x89; // 8-bit 精度, 中断使能
ADC1 = 0x01; // AN0 脚输入
}
// subroutine to send the calculated voltage to the terminal and show on 7-Seg
void calc(void)
{
tmp= ADDL>> 2; // 低两位清除( 通常会摆动 )
if (tmp != memo >> 2) // 只当电压发生变化时,刷新结果
{
UART_sendstring(volt); // 传输数据
calco = faktor * ADDL; // 计算电压值
ziff = calco;
ch = ("%c" , '0' + ziff);
UART_sendbyte(ch);
PDR0 = seg_display[ziff]; // 用7 段LED 显示电压
ch = ("%c" , ',');
UART_sendbyte(ch);
for (x=0;x<3;x++) // 将每一位依次传输过去
{
calco =( calco * 10 )-( ziff * 10 );
ziff = calco;
ch = ("%c" , '0' + ziff);
UART_sendbyte(ch);
}
UART_sendstring(" V\r"); // 最后将V 标志传输过去
}
memo = ADDL; // 保持最后一个电压
}
/*****************************************************************************/
/*主程序部分*/
/*****************************************************************************/
void main(void)
{
// initialize I/O-ports
PDR0 = 0xff; // Port 0:
DDR0 = 0xff; // 7 段显示
PDR1 = 0x00; // Port 1:
DDR1 = 0xfe; // P10 = UI0 作为UART 输入
PDR2 = 0x00; // Port 2:
DDR2 = 0xff;
PDR3 = 0x00; // Port 3:
AIDRL = 0xfe; // P30 作为模拟信号输入
DDR3 = 0xfe; // P30 作为输入口
PDR4 = 0x00; // Port 4:
AIDRH = 0xff;
DDR4 = 0xff;
PDR5 = 0x00; // Port 5:
DDR5 = 0xff;
PDR6 = 0xff; // Port 6:
DDR6 = 0xff;
PDR7 = 0x00; // Port 7:
DDR7 = 0xff;
PDR8 = 0x00; // Port 8:
DDR8 = 0xff;
PDRE = 0x00; // Port E:
DDRE = 0xfc; // PE0=INT10, PE1=INT11 键位输入
InitIrqLevels();
__EI();
__set_il(3);
// 初始化UART
UART_init(); // 初始UART
UART_sendstring("\n");
UART_sendstring (welcome);
// initialize ADC
InitADC(); // 初始AD 转换
while(1) // 等待中断
__asm("\tnop");
}
__interrupt void ISR_ADC (void)
{
calc(); // 电压计算
ADC1 = 0x01; // 清空标志位
}


