打印

AD转换送LCD显示问题

[复制链接]
1306|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
tiercel|  楼主 | 2014-8-2 15:35 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 tiercel 于 2014-8-2 15:45 编辑

       这是ADC-to-LCD的一个程序,是psoc creator里面的示例程序。把AD转换的结果送LCD显示之前有一大堆对转换结果的处理,我看不太懂,但是我把那部分程序删除后,下载到板子上结果没变化,请哪位大神帮我看一下
         
这是原来的程序:红色部分是看不懂的那部分
#include <device.h>
#include "stdio.h"
#include "math.h"

/* Number of samples to be taken before averaging the ADCvalue */
#define MAX_SAMPLE                 ((uint8)128)

/* Threshold value to reset the filter for sharp changein signal */
#define SIGNAL_SLOPE               1000

/* Number of shifts for calculating the sum and averageof MAX_SAMPLE */
#define DIV                        7

int main(void)
{
    uint8 i;
   
    /* Array tostore ADC count for moving average filter */
    int32 adcCounts[MAX_SAMPLE] = {0};
   
    /* Variable to hold ADCconversion result */
    int32 result = 0;
   
    /* Variable to store accumulatedsample for filter array */
    int32 sum = 0;
   
    /* Variable for testing sharpchange in signal slope */
    int16 diff = 0;
   
    /* Variable to hold the result in microvolts converted from filtered
     * ADC counts */
    int32 microVolts = 0;
      
    /* Variable to hold the movingaverage filtered value */
    int32 averageCounts = 0;
      
    /* Index variable to work on the filterarray */
    uint8 index = 0;
   
    /* Characterarray to hold the micro volts*/
    char displayStr[15] = {'\0'};      

    CYGlobalIntEnable;

    /* Start ADC and start conversion */
    ADC_Start();
    ADC_StartConvert();

    /* Start LCD and set position */
    LCD_Start();
    LCD_Position(0,0);
    LCD_PrintString("ADC inputvolt");

    /* Print μV unit on the LCD */
    LCD_Position(1,8);
    LCD_WriteData(0xE4);
    LCD_PutChar('V');
   
    /* Read one sample from the ADCand initialize the filter */
    ADC_IsEndConversion(ADC_WAIT_FOR_RESULT);
    result = ADC_GetResult32();
   
    for(i = 0; i < MAX_SAMPLE;i++)
    {
        adcCounts = result;
    }
   
    /* Store sum of128 samples*/
    sum = result << DIV;
   
    /* Average countis equal to one single sample for first ADC reading */
    averageCounts = result;
   
   
    while(1)
    {
       ADC_IsEndConversion(ADC_WAIT_FOR_RESULT);
        result = ADC_GetResult32();
        
        diff = abs(averageCounts -result);
        /* Ifsharp change in the signal then reset the filter with the new
         * signal value */
        if(diff > SIGNAL_SLOPE)
        {
            for(i = 0; i< MAX_SAMPLE; i++)
            {
               adcCounts = result;
            }
            
            /*Store sum of 128 samples*/
            sum = result<< DIV;
   
            /* Averagecount is equal to new sample */
            averageCounts =result;
            index = 0;
        }
        
        /* Get moving average */
        else
        {
            /* Remove theoldest element and add new sample to sum and get
             * theaverage */
            sum = sum -adcCounts[index];
            sum = sum +result;
            averageCounts =sum >> DIV;
            
            /* Removethe oldest sample and store new sample */
           adcCounts[index] = result;
            index++;
            if (index ==MAX_SAMPLE)
            {
               index = 0;
            }
        }
        microVolts =ADC_CountsTo_uVolts(averageCounts);
            
        /*Convert micro volts to string and display on the LCD */
       sprintf(displayStr,"%7ld",microVolts);
        LCD_Position(1,0);
        LCD_PrintString(displayStr);
    }
}
这是修改后的程序
#include <device.h>
#include "stdio.h"
void main()
{      
    /* Variable to hold ADC conversionresult */
    int32 result = 0;
   
    /* Variable to hold the result in microvolts converted from filtered
     * ADC counts */
    int32 microVolts = 0;
      
    /* Character array to hold the microvolts*/
    char displayStr[15] = {'\0'};      
    /* Start ADC and startconversion */
    ADC_Start();
    ADC_StartConvert();
    /* Start LCD andset position */
    LCD_Start();
    LCD_Position(0,0);
    LCD_PrintString("ADC inputvolt");
    /* Print μV unit on the LCD */
    LCD_Position(1,8);
    LCD_WriteData(0xE4);
    LCD_PutChar('V');
   
    /* Read one sample from the ADC andinitialize the filter */
    ADC_IsEndConversion(ADC_WAIT_FOR_RESULT);
    result = ADC_GetResult32();
   
        microVolts =ADC_CountsTo_uVolts(result);
          sprintf(displayStr,"%7ld",microVolts);
       LCD_Position(1,0);
       LCD_PrintString(displayStr);
        
}
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

2

主题

4

帖子

0

粉丝