📄 adc.c
字号:
#include <string.h>
#include "44b.h"
#include "44blib.h"
#include "adc.h"
#include "GUI.h"
#include "rtc.h"
void __irq BDMA1_Done(void);
int ReadAdc(int ch);
extern int Sec_Old,nsec;
void Draw_Line(void)
{
float Y_Pos=0;
int y=0,i=0;
GUI_AA_SetFactor(6);
GUI_SetColor(GUI_YELLOW);
GUI_AA_DrawLine(20,119,640,119) ;
GUI_AA_DrawLine(20,0,20,239) ;
GUI_SetColor(GUI_WHITE);
for (i=0;i<20;i++)
{
GUI_GotoXY(0,232-y);
y+=22;
GUI_DispFloat(Y_Pos,3);
Y_Pos+=0.3;
}
}
void Disp_AdcPicture(void)
{
int a0_Old,a1_Old,a2_Old,a0_New,a1_New,a2_New,a0_Y=0,a1_Y=0,a2_Y=0,X=20;
GUI_Clear();
Draw_Line();
GUI_SetFont(&GUI_FontHZ12);
GUI_DispStringAt("基于ARM和uCGUI的多通道数据采集系统,3通道10位AD转换,数据同时存储在FLASH 39VF160中",30,226);
while(1)
{
Read_Rtc();
if(Sec_Old!=nsec)
{
Sec_Old=nsec;
Disp_TimeAd();
a0_Old=a0_New;
a0_New=Get_AverageAd(0) ;
a1_Old=a1_New;
a1_New=Get_AverageAd(1) ;
a2_Old=a2_New;
a2_New=Get_AverageAd(2) ;
GUI_SetColor(GUI_RED);
GUI_AA_DrawLine(X,a0_Old,X+2,a0_New) ;
GUI_SetColor(GUI_GREEN);
GUI_AA_DrawLine(X,a1_Old,X+2,a1_New) ;
GUI_SetColor(GUI_BLUE);
GUI_AA_DrawLine(X,a2_Old,X+2,a2_New) ;
GUI_DispStringAt("a0= a1= a2= ",335,0);
GUI_SetColor(GUI_WHITE);
GUI_DispDecAt(a0_New,360,0,3);
GUI_DispDecAt(a1_New,420,0,3);
GUI_DispDecAt(a2_New,480,0,3);
//GUI_SetFont(GUI_FontHZ12);
//GUI_DispStringAt("
X+=2;
if(X>=639) {
X=20;
GUI_Clear();
Draw_Line();
}
}
}
}
//GUI_DrawPixel//
int Get_AverageAd(int ch)
{
int i=0,total=0;
for (i=0;i<10;i++)
{
total+=ReadAdc(ch);
}
total=(239-total/43);
return total;
}
void Test_Adc(void)
{
int a0=0,a1=0,a2=0,a3=0,a4=0,a5=0,a6=0,a7=0;
int adcpsr;
Uart_Printf("The ADC_IN are adjusted to the following values.\n");
Uart_Printf("Push any key to exit!!!\n");
rCLKCON=0x7ff8;
rADCCON=0x1|(0<<2); //Enable ADC
Delay(100); //delay for 10ms for ADC reference voltage stabilization.
//Uart_Printf("Input ADCPSR value:");
//adcpsr=Uart_GetIntNum();
adcpsr=255;
rADCPSR=adcpsr;
Uart_Printf("ADC conv. freq.=%d(Hz)\n",(int)(MCLK/(2.*(adcpsr+1.))/16.) );
while(Uart_GetKey()==0)
{
Delay(9000);
a0=ReadAdc(0);
a1=ReadAdc(1);
a2=ReadAdc(2);
//a3=ReadAdc(3);
//a4=ReadAdc(4);
//a5=ReadAdc(5);
//a6=ReadAdc(6);
//a7=ReadAdc(7);
Uart_Printf("0: %04d 1: %04d 2: %04d \n",/*3:%04d 4:%04d 5:%04d 6:%04d 7:%04d\n",*/
a0,a1,a2,a3,a4,a5,a6,a7);
}
}
int ReadAdc(int ch)
{
int i;
static int prevCh=-1;
if(prevCh!=ch)
{
rADCCON=0x0|(ch<<2); //setup channel.
for(i=0;i<150;i++); //min. 15us
}
rADCCON=0x1|(ch<<2); //Start A/D conversion
while(rADCCON &0x1); //To avoid The first FLAG error case.
//(The START bit is cleared in one ADC clock.)
while(!(rADCCON & 0x40));
for(i=0;i<rADCPSR;i++); //To avoid The second FLAG error case
prevCh=ch;
return rADCDAT;
}
volatile char end_test;
void Test_DMA_Adc(void)
{
unsigned int *dst, *temp,i;
end_test=0;
pISR_BDMA1=(unsigned)BDMA1_Done;
rINTMSK=~(BIT_GLOBAL|BIT_BDMA1);
Uart_Printf("Test of the 'Start by read' in ADC block.\n");
Uart_Printf("Change the value of AIN0\n");
// rCLKCON=0x7ff8;
dst=(unsigned int *)malloc(0x100);
rNCACHBE0= ((int)dst>>12) + ( (((int)dst>>12) +1)<<16 );
temp=dst;
/***ADC init***/
rADCCON=0x2; //normal,AIN0,enable start by read
rADCPSR=0x4; // MCLK/2
/***BDMA1 init***/
rBDISRC1=(2<<30)+(3<<28)+0x1d40008; //word,peri.(ADC)
rBDIDES1=(2<<30)+(1<<28)+(unsigned)dst; //IO2M,++
rBDICNT1=(1<<30)+(1<<26)+(3<<22)+(0<<20)+10*4;//timer,unit,end->interrupt,disable,count=10(word)
rBDICNT1 |= (1<<20);//enable
rBDCON1 = 0x0;
/***Timer0 init***/
rTCFG0=255; //prescaler0=255
rTCFG1=(1<<24)+4; //Timer0 DMA, div=32
rTCNTB0=4902; //(1/(40MHz/255/32))*4902=1.xx s
rTCON=0xa; //auto, update
rTCON=0x9; //Start
while(!end_test);
Uart_Printf("dst=0x%x,temp=0x%x\n",dst,temp);
for(i=0;i<10;i++)
Uart_Printf("%d=0x%03x\n",i,*temp++);
Uart_Printf("dst=0x%x,temp=0x%x\n",dst,temp);
free(dst);
rINTMSK=BIT_GLOBAL;
}
void __irq BDMA1_Done(void)
{
rI_ISPC=BIT_BDMA1; //clear pending bit
rTCON=0x0; //Stop
end_test=1; //set end flag
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -