I cant get this to work. I only get a constant mean of 4.6V with no PWM and Frequency on the oscilloscope using Vin and GND to power UNO atmega328p. I think the flags are not being cleared because when I force TCCR1C I do get something but it is just the PWM and frequency jumping everywhere. I am trying to manipulate the Frequency and have PWM with WGM1 mode 11. I tried a combination of all these things but nothing works the way I want it to. Any ideas. Hear is my code.
#include <avr/io.h>
#include <avr/interrupt.h>
ISR(TIM1_COMPA);
ISR(TIM1_CAPT);
int analogInPin = A0;
int analogOutPin = 9;
void setup() {
pinMode(analogInPin, INPUT);
TCCR1A |= (1<<WGM11)|(1<<WGM10)|(1<<COM1A1)|(1<<COM1A0)|(0<<COM1B1)|(0<<COM1B0);
TCCR1B |= (0<<CS12)|(1<<CS11)|(0<<CS10)|(0<<WGM12)|(1<<WGM13); //WGM1 set mode 11, PWM, Phase Correct
TCCR1C |= (0<<FOC1A); //only for non-PWM
TIMSK1 |= (1<<TOIE1)|(1<<OCIE1A)|(0<<OCIE1B)|(1<<ICIE1); // I-flag in the Status Register is set with OCIE1A
OCR1A=500; //fOCnxPFCPWM equation pg.130 says use this for 1000Hz
//OCR1AH=500;
//OCR1AL=0;
// OCR1B=0;
TCNT1=250; //Figure 13-13 of https://www.sparkfun.com/datasheets/Components/SMD/ATMega328.pdf pg.134 says use half OCR1x
//Quote "The PWM waveform is generated by setting (or clearing) the OC1x Register at the compare match between
//OCR1x and TCNT1 when the counter increments, and clearing (or setting) the OC1x Register at compare
//match between OCR1x and TCNT1 when the counter decrements" pg.130 says set COM1A to mode 3 or 4
// TCNT1H=250;
// TCNT1L=0;
TIFR1 |= (1<<TOV1)|(1<<OCF1A)|(0<<OCF1B)|(0<<ICF1);
ACSR |= (0<<ACIS1)|(0<<ACIS0);
//SREG
TWAR |= (0<<TWGCE);
TWCR |= (0<<TWINT)|(0<<TWIE);
MCUSR |= (0<<PORF);
sei();
pinMode(9, OUTPUT);
}
void loop() {
int sensorValue = analogRead(analogInPin);
analogWrite(analogOutPin, sensorValue/4);
}
When setting individual bits in a register (or anywhere, for that matter), you can not shift a 0 and then or that into the variable. It does nothing. 0 ORed with X is simply X.
You can do that to set a bit, but you have to use the inverse to clear a bit (AND it with the negative)