📄 adcseries.c
字号:
/******************************************************************************
* *
* ********** *
* ************ *
* *** *** *
* *** ++ *** *
* *** + + *** CHIPCON *
* *** + *
* *** + + *** *
* *** ++ *** *
* *** *** *
* ************ *
* ********** *
* *
*******************************************************************************
Filename: adcSeries.c
Target: cc2430
Author: EFU
Revised: 30/5-2007
Revision: 1.2
Description:
This example prints the pot-meter voltage versus time on the LCD.
The sampling instant is set by timer 1 and the converted data is transferred
using DMA. The data is then converted to be printed on the LCD.
******************************************************************************/
/******************************************************************************
* INCLUDES
*/
#include "app_ex.h"
#include <string.h>
/******************************************************************************
* CONSTANTS
*/
#define NUMBER_OF_SAMPLES 40
#define NUMBER_OF_SYMBOLS 16
/******************************************************************************
* TYPEDEFS
*/
typedef struct
{
char symbol[8];
} SYMBOL;
/******************************************************************************
* GLOBAL VARIABLES
*/
/******************************************************************************
* LOCAL VARIABLES
*/
/******************************************************************************
* LOCAL FUNCTIONS
*/
void initAdc(void);
void adc_main(void);
void updateVoltageLCD(int8 potVoltage, int8 adc_value);
void updateJoystickDirectionLCD(JOYSTICK_DIRECTION direction);
void updateCounter(int8 delay);
int8 scaleValue(int8 adc_value);
void shiftValues(int8* values, byte length);
void graphElement(int8 values[], SYMBOL *height, byte length);
void adc_series_main(void);
/******************************************************************************
* @fn adc_series_main
*
* @brief
* Main function for ADC series conversion application example
*
* Parameters:
*
* @param void
*
* @return void
*
******************************************************************************/
#ifdef COMPLETE_APPLICATION
void adc_series_main(void){
#else
void main(void){
#endif
DMA_DESC dmaAdc;
SYMBOL graphHeight[NUMBER_OF_SYMBOLS];
int8 adcValues[NUMBER_OF_SAMPLES];
byte i, k;
char lcdData[2][16];
INIT_GLED();
INIT_YLED();
initLcd();
if( CHVER < REV_C )
{
// This application will not run on revision B, please refer
// to errata note for CC2430 rev. B. document rev. 1.2
// Errate number #19 (ADCCON2 and ADCCON3 can not be used at the sime time)
lcdUpdate((char*)"Not for", "revision B");
while( !stopApplication() );
return;
}
DISABLE_ALL_INTERRUPTS();
lcdUpdate((char*)"Turn Potmeter",(char*)"");
halWait(200);
halWait(200);
halWait(200);
memset(graphHeight, 0, sizeof(graphHeight));
memset(adcValues, 0, sizeof(adcValues));
strcpy(lcdData[0], (char*)"3.3V ");
strcpy(lcdData[1], (char*)"0.0V_");
lcdUpdate(lcdData[0], lcdData[1]);
lcdUpdateSymbol(LINE1, 4, 0x46);
SET_MAIN_CLOCK_SOURCE(CRYSTAL);
// Setting up the DMA
SET_WORD(dmaAdc.SRCADDRH, dmaAdc.SRCADDRL, &X_ADCH);
SET_WORD(dmaAdc.DESTADDRH, dmaAdc.DESTADDRL, &adcValues);
SET_WORD(dmaAdc.LENH, dmaAdc.LENL, 1);
dmaAdc.VLEN = VLEN_USE_LEN;
dmaAdc.PRIORITY = PRI_HIGH;
dmaAdc.M8 = M8_USE_8_BITS;
dmaAdc.IRQMASK = FALSE;
dmaAdc.DESTINC = DESTINC_0;
dmaAdc.SRCINC = SRCINC_0;
dmaAdc.TRIG = DMATRIG_ADC_CH7;
dmaAdc.TMODE = TMODE_SINGLE_REPEATED;
dmaAdc.WORDSIZE = WORDSIZE_BYTE;
// Setting the descriptor pointer and arming the DMA. Using DMA channel 0
DMA_SET_ADDR_DESC0(&dmaAdc);
DMA_ABORT_CHANNEL(0);
DMA_ARM_CHANNEL(0);
// Setting up timer 1 to generate sampling commands to the ADC.
TIMER1_INIT();
halSetTimer1Period(40000);
T1CCTL0 = 0x24;
// Setting up the ADC to sample from channel 7 (pin P0.7 pot meter)
ADC_ENABLE_CHANNEL(7);
ADC_SEQUENCE_SETUP(ADC_REF_AVDD | ADC_14_BIT | ADC_AIN7);
ADC_TRIGGER_FROM_TIMER1();
// Starting timer 1
TIMER1_RUN(TRUE);
while( !stopApplication() )
{
// Checking if a DMA transfer has been performed.
if(DMAIRQ & DMA_CHANNEL_0)
{
DMAIRQ = ~DMA_CHANNEL_0;
// Converting the new sample to "LCD-format"
graphElement((int8*)adcValues, graphHeight, NUMBER_OF_SYMBOLS/2);
// Shifting the converted values
shiftValues((int8*)adcValues, NUMBER_OF_SAMPLES);
//init new symbols
for(i = 0; i < NUMBER_OF_SYMBOLS-1; i++)
{
initNewSymbol( (char*)&graphHeight[i], CHAR1_ADDRESS + (i*0x08));
}
// Updating the LCD
for(i = 11, k = 1; i > 4; i--, k += 2)
{
lcdUpdateChar(LINE1, i, k);
lcdUpdateChar(LINE2, i, k+1);
}
}
}
// Stopping the conversions
ADC_STOP();
DMA_ABORT_CHANNEL(0);
TIMER1_RUN(FALSE);
ADC_SEQUENCE_SETUP(0x00);
}
/******************************************************************************
* @fn shiftValues
*
* @brief
* Right-shift of int8 vectors
*
* Parameters:
*
* @param int8* values
*
* byte length
*
* @return void
*
******************************************************************************/
void shiftValues(int8* values, byte length)
{
length--;
while(length)
{
values[length] = values[length-1];
length--;
}
}
/******************************************************************************
* @fn graphElement
*
* @brief
* Processing new graph elements
*
* Parameters:
*
* @param int8 values[]
* SYMBOL *height
* byte length
*
* @return void
*
******************************************************************************/
void graphElement(int8 values[], SYMBOL *height, byte length)
{
int8 i;
byte j;
byte k;
int8 temp = 0;
for(k = 0; k < length; k++)
{
// resetting all entries
for(i = 0; i < 8; i++)
{
height[2*k].symbol[i] = 0;
height[2*k+1].symbol[i] = 0;
}
// processing values in blocks of 5
for(j = 0; j < 5; j++)
{
if(values[j+5*k] > 0)
{
temp = (values[j+5*k] >> 3);
}
else
{
temp = 0;
}
i = 7;
while((temp > 0) && (i >= 0))
{
height[2*k+1].symbol[i] |= (1 << j);
i--;
temp -= 1;
}
i = 7;
while((temp > 0) && (i >= 0))
{
height[2*k].symbol[i] |= (1 << j);
i--;
temp -= 1;
}
}
}
}
/******************************************************************************
* @fn adc_series_init
*
* @brief
* Initializes ADC application example.
*
* Parameters:
*
* @param APPLICATION *a
* Main application
*
* @return void
*
******************************************************************************/
#ifdef COMPLETE_APPLICATION
void adc_series_init(APPLICATION *a){
a->menuText = (char*)"ADC Series";
a->description = (char*)"T1 and DMA";
a->main_func = adc_series_main;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -