




1、设置输入口DDR1
2、设定中断级别ILR4
3、使能AD输入ADC2:ANS0--ANS3
4、设定采样时间ADC2:TIM1--TIM0
5、选择时钟ADC2:CKDIV1 CKDIV0
6、设定AD转换精度ADC2:AD8
7、选择操作模式ADC2:EXT
8、选择启动触发ADC2:ADCK
9、使能中断ADC2:ADIE=1
10、激活AD转换器ADC1:AD=1
中断处理:
1、ADC1:AD1=0
2、读取转换值ADDH、ADDL
3、激活AD转换器ADC1:AD=1

程序1:查询方式::::::::::::::::::
LIN_UART_Syn.rar
#include "mb95200.h"
int ADVoltage=0x01;
void UART_chars(unsigned char cdata);
void UART_init (void)
{
BGR1 = 0x00; // Reloadvalue = 0d51 = 0x33 (500KHz, 9600Baud)
BGR0 = 0x33; // Reloadvalue = 0x33 = 0x00<<8 + 0x33 (500KHz, 9600Baud)
SMR = 0x87; // enable SOT, Reset, synchronous mode
SSR = 0x00; // clear flags, no interrupt
ECCR_SSM = 1; // have ST/STP bit
SCR = 0xc3; // enable transmit, Odd parity
}
char UART_readbyte (void)
{
while (! SSR_RDRF); // wait, until a byte is received
return (RDR_TDR); // return the received byte
}
void delay(unsigned int i)
{
unsigned int m,n;
for(m=0;m<i;m++)
{
for(n=0;n<6000;n++)
{
asm("\tNOP");
}
}
}
void UART_sendbyte (char ch)
{
while (! SSR_TDRE);
RDR_TDR = ch; //transmit data
}
void UARTPut(const char *data,unsigned char len)
{
unsigned char i;
for(i=0; i<len; i++) UART_sendbyte(data[i]);
}
void UART_chars(unsigned char cdata)
{
unsigned char i;
i=cdata>>4;
if(i>=0 && i<=9) UART_sendbyte(i+0x30);
else UART_sendbyte(i+0x37);
i=cdata & 0x0F;
if(i>=0 && i<=9) UART_sendbyte(i+0x30);
else UART_sendbyte(i+0x37);
}
void InitADC(void)
{
ADC2 = 0x81; // 8-bit resolution, enable interrupts
ADC1 = 0x01; // AN0 pin as input, start AD
}
void AD_Sample (void)
{
ADVoltage=ADD_ADDL;
ADC1=0x01;
//ADC2 = 0x81;
}
/******************************************************************************
NAME : main ()
FUNCTION: synchronously transmit a byte or receive a byte
******************************************************************************/
void main (void)
{
//unsigned char rec_data = 0;
DDR0_P00 = 0; //P00 intput
//DDR0_P05 = 1; //P05 output
//DDR0_P06 = 0; //P06 input
DDR0_P03 = 1; //P03 output
DDR0_P04 = 0; //P04 input
AIDRL = 0xfe; //Port input enabled
PDR0_P00 = 0x00;
//PDR0_P05 = 0x01;
UART_init ();
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
InitADC(); // init AD - converte
while (1)
{
asm("\tNOP");
//UART_sendbyte(0xaa);
delay(10);
UARTPut("ADVal:",7);
//DDR0_P00 = 0;
//delay(1);
AD_Sample();
UART_chars(ADVoltage);
UARTPut("\n",2);
//rec_data = UART_readbyte ();
asm("\tNOP");
asm("\tNOP");
}
}


AD的引脚接到地线时:采集的电压是:(00~=0V)

AD引脚悬空的采集电压是:(96~=2.94V)

程序2:中断方式::::::::::::::::::
#include "mb95200.h"
int ADVoltage=0x01;
void UART_chars(unsigned char cdata);
void UART_init (void)
{
BGR1 = 0x00; // Reloadvalue = 0d51 = 0x33 (500KHz, 9600Baud)
BGR0 = 0x33; // Reloadvalue = 0x33 = 0x00<<8 + 0x33 (500KHz, 9600Baud)
SMR = 0x87; // enable SOT, Reset, synchronous mode
SSR = 0x00; // clear flags, no interrupt
ECCR_SSM = 1; // have ST/STP bit
SCR = 0xc3; // enable transmit, Odd parity
}
char UART_readbyte (void)
{
while (! SSR_RDRF); // wait, until a byte is received
return (RDR_TDR); // return the received byte
}
void delay(unsigned int i)
{
unsigned int m,n;
for(m=0;m<i;m++)
{
for(n=0;n<6000;n++)
{
asm("\tNOP");
}
}
}
void UART_sendbyte (char ch)
{
while (! SSR_TDRE);
RDR_TDR = ch; //transmit data
}
void UARTPut(const char *data,unsigned char len)
{
unsigned char i;
for(i=0; i<len; i++) UART_sendbyte(data[i]);
}
void UART_chars(unsigned char cdata)
{
unsigned char i;
i=cdata>>4;
if(i>=0 && i<=9) UART_sendbyte(i+0x30);
else UART_sendbyte(i+0x37);
i=cdata & 0x0F;
if(i>=0 && i<=9) UART_sendbyte(i+0x30);
else UART_sendbyte(i+0x37);
}
void InitADC(void)
{
ADC2 = 0x81; // 8-bit resolution, enable interrupts
ADC1 = 0x01; // AN0 pin as input, start AD
}
__interrupt void ISR_ADC (void)
{
ADVoltage=ADD_ADDL; // voltage calculating
ADC1=0x01; // clear interrupt flag, start AD again
}
/******************************************************************************
NAME : main ()
FUNCTION: synchronously transmit a byte or receive a byte
******************************************************************************/
void main (void)
{
//unsigned char rec_data = 0;
DDR0_P00 = 0; //P00 intput
//DDR0_P05 = 1; //P05 output
//DDR0_P06 = 0; //P06 input
DDR0_P03 = 1; //P03 output
DDR0_P04 = 0; //P04 input
AIDRL = 0xfe; //Port input enabled
PDR0_P00 = 0x00;
//PDR0_P05 = 0x01;
UART_init ();
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
InitADC(); // init AD - converte
while (1)
{
asm("\tNOP");
//UART_sendbyte(0xaa);
delay(10);
UARTPut("ADVal:",7);
//DDR0_P00 = 0;
//delay(1);
UART_chars(ADVoltage);
UARTPut("\n",2);
asm("\tNOP");
asm("\tNOP");
}
}
1、ILR4 = 0xDF;
。。。。。。。
2、
__interrupt void DefaultIRQHandler (void);
__interrupt void ISR_ADC (void);
。。。。。。。。。。。。。。。。。。
3、
修改:#pragma intvect ISR_ADC 18


中断请求号 中断级设置寄存器 中断源
IRQ0 L00【1:0】 外部中断ch4
IRQ1 L01【1:0】 外部中断ch5
IRQ2 L02【1:0】 外部中断ch2、ch6
IRQ3 L03【1:0】 外部中断ch7
外部中断电路0注意事项:
1、边缘极性选择位SL值位过程中,使中断请求位EIE清0(禁止中断请求),设定边缘极性后,外部中断请求标志位EIR清0
2、若外部中断请求标志位置1,且使能中断中断请求使能位,操作不能从中断服务例程中回复,必须始终清理中断服务例程中的外部中断请求使能位
回复
打赏帖 | |
---|---|
【Cortex-M】Systick Timer使用被打赏10分 | |
分享汽车防盗系统的组成与分类(一)被打赏5分 | |
VOFA+波形显示+JYD-31蓝牙发送和解析不定长数据被打赏10分 | |
宏定义和const关键字定义被打赏5分 | |
换取逻辑分析仪】STM32G4从入门到大师之五:ADC中断方式单路采集电压被打赏16分 | |
【换取逻辑分析仪】STM32G4从入门到大师之四:ADC查询方式单路采集电压被打赏14分 | |
【分享开发笔记,赚取电动螺丝刀】几个单片机I2S外设的BLCK时钟对比被打赏20分 | |
【功率监测与控制系统DIY活动成果贴】DIY功率计与LabVIEW数据采集被打赏100分 | |
【分享开发笔记,赚取电动螺丝刀】使用ESP32S3调试I2S音频模块MAX98357被打赏22分 | |
【Freertos】任务管理被打赏10分 |