Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] how to send atmega adc reading on serial port

Status
Not open for further replies.

ud23

Advanced Member level 3
Joined
Apr 20, 2011
Messages
926
Helped
185
Reputation
370
Reaction score
180
Trophy points
1,323
Activity points
6,138
hi all i want to send atmega16 internal adc readings to the serial port of pc
Code:
#include <avr/io.h>
#include <inttypes.h>
#include<util/delay.h>
 
void ADC_init(void);
unsigned int ADC_read(unsigned char);

//This function is used to initialize the USART
//at a given UBRR value
void USARTInit(uint16_t ubrr_value)
{

   //Set Baud rate

   UBRRL = ubrr_value;
   UBRRH = (ubrr_value>>8);

   /*Set Frame Format


   >> Asynchronous mode
   >> No Parity
   >> 1 StopBit

   >> char size 8

   */

   UCSRC=(1<<URSEL)|(3<<UCSZ0);


   //Enable The receiver and transmitter

   UCSRB=(1<<RXEN)|(1<<TXEN);


}



char USARTReadChar()
{
   //Wait untill a data is available

   while(!(UCSRA & (1<<RXC)))
   {
      //Do nothing
   }

   //Now USART has got data from host
   //and is available is buffer

   return UDR;
}



void USARTWriteChar(char data)
{
   //Wait untill the transmitter is ready

   while(!(UCSRA & (1<<UDRE)))
   {
      //Do nothing
   }

   //Now write the data to USART buffer

   UDR=data;
}

int main()
{
   
   char data;

   /*First Initialize the USART with baud rate = 19200bps
   
 

   */

   USARTInit(51);    

  
   
ADC_init();

   while(1)
   {


       unsigned int value;
        DDRB=0xFF;
        DDRD=0x03; // Initialization of ADC



value=ADC_read(0);
PORTB=value;
_delay_ms(1000);

      data=value;
      

     

  
      USARTWriteChar(data);
 
      
       


   }

   }





 

 
void ADC_init(void) // Initialization of ADC
{
ADMUX=(1<<REFS0); // AVcc with external capacitor at AREF
ADCSRA=(1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);
// Enable ADC and set Prescaler division factor as 128
}
 
unsigned int ADC_read(unsigned char ch)
{
ch= ch & 0b00000111; // channel must be b/w 0 to 7
ADMUX |= ch; // selecting channel
 
ADCSRA|=(1<<ADSC); // start conversion
while(!(ADCSRA & (1<<ADIF))); // waiting for ADIF, conversion complete
ADCSRA|=(1<<ADIF); // clearing of ADIF, it is done by writing 1 to it
 
return (ADC);
}

i wrote this code but it is sending continuous data to serial port i want to send data when adc reading got change.
 

ud23 said:
i wrote this code but it is sending continuous data to serial port i want to send data when adc reading got change.
You could declare a second variable to store the previous adc value. You will send only when the two variables are different.

Code:
unsigned int value = 0, oldValue = 0;
while(1)
{
  value=ADC_read(0);
  PORTB=value;
  _delay_ms(1000);

  if (value != oldValue)
  {
    data=value;  
    USARTWriteChar(data);
  }

  oldValue = value;
}

Didn't check the code but it should work, please let me know if you have any problems.
However, the read value of ADC could be changed, even if the analog voltage is the same. For example you could read 0x139 the first time and 0x138 the second time. Same analog voltage, different ADC result.
Finally, a 16-bit value is returned from the ADC_read() function, but you send an 8-bit value to the serial port. So the value sent to the serial port will be wrong, except if the ADC result is always smaller that 255. You could get an 8-bit value from the ADC, refer to left or right adjust of the result in the ADC section of the datasheet.

Alexandros
 
  • Like
Reactions: ud23

    ud23

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top