Hi there. I'd like to ask for some help with a piece of code I'm trying to write.
I'm using MPLAB, and PIC 16F628A. Although this I could use for other things (not only PWM) that range from 0 to 100%.
Within the main program, I need to set the Dutycycle from 0% to 100% depending on some readings, to make things easy, it would be 0 for 0% all the way to 255 for 100%.
I'm using counters which increment at a fixed time length (with TMR0), as readings.
So I would have my first variable -which is a constant actually in the rest of the program- that determines the max value. I'll call it MAX, it would be a single byte and would normally be initialized at 80-100 or 180-200 (it may vary some more, but I don't think that makes much of a difference anyway).
Then a second variable, which goes from 0 to MAX. I'll call it DCycle since it determines the dutycycle.
a)I'm used to working with C so I first thought of a simple: (255/MAX)*DCycle. That would give me a value between 0 and 255.
b)Then I thought of a table. to which I pass the value of DCycle. However, since DCycle's max number isn't always the same, the last values of the table wouldn't always work.
So, I'm stuck here. Should I try approach a)? and start writing division and multiplication rutines. If so, I'd like some help there, since I think those rutines could be less complex than what I've found, given that I'm not doing it for ANY value, just for my readings which are kind of limited and I'm working with 8-bit numbers only.
Or b)? and do something about those values depending on MAX. I'm not sure if that can be done though. I would certainly need some guidance in that case
Any other approach is welcomed, I just didn't think of anything else.
Thanks to all who took the time to read this.
I apologize if this was already discussed in some thread, I looked for it but couldn't find anything.
By the way, note that I'm not that experienced in programming with Assembler.
I'm using MPLAB, and PIC 16F628A. Although this I could use for other things (not only PWM) that range from 0 to 100%.
Within the main program, I need to set the Dutycycle from 0% to 100% depending on some readings, to make things easy, it would be 0 for 0% all the way to 255 for 100%.
I'm using counters which increment at a fixed time length (with TMR0), as readings.
So I would have my first variable -which is a constant actually in the rest of the program- that determines the max value. I'll call it MAX, it would be a single byte and would normally be initialized at 80-100 or 180-200 (it may vary some more, but I don't think that makes much of a difference anyway).
Then a second variable, which goes from 0 to MAX. I'll call it DCycle since it determines the dutycycle.
a)I'm used to working with C so I first thought of a simple: (255/MAX)*DCycle. That would give me a value between 0 and 255.
b)Then I thought of a table. to which I pass the value of DCycle. However, since DCycle's max number isn't always the same, the last values of the table wouldn't always work.
So, I'm stuck here. Should I try approach a)? and start writing division and multiplication rutines. If so, I'd like some help there, since I think those rutines could be less complex than what I've found, given that I'm not doing it for ANY value, just for my readings which are kind of limited and I'm working with 8-bit numbers only.
Or b)? and do something about those values depending on MAX. I'm not sure if that can be done though. I would certainly need some guidance in that case
Any other approach is welcomed, I just didn't think of anything else.
Thanks to all who took the time to read this.
I apologize if this was already discussed in some thread, I looked for it but couldn't find anything.
By the way, note that I'm not that experienced in programming with Assembler.