Jump to content


Photo

sample code for ADC of ATmeag32/16


  • Please log in to reply
5 replies to this topic

#1 xolang1

xolang1

    Junior Member

  • Members
  • PipPip
  • 9 posts
  • Location:Baguio City

Posted 23 September 2003 - 04:26 AM

hi,

im a begginer on ADC.I would like a simple code in C that will use the ADC of ATmega's. The code will read 1 ADC channel, convert it and display the results in the USART using printf/putf/puts. I just like to prove the theory on ADC's.
I am using stk500 development board with mega16/32 on it. also, im using codevision compiler
I hope some one could help. Thanks in advance

#2 marke

marke

    Posting Freak

  • Moderator
  • PipPipPipPipPipPip
  • 2,617 posts
  • Gender:Male
  • Location:Christchurch, New Zealand

Posted 23 September 2003 - 07:33 PM

Hello xolang1

Welcome to the forum.
If you are using the codvision compiler, then you can use the code wizard to set up the code for the ADC for you. You can specify how many channels to read and whether to run under interrupt.
I would suggest that you have a play with this and look at the code generated and that will get you underway for the ACD side. The rest is plain sailing, just standard C codeing.
Out of preference, I would run the ADC converter under interupt so that at any stage you can just read in the last reading rather than haveing to start the reading and waiting for the result.
Best regards,

#3 xolang1

xolang1

    Junior Member

  • Members
  • PipPip
  • 9 posts
  • Location:Baguio City

Posted 24 September 2003 - 12:40 AM

Hi mark,

Im 1-month old into doing microcontrollers (and codevision) and have never tried the wizard yet. I always manually created my projects, i didnt know that the wizard can do the ADC code. Thanks for informing! I'll be trying this in a while

thankyou

xolang1


:)

#4 marke

marke

    Posting Freak

  • Moderator
  • PipPipPipPipPipPip
  • 2,617 posts
  • Gender:Male
  • Location:Christchurch, New Zealand

Posted 24 September 2003 - 08:16 PM

Have a good play with the wizard and see what it can do for you. Saves a lot of manual coding and setting up. Life then gets easier for writing the real code.

Best regards,

#5 gumpu

gumpu

  • Members
  • Pip
  • 1 posts

Posted 19 February 2008 - 12:39 PM

QUOTE (xolang1 @ Sep 23 2003, 09:56 AM) <{POST_SNAPBACK}>
hi,

im a begginer on ADC.I would like a simple code in C that will use the ADC of ATmega's. The code will read 1 ADC channel, convert it and display the results in the USART using printf/putf/puts. I just like to prove the theory on ADC's.
I am using stk500 development board with mega16/32 on it. also, im using codevision compiler
I hope some one could help. Thanks in advance






i am sending the code hope this may help u
#include <avr/interrupt.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/iom32.h>
#define A_DEN 0x80
#define A_DSC 0X40
#define S_REG 0x80

unsigned char Channel = 1;



unsigned char value = 0;
unsigned char value1 = 0;
unsigned char value6 = 0;
unsigned char Ready = 0;

unsigned char val1 =0;
unsigned char val2 =0;
unsigned char val3 =0;
unsigned char val4 =0;
unsigned char val5 =0;
unsigned char val6 =0;



void delay(int del);
void Tx_Comm(unsigned char S_Data);
void Serial_Tx(void);


//This section convertes Higher Nibble HEX to ASCII Value & Transmits to PC

void Serial_Tx(void)
{
value1 = ADCH;
(val2 = value1 + 0x30);
Tx_Comm(val2); // Transmit the Value.

value = ADCL;

val3 = (value >>4);
val3 = (val3 & 0x0f);
if(val3 >= 0x0A)
{
(val2 = val3 + 0x37);
}
else
{
(val2 = val3 + 0x30);
}
Tx_Comm(val2); // Transmit the Value.

value = ADCL;

// This section convertes Lower Nibble HEX to ASCII Value & Transmits to PC

(val4 = value & 0x0f);
if (val4 >= 0x0A)
{
(val2 = val4 + 0x37);
}
else
{
(val2 = val4 + 0x30);
}
Tx_Comm(val2);

Tx_Comm(0x0D);
Tx_Comm(0x0A);
}



int main (void)
{
UBRRH = 0x00;
UBRRL = 23;
// Enable receiver and transmitter
UCSRB = (1<<RXEN)|(1<<TXEN);
// Set frame format: 8data, 1stop bit
UCSRC = (1<<URSEL)|(3<<UCSZ0);

DDRB = 0xFF;
DDRA = 0x00;
//GICR = 0X02;
//SREG = 0x80 ; // SREG Bit 7 - Global Interrupt Enable. I bit set to 1
//sei();


//ADCSRA |= A_DEN; //ENABLE ADC
ADCSRA = 0x84; //
ADMUX =0xC0; //reference external voltage, channel selection

ADCSRA |= 0X80; //ENABLE ADC

PORTB = 0xFF; //switch of all led's
SFIOR=0x00; //free running mode selection

while(1)
{

PORTB= 0X0f;
delay(10000);
delay(10000);
delay(10000);
delay(10000);
Tx_Comm(0x09);
Tx_Comm(Channel + 0x30);
Tx_Comm(0x20);

//Tx_Comm('a');
ADC_convert();
Serial_Tx();
delay(1000);

PORTB= 0Xf0;
delay(10000);
delay(10000);
delay(10000);
delay(10000);

}
}

void delay(int del)
{
while(del--);

}

void Tx_Comm(unsigned char S_Data)
{
while(1)
{
value6 = (UCSRA & 0x20);
if(value6 == 0x20)
break;
}
UDR = S_Data;
}

void ADC_convert()
{
value=0x00;
value1=0x00;

ADCSRA |= 0X80; //ENABLE ADC
ADCSRA|= 0x40; //starting the conversion
//delay(50);
while(ADCSRA & 0x40); // wait for the end of the conversion
delay(100);
value = ADCL;
value1 = ADCH;

}

#6 Bysikmode

Bysikmode

    Intermediate Member

  • Full Member
  • PipPipPip
  • 11 posts

Posted 20 April 2014 - 01:07 PM

Thank you for the informative posts.



 


dubli

 





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users