• 已解决 73482 个问题
  • 已帮助 5993 位优秀工程师

请大家帮忙将两份代码合并为一份代码

劳特伦 2017-08-08 浏览量:1943

  问题: 如何让这两份arduino代码合成为一份且能达到两份分别独立工作的效果?我有的开发板:UNO,mega2560,Due.

  项目描述:目前再做一个用arduino作为主控的正弦波逆变器,第一部分电路用arduino产生占空比互补的两路Pwm通过驱动电路给Mosfet经过变压器实现dc12v to dc350v的效果,第二部分电路dc350v输入到由另外两路互补sin码pwm控制H桥逆变逆变电路,经过变压器滤波后出来一个波形很好的正弦波。两部分电路经过两份Pwm产生的代码仿真结果均达到预期结果。

  问题描述: 目前有两份代码,第一份是用定时器控制9,10引脚输出电平相反的pwm波。如图

   yh1.png

第二份是用定时器控制5,6引脚输出通过频率计算sin数组的pwm波,经过H桥逆变成为正弦波,如图

yh2.png

但是两份代码好像都用到了定时器1,当他们一起工作时仅仅只有第5脚输出了之前单独测试的波形,因为本人水平有限,没能搞清这两份代码之间具体的冲突细节在哪,想请各位能够帮忙修改一下代码,使两份能合成为一份能输出4路电路需要的pwm波从而使电路能够正常工作达到12v直流电逆变为220v50Hz正弦交流电的目的​,开发板可以从我有的三种里任选(如果UNO满足不了硬件需要的话),如果实在不能用一个处理器来完成这些事的话能否采用双处理器的方式来完成?


第一份代码
#include <TimerOne.h>

//#include "TimerOne.h"               // include TimerOne.h
#define low_battery_voltage 10.2               // define high battery voltage limit as 14.2
#define high_battery_voltage 14.4               // define low battery voltage limit as 10.2
int dutycycle = 0;               // Initailize duty cylce variable as integer data type
int sense_value =0;               // Initialize sense_value variable to capture the adc reading of battery voltage (range from 0 to 1023)
float battery_voltage = 0.0;               // Initialize battery_voltage variable as a float data type to convert sense_value to actual battery voltage
void battery_voltage_measurement()               // battery_voltage_measurement function starts
{ sense_value = (analogRead(A0));               // read battery voltage on pin A0 & capture that value in sense_value variable
                                             // {warning - arduino pin accept only upto 5v so don't forget to map upper 
                                             //  battery volatge i.e 14.2v to 5v using voltage divider resistor network}
                                                 
 battery_voltage = sense_value * (14.4/1023.0);               // convert sense_value (0 to 1023) to range (0 to 14.2)
 if(battery_voltage < 14.4 && battery_voltage > 10.2) // if battery voltage is under limit i.e between 10.2 and 14.2 then dutycycle will be 150
        {
          dutycycle = 512;
        }
                                                 
                                                
 else if(battery_voltage < 10.2 || battery_voltage > 14.4)              // if battery voltage is below 10.2v or above 14.2v , 
       {
          dutycycle = 512;         //set the duty cycle to 0 and inverter will go in cutoff mode
       }                                      
                                                 
                                                
}              // battery_voltage_measurement function ends                                                 
                                                 
void setup()               // Setup function
{
  pinMode (1,OUTPUT);              // set pin 9 as an output pin for pwm
  pinMode (1,OUTPUT);             // set pin 10 as an output pin for pwm 
  Timer1.initialize(20000);             // Initailize timer1 time period as 20 milli second (50 Hz frequency)
  Timer1.attachInterrupt(battery_voltage_measurement);      // battery_voltage_measurement function will be executed every 20 milli second using timer 1 overflow interrupt
  TCCR1A = (TCCR1A & 0x0F) | 0xB0 ;             // set pin 10 inverted of pin 9
}  
   

                                                 
                                                 
 void loop()              // loop function starts
{
  Timer1.pwm(9,dutycycle,20000);              // Timer1.pwm function takes argument as (pin no. , dutycycle , time period)
  Timer1.pwm(10,1023-dutycycle,20000);                         
                                                                          
}               // loop function ends

第二份代码
int i=0;
int x=0;
int OK=0;
int sinPWM[]={1,2,5,7,10,12,15,17,19,22,24,27,30,32,34,37,39,42,

44,47,49,52,54,57,59,61,64,66,69,71,73,76,78,80,83,85,88,90,92,94,97,99,

101,103,106,108,110,113,115,117,119,121,124,126,128,130,132,134,136,138,140,142,144,146,

148,150,152,154,156,158,160,162,164,166,168,169,171,173,175,177,178,180,182,184,185,187,188,190,192,193,

195,196,198,199,201,202,204,205,207,208,209,211,212,213,215,216,217,219,220,221,222,223,224,225,226,227,

228,229,230,231,232,233,234,235,236,237,237,238,239,240,240,241,242,242,243,243,244,244,245,245,246,246,

247,247,247,248,248,248,248,249,249,249,249,249,250,250,250,250,249,249,249,249,249,248,

248,248,248,247,247,247,246,246,245,245,244,244,243,243,242,242,241,240,240,239,238,237,237,236,235,234,

233,232,231,230,229,228,227,226,225,224,223,222,221,220,219,217,216,215,213,212,211,209,208,207,205,204,

202,201,199,198,196,195,193,192,190,188,187,185,184,182,180,178,177,175,173,171,169,168,166,164,162,160,

158,156,154,152,150,148,146,144,142,140,138,136,134,132,130,128,126,124,121,119,117,115,113,110,108,106,

103,101,99,97,94,92,90,88,85,83,80,78,76,73,71,69,66,64,61,59,57,54,52,49,47,44,42,39,37,34,32,30,

27,24,22,19,17,15,12,10,7,5,2,1};

//First value is 1 because we want to reduce the dead time betwen half cycles of sine signal.

//To write the duty cycles we will use OCR0A and OCR0B for timer 0(pins  5 and 6), that means for one half cycle OCR0A will be equal with every component of vector myPWM and for other half cycle OCR0B will do that-see the post with Timer 0.

//With the program below we generate phase correct signal at a 31372 Hz and 100 duty cycle(is between 0 and 255 on Timer 0).

void setup() {
pinMode(5, OUTPUT);
pinMode(6,OUTPUT);
cli();// stop interrupts
TCCR0A=0;//reset the value
TCCR0B=0;//reset the value
TCNT0=0;//reset the value
//0b allow me to write bits in binary
TCCR0A=0b10100001;//phase correct pwm mode
TCCR0B=0b00000001; //no prescaler
TCCR1A=0;//reset the value
TCCR1B=0;//reset the value
TCNT1=0;//reset the value
OCR1A=509;// compare match value
TCCR1B=0b00001001; //WGM12 bit is 1 and no prescaler

TIMSK1 |=(1 << OCIE1A);

sei();// enable interrupts
}
ISR(TIMER1_COMPA_vect){// interrupt when timer 1 match with OCR1A value
if(i>313 && OK==0){// final value from vector for pin 6
i=0;// go to first value of vector
OK=1;//enable pin 5
}
if(i>313 && OK==1){// final value from vector for pin 5
i=0;//go to firs value of vector
OK=0;//enable pin 6
}
x=sinPWM[i];// x take the value from vector corresponding to position i(i is zero indexed)
i=i+1;// go to the next position
if(OK==0){
OCR0B=0;//make pin 5 0
OCR0A=128;//enable pin 6 to corresponding duty cycle
}
if(OK==1){
OCR0A=0;//make pin 6 0
OCR0B=128;//enable pin 5 to corresponding duty cycle
}
}
void loop() {

}

0 0 收起

我来回答

上传资料:
选择文件 文件大小不超过15M(格式支持:doc、ppt、xls、pdf、zip、rar、txt)
所有亮答 数量:3
  • 额。。。是有点难,搞了半天还是不行,想用3525代替两路了。。。

  • 如果两个程序合一块你就要改改结构了

  • 这种程序一般都要重写,可以复制部分代码,但框架要重新构建
电子老工程师 回复了 :1.BUK6Y24-40PX 不是完全替代,有货,但是参数没有原型号参数高。 2.PSMN8R2-80YS,115,不是完全替代,这个电流大 回复

相关问题

问题达人换一批

请大家帮忙将两份代码合并为一份代码