Root/Software/sie_cg/templates/basic/jz_adc_peripheral.cpp

1/* ADC Peripheral.c
2
3Copyright (C) 2010
4
5This program is free software; you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
7the Free Software Foundation; either version 2 of the License, or
8(at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program; if not, write to the Free Software
17Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
18
19#include <stdio.h>
20#include <unistd.h>
21
22#include "jz_adc_peripheral.h"
23
24void ADCTest(JZ_REG * ADCBuffer)
25{
26if(ADCBuffer!=0)
27{
28    int aggregate=0; float errorT=0;
29    /******************************* TEST 1 ***********************************/
30    printf("\nINIT TEST1: Autoselft {(Vref+) - (Vref-)}/2 -> Return 0x0200 \n");
31    ADCConfig(ADCBuffer,ADC_CMD_SET_AUTOSELFT_1);
32    ADCConfig(ADCBuffer,ADC_CMD_READ_AUTOSELFT_1);
33    for(int i=ADC_BUFFER_OFFSET; i< ADC_BUFFER_LEN/2+ADC_BUFFER_OFFSET; i++)
34    {
35        aggregate += (ADCBuffer[i]&0xFFFF + ((ADCBuffer[i]>>16)&0x0FFF));
36        printf("[%08X]", ADCBuffer[i]);
37    }
38    errorT=(aggregate/16)*100/0x200;
39    if((errorT<95)||(errorT>105))
40        printf("\n**WARNING** Test FAILED.\n\n");
41    else
42        printf("\nTest OK\n\n");
43
44    fflush (stdout);
45}
46}
47
48void ADCPowerDown(JZ_REG * ADCBuffer)
49{
50    ADCConfig(ADCBuffer,ADC_CMD_SET_POWER_DOWN);
51}
52
53JZ_REG * ADCTakeSamples(JZ_REG * ADCBuffer,int CHANNEL)
54{
55    ADCConfig(ADCBuffer,ADC_CMD_SET_CHANNEL0+CHANNEL);
56    ADCConfig(ADCBuffer,ADC_CMD_READ_CHANNEL0+CHANNEL);
57    return (JZ_REG*)(ADCBuffer+ADC_BUFFER_OFFSET);
58}
59
60void ADCConfig(JZ_REG * ADCBuffer,uchar CMD)
61{
62    if(ADCBuffer!=0)
63    {
64        ADCBuffer[0] = (((ADC_MUX_CHANNELS<<6) + CMD)<<24) + \
65                ((ADC_BUFFER_LEN+ADC_BUFFER_OFFSET*2) << 8) + \
66                (ADC_SPI_CLKDIV);
67        while(ADCCheckBufferFull(ADCBuffer)) usleep (10);
68    }
69}
70
71int ADCCheckBufferFull(JZ_REG * ADCBuffer)
72{
73    if(ADCBuffer!=0)
74    {
75        return ADCBuffer[0]&0x20000000;
76    }
77    return 0;
78}
79

Archive Download this file

Branches:
master



interactive