1. crazyengineer

    Thread Starter Member

    Dec 29, 2010
    156
    2
    Okay, so I'm trying to transmit my ADC values to my computer using USART using an atmega48. However, when I used my serial monitoring program, I keep getting this output

    [​IMG]

    Here's my code
    Code ( (Unknown Language)):
    1.  
    2. //  Includes and Defines
    3.  
    4. #define F_CPU 8000000UL
    5. #define BAUD 9600
    6. #define MUBRR (F_CPU/16/BAUD)-1
    7. #include <avr/io.h>
    8. #include <util/delay.h>
    9.  
    10.  
    11.  
    12.  
    13.  
    14.  
    15.  
    16. //  ADC Init
    17.  
    18. void ADC_INIT(void)
    19. {
    20.     ADMUX=(1<<REFS0);
    21.     ADCSRA=(1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);
    22. }
    23.  
    24.  
    25.  
    26.  
    27.  
    28.  
    29.  
    30.  
    31.  
    32.  
    33. //  ADC Read
    34.  
    35. unsigned int ADC_READ(int channel)
    36. {
    37.     ADMUX=channel;
    38.     ADCSRA |= (1<<ADSC);
    39.     while(!(ADCSRA & (1<<ADIF)));
    40.     ADCSRA|=(1<<ADIF);
    41.    return(ADC);
    42. }
    43.  
    44.  
    45.  
    46.  
    47.  
    48.  
    49.  
    50.  
    51.  
    52. //  Usart Init
    53.  
    54. void USART_INIT(unsigned int ubrr)
    55. {
    56.     UBRR0H = (unsigned char)(ubrr>>8);
    57.     UBRR0L = (unsigned char)ubrr;
    58.     /*Enable receiver and transmitter */
    59.     UCSR0B = (1<<RXEN0)|(1<<TXEN0);
    60.     /* Set frame format: 8data, 2stop bit */
    61.     UCSR0C = (1<<USBS0)|(3<<UCSZ00);
    62. }
    63.  
    64.  
    65.  
    66.  
    67.  
    68.  
    69.  
    70.  
    71.  
    72. //  Usart Transmit
    73.  
    74. void USART_Transmit( unsigned int data )
    75. {
    76.     while ( !( UCSR0A & (1<<UDRE0)));
    77.     UDR0 = data;
    78.     return;
    79. }
    80.  
    81.  
    82.  
    83.  
    84.  
    85.  
    86.  
    87.  
    88.  
    89. //  Main
    90.  
    91. int main(void)
    92. {
    93.     ADC_INIT();
    94.     USART_INIT(MUBRR);
    95.     while(1)
    96.     {
    97.         USART_Transmit(ADC_READ(0));
    98.         _delay_ms(1000);
    99.     }
    100.    
    101.     return 0;
    102. }
    103.  
     
    Last edited: Mar 24, 2012
  2. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,014
    You are trying to display an integer value as a character.

    You need to convert to character first.
     
  3. crazyengineer

    Thread Starter Member

    Dec 29, 2010
    156
    2
    I thought I don't need to since the function argument has an unsigned int .
     
  4. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    The serial terminal program will just display the ASCII character that the number corresponds to.
    Try Realterm, it can display the actual number instead. It's a little buggy in places but full of features.
    Or write a program like mine to display it:
     
  5. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,014
    You thought wrong. It is an integer number that you get from the analog port not BCD.

    The easy but less efficient way is to use sprintf with the %d format. assuming your compiler supports it.

    The more efficient but more difficult way is to turn it into BCD. Your compiler may or may not have a BCD function.

    You could also display as hex characters. I think someone posted such a function in this forum not too long ago.


    Also remember you are displaying some number that is just a representation of voltage. You are going to have to calculate the actual voltage based on the number of bits your analog input supports and the values in your voltage divider.
     
  6. johngoh2007

    New Member

    Mar 27, 2012
    4
    0
    You can try to convert your ADC into ASCII character by adding 0x30h to your adc reading if i am not mistaken before sending data via USART.

    http://www.myplogger.com
     
  7. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,014
    You are mistaken.

    You are thinking of BCD numbers. OP has an integer.
     
Loading...