Loading ...
Sorry, an error occurred while loading the content.

1812HAVING PROBLEM WITH ADC

Expand Messages
  • danial_1000
    Aug 2, 2009
      hi,
      i m actually new to avr ,i want to read the analog voltage through avr
      adc using avcc as a refrence and want to display it on terminal window
      using UART of avr.i tried to do so but i don,t sucess to display the
      correct digits on the bray's terminal window ,i connect a multi turn
      potentometer to vary the analog level from 0 volts to +5 volts.when i
      turn the pot i gets 00 ascii charcters on the screen .plz any body help
      me i will be thankfull.
      regards
      dany




      /*----------------------------------------------------------------
      -----------------HEADER FILES-------------------------------------
      -----------------------------------------------------------------*/
      #include <avr/io.h>
      /*----------------------------------------------------------------
      -----------------PROTOTYPES------------------------------------
      -----------------------------------------------------------------*/
      void Init_ADC(void);
      int Read_ADC(void);
      void InitUART( unsigned char baudrate );
      unsigned char ReceiveByte( void );
      void TransmitByte( unsigned char data );
      /*----------------------------------------------------------------
      -----------------MAIN FUNCTION------------------------------------
      -----------------------------------------------------------------*/

      int main(void)
      {
      int b,i,k;
      unsigned char str[7]={'V','O','L','T',':',' ',' '};//at starting some
      backspace occurs
      unsigned char del[2]={0x08,0X08}; //for deleting previous reading
      //serial port initiliasation
      InitUART( 207 ); //8Mhz 2400 8n1
      //ADC startups
      Init_ADC();
      //Read_ADC(void)
      //sending the string

      for(i=0;i<7;i++)
      {
      TransmitByte(str[i]);
      }
      b=0;

      do
      {
      k=Read_ADC();
      if(k!=b)
      {
      //first delete the previous values for uart
      for(i=0;i<2;i++)
      {
      TransmitByte(del[i]);
      }

      k=(((ADCL=0*256+ADCH)*4.5)/512);
      TransmitByte(k+0x30); //0x30 have been added to display ascii
      TransmitByte(k+0x30);
      TransmitByte(k+0x30);
      }
      }
      while(1);
      return o;
      }
      /*----------------------------------------------------------------
      --------------FUNCTION TO INIT_ADC------------------------------------
      -----------------------------------------------------------------*/
      void Init_ADC(void)
      {

      ADMUX=0;//SELECTING CHANNEL0
      ADCSRA=0;
      ADMUX|=(1<<ADLAR) | (1<<REFS0)) ;
      // AREF IS AVCC AND THE CONVERSION IS LEFT ADJUSTED
      ADCSRA|=(1<<ADEN) | (1<<ADPS2) | (1<<ADPS0) | (1<<ADPS1) ; //ADC
      PRESCALLER IS 128
      }
      /*----------------------------------------------------------------
      --------------FUNCTION TO READ_ADC------------------------------------
      -----------------------------------------------------------------*/
      int Read_ADC(void)
      {
      ADCSRA|=(1<<ADSC); //start conversion
      while(!((ADCSRA & (1<<ADIF));
      return ADCH;
      }
      /*----------------------------------------------------------------
      ------------FUNCTIONS TO Initialize UART--------------------------
      -----------------------------------------------------------------*/

      void InitUART( unsigned char baudrate )
      {
      UBRRL = baudrate; /* Set the baud rate */
      UCSRB = (UCSRB | (1<<RXEN) | (1<<TXEN) ); /* Enable UART receiver
      and transmitter */
      }
      /*----------------------------------------------------------------
      ------------FUNCTIONS TO READ UART-------------------------------
      -----------------------------------------------------------------*/
      unsigned char ReceiveByte( void )
      {
      while ( !(UCSRA & (1<<RXC))); /* Wait for incomming data */
      return UDR;/* Return the data */
      }

      /*----------------------------------------------------------------
      ------------FUNCTIONS TO WRITE UART-------------------------------
      -----------------------------------------------------------------*/
      void TransmitByte( unsigned char data )
      {
      while ( !(UCSRA & (1<<UDRE))); /* Wait for empty transmit
      buffer */
      UDR = data; /* Start transmittion */
      }
    • Show all 9 messages in this topic