const int analogInPin = 11;
const int pwmOutPin = PA1;
int sensorValue = 0;
int outputValue = 0;
HardwareTimer pwmtimer(2);
void setup() {
pinMode(analogInPin, INPUT_ANALOG);
Serial.begin(115200);
pinMode(pwmOutPin, PWM);
pwmtimer.pause();
pwmtimer.setPrescaleFactor(1);
pwmtimer.setCount(0);
//pwmtimer.setOverflow(4000);
pwmtimer.setCompare(TIMER_CH2, 1000);
pwmtimer.refresh();
pwmtimer.resume();
pinMode(pwmOutPin, PWM);
}
void loop() {
sensorValue = analogRead(analogInPin);
outputValue = map(sensorValue, 0, 4095, 0, 65535);
pwmWrite(pwmOutPin, outputValue);
Serial.print("sensor = " );
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);
}
http://leaflabs.com/docs/lang/api/analogwrite.html
So the maximum is about 1.1KHZ.
I didn’t researched further (creating pwm from the core), because I switched to an octal DAC for my project.
HardwareTimer pwmtimer(2);
void setup() {
// pinMode(11, INPUT_ANALOG);
// Serial.begin(115200);
pinMode(PA1, PWM);
pwmtimer.setPrescaleFactor(1);
pwmtimer.setPeriod(50);
}I am looking for complementary PWM output from a couple of pins, as part of my micro-solar inverter project
Has anyone got code to support this timer mode – that is compatible with PWMwrite function?
Thanks in advance
Ken
I am looking for complementary PWM output from a couple of pins, as part of my micro-solar inverter project
Has anyone got code to support this timer mode – that is compatible with PWMwrite function?
Thanks in advance
Ken
I really just wanted to know if the means to select the complimentary mode was included within the “Leaf Labs” library implementation.
Ken
I really just wanted to know if the means to select the complimentary mode was included within the “Leaf Labs” library implementation.
Ken
Don’t cross post.
Several people have replied to your other thread.
I will remove your posting to this thread as its unnecessary
HardwareTimer pwmtimer(2);
void setup() {
// pinMode(11, INPUT_ANALOG);
// Serial.begin(115200);
pinMode(PA1, PWM);
pwmtimer.setPrescaleFactor(1);
pwmtimer.setPeriod(50);
}You code does not work, because you pwmWrite is in the loop and it is permanently overwriting the signal.
Here is a code which work
#define PWM_OUT PA8 //PWM output
#define PWM_OUT_COMP PB13 //complementary output
void setup(){
// initialize digital pin PC13 as an output.
pinMode(PC13, OUTPUT);
HardwareTimer timer1 = HardwareTimer(1);
timer1.setPrescaleFactor(1);
timer1.setPeriod(2041); //490
//timer1.setPeriod(10); //100kHz
pinMode(PWM_OUT, PWM); //Ideally these would be done as bit wise as I dont know exactly whats being set here..
pinMode(PWM_OUT_COMP, PWM);
timer_dev *t = TIMER1; //refers t to Timer 8 memory location, how to read back?
timer_reg_map r = t->regs;
bitSet(r.adv->CCER,0); //this should enable complimentary outputs
bitSet(r.adv->CCER,2);
pwmWrite(PWM_OUT, 10); //
}
void loop(){
// I am alive
digitalWrite(PC13, HIGH); // turn the LED on (HIGH is the voltage level)
//digitalWrite(PA5, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(PC13, LOW); // turn the LED off by making the voltage LOW
//digitalWrite(PA5, LOW); // turn the LED off by making the voltage LOW
delay(1000);
}


