understanding the code

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
#include              //MCU=ATMEGA168
#include
#define USART_BAUDRATE 9600
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)
 
void ADC_init(void);
unsigned int ADC_read(unsigned char);
 
// ------------------------------------------------
int main(void)
{
	unsigned int value;
	unsigned int value1;
	DDRB=0xFF;
	DDRD=0x03;
	ADC_init();	// Initialization of ADC
	// ch=0;
	while(1)
	{
		value=ADC_read(0);
		PORTB=value;
		_delay_ms(500);
	} 

UCSRB |= (1 << RXEN) | (1 << TXEN);   // Turn on the transmission and reception circuitry
   UCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1); // Use 8-bit character sizes

   UBRRH = (BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate value into the high byte of the UBRR register
   UBRRL = BAUD_PRESCALE; // Load lower 8-bits of the baud rate value into the low byte of the UBRR register
while ((UCSRA & (1 << UDRE)) == 0) {} // Do nothing until UDR is ready for more data to be written to it
UDR = value; // Send out the byte value in the variable "ByteToSend"

}

//------------------------------------------------
 
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);
}
 

this code is what i have got for continously reading adc channels of microcontroller and then convert them to digital and send them via usart to pc....
kindly check and tell me is this going to work according to what i am thinking or not..??
need help of experts

Last Edited: Sun. Apr 28, 2013 - 08:31 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Have you tried running the code? Does it work or not "according to what you are thinking"?

That's the best way to find out, then if it doesn't work you can ask specific questions. :-)

John Samperi

Ampertronics Pty. Ltd.

https://www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

i tried to build it in avr studio4 and it successfully builded now as i dont actually know much of coding stuff...i need to take help of experts like you sir to know that is this working in the manner i have said or not!!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ankit0916 wrote:
i tried to build it in avr studio4 and it successfully builded now as i dont actually know much of coding stuff...i need to take help of experts like you sir to know that is this working in the manner i have said or not!!
But why aren't you just putting the code in the MCU as John stated and going from there ? If it does have to be checked, why not post which MCU is being used ( needful info in one's first post ) ?

1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1

Last Edited: Sun. Apr 28, 2013 - 07:49 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As I said run the code in the chip, that's the way I find out if it works or not.

Looking at code gives me headaches and I'm likely to miss things, the chip doesn't have such problems.

I usually start by plugging a debugger into my board and start debugging. If it doesn't work as it should I can then narrow down on the bit of code which may have problems.

Of course there are "real programmer" which can look at code and not get headaches. :-)

John Samperi

Ampertronics Pty. Ltd.

https://www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
why not post which MCU is being used ( needful info in one's first post ) ?

atmega168...:)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What about my other question ? :roll: You init. the usart AFTER the while(1), so it'll NEVER be executed.

Look at the usart and adc tutorials and compare their codes to yours, that's why that forum exists.

1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
   ch= ch & 0b00000111;      // channel must be b/w 0 to 7
   ADMUX |= ch;            // selecting channel 

OR is cumulative so you will never get back to 0 after using another channel. You need to mask ADMUX using AND. Either that or use assignment every time.

Also the code blocks on ADIF (and is then forced to reset it). Far better to block on ADSC as it self-clears.