Author Topic: Ask Sending ADC data to PC, via serial  (Read 1365 times)

0 Members and 1 Guest are viewing this topic.

Offline hendrachubbyTopic starter

  • Full Member
  • ***
  • Posts: 49
  • Helpful? 0
Ask Sending ADC data to PC, via serial
« on: June 24, 2009, 09:40:14 AM »
Hello, I want to ask about C code, i'm trying to send adc data via serial communication..but i'm stuck when i try to view the value in my PC...I'm using atmega640 uart1..here is my code :

Code: [Select]

#include <mega640.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <delay.h>

#define MAX 0xFF
#define RXB8 1
#define TXB8 0
#define UPE 2
#define OVR 3
#define FE 4
#define UDRE 5
#define RXC 7
#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<OVR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)
#define ADC_VREF_TYPE 0x20

unsigned char read_adc(unsigned char adc_input)
{
    ADMUX=(adc_input & 0x07) | (ADC_VREF_TYPE & 0xff);
    if (adc_input & 0x08) ADCSRB |= 0x08;
    else ADCSRB &= 0xf7;
    // Delay needed for the stabilization of the ADC input voltage
    delay_us(10);
    // Start the AD conversion
    ADCSRA|=0x40;
    // Wait for the AD conversion to complete
    while ((ADCSRA & 0x10)==0);
    ADCSRA|=0x10;
    return ADCH;
}

#pragma used+
char getchar1(void)
{
    char status,data;
    while (1)
    {
        while (((status=UCSR1A) & RX_COMPLETE)==0);
        data=UDR1;
        if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
        return data;
    };
}
#pragma used-


#pragma used+
void putchar1(char c)
{
    while ((UCSR1A & DATA_REGISTER_EMPTY)==0);
    UDR1=c;
}
#pragma used-


void printf1(char flash *fmtstr,...)
{
    while ((*fmtstr != '\0'))
    {
      while ((UCSR1A & DATA_REGISTER_EMPTY)==0);
      UDR1 = *fmtstr;
      fmtstr++;
    }   
}

/*Setting Port*/
void set_ports()

    //Port Analog
    DDRF    = 0b00000000;   
    PORTF   = 0b00000000;   
   
    DDRD = 0b11111011;
   
    }

void set_usart1()
{
    UCSR1A=0x00;
    UCSR1B=0x18;
    UCSR1C=0x06;
    UBRR1H=0x00;
    UBRR1L=0x08;
}

void set_ADC()
{
    DIDR0=0x00;
    DIDR2=0x00;
    ADMUX=ADC_VREF_TYPE & 0xff;
    ADCSRA=0xA4;
    ADCSRB&=0xF8;
}


void scan_sensor()
{
    printf1("\rsensor value\n");
    read_adc(0);
    printf1("\radc0 : %c \n", ADCH);
    printf1("\rdone\n");
}

           
void main()
{
    set_ports();
    set_ADC();
    set_usart1();
    scan_sensor();       
}




I just see this on my terminal :

sensor value
adc0 : %c
done

can anybody help me solve this problem ?

Offline Finnik

  • Full Member
  • ***
  • Posts: 59
  • Helpful? 4
Re: Ask Sending ADC data to PC, via serial
« Reply #1 on: June 24, 2009, 12:35:31 PM »
One problem could be that you use the "%c" format specifier, which is for characters. Try using the one for the correct data-type, such as "%d" for signed integers.

Another problem I see is that the variable "ADCH" in which you store the information from the ADC, is not defined anywhere. You should define any variable before you use it.

Correct me if I'm wrong though, I'm not an expert on C programming.
Think outside the box... inside is to crowded.

Offline tamamontu

  • Beginner
  • *
  • Posts: 3
  • Helpful? 0
Re: Ask Sending ADC data to PC, via serial
« Reply #2 on: June 24, 2009, 09:46:00 PM »
printf1("\radc0 : %c \n", ADCH);


as per your functon read_adc() it returns an unsigned  char.

you are calling the function but not storing the result  and in prinft1 you are directly using ADCH. If it is not defined as volatile the complier would see this as a constant value and would replace it with a constant value.

try
printf1("\radc0 : %c \n", read_adc(0));


or define ADCH as volatile and then use it. declaring this ways the compiler would not optimse it.

Offline hendrachubbyTopic starter

  • Full Member
  • ***
  • Posts: 49
  • Helpful? 0
Re: Ask Sending ADC data to PC, via serial
« Reply #3 on: June 25, 2009, 02:50:22 AM »
I have tried it, but the result is still the same.. :(

 


Get Your Ad Here

data_list