Re: Code Generated for Fixed Point numbers
Simulink handling of fractional datatype is working great (using it for many years, never encountered issue with datatype handling yet)
Let try providing a shed light on datatype representation with simulink.
You perform an ADC conversion and expect a fractional output.
The ADC block is configured for signed conversion with left align of the result (or should be set like this).
Note that whatever the ADC block configuration, its output is always a pure integer: the ADC peripheral output conversion register.
Your signed left aligned result is within +-32767/8. (with 4 or 8 LSB set with 0)
You must indicate your model that this output represents a fractional "Q1.15".
==> add the Data type Converter block after the ADC block.
1) You are modifying the representation: select "Stored Integer" instead of Real Word Value. The bitfield is not modified (not converted)
2) Specify the datatype it represents.
The datatype converter block output is a fractional which represent values within +-1 (roughly).
Now your ADC output through the Conversion block provide values within [-1 1] ; you can perform the comparisons with the values 0.5 and 0.8.
Note that in the generated code, everything is handled as integer.
Simulink will handle any shift required in an efficient way (in your case here, you should not see any shift).
You benefit from fractional representation at Simulink design level, and efficient code generated underneath.
A side remark: you might directly make the datatype converter block output to represent your physical value. If it is volt, setting the slope (value of the LSB) to 3.3/65535 and your output datatype represent Volts (with no calculation time cost so far).
You might post your model if you still encounter issue. Simulink docs might provide examples with datatypes.
Maybe the fixed-point toolbox is required to do advanced things with datatype.