boa tarde pessoal
alguem me poderia ajudar a contruir duas ondas seno pwm??? uma espelhada da outra?? para controle de ponte H completa
#include "avr/pgmspace.h"
#include "avr/io.h"
PROGMEM prog_uchar sine256[] ={
0x80,0x83,0x86,0x89,0x8c,0x8f,0x92,0x95,0x98,0x9c,0x9f,0xa2,0xa5,0xa8,0xab,0xae,
0xb0,0xb3,0xb6,0xb9,0xbc,0xbf,0xc1,0xc4,0xc7,0xc9,0xcc,0xce,0xd1,0xd3,0xd5,0xd8,
0xda,0xdc,0xde,0xe0,0xe2,0xe4,0xe6,0xe8,0xea,0xec,0xed,0xef,0xf0,0xf2,0xf3,0xf5,
0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfc,0xfd,0xfe,0xfe,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfe,0xfd,0xfc,0xfc,0xfb,0xfa,0xf9,0xf8,0xf7,
0xf6,0xf5,0xf3,0xf2,0xf0,0xef,0xed,0xec,0xea,0xe8,0xe6,0xe4,0xe2,0xe0,0xde,0xdc,
0xda,0xd8,0xd5,0xd3,0xd1,0xce,0xcc,0xc9,0xc7,0xc4,0xc1,0xbf,0xbc,0xb9,0xb6,0xb3,
0xb0,0xae,0xab,0xa8,0xa5,0xa2,0x9f,0x9c,0x98,0x95,0x92,0x8f,0x8c,0x89,0x86,0x83,
0x80,0x7c,0x79,0x76,0x73,0x70,0x6d,0x6a,0x67,0x63,0x60,0x5d,0x5a,0x57,0x54,0x51,
0x4f,0x4c,0x49,0x46,0x43,0x40,0x3e,0x3b,0x38,0x36,0x33,0x31,0x2e,0x2c,0x2a,0x27,
0x25,0x23,0x21,0x1f,0x1d,0x1b,0x19,0x17,0x15,0x13,0x12,0x10,0x0f,0x0d,0x0c,0x0a,
0x09,0x08,0x07,0x06,0x05,0x04,0x03,0x03,0x02,0x01,0x01,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x02,0x03,0x03,0x04,0x05,0x06,0x07,0x08,
0x09,0x0a,0x0c,0x0d,0x0f,0x10,0x12,0x13,0x15,0x17,0x19,0x1b,0x1d,0x1f,0x21,0x23,
0x25,0x27,0x2a,0x2c,0x2e,0x31,0x33,0x36,0x38,0x3b,0x3e,0x40,0x43,0x46,0x49,0x4c,
0x4f,0x51,0x54,0x57,0x5a,0x5d,0x60,0x63,0x67,0x6a,0x6d,0x70,0x73,0x76,0x79,0x7c
};
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#define PWM_OUT_1 11
#define PWM_OUT_2 10
#define LED_PIN 13
#define TEST_PIN 7
#define OFFSET_2 170
double dfreq;
const double refclk = 31376.6;
const uint64_t twoTo32 = pow(2, 32);
volatile uint8_t icnt;
volatile uint8_t icnt1;
volatile uint8_t c4ms;
volatile uint32_t phase_accum;
volatile uint32_t tword_m;
void setup()
{
pinMode(LED_PIN, OUTPUT);
pinMode(TEST_PIN, OUTPUT);
pinMode(PWM_OUT_1, OUTPUT);
pinMode(PWM_OUT_2, OUTPUT);
setup_timer1();
setup_timer2();
cbi (TIMSK0, TOIE0);
sbi (TIMSK2, TOIE1);
dfreq = 50;
tword_m = twoTo32 * dfreq / refclk;
}
void loop()
{
if (c4ms > 250)
{
c4ms = 0;
dfreq = 1000;
cbi (TIMSK2, TOIE2);
tword_m = twoTo32 * dfreq / refclk;
sbi (TIMSK2, TOIE2);
}
}
void setup_timer1(void)
{
sbi (TCCR1B, CS10);
cbi (TCCR1B, CS11);
cbi (TCCR1B, CS12);
cbi (TCCR1A, COM1A0);
sbi (TCCR1A, COM1A1);
cbi (TCCR1A, COM1B0);
sbi (TCCR1A, COM1B1);
sbi (TCCR1A, WGM10);
cbi (TCCR1A, WGM11);
cbi (TCCR1B, WGM12);
cbi (TCCR1B, WGM13);
}
void setup_timer2(void)
{
sbi (TCCR2B, CS20);
cbi (TCCR2B, CS21);
cbi (TCCR2B, CS22);
cbi (TCCR2A, COM2A0);
sbi (TCCR2A, COM2A1);
sbi (TCCR2A, WGM20);
cbi (TCCR2A, WGM21);
cbi (TCCR2B, WGM22);
}
ISR(TIMER2_OVF_vect)
{
sbi(PORTD, TEST_PIN);
phase_accum += tword_m;
icnt = phase_accum >> 24;
OCR2A = pgm_read_byte_near(sine256 + icnt);
OCR1B = pgm_read_byte_near(sine256 + (uint8_t)(icnt + OFFSET_2));
if (icnt1++ == 125)
{
c4ms++;
icnt1 = 0;
}
cbi(PORTD, TEST_PIN);
}
Tags:
Nao e bem como uma entrada e high e outra e low esse programa tinha noutro pc
Oi Carlos,
algo assim; (Não testei para ver se tem erros de lógica ou de sintaxe.)
Ligue a saída D10 PWM x em D4
Ligue a saída D11 PWM x em D6
inclua
const int PMWx = 4;
const int PMWy = 6;
const int PMWxS = 5;
const int PMWyS = 7;
int PWM_A = 0;
int PWM_B = 0;
em void setup()
pinMode(PWMx, INPUT);
pinMode(PWMy, INPUT);
pinMode(PWMxS, OUTPUT);
pinMode(PWMyS, OUTPUT);
em algum lugar do void loop()
PWM_A = digitalRead(PWMx);
PWM_B = digitalRead(PWMy);
if (PWM_A ==HIGH) PWMxS = LOW;
else PWMxS = HIGH;
if (PWM_B ==HIGH) PWMyS =LOW;
else PWMyS = HIGH;
tenho aqui o meu sketch de high and low
#include "avr/pgmspace.h"
// table of 256 sine values / half sine period / stored in flash memory
PROGMEM prog_uchar sine256[] = {
1,3,6,8,11,14,17,20,23,25,28,31,34,37,39,42,45,48,50,53,56,59,61,64,67,69,72,75,77,80,83,85,
88,91,93,96,98,101,103,106,108,111,113,116,118,121,123,125,128,130,132,135,137,139,142,144,
146,148,150,152,154,157,159,161,163,165,167,169,170,172,174,176,178,180,181,183,185,186,188,
190,191,193,194,196,197,199,200,201,203,204,205,207,208,209,210,211,212,214,215,216,217,217,
218,219,220,221,222,222,223,224,224,225,226,226,227,227,228,228,228,229,229,229,229,230,230,
230,230,230,230,230,230,230,230,230,229,229,229,229,228,228,228,227,227,226,226,225,224,224,
223,222,222,221,220,219,218,217,217,216,215,214,212,211,210,209,208,207,205,204,203,201,200,
199,197,196,194,193,191,190,188,186,185,183,181,180,178,176,174,172,170,169,167,165,163,161,
159,157,154,152,150,148,146,144,142,139,137,135,132,130,128,125,123,121,118,116,113,111,108,
106,103,101,98,96,93,91,88,85,83,80,77,75,72,69,67,64,61,59,56,53,50,48,45,42,39,37,34,31,28,
25,23,20,17,14,11,8,6,3,
};
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
double dfreq;
// const double refclk=320000; // =16MHz / 50Hz
const double refclk=312500; // measured
// variables used inside interrupt service declared as voilatile
volatile byte icnt; // var inside interrupt
volatile byte icnt1; // var inside interrupt
volatile byte c4ms; // counter increment
volatile unsigned long phaccu; // phase accumulator
volatile unsigned long tword_m; // dds tuning word m
boolean toggle1 = 0;
void setup()
{
pinMode(9, OUTPUT); // sets the digital pin as output
pinMode(10, OUTPUT); // sets the digital pin as output
pinMode(11, OUTPUT); // pin11= PWM output / frequency output
Setup_timer1();
cli(); //stop interrupts
//set timer1 interrupt at 100Hz
TCCR1A = 0; // set entire TCCR1A register to 0
TCCR1B = 0; // same for TCCR1B
TCNT1 = 0; //initialize counter value to 0
// set compare match register for 100hz increments
OCR1A = 155.25; // = (16*10^6) / (100*1024) - 1 (must be <65536)
// turn on CTC mode
TCCR1B |= (1 WGM12);
// Set CS12 and CS10 bits for 1024 prescaler
TCCR1B |= (1 CS12) | (1 CS10);
// enable timer compare interrupt
TIMSK1 |= (1 OCIE1A);
sei();//allow interrupts
Setup_timer2();
// disable interrupts to avoid timing distortion
cbi (TIMSK0,TOIE0); // disable Timer0 !!! delay() is now not available
sbi (TIMSK2,TOIE2); // enable Timer2 Interrupt
dfreq=1000.0; // initial output frequency = 1000.o Hz
tword_m=pow(2,32)*dfreq/refclk; // calulate DDS new tuning word
}
void loop()
{
while(1) {
if (c4ms > 256) { // timer / wait fou a full second
c4ms=0;
cbi (TIMSK2,TOIE2); // disable Timer2 Interrupt
tword_m=pow(2,32)*dfreq/refclk; // calulate DDS new tuning word
sbi (TIMSK2,TOIE2); // enable Timer2 Interrupt
}
}
}
//******************************************************************
// timer1 setup
void Setup_timer1(){
}
// timer2 setup
// set prscaler to 1, PWM mode to phase correct PWM, 16000000/510 = 31372.55 Hz clock
void Setup_timer2() {
// Timer2 Clock Prescaler to : 1
sbi (TCCR2B, CS20);
cbi (TCCR2B, CS21);
cbi (TCCR2B, CS22);
// Timer2 PWM Mode set to Phase Correct PWM
cbi (TCCR2A, COM2A0); // clear Compare Match
sbi (TCCR2A, COM2A1);
sbi (TCCR2A, WGM20); // Mode 1 / Phase Correct PWM
cbi (TCCR2A, WGM21);
cbi (TCCR2B, WGM22);
}
//******************************************************************
// Timer1 Interrupt Service at 100 Hz
//generates pulse wave of frequency 100Hz/2 = 50Hz (takes two cycles for full wave- toggle high then toggle low)
ISR(TIMER1_COMPA_vect){ //timer1 interrupt 100Hz toggles pin 9,10
if (toggle1){
digitalWrite(9,HIGH);
digitalWrite(10,LOW);
toggle1=0;
}
else{
digitalWrite(9,LOW);
digitalWrite(10,HIGH);
toggle1= 1;
}
}
// Timer2 Interrupt Service at 320 KHz = 3.125uSec
// this is the timebase REFCLOCK for the DDS generator
// FOUT = (M (REFCLK)) / (2 exp 32)
ISR(TIMER2_OVF_vect) {
phaccu=phaccu+tword_m; // soft DDS, phase accu with 32 bits
icnt=phaccu >> 24; // use upper 8 bits for phase accu as frequency information
// read value fron ROM sine table and send to PWM DAC
OCR2A=pgm_read_byte_near(sine256 + icnt);
if(icnt1++ == 1) { // increment variable c4ms
c4ms++;
icnt1=0;
}
}
tinha estes dois programas
Carlos, tem varios erros de sintaxe, corrigi e testei a saída ficou muito lenta.
Vou continuar testando.
Rui
Bgdao Rui...encontrei hj outro tipo de erro e que eu tenho que dar um intervalo aquele low i high PK senão a ponte H podia queimar....agora com a porta NOT que me explicou eu NK iria e asseguro que não queimo a ponte H
#include <stdint.h>
#include <avr/interrupt.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#define SAMPLE_RATE 8000
const int sinewave_length=256;
const unsigned char sinewave_data[] PROGMEM = {
0x80,0x83,0x86,0x89,0x8c,0x8f,0x92,0x95,0x98,0x9c,0x9f,0xa2,0xa5,0xa8,0xab,0xae,
0xb0,0xb3,0xb6,0xb9,0xbc,0xbf,0xc1,0xc4,0xc7,0xc9,0xcc,0xce,0xd1,0xd3,0xd5,0xd8,
0xda,0xdc,0xde,0xe0,0xe2,0xe4,0xe6,0xe8,0xea,0xec,0xed,0xef,0xf0,0xf2,0xf3,0xf5,
0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfc,0xfd,0xfe,0xfe,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfe,0xfd,0xfc,0xfc,0xfb,0xfa,0xf9,0xf8,0xf7,
0xf6,0xf5,0xf3,0xf2,0xf0,0xef,0xed,0xec,0xea,0xe8,0xe6,0xe4,0xe2,0xe0,0xde,0xdc,
0xda,0xd8,0xd5,0xd3,0xd1,0xce,0xcc,0xc9,0xc7,0xc4,0xc1,0xbf,0xbc,0xb9,0xb6,0xb3,
0xb0,0xae,0xab,0xa8,0xa5,0xa2,0x9f,0x9c,0x98,0x95,0x92,0x8f,0x8c,0x89,0x86,0x83,
0x80,0x7c,0x79,0x76,0x73,0x70,0x6d,0x6a,0x67,0x63,0x60,0x5d,0x5a,0x57,0x54,0x51,
0x4f,0x4c,0x49,0x46,0x43,0x40,0x3e,0x3b,0x38,0x36,0x33,0x31,0x2e,0x2c,0x2a,0x27,
0x25,0x23,0x21,0x1f,0x1d,0x1b,0x19,0x17,0x15,0x13,0x12,0x10,0x0f,0x0d,0x0c,0x0a,
0x09,0x08,0x07,0x06,0x05,0x04,0x03,0x03,0x02,0x01,0x01,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x02,0x03,0x03,0x04,0x05,0x06,0x07,0x08,
0x09,0x0a,0x0c,0x0d,0x0f,0x10,0x12,0x13,0x15,0x17,0x19,0x1b,0x1d,0x1f,0x21,0x23,
0x25,0x27,0x2a,0x2c,0x2e,0x31,0x33,0x36,0x38,0x3b,0x3e,0x40,0x43,0x46,0x49,0x4c,
0x4f,0x51,0x54,0x57,0x5a,0x5d,0x60,0x63,0x67,0x6a,0x6d,0x70,0x73,0x76,0x79,0x7c};
int pwm = 6;
int pwm2 = 3;
volatile uint16_t sample;
ISR(TIMER1_COMPA_vect) {
if (sample >= sinewave_length) {
sample = -1;
}
else {
OCR0A = pgm_read_byte(&sinewave_data[sample]);
}
++sample;
}
void startPlayback(){
pinMode(outputPin, OUTPUT);
TCCR0A |= _BV(WGM01) | _BV(WGM00);
TCCR0B &= ~_BV(WGM02);
// Do non-inverting PWM on pin OC0A, arduino digital pin 6
TCCR0A = (TCCR0A | _BV(COM0A1)) & ~_BV(COM0A0);
TCCR0A &= ~(_BV(COM0B1) | _BV(COM0B0));
TCCR0B = (TCCR0B & ~(_BV(CS02) | _BV(CS01))) | _BV(CS00);
OCR0A = pgm_read_byte(&sinewave_data[0]);
cli();
TCCR1B = (TCCR1B & ~_BV(WGM13)) | _BV(WGM12);
TCCR1A = TCCR1A & ~(_BV(WGM11) | _BV(WGM10));
TCCR1B = (TCCR1B & ~(_BV(CS12) | _BV(CS11))) | _BV(CS10);
OCR1A = F_CPU / SAMPLE_RATE;
TIMSK1 |= _BV(OCIE1A);
sample = 0;
sei(); // enable interrupts
}
void setup()
{
startPlayback();
}
void loop()
{
while (true){
}
}
void starsoft()
{
digitalWrite(6, HIGH);
digitalWrite(3, LOW);
delayMicroseconds(10);
digitalWrite(5, HIGH);
digitalWrite(3, LOW);
delayMicroseconds(10);
}
uma versão mais simples mas falta me um pino a LOW
Bem-vindo a
Laboratorio de Garagem (arduino, eletrônica, robotica, hacking)
© 2024 Criado por Marcelo Rodrigues. Ativado por