I am slightly discouraged, please don’t take my post to be too curt. I wrote a very long message detailing everything I understood about the code I was implementing. I then proceeded to ask if anyone can see what I’m doing wrong. I clicked submit, but something to do with the time it took me to write my post meant I had to log in again, and so I lost whatever I tried to post. Nothing in the cache. Eventually I’ll try to rewrite [and post elsewhere] my understanding of everything, because I think that sort of newbie-guide is missing to timer information. I read a lot about timers to try and understand what I was doing [4-5 hours this weekend], but I felt there could be a bit more information.
Anyway, I suggest either
(1) i implemented this code [i’ll post it soon] incorrectly.
(2) there’s something in the arduino environment that is overriding my setup.
Here’s my initialization code:
TCCR1A = 0xA2; //Sets to 9-bit phase correct pwm. Note to self: Check the binary to figure out of it's the TOP set version or the regular version...I think it's regular. I don't set the TOP, it's assumed 9-bit.
OCR1A = 0; // 0% duty cycle on OC1A (PB1)
OCR1B = 0; // 0% duty cycle on OC1B (PB2)
TCCR1B = 1;
Explanation: TCCR1A is set to 162. This means I have 10100010. According to avrbeginners.net/architectur … html#tccra, I believe that means I’ve set COM1A1 = 1, and COM1A0 = 0. This means “Toggle OC1 on compare match.” So when timer1 = the value I put in for the duty cycle of pin 9 or 10, then they will toggle from HIGH to LOW and then [on the downcount] low to high. At the bottom two bits of 162, I can see “10”. This means PWM11=1, PWM10=0. This means Timer/Counter is 9-bit PWM. This means that it counts from 0 to 511. I hope I have also set it to phase-correct mode, so it counts from 0-511 and then 511-0.
Sidenote: I’m kind of curious why 10100010 was chosen. I got this number from, I believe actually ben’s code on these forums, but in reality it looks like only the first two and last two bits are used; the rest are empty…so couldn’t 10000010 work equally well?
Next I’ve set OCR1A and OCR1B to zero. This shouldn’t matter; just means a zero percent duty cycle for now.
TCCR1B is set to 1, which means NO prescale divisor. So timer1 ticks once per cycle, and at 20Mhz (baby orangutan here) means 20,000,000 times a second.
Here’s my function that changes the duty:
void HighFreqPWM(byte pin,int duty){ //Takes a duty of 0-511 for an input, does 19.6khz signal.
if (pin==9){
OCR1A = duty;
}
else{
OCR1B = duty;
}
}
So I change the value for the OCR1 that corresponds with the pin that I am trying to PWM (only either 9 or 10, the two pins connected to timer1.) The pin toggles when the timer reaches that value. So if you put something like 50, then for a brief period of time the pin is HIGH, until count=50 when it becomes LOW, and then it goes ~460 counts until 512, and then another ~460 counts from 512 to 50 again, and it becomes HIGH. Thus OCR1/511 = % duty cycle.
Since I am counting from 0-511, and then back down from 511-0, and ticking 20,000,000 increments a second, then I am making a cycle about 20mhz/1024 = ~19.5khz. This is exactly what I want to run my motor driver with an ultrasonic limit of 20Khz (the pololu one.)
However, my code doesn’t work! The driver doesn’t move, even though it works when I use the arduino enviroment analogwrite and don’t mess with the timer (i.e, I am running at ~610hz.) I DO have SLEW setup correctly for my >10khz signal.
[size=200]I am hoping I have written enough for someone else to offer insight. [/size]
I assume either
(1) I didn’t do what I wanted to do or think I’m doing.
(2) Don’t know that arduino environment is destroying what I’m trying to do
(3) Maybe - just thinking now- I am not implementing phase-correct PWM? If I’m not implementing phase-correctPWM [just fast-pwm] then I would be sending a 40Khz signal, which is faster than my driver can handle.