Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Message

Re: [AVR-Chat] Calculate signed and unsigned

2011-09-02 by David Kelly

On Sep 2, 2011, at 5:10 PM, Антощенков Роман Викторович wrote:

> Hello!
> 
> I using AtMega32A, AVRStudio 4.18SP3 and WinAVR 2011.
> 
> unsigned int param1;
>  signed int param2; (
> unsigned int param3;
> 
> OCR1B = (unsigned int)(param1 - param2);
> OCR1A = (unsigned int)(param1 - param2 - param3);
> 
> I do not get the correct result. How to calculate signed and unsigned variables?

You get exactly what you asked for.

param1 and param3 get promoted to signed because you mixed signed in with the math. The cast only occurs after the math.

I don't know what you expect or what values you are using but guessing the following. As others have suggested "int" is an indeterminate size depending on the CPU and compiler options. But uint16_t and int16_t are the same everywhere. Get used to it.

OCR1B = (uint16_t)param1 - (uint16_t)param2;
OCR1A = (uint16_t)param1 - (uint16_t)param2 - (uint16_t)param3;

--
David Kelly N4HHE, dkelly@HiWAAY.net
========================================================================
Whom computers would destroy, they must first drive mad.

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.