How can i decrement a value after it reached a maximum

I have a 12 bit DAC And i want to send data by incrementing a variable from 0 to 4096 and after that continue to send data by decrementing from the Last point (4096) to the first one (0). My Code stops when 'i' reach the maximum (in my case the 'V' set from my keyboard)
Code:
For i=0:V
If i < V+1
Fwrite()
I=i+1
Else i > V-1
Fwrite()
I=i-1
End
End

3 comentarios

madhan ravi
madhan ravi el 15 de Ag. de 2018
Editada: madhan ravi el 15 de Ag. de 2018
Can you show an example?
Well i have a 12 bit DAC so 4096 points. I want to generate a sine wave so i was thinking about increment a variable from 0 to 4096 and decrement from 4096 to 0 but in the code that i posted after the variable reach 4096 it stops.
I attached an Image of the code.

Iniciar sesión para comentar.

 Respuesta aceptada

vals = uint16([0 : V, (V-1) : -1 : 0]);
for i = 1 : length(vals)
fwrite(DEVICE, vals(i), 'uint16');
end
However, you need to pay attention to the byte order your device needs, and to how it wants to encode the 12 bits into 16 bits.

2 comentarios

Thank you very much! Well the data that i send must be the amplitude of a sine wave so basicaly a sigle Information, for the future i will try to send amplitude And frequency.
Any time you are sending more than 8 bits for a piece of information, you need to worry about the byte order.
If you have uint8(258), binary 0000000100000010, in MATLAB on any implementation over the last several years, then that is always stored in memory as the byte pair [2, 1], binary 00000010 followed by 00000001 . This is called "little endian", which is a situation in which the address of a piece of data is the address of it's "little end" (least significant byte) and the significance of the bytes increase in increasing memory address until you reach the most significant byte (the one that has the larger contribution to the numeric value.)
But that might not be the same byte order as the equipment you are talking to. The equipment might be using "big endian", which is a situation in which the address of a piece of data is the address of it's "big end" (most significant byte) and the significance of the bytes decreases in increasing memory until you reach the least significant byte (the one that has the least contribution to the numeric value.) Western mathematics tends to write values in "big endian" format.

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by