What wrong with that counter ?

function [wr,count] = wr_blk(rst,clk,sig)
%#codegen
count = 0;
if (rst==0)
wr=0;
else
if((sig==1) && (clk==1))
count = count + 1;
else
count=0;
end
end
end
Thanks, Henry

9 comentarios

Torsten
Torsten el 9 de Dic. de 2015
You don't set return parameter "wr" in case that rst is not zero.
And - as far as I can see - there is one "end" too much.
Best wishes
Torsten.
Henry Buck
Henry Buck el 9 de Dic. de 2015
OK, One end is for if condition and the other is for the function.
About wr signal - that correct, I need to set it to 1 ifthe counter will increment to 4 (for example). By seting if condition, the counter will increment ?
Thanks, Henry
Torsten
Torsten el 9 de Dic. de 2015
The counter will never increment to 4 since you reset it to zero each time you call the function. Thus the maximum value count can be is one.
Best wishes
Torsten.
Henry Buck
Henry Buck el 9 de Dic. de 2015
thanks,
So, how can I count the numbers of clk, while sig is high ?
Henry Buck
Henry Buck el 9 de Dic. de 2015
what is wrong with this one :
if (rst==0)
count=0;
else
if(sig==1)
if(clk)
count = count + 1;
end
else
count=0;
end
end
if (rst==0) wr=0; else if((count==4)) wr=1; else wr=0; end end
Thanks, Henry
Torsten
Torsten el 9 de Dic. de 2015
Editada: Torsten el 9 de Dic. de 2015
count = sum((sig==1) & (clk==1));
if sig and clk are arrays of the same size.
Best wishes
Torsten.
Henry Buck
Henry Buck el 9 de Dic. de 2015
Thank you Torsten but I need the counter to be increment as long as sig is high - the counter will count the numbers of clk(as long as sig is high, other will zero).
Can I do it in a way as I wrote it above ?
Thanks, Henry
Torsten
Torsten el 9 de Dic. de 2015
I guess that's what my suggested solution does.
My line of code outputs the number of ones in the array clk at the positions where sig = 1.
Best wishes
Torsten.
Henry Buck
Henry Buck el 9 de Dic. de 2015
Thanks

Respuestas (0)

La pregunta está cerrada.

Etiquetas

Preguntada:

el 9 de Dic. de 2015

Cerrada:

el 20 de Ag. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by