Fast-sampling issues on ATmega 328p-pu within on-board 10-Bit ADC

Go To Last Post
9 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Guys,

 

The ATmega 328p-pu with onboard-10 bit ADC can sample up to /2 of the system clock prescale. So I have initialized the hardware with pre-scale settings and it runs just fine.

static inline void init_ADC()
 {
     ADMUX|=(1<<REFS0);  //ADC reference on AVCC with external capacitor on AREF pin.
     ADMUX&=~(1<<ADLAR); //right adjust results, all 10 bits needed.
     ADCSRA|=(1<<ADPS0); //ADC clock pre-scaler division factor /2.
     ADCSRA|=(1<<ADEN); // enable ADC hardware.
 }

now, in my event loop, I do sample 3 different channels on multiplexer. That said, I have to change the channels quite often. So I have setup another function to do that for me.

uint16_t adc_Channel_Sampler(uint8_t channel)
 {
     ADMUX|=(0b11110000 & ADMUX)| channel; // select the channel.
     ADCSRA|=(1<<ADSC); // start a single conversion.
     while((ADCSRA & (1<<ADSC))==0) // wait until conversion is finalized.
     {
       return(ADC); // return the 10-bit ADC conversion result
     }
     //adc_data[channel]=ADC;
 }

Now, in event loop when I use this function and send the results via USART to PC, I get the same data no matter what. Looks like if I am using the function for just one ADC channel, its all works and functions as expected, but when I include more than one channel, I get same results for both channels. How is that possible?=

 

event loop code below:

while(1)
{
    //sei();
    OCR0A=252;
    OCR1B=500;
    OCR1A=55000;
    OCR2A=252;
    adc_Channel_Sampler(PC3);
    POTTI_data=ADC;
    printString("potentiometer readings(a.u.)=");
    printWord(POTTI_data);
    printString("\t\t\t\t\t");
    adc_Channel_Sampler(PC0);
    LDR_data=ADC;
    printString("LDR-Sensor readings(a.u.)=");
    printWord(LDR_data);
    printString("\r\n");
}
return(0);
}

-----------------------------------------------------------------------------------------------------------------------------Console readings realtime
potentiometer readings(a.u.)=01023                                      LDR-Sensor readings(a.u.)=01023
potentiometer readings(a.u.)=01022                                      LDR-Sensor readings(a.u.)=01023
potentiometer readings(a.u.)=01023                                      LDR-Sensor readings(a.u.)=01023
potentiometer readings(a.u.)=01023                                      LDR-Sensor readings(a.u.)=01023
potentiometer readings(a.u.)=01023                                      LDR-Sensor readings(a.u.)=01023
potentiometer readings(a.u.)=01023                                      LDR-Sensor readings(a.u.)=01022
potentiometer readings(a.u.)=01023                                      LDR-Sensor readings(a.u.)=01023
potentiometer readings(a.u.)=01023                                      LDR-Sensor readings(a.u.)=01023
potentiometer readings(a.u.)=01023                                      LDR-Sensor readings(a.u.)=01023
potentiometer readings(a.u.)=01023                                      LDR-Sensor readings(a.u.)=01022
potentiometer readings(a.u.)=01023                                      LDR-Sensor readings(a.u.)=01023
potentiometer readings(a.u.)=01023                                      LDR-Sensor readings(a.u.)=01023
potentiometer readings(a.u.)=01023                                      LDR-Sensor readings(a.u.)=01023
potentiometer readings(a.u.)=01023                                      LDR-Sensor readings(a.u.)=01023
potentiometer readings(a.u.)=01023                                      LDR-Sensor readings(a.u.)=01022
potentiometer readings(a.u.)=01023                                      LDR-Sensor readings(a.u.)=01023
potentiometer readings(a.u.)=01023                                      LDR-Sensor readings(a.u.)=01023
potentiometer readings(a.u.)=01023                                      LDR-Sensor readings(a.u.)=01023
potentiometer readings(a.u.)=01023                                      LDR-Sensor readings(a.u.)=01023
potentiometer readings(a.u.)=01023                                      LDR-Sensor readings(a.u.)=01023
potentiometer readings(a.u.)=01023                                      LDR-Sensor readings(a.u.)=01023
potentiometer readings(a.u.)=01022                                      LDR-Sensor readings(a.u.)=01023
potentiometer readings(a.u.)=01023                                      LDR-Sensor readings(a.u.)=01023
potentiometer readings(a.u.)=01023                                      LDR-Sensor readings(a.u.)=01023
potentiometer readings(a.u.)=01023                                      LDR-Sensor readings(a.u.)=01023
potentiometer readings(a.u.)=01023                                      LDR-Sensor readings(a.u.)=01023
potentiometer readings(a.u.)=01023                                      LDR-Sensor readings(a.u.)=01023
potentiometer readings(a.u.)=01023                                      LDR-Sensor readings(a.u.)=01023
potentiometer readings(a.u.)=01023                                      LDR-Sensor readings(a.u.)=01023
potentiometer readings(a.u.)=01023                                      LDR-Sensor readings(a.u.)=01023
potentiometer readings(a.u.)=01023                                      LDR-Sensor readings(a.u.)=01023
potentiometer readings(a.u.)=01023                                      LDR-Sensor readings(a.u.)=01023
potentiometer readings(a.u.)=01023                                      LDR-Sensor readings(a.u.)=01023
potentiometer readings(a.u.)=01023                                      LDR-Sensor readings(a.u.)=01023
potentiometer readings(a.u.)=01023                                      LDR-Sensor readings(a.u.)=01023
potentiometer readings(a.u.)=01023                                      LDR-Sensor readings(a.u.)=01023
potentiometer readings(a.u.)=01023                                      LDR-Sensor readings(a.u.)=01023
potentiometer readings(a.u.)=01023                                      LDR-Sensor readings(a.u.)=01023
potentiometer readings(a.u.)=01023                                      LDR-Sensor readings(a.u.)=01023
potentiometer readings(a.u.)=01023                                      LDR-Sensor readings(a.u.)=01023
potentiometer readings(a.u.)=01023                                      LDR-Sensor readings(a.u.)=01023
potentiometer readings(a.u.)=01023                                      LDR-Sensor readings(a.u.)=01023
potentiometer readings(a.u.)=01023                                      LDR-Sensor readings(a.u.)=01023
potentiometer readings(a.u.)=01023                                      LDR-Sensor readings(a.u.)=01023
potentiometer readings(a.u.)=01023                                      LDR-Sensor readings(a.u.)=01023
potentiometer readings(a.u.)=01023                                      LDR-Sensor readings(a.u.)=01023
potentiometer readings(a.u.)=01023                                      LDR-Sensor readings(a.u.)=01022
potentiometer readings(a.u.)=01023                                      LDR-Sensor readings(a.u.)=01023
potentiometer readings(a.u.)=01023                                      LDR-Sensor readings(a.u.)=01023
potentiometer readings(a.u.)=01023                                      LDR-Sensor readings(a.u.)=01023
potentiometer readings(a.u.)=01023                                      LDR-Sensor readings(a.u.)=01023
potentiometer readings(a.u.)=01023                                      LDR-Sensor readings(a.u.)=01023
potentiometer readings(a.u.)=01023                                      LDR-Sensor readings(a.u.)=01023
potentiometer readings(a.u.)=01023                                      LDR-Sensor readings(a.u.)=01023
potentiometer readings(a.u.)=01023

I would kindly accept any help here, to find out the reason.

 

 

This topic has a solution.

program is working......

Last Edited: Tue. Sep 25, 2018 - 03:38 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Please learn more about the behavior of '|='.
For example, this part never clears MUX3-0, so PC0 can not be selected.

ADMUX |= (0b11110000 & ADMUX) | channel; // select the channel.

If you are using AtmelStudio please use the simulator to check the value of the register.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
ADMUX |= (0b11110000 & ADMUX) | channel; // select the channel.

   found the mistake >>> the  "ADMUX|=" should be replaced "ADMUX= " Or-ing is not needed.

works now.

program is working......

Last Edited: Tue. Sep 25, 2018 - 08:42 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Dave_Zeb. wrote:
The ATmega 328p-pu with onboard-10 bit ADC can sample up to /2 of the system clock prescale.
No it can't (well not unless you clock the chip at F_CPU = 400kHz!)

 

For the ADC to deliver full 10 bit accuracy it has to be clocked in the range 50kHz..200kHz. If you run it at more than 200kHz you won't get the full 10 bit range.

 

So yes, if F_CPU was 4MHz (say) and you set ADPS to /2 then the ADC will be clocked at 2MHz but you won't get anywhere near 10 bit accuracy. Sure you still get 10 bit readings but you cannot rely on the accuracy of the lower bits.

 

On a typical 8MHz CPU  you would actually set ADPS to something like /64 to run the ADC at 125kHz. On 16MHz you would use /128 to hit the same ADC clock. If you do this then, because an ADC conversion takes 13 ADC clocks, your sample rate would be 9.6kHz. If you pick F_CPU just right do that it can be binary divided down to exactly the 200kHz limit then your ADC max sampling rate (with 10 bits) is 15.3kHz. You simply cannot make a tiny/mega ADC sample any faster than this for 10 bits accuracy. If you want a higher rate you need to switch to Xmega.

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

according to my settings now it samples at 500KHz, because the main processor clock set_clock_pre-scale(clock_div_8), so it runs at 1 Mhz clock frequency. But I would like to keep this speed, so that the USART settings and everything else stays tuned, otherwise need to change bunch of settings. What if i choose the the 8/ divider? then it should be 250KHz which is pretty close the values your mentioned. (^_^)

program is working......

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

Yeah, 250kHz is often close enough that you lose little.

 

However 1MHz as F_CPU is a terrible choice when USART is involved. You cannot really use anything beyond 4800 baud.

 

if this is an app that involves both UART and ADC I would first pick an F_CPU that made a useful baud rate possible but then I would also try and pick the speed so that it's a binary multiple of 200kHz or as close to that as possible to get the best ADC performance. Of course this does depend on your required sample rate - only do this if the required rate is "as fast and as accurate as possible".

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

this setbaud.h header file, might be familiar to you ^^), I guess you created that file in 2006. But I can't make use of it.

/* Copyright (c) 2006  Cliff Lawson
   All rights reserved.

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions are met:

   * Redistributions of source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.

   * Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in
     the documentation and/or other materials provided with the
     distribution.

   * Neither the name of the copyright holders nor the names of
     contributors may be used to endorse or promote products derived
     from this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  POSSIBILITY OF SUCH DAMAGE. */

#ifndef _UTIL_SETBAUD_H_
#define _UTIL_SETBAUD_H_

/*************************************************************
*
* util/setbaud.h
*
* This .h requires that on entry values are already defined
* for F_CPU and BAUD such as:
*
*   #define F_CPU 11059200
*   #define BAUD 38400
*   #include <util/setbaud.h>
*
* Assuming that the requested BAUD is valid for the given F_CPU
* then UBRRL_VALUE is set to the lower byte of the UBRR_VALUE
* and UBRRH_VALUE is set to the upper byte so that they can be
* used simply as (depending on IO register naming!):
*
*   UBRRH = UBRRH_VALUE;
*   UBRRL = UBRRL_VALUE;
*
* The value USE_2X will be undefined unless the baud can only be reached
* using U2X mode in which case it is defined and code should be added
* to set the U2X bit:
*
*   #ifdef USE_2X
*      UCSRA |= (1 << U2X);
*   #endif
*
*
* NB: Only baud rates with Error% between -2% and +2% are considered
* "valid" and the U2X=0 is always used where possible. The U2X=1 values for
* UBRR are only used when the value for U2X=0 would give an error out
* of the +/- 2% range
*
* V1.1, 29-8-06, by Cliff Lawson
*
*************************************************************/


#ifndef F_CPU
#error "setbaud.h requires F_CPU to be defined"
#else
#ifndef BAUD
#error "setbaud.h requires BAUD to be defined"
#else

#undef USE_2X

#if F_CPU == 1000000
#if (BAUD == 2400) 
#define UBRR_VALUE  25
#elif (BAUD == 4800)
#define UBRR_VALUE  12
#elif (BAUD == 9600)
#define UBRR_VALUE  12
#define USE_2X 
#else
#error "Invalid baud for given CPU frequency"
#error "Only valid baud for 1000000 are 2400, 4800, 9600*"
#endif

#elif F_CPU == 1834200
#if (BAUD == 2400)
#define UBRR_VALUE  47
#elif (BAUD == 4800)
#define UBRR_VALUE  23
#elif (BAUD == 9600)
#define UBRR_VALUE 11
#elif (BAUD == 14400)
#define UBRR_VALUE 7
#elif (BAUD == 19200)
#define UBRR_VALUE 5
#elif (BAUD == 28800)
#define UBRR_VALUE 3
#elif (BAUD == 38400)
#define UBRR_VALUE 2
#elif (BAUD == 57600)
#define UBRR_VALUE 1
#elif (BAUD == 76800)
#define UBRR_VALUE 2
#define USE_2X
#elif (BAUD == 115200)
#define UBRR_VALUE 0
#else
#error "Invalid baud for given CPU frequency"
#error "Only valid baud for 1834200 are 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, 76800*, 115200"
#endif

#elif F_CPU == 2000000
#if (BAUD == 2400)
#define UBRR_VALUE 51
#elif (BAUD == 4800)
#define UBRR_VALUE 25
#elif (BAUD == 9600)
#define UBRR_VALUE 12
#elif (BAUD == 19200)
#define UBRR_VALUE 12
#define USE_2X
#else
#error "Invalid baud for given CPU frequency"
#error "Only valid baud for 2000000 are 2400, 4800, 9600, 19200*"
#endif


#elif F_CPU == 3686400
#if (BAUD == 2400)
#define UBRR_VALUE 95
#elif (BAUD == 4800)
#define UBRR_VALUE 47
#elif (BAUD == 9600)
#define UBRR_VALUE 23
#elif (BAUD == 14400)
#define UBRR_VALUE 15
#elif (BAUD == 19200)
#define UBRR_VALUE 11
#elif (BAUD == 28800)
#define UBRR_VALUE 7
#elif (BAUD == 38400)
#define UBRR_VALUE 5
#elif (BAUD == 57600)
#define UBRR_VALUE 3
#elif (BAUD == 76800)
#define UBRR_VALUE 2
#elif (BAUD == 115200)
#define UBRR_VALUE 1
#elif (BAUD == 230400)
#define UBRR_VALUE 0
#else
#error "Invalid baud for given CPU frequency"
#error "Only valid baud for 3686400 are 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, 76800, 115200, 230400"
#endif

#elif F_CPU == 4000000
#if (BAUD == 2400)
#define UBRR_VALUE 103
#elif (BAUD == 4800)
#define UBRR_VALUE 51
#elif (BAUD == 9600)
#define UBRR_VALUE 25
#elif (BAUD == 14400)
#define UBRR_VALUE 34
#define USE_2X
#elif (BAUD == 19200)
#define UBRR_VALUE 12
#elif (BAUD == 38400)
#define UBRR_VALUE 12
#define USE_2X
#elif (BAUD == 250000)
#define UBRR_VALUE 0
#else
#error "Invalid baud for given CPU frequency"
#error "Only valid baud for 4000000 are 2400, 4800, 9600, 14400*, 19200, 38400*, 250000"
#endif

#elif F_CPU == 7372800
#if (BAUD == 2400)
#define UBRR_VALUE 191
#elif (BAUD == 4800)
#define UBRR_VALUE 95
#elif (BAUD == 9600)
#define UBRR_VALUE 47
#elif (BAUD == 14400)
#define UBRR_VALUE 31
#elif (BAUD == 19200)
#define UBRR_VALUE 23
#elif (BAUD == 28800)
#define UBRR_VALUE 15
#elif (BAUD == 38400)
#define UBRR_VALUE 11
#elif (BAUD == 57600)
#define UBRR_VALUE 7
#elif (BAUD == 76800)
#define UBRR_VALUE 5
#elif (BAUD == 115200)
#define UBRR_VALUE 3
#elif (BAUD == 230400)
#define UBRR_VALUE 1
#else
#error "Invalid baud for given CPU frequency"
#error "Only valid baud for 7372800 are 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, 76800, 115200, 230400"
#endif

@@@ add USE_2X from here on...

#elif F_CPU == 8000000
#if (BAUD == 2400)
#define UBRR_VALUE 207
#elif (BAUD == 4800)
#define UBRR_VALUE 103
#elif (BAUD == 9600)
#define UBRR_VALUE 51
#elif (BAUD == 14400)
#define UBRR_VALUE 34
#elif (BAUD == 19200)
#define UBRR_VALUE 25
#elif (BAUD == 28800)
#define UBRR_VALUE 34
#define USE_2X
#elif (BAUD == 38400)
#define UBRR_VALUE 12
#elif (BAUD == 76800)
#define UBRR_VALUE 12
#define USE_2X
#elif (BAUD == 250000)
#define UBRR_VALUE 1
#elif (BAUD == 500000)
#define UBRR_VALUE 0
#else
#error "Invalid baud for given CPU frequency"
#error "Only valid baud for 8000000 are 2400, 4800, 9600, 14400, 19200, 28800*, 38400, 76800*, 250000, 500000"
#endif

#elif F_CPU == 11059200
#if (BAUD == 2400)
#define UBRR_VALUE 287
#elif (BAUD == 4800)
#define UBRR_VALUE 143
#elif (BAUD == 9600)
#define UBRR_VALUE 71
#elif (BAUD == 14400)
#define UBRR_VALUE 47
#elif (BAUD == 19200)
#define UBRR_VALUE 35
#elif (BAUD == 28800)
#define UBRR_VALUE 23
#elif (BAUD == 38400)
#define UBRR_VALUE 17
#elif (BAUD == 57600)
#define UBRR_VALUE 11
#elif (BAUD == 76800)
#define UBRR_VALUE 8
#elif (BAUD == 115200)
#define UBRR_VALUE 5
#elif (BAUD == 230400)
#define UBRR_VALUE 2
#else
#error "Invalid baud for given CPU frequency"
#error "Only valid baud for 11059200 are 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, 76800, 115200, 230400"
#endif

#elif F_CPU == 14745600
#if (BAUD == 2400)
#define UBRR_VALUE 383
#elif (BAUD == 4800)
#define UBRR_VALUE 191
#elif (BAUD == 9600)
#define UBRR_VALUE 95
#elif (BAUD == 14400)
#define UBRR_VALUE 63
#elif (BAUD == 19200)
#define UBRR_VALUE 47
#elif (BAUD == 28800)
#define UBRR_VALUE 31
#elif (BAUD == 38400)
#define UBRR_VALUE 23
#elif (BAUD == 57600)
#define UBRR_VALUE 15
#elif (BAUD == 76800)
#define UBRR_VALUE 11
#elif (BAUD == 115200)
#define UBRR_VALUE 7
#elif (BAUD == 230400)
#define UBRR_VALUE 3
#else
#error "Invalid baud for given CPU frequency"
#error "Only valid baud for 14745600 are 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, 76800, 115200, 230400"
#endif

#elif F_CPU == 16000000
#if (BAUD == 2400)
#define UBRR_VALUE 416
#elif (BAUD == 4800)
#define UBRR_VALUE 207
#elif (BAUD == 9600)
#define UBRR_VALUE 103
#elif (BAUD == 14400)
#define UBRR_VALUE 68
#elif (BAUD == 19200)
#define UBRR_VALUE 51
#elif (BAUD == 28800)
#define UBRR_VALUE 34
#elif (BAUD == 38400)
#define UBRR_VALUE 25
#elif (BAUD == 57600)
#define UBRR_VALUE 24
#define USE_2X
#elif (BAUD == 76800)
#define UBRR_VALUE 12
#elif (BAUD == 250000)
#define UBRR_VALUE 3
#elif (BAUD == 500000)
#define UBRR_VALUE 1
#elif (BAUD == 1000000)
#define UBRR_VALUE 0
#else
#error "Invalid baud for given CPU frequency"
#error "Only valid baud for 16000000 are 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600*, 76800, 250000, 500000, 1000000"
#endif

#elif F_CPU == 18432000
#if (BAUD == 2400)
#define UBRR_VALUE 479
#elif (BAUD == 4800)
#define UBRR_VALUE 239
#elif (BAUD == 9600)
#define UBRR_VALUE 119
#elif (BAUD == 14400)
#define UBRR_VALUE 79
#elif (BAUD == 19200)
#define UBRR_VALUE 59
#elif (BAUD == 28800)
#define UBRR_VALUE 39
#elif (BAUD == 38400)
#define UBRR_VALUE 29
#elif (BAUD == 57600)
#define UBRR_VALUE 19
#elif (BAUD == 76800)
#define UBRR_VALUE 14
#elif (BAUD == 115200)
#define UBRR_VALUE 9
#elif (BAUD == 230400)
#define UBRR_VALUE 4
#else
#error "Invalid baud for given CPU frequency"
#error "Only valid baud for 18432000 are 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, 76800, 115200, 230400"
#endif

#elif F_CPU == 20000000
#if (BAUD == 2400)
#define UBRR_VALUE 520
#elif (BAUD == 4800)
#define UBRR_VALUE 259
#elif (BAUD == 9600)
#define UBRR_VALUE 129
#elif (BAUD == 14400)
#define UBRR_VALUE 86
#elif (BAUD == 19200)
#define UBRR_VALUE 64
#elif (BAUD == 28800)
#define UBRR_VALUE 42
#elif (BAUD == 38400)
#define UBRR_VALUE 32
#elif (BAUD == 57600)
#define UBRR_VALUE 21
#elif (BAUD == 76800)
#define UBRR_VALUE 15
#elif (BAUD == 115200)
#define UBRR_VALUE 10
#elif (BAUD == 239400)
#define UBRR_VALUE 10
#define USE_2X
#elif (BAUD == 250000)
#define UBRR_VALUE 4
#else
#error "Invalid baud for given CPU frequency"
#error "Only valid baud for 20000000 are 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, 76800, 115200, 230400*, 250000"
#endif

#else
#error "F_CPU value not recognised by setbaud.h"
#endif 

#ifdef UBRR_VALUE
#define UBRRL_VALUE (UBRR_VALUE & 0xFF)
#define UBRRH_VALUE (UBRR_VALUE >> 8)
#endif

#endif // BAUD is defined
#endif // F_CPU is defined

#endif /* _UTIL_SETBAUD_H_ */

Let's consider this scenario:

I use the makefile below,  and there the "F_CPU" and "Baud" are/must be defined. I highlighted them with orange color.


##########------------------------------------------------------##########
##########              Project-specific Details                ##########
##########    Check these every time you start a new project    ##########
##########------------------------------------------------------##########

MCU   = atmega328p
F_CPU = 1000000UL 
BAUD  = 9600UL
## Also try BAUD = 19200 or 38400 if you're feeling lucky.

## A directory for common include files and the simple USART library.
## If you move either the current folder or the Library folder, you'll
##  need to change this path to match.
LIBDIR = ../../AVR-Programming-Library

##########------------------------------------------------------##########
##########                 Programmer Defaults                  ##########
##########          Set up once, then forget about it           ##########
##########        (Can override.  See bottom of file.)          ##########
##########------------------------------------------------------##########

Now if I want to change the "F_CPU" and "Baud rate" then first I just change them inside the makefile to desired speed "according to your created setbaud.h" file.

Then I should also change it my code?  "clock_prescale_set( desired value);" as well?

 

currently, as soon as I change my ATmega "clock_prescale_set( desired value);" it start sending garbage in console.

What I would like to know, the steps to do it in right way.  Step1. then Step2. ans so on. 

 

program is working......

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

My intended use of setbaud.h is:

#define F_CPU 11059200
#define BAUD 38400
#include <util/setbaud.h>
...

You can tell it's like that because that's what the manual says...

 

https://www.nongnu.org/avr-libc/...

 

Obviously those F_CPU and BAUD could be -D on the command line to the compiler if you preferred. So if your Makefile has:

F_CPU = 1000000UL 
BAUD  = 9600UL

then at some point later where there is the rule to invoke the compiler (perhaps after "CFLAGS" definitions?) you might have:

CFLAGS += -DF_CPU=$(F_CPU) -DBAUD=$(BAUD)
..
	$(CC) -c $(CFLAGS) $< -o $@ 

or whatever. That "feeds" the defined values from the Makefile all the way into the compiled code. As an example the Mfile template already has things like:

CFLAGS = -g$(DEBUG)
CFLAGS += $(CDEFS)
CFLAGS += -O$(OPT)
...
CFLAGS += $(CSTANDARD)

where it passes Makefile defined variables into the compilation command.

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

now everything is running at 38400 Baud, such a huge boost in AVR's speed. I like it ^^ )

program is working......