how to move a variable from analogRead() function?

Hello people,

I´m having a problem im my code.. i mixed arduino(just for adc use) with c code and now i need to move the variable that i got from analogRead function to the timer´s compare match registers, how can i do that?

Please post your code so the forum can check for the best solution,

void conversao() { int adc; adc = analogRead(A0); OCR1AH = ??; OCR1AL = ?? TCCR1B = 0x9; // inicia o timer da fase A }

i dont know how to set the compare match with "adc" ... anyone knows?

thanks

What are you trying to accomplish? What outcome are you looking for?

Are you setting anything else to TCCR1A or TCCR1B other than WGM12 and CS10 elsewhere in your code? I think you are leaving some stuff out here that might be nice to know.

The easy way would be

OCR1A = adc;

I think.

You could break out the bytes from adc and set the high and low registers but the above works for me.

Yes man, i put just this part of the code because the really problem is about to set the compare match with the variable "adc" (adc = analogRead(A0);).. the others things i already tested and its working well =) can i really use OCR1A = adc ?

Well, the reason I state that is that there may be things going on in the other parts of your code affecting this part, as I can't see them as you have not posted them, nor have you explained what you are even trying to do making it difficult to give suggestions.

If you really have doubts about that assignment you can do this I suppose;

OCR1AH = (char) (adc>>8); //Edited in these parentheses, bit-shifts have strange precedence and I'm not sure if it would cast or shift first without.
OCR1AL = (char) adc;

would do the same thing.

edelll: Yes man, i put just this part of the code because the really problem is about to set the compare match with the variable "adc" (adc = analogRead(A0);).. the others things i already tested and its working well =) can i really use OCR1A = adc ?

Yes you can, and you should even use it always and not doing yourself the shifts and bitmaks, because using the OCR1A instead of the H and L registers the compiler knows what you want to do and will always generate the right code, give a thanks to all the persons that contributed and still contribute in the avr-libc/avr-gcc project

Very thanks for those people! Saved my life :grin: :grin: :grin: :grin: :grin:

Senso:

edelll: Yes man, i put just this part of the code because the really problem is about to set the compare match with the variable "adc" (adc = analogRead(A0);).. the others things i already tested and its working well =) can i really use OCR1A = adc ?

Yes you can, and you should even use it always and not doing yourself the shifts and bitmaks, because using the OCR1A instead of the H and L registers the compiler knows what you want to do and will always generate the right code, give a thanks to all the persons that contributed and still contribute in the avr-libc/avr-gcc project