各位大虾,请帮忙看看。程序通过PB和PC口选择模拟开关,每次开一路,在ADC1_0上采集。
程序运行中,在断点单步调试过程中,在watch观看nAdcResult[i]数据时,0~19路转换数据都正常;
但在全速运行时,通过串口调试助手查看数据时,仅第16~19数据就是错误的。
请大家帮忙看看。
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "stm8l15x.h"
#include "stdio.h"
#define STRINGVECTOR(x) #x
#define VECTOR_ID(x) STRINGVECTOR( vector = (x) )
#define INTERRUPT_HANDLER( a, b ) \
_Pragma( VECTOR_ID( (b)+2 ) ) \
__interrupt void (a)( void )
int g_nTim4Flag = 0;
int g_ArrMux[20] = {0x0008,0x0009,0x000A,0x000B,//模拟开关通道参数设置,
0x000C,0x000D,0x000E,0x000F,//PB3~PB0对应0~7通道,0X0080表示0通道
0x0080,0x0090,0x00A0,0x00B0,//PB7~PB4对应8~16通道,0X0080表示8通道
0x00C0,0x00D0,0x00E0,0x00F0,//PC3~PC0对应17~20通道,0x0800表示17通道
0x0800,0x0900,0x0A00,0x0B00};//
void SelectChannel(int i)
{
GPIOB->ODR = (unsigned char)(g_ArrMux[i] & 0x00FF);//PB设端口置
GPIOC->ODR = (unsigned char)(g_ArrMux[i] >> 8); //PC端口设置
}
void ExchangeLED()
{
GPIOD->ODR = ~GPIOD->ODR;//LED 灯点亮
}
void Uart2_Int(void)//UART2初始化
{
USART2->CR1=0x00;
USART2->CR2=0x2c;
USART2->CR3=0x00;
USART2->BRR2=0x00;
USART2->BRR1=0X24;
}
void Uart3_Int(void)//UART3初始化
{
USART3->CR1=0x00;
USART3->CR2=0x2c;
USART3->CR3=0x00;
USART3->BRR2=0x00;
USART3->BRR1=0x24;
}
void putch(unsigned char ch)//UART3、UART2发送相同数据
{
while(((USART2->SR&0x40)==0) || ((USART3->SR&0x40)==0));
USART2->DR=ch;
USART3->DR=ch;
while(((USART2->SR&0x80)==0) || ((USART2->SR&0x80)==0));
}
void delay(unsigned int t)
{
while(t--);
}
void Init_GPIO()//端口设置
{
//Led
GPIOD->DDR=0XFF;
GPIOD->CR1=0XFF;
GPIOD->CR2=0X00;
//Usart
GPIOE->DDR=0X00;
GPIOE->CR1=0XFF;
GPIOE->CR2=0X00;
//Mux
GPIOB->DDR=0xFF;
GPIOB->CR1=0xFF;
GPIOB->CR2=0xFF;
GPIOC->DDR=0xFF;
GPIOC->CR1=0xFF;
GPIOC->CR2=0xFF;
}
void Init_CLK()
{
CLK->ECKCR = 0X21;
while(0x02 != (CLK->ECKCR & 0x02));
CLK->SWCR = CLK_SWCR_SWEN; //Enable switch clk
CLK->SWR = 0x04; //Select external osc
CLK->CKDIVR = 0x01; //Div by 2
while(0x04 != (CLK->SCSR));
CLK->SWCR=0;
CLK->ICKCR =0;
CLK->PCKENR1 = 0X04; //Enable TIM4
CLK->PCKENR2 = 0X01; //Enable ADC
CLK->PCKENR3 = 0X18; //Enable USART2 & USART3
}
void InitTimer()
{
TIM4->CNTR = 0;
TIM4->PSCR = 0x0B;
TIM4->ARR = 135;
TIM4->EGR = 0x01;
TIM4->IER = 0x00;
TIM4->IER = 0x01;
TIM4->CR1 = 0x11;
}
void UasrtSend(unsigned int *nAdcResult )//发送
{
int i;
unsigned int nCheckSum = 0;
unsigned char tempH = 0,tempL = 0;
putch(0x55);
putch(0xAA);
for(i = 0;i < 20;i++)
{
tempL = (unsigned char)(nAdcResult[i] & 0x00ff);
tempH = (unsigned char)(nAdcResult[i] >> 8);
putch(tempL);
putch(tempH);
nCheckSum += tempL;
nCheckSum += tempH;
}
putch((unsigned char)nCheckSum);
//putch3((unsigned char)nCheckSum);
}
int main( void )
{
int i=0;
unsigned int nAdcResult[20] = {0};
Init_CLK();
Init_GPIO();
// Init_ADC();
ADC_Init (ADC1,ADC_ConversionMode_Single,ADC_Resolution_12Bit,ADC_Prescaler_1);
ADC_Cmd(ADC1,ENABLE);
ADC_ChannelCmd (ADC1,ADC_Channel_0,ENABLE);
Uart2_Int();
Uart3_Int();
InitTimer();
//enableInterrupts();
while(1)
{
ExchangeLED();
for(i = 0;i < 20;i++)
{
SelectChannel(i);
// delay(50);
ADC_SoftwareStartConv (ADC1);
while(!ADC_GetFlagStatus (ADC1,ADC_FLAG_EOC));
ADC_ClearFlag (ADC1,ADC_FLAG_EOC);
nAdcResult[i]=ADC_GetConversionValue (ADC1);
}
//ExchangeLED();
while(TIM4->SR1 == 0);
TIM4->SR1 = 0;
UasrtSend(nAdcResult);
}
}
INTERRUPT_HANDLER(USART2_RECV_IRQHandler,20)
{
//putch3(getch2());
}
INTERRUPT_HANDLER(USART3_RECV_IRQHandler,22)
{
//putch2(getch3());
}
INTERRUPT_HANDLER(TIM4_OVF_IRQHandler,23)
{
//putch2(getch3());
g_nTim4Flag = 1;
TIM4->SR1 = 0;
}
|