Annexe A
Code arduino pour la commande SPWM [code]
#include "arduino.h"
#include "avr/pgmspace.h" #include "avr/io.h"
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
const byte sine256[] PROGMEM = {
127,130,133,136,139,143,146,149,152,155,158,161,164,167,170,173,176,178,181,184,187,190,192,195,198,20
0,203,205,208,210,212,215,217,219,221,223,225,227,229,231,233,234,236,238,239,240,
242,243,244,245,247,248,249,249,250,251,252,252,253,253,253,254,254,254,254,254,254,254,253,253,253,25
2,252,251,250,249,249,248,247,245,244,243,242,240,239,238,236,234,233,231,229,227,225,223,
221,219,217,215,212,210,208,205,203,200,198,195,192,190,187,184,181,178,176,173,170,167,164,161,158,15
5,152,149,146,143,139,136,133,130,127,124,121,118,115,111,108,105,102,99,96,93,90,87,84,81,78,
76,73,70,67,64,62,59,56,54,51,49,46,44,42,39,37,35,33,31,29,27,25,23,21,20,18,16,15,14,12,11,10,9,7,6,5,5,4,3
,2,2,1,1,1,0,0,0,0,0,0,0,1,1,1,2,2,3,4,5,5,6,7,9,10,11,12,14,15,16,18,20,21,23,25,27,29,31,
33,35,37,39,42,44,46,49,51,54,56,59,62,64,67,70,73,76,78,81,84,87,90,93,96,99,102,105,108,111,115,118,121,
124
};
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#define INPUT_DIR ((PINC&0x04)==0) // 00000010 = A2 //Control
Direction
int PWM1= 9; //PWM1 output, phase 1
int PWM2 = 10; //PWM2 output, phase 2
int PWM3 = 11; //PWM3 output, phase 3
int offset_1; //offset 1 is 120 degrees out of phase with
previous phase, Refer to PWM to sine.xls
int offset_2; //offset 2 is 120 degrees out of phase with offset
1. Refer to PWM to sine.xls
int ISR_exec_time = A4; //monitor how long the interrupt takes
int INVERTOR_ENABLE = A1; //INVERTOR ENABLE
double ad_cel; double spd_ref;
61
double spd_ref_max = 481; //60Hz
double spd_ref_min = 20; //2.5Hz
double speed;
unsigned char direction; // (0 forwared, 1 reverse)
unsigned char run;
int num = 0; // frequency
const double refclk=31376.6; // measured output frequency
//
const int ledPin = A5;
int ledState = LOW;
long previousMillis = 0;
long interval = 50000;
volatile byte current_count;
volatile byte ms4_delay;
volatile byte c4ms;
volatile unsigned long phase_accumulator;
volatile unsigned long tword_m;
void setup()
{
//Serial.begin(9600);
pinMode(PWM1, OUTPUT); pinMode(PWM2, OUTPUT); pinMode(PWM3,
OUTPUT); pinMode(ledPin, OUTPUT); pinMode(ISR_exec_time, OUTPUT);
pinMode(INVERTOR_ENABLE, OUTPUT); digitalWrite(INVERTOR_ENABLE, LOW);
//sbi(PORTB,program_exec_time); //Sets the pin
//digitalWrite(program_exec_time, HIGH);
Setup_timer0();
Setup_timer1();
Setup_timer2();
sbi (TIMSK2,TOIE2);
tword_m=pow(2,32)*speed/refclk; //calulate DDS new tuning word
//lcd.init(); lcd.begin(16,2);
lcd.clear();
digitalWrite(ledPin, HIGH); WaitLoop(30000);
digitalWrite(ledPin, LOW);
}
void loop()
{
while(1)
{
ReadAnalogs();
62
unsigned long currentMillis = millis(); // For ledState
// Control Power IR2111
if (speed > spd_ref_min){
offset_1 = 85;
offset_2 = 170;
run = 1;
digitalWrite(INVERTOR_ENABLE, HIGH);
}
else {
offset_1 = 0;
offset_2 = 0;
run = 0;
digitalWrite(INVERTOR_ENABLE, LOW);
}
// LCD HZ
num = (speed/8);
if(speed == spd_ref_min) num = 0;
lcd.setCursor(0, 0); lcd.print("TAN SO: ");lcd.print(num);
lcd.setCursor(10,0);lcd.print("Hz");
// Monitor program if((currentMillis - previousMillis) >
(interval/(num+1))) { // save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
//
if (c4ms > 0) // c4ms = 4ms, thus 4ms *250 = 1 second delay
{
c4ms=0; //Reset c4ms
cbi (TIMSK2,TOIE2); //Disable Timer2
Interrupt tword_m=pow(2,32)*speed/refclk; //Calulate DDS new tuning word
sbi (TIMSK2,TOIE2); //Enable Timer2 Interrupt
}
}
}
void WaitLoop(unsigned int time) {
unsigned int i,j;
for (j=0;j<time;j++)
63
{
for (i=0;i<200;i++) //the ATmega is runs at 16MHz
if (PORTC==0xFF) DDRB|=0x02; //just a dummy instruction
}
}
void ReadAnalogs(void) {
spd_ref=map(analogRead(0),0,1023,0,spd_ref_max); //Read
voltage on analog 1 to see desired output frequency, 0V = 0Hz, 5V =
1.023kHz
ad_cel=map(analogRead(3),0,1023,1,200); // Manat Add
if(spd_ref > spd_ref_max) spd_ref = spd_ref_max; // Manat add
maximum 60Hz if(spd_ref < spd_ref_min) spd_ref = 0; // Manat Add minimum
2.5Hz
if (INPUT_DIR)
{
if (direction==0) spd_ref=spd_ref_min;
if (speed==spd_ref_min) direction=1; //only allow direction
change at minimum speed
}
else
{
if (direction==1) spd_ref=spd_ref_min;
if (speed==spd_ref_min) direction=0; //only alow direction change
at minimum speed
}
//if (spd_ref>speed) speed=speed+0.02; // Hz step //if
(spd_ref<speed) speed=speed-0.02; if (spd_ref>speed)
speed=speed+(1/ad_cel); // Hz step if (spd_ref<speed)
speed=speed-(1/ad_cel); if (speed<spd_ref_min) speed=spd_ref_min;
}
void Setup_timer0(void)
{
TCCR0B = (TCCR0B & 0b11111000) | 0x02;
cbi (TCCR0A, COM0A0);
sbi (TCCR0A, COM0A1);
cbi (TCCR0A, COM0B0);
sbi (TCCR0A, COM0B1);
sbi (TCCR0A, WGM00);
cbi (TCCR0A, WGM01);
}
void Setup_timer1(void) {
64
TCCR1B = (TCCR1B & 0b11111000) |0x02;
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()
{
TCCR2B = (TCCR2B & 0b11111000) | 0x02; cbi (TCCR2A, COM2A0);
sbi (TCCR2A, COM2A1); cbi (TCCR2A, COM2B0); sbi (TCCR2A, COM2B1); sbi (TCCR2A,
WGM20); cbi (TCCR2A, WGM21); cbi (TCCR2B, WGM22);
}
ISR(TIMER2_OVF_vect)
{
//cbi(PORTC,program_exec_time); //Clear the pin
//sbi(PORTC,ISR_exec_time); // Sets the
pin //digitalWrite(program_exec_time, LOW);
digitalWrite(ISR_exec_time, HIGH); if (direction==0)
phase_accumulator=phase_accumulator+tword_m; else
phase_accumulator=phase_accumulator-tword_m;
if (run==0)
current_count=0;
else
current_count=phase_accumulator >> 24; // use upper 8 bits
of phase_accumulator as frequency
information
OCR1A = pgm_read_byte_near(sine256 + current_count); // read
value fron ROM sine table and send
to PWM
OCR1B = pgm_read_byte_near(sine256 + (uint8_t)(current_count +
offset_1)); // read value fron ROM sine table and send to PWM, 120 Degree out
of phase of PWM1
OCR2A = pgm_read_byte_near(sine256 + (uint8_t)(current_count +
offset_2));// read value fron ROM sine table and send to PWM, 120 Degree out of
phase of PWM2
//increment variable ms4_delay every 4mS/125 = milliseconds
32uS
if(ms4_delay++ == 125)
{
c4ms++;
ms4_delay=0; //reset count
}
65
digitalWrite(ISR_exec_time, LOW); }
[/code]
66
|
|