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 :
#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 ?