shark38j
shark38j

Reputation: 124

Atmega328 Analog-to-Digital Converter

I am working on understanding ADC with an arduino and a LCD shield that has buttons. I like to work in Atmel Studio and work to write my own libraries through research and the datasheet. My goal is to write the ADC value to the screen. If I write an Arduino Sketch with the analogRead() function it works, but I can not get it to read anything on the LCDw with my Atmel Studio program. The LCD does work as I can write other information on the LCD. My program is below. Any ideas would really help. Thanks!!!

Analog Header

 #include<util/delay.h>

 void adc_init(void)
 {
     ADMUX = (1<<REFS0);     //select AVCC as reference
     ADCSRA = (1<<ADEN) | 7;  //enable (ADEN) and prescale = 128 (16MHz/128 = 125kHz)
 }

 int readAdc(char chan)
 {
     ADMUX = (1<<REFS0) | (chan & 0x0f);  //select ref (REFS0) and channel
     ADCSRA |= (1<<ADSC);                 //start the conversion
     while (ADCSRA & (1<<ADSC));          //wait for end of conversion
     return ADC;                            //Return 16 Bit Reading Register
 }

Main Program

 #ifndef F_CPU
 #define F_CPU 16000000UL // 16 MHz clock speed
 #endif


 #define D4 eS_PORTD4
 #define D5 eS_PORTD5
 #define D6 eS_PORTD6
 #define D7 eS_PORTD7
 #define RS eS_PORTB0
 #define EN eS_PORTB1

 #include <avr/io.h>
 #include <util/delay.h>
 #include <stdio.h>
 #include "lcd328.h"
 #include "myHeader328.h"

 /*--------------------------------------------------------
 -- initScreen() initializes the screen
 --------------------------------------------------------*/
 void initScreen()
 {
     Lcd4_Init();  //Initialize Screen
     Lcd4_Clear();  //Clear Screen
     Lcd4_Set_Cursor(1,0);
     Lcd4_Write_String("Count:0");
 }

 /*--------------------------------------------------------
 -- refreshScreen() refreshed the screen
 --------------------------------------------------------*/
 void refreshScreen(int count2)
 {
     char countStr[5];
     sprintf(countStr, "%i", count2);
     Lcd4_Set_Cursor(1,6);
     Lcd4_Write_String(countStr);
     _delay_ms(50);
 }

 /*--------------------------------------------------------
 -- main()
 --------------------------------------------------------*/

 int main(void)
 {
     DDRD = 0xFF;
     DDRB = 0xFF;
     DDRC = 0x00;
     initScreen();
     PORTB = PORTB | (1<<PORTB2);  //Turn on backlight
     int adcOut = 4;
     while(1)
     {
         adcOut = readAdc(0);
         refreshScreen(adcOut);
         _delay_ms(500);

     }
 }

Upvotes: 1

Views: 366

Answers (1)

Mike
Mike

Reputation: 4288

You had to call adc_init() in your main loop.

/*--------------------------------------------------------
 -- main()
 --------------------------------------------------------*/

 int main(void)
 {
     DDRD = 0xFF;
     DDRB = 0xFF;
     DDRC = 0x00;
     initScreen();
     adc_init();       //!!!!!
     PORTB = PORTB | (1<<PORTB2);  //Turn on backlight
     int adcOut = 4;
     while(1)
     {
         adcOut = readAdc(0);
         refreshScreen(adcOut);
         _delay_ms(500);

     }
 }

Upvotes: 1

Related Questions