asked    Charlotte     2018-10-22       c       77 view        1 Answer

[SOLVED] Pre processor: #if comparing two values

I was working on setting up a UART for ATmega168 micro controller. My first code didn't work. While checking on that I came across an header file used in a working program. It uses preprocessor directives.

It was quite confusing for me, so I decided to run it in a c compiler and check outputs.

#include <stdio.h>

#define F_CPU 1000000UL

#ifndef BAUD                          /* if not defined in Makefile... */
#define BAUD  9600                     /* set a safe default baud rate */
#endif

#define BAUD_TOL 2
#define UBRR_VALUE (((F_CPU) + 8UL * (BAUD)) / (16UL * (BAUD)) -1UL)

#if 100 * (F_CPU) > (16 * ((UBRR_VALUE) + 1)) * (100 * (BAUD) + (BAUD) * (BAUD_TOL))
#define USE_2X 1
#define TEST 1

#elif 100 * (F_CPU) < (16 * ((UBRR_VALUE) + 1)) * (100 * (BAUD) - (BAUD) * (BAUD_TOL))
#define USE_2X 1
#define TEST 11

#else
#define USE_2X 0
#define TEST 111

#endif


int main(void) {

    printf("UBRR_VALUE: %ld\n", UBRR_VALUE);
    printf("USE_2X: %d\n", USE_2X);
    printf("%ld\n", (16 * ((UBRR_VALUE) + 1)) * (100 * (BAUD) - (BAUD) * (BAUD_TOL)));
    printf("F_CPU * 100 = %ld\n", (100*F_CPU));
    printf("TEST = %d\n", TEST);
    return 0;
}

Output:

UBRR_VALUE: 6
USE_2X: 1
105369600
F_CPU * 100 = 100000000
TEST = 11

since 100 * F_CPU is less than (16 * ((UBRR_VALUE) + 1)) * (100 * (BAUD) - (BAUD) * (BAUD_TOL)), TEST is assigned a value of 11.

But it I change the F_CPU to 16000000UL:

UBRR_VALUE: 103
USE_2X: 0
1565491200
F_CPU * 100 = 1600000000
TEST = 111

Here's also 100 * F_CPU is less than (16 * ((UBRR_VALUE) + 1)) * (100 * (BAUD) - (BAUD) * (BAUD_TOL)).

But how come TEST is 111?

Edit: *Here's 100 * F_CPU is greater than (16 * ((UBRR_VALUE) + 1)) * (100 * (BAUD) - (BAUD) * (BAUD_TOL))

  1 Answer  

        answered    Louis     2018-10-22      

The macros are expanded as explained below:

For the second case F_CPU to 16000000UL (Larger value)

100 * (F_CPU) > (16 * ((UBRR_VALUE) + 1)) * (100 * (BAUD) + (BAUD) * (BAUD_TOL)) 

evaluates to 0x5f5e1000 > 0x611e8000 which is false

100 * (F_CPU) < (16 * ((UBRR_VALUE) + 1)) * (100 * (BAUD) - (BAUD) * (BAUD_TOL))

evaluates to 0x5f5e1000 < 0x5d4f8000 which is also false.

Hence the below macros are applicable:

#define USE_2X 0
#define TEST 111




Your Answer





 2018-10-22         Ella

es6 class methods can't reach this instance

So I made a class and have no idea why I can't reach this instance inside one method.So in my router I'm calling the method like thisimport Emails from '../controllers/emails'import router from 'express'....route.post('/', Emails.setupEmail)so after calling POST method it calls setupEmail method but it crashes with the message: TypeError: Cannot read property 'availableEmailTypes' of undefinedand the code of class:class Emails {constructor() { this.availableEmailTypes = ['registration', 'forgot-password', 'two-factor']}setupEmail(req, res) { if (!req.body.type || !re...
 javascript                     1 answers                     54 view
 2018-10-22         Dennis

How to return data from inside promise.all

findCustomerOrders(customerId) { return APIService.getCustomerOrders(this, customerId) .then((data) => { data.map(order => { return Promise.all([APIService.getShippingAddress(this, order.id), APIService.getProducts(this,order.id), APIService.getCustomerById(this, customerId)]) .then((returnedData)=>{ return buildOrder(returnedData); }); }); }); }The function where the data is to be returned isfindCustomerOrders(1).then((final) =>{console.log(final)});I have to return the data which is returned by buil...
 node.js                     4 answers                     55 view
 2018-10-22         Annabelle

how to store and validate qr code in server using nodejs and mongoose

We are implementing a mobile app using the following technologies:Node.JS & express MongoDB & mongooseFlutterSuppose we should generate QR Code with the goods information like name, price, expiration date and etc. which should be unique and whenever customer scan the QR Code it should be validated by connecting to the server, fetching data and add the selected good to cart.As I have searched on the internet, I found that we should convert the data to canvas (to have QR Code). Is there any solution to generate QR code in server side, store and validate it in mongodb?...
 node.js                     1 answers                     57 view