การเข้าผลสมาบัติ#อารมณ์พระนิพพาน#จึงมุ่งสู่พุทธภูมิ#เธออย่าได้กล่าวคำหยา...
<pre>
<span style="color: #7E7E7E;">// motor x5 mit ATMEGA 2560 PWM 4KHZ</span>
#include <span style="color: #006699;">"arduino.h"</span> <span style="color: #7E7E7E;">//Store data in flash (program) memory instead of SRAM</span>
#include <span style="color: #006699;">"avr/pgmspace.h"</span>
#include <span style="color: #006699;">"avr/io.h"</span>
<span style="color: #CC6600;">const</span> <span style="color: #CC6600;">byte</span> 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,200,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,252,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,155,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, <span style="color: #CC6600;">bit</span>) (_SFR_BYTE(sfr) &= ~_BV(<span style="color: #CC6600;">bit</span>)) <span style="color: #7E7E7E;">//define a bit to have the properties of a clear bit operator</span>
#define sbi(sfr, <span style="color: #CC6600;">bit</span>) (_SFR_BYTE(sfr) |= _BV(<span style="color: #CC6600;">bit</span>))<span style="color: #7E7E7E;">//define a bit to have the properties of a set bit operator</span>
<span style="color: #CC6600;">int</span> PWM1= 2;<span style="color: #7E7E7E;">// PWM1 output, phase 1</span>
<span style="color: #CC6600;">int</span> PWM2 = 3; <span style="color: #7E7E7E;">//[WM2 ouput, phase 2</span>
<span style="color: #CC6600;">int</span> PWM3 = 4; <span style="color: #7E7E7E;">//PWM3 output, phase 3</span>
<span style="color: #CC6600;">int</span> PWM4= 5;<span style="color: #7E7E7E;">// PWM1 output, phase 1</span>
<span style="color: #CC6600;">int</span> PWM5 = 6; <span style="color: #7E7E7E;">//[WM2 ouput, phase 2</span>
<span style="color: #CC6600;">int</span> PWM6 = 7; <span style="color: #7E7E7E;">//PWM3 output, phase 3</span>
<span style="color: #CC6600;">int</span> PWM7 = 8;<span style="color: #7E7E7E;">// PWM1 output, phase 1</span>
<span style="color: #CC6600;">int</span> PWM8 = 9; <span style="color: #7E7E7E;">//[WM2 ouput, phase 2</span>
<span style="color: #CC6600;">int</span> PWM9 = 10; <span style="color: #7E7E7E;">//PWM3 output, phase 3</span>
<span style="color: #CC6600;">int</span> PWM10 = 11;<span style="color: #7E7E7E;">// PWM1 output, phase 1</span>
<span style="color: #CC6600;">int</span> PWM11 = 12; <span style="color: #7E7E7E;">//[WM2 ouput, phase 2</span>
<span style="color: #CC6600;">int</span> PWM12 = 13; <span style="color: #7E7E7E;">//PWM3 output, phase 3</span>
<span style="color: #CC6600;">int</span> PWM13 = 44;<span style="color: #7E7E7E;">// PWM1 output, phase 1</span>
<span style="color: #CC6600;">int</span> PWM14 = 45; <span style="color: #7E7E7E;">//[WM2 ouput, phase 2</span>
<span style="color: #CC6600;">int</span> PWM15 = 46; <span style="color: #7E7E7E;">//PWM3 output, phase 3</span>
<span style="color: #CC6600;">int</span> offset_1 = 85; <span style="color: #7E7E7E;">//offset 1 is 120 degrees out of phase with previous phase, Refer to PWM to sine.xls</span>
<span style="color: #CC6600;">int</span> offset_2 = 170; <span style="color: #7E7E7E;">//offset 2 is 120 degrees out of phase with offset 1. Refer to PWM to sine.xls</span>
<span style="color: #CC6600;">int</span> program_exec_time = 52; <span style="color: #7E7E7E;">//monitor how quickly the interrupt trigger</span>
<span style="color: #CC6600;">int</span> ISR_exec_time = 53; <span style="color: #7E7E7E;">//monitor how long the interrupt takes</span>
<span style="color: #CC6600;">double</span> dfreq;
<span style="color: #CC6600;">const</span> <span style="color: #CC6600;">double</span> refclk=31376.6; <span style="color: #7E7E7E;">// measured output frequency</span>
<span style="color: #7E7E7E;">// variables used inside interrupt service declared as voilatile</span>
volatile <span style="color: #CC6600;">byte</span> current_count; <span style="color: #7E7E7E;">// Keep track of where the current count is in sine 256 array</span>
volatile <span style="color: #CC6600;">byte</span> ms4_delay; <span style="color: #7E7E7E;">//variable used to generate a 4ms delay</span>
volatile <span style="color: #CC6600;">byte</span> c4ms; <span style="color: #7E7E7E;">// after every 4ms this variable is incremented, its used to create a delay of 1 second</span>
volatile <span style="color: #CC6600;">unsigned</span> <span style="color: #CC6600;">long</span> phase_accumulator; <span style="color: #7E7E7E;">// pahse accumulator</span>
volatile <span style="color: #CC6600;">unsigned</span> <span style="color: #CC6600;">long</span> tword_m; <span style="color: #7E7E7E;">// dds tuning word m, refer to DDS_calculator (from Martin Nawrath) for explination.</span>
<span style="color: #CC6600;">void</span> <span style="color: #CC6600;"><b>setup</b></span>()
{
<span style="color: #CC6600;">pinMode</span>(PWM1, <span style="color: #006699;">OUTPUT</span>); <span style="color: #7E7E7E;">//sets the digital pin as output</span>
<span style="color: #CC6600;">pinMode</span>(PWM2, <span style="color: #006699;">OUTPUT</span>); <span style="color: #7E7E7E;">//sets the digital pin as output</span>
<span style="color: #CC6600;">pinMode</span>(PWM3, <span style="color: #006699;">OUTPUT</span>);
<span style="color: #CC6600;">pinMode</span>(PWM4, <span style="color: #006699;">OUTPUT</span>); <span style="color: #7E7E7E;">//sets the digital pin as output</span>
<span style="color: #CC6600;">pinMode</span>(PWM5, <span style="color: #006699;">OUTPUT</span>); <span style="color: #7E7E7E;">//sets the digital pin as output</span>
<span style="color: #CC6600;">pinMode</span>(PWM6, <span style="color: #006699;">OUTPUT</span>); <span style="color: #7E7E7E;">//sets the digital pin as output</span>
<span style="color: #CC6600;">pinMode</span>(PWM7, <span style="color: #006699;">OUTPUT</span>); <span style="color: #7E7E7E;">//sets the digital pin as output</span>
<span style="color: #CC6600;">pinMode</span>(PWM8, <span style="color: #006699;">OUTPUT</span>); <span style="color: #7E7E7E;">//sets the digital pin as output</span>
<span style="color: #CC6600;">pinMode</span>(PWM9, <span style="color: #006699;">OUTPUT</span>);
<span style="color: #CC6600;">pinMode</span>(PWM10, <span style="color: #006699;">OUTPUT</span>); <span style="color: #7E7E7E;">//sets the digital pin as output</span>
<span style="color: #CC6600;">pinMode</span>(PWM11, <span style="color: #006699;">OUTPUT</span>); <span style="color: #7E7E7E;">//sets the digital pin as output</span>
<span style="color: #CC6600;">pinMode</span>(PWM12, <span style="color: #006699;">OUTPUT</span>);
<span style="color: #CC6600;">pinMode</span>(PWM13, <span style="color: #006699;">OUTPUT</span>); <span style="color: #7E7E7E;">//sets the digital pin as output</span>
<span style="color: #CC6600;">pinMode</span>(PWM14, <span style="color: #006699;">OUTPUT</span>); <span style="color: #7E7E7E;">//sets the digital pin as output</span>
<span style="color: #CC6600;">pinMode</span>(PWM15, <span style="color: #006699;">OUTPUT</span>);
<span style="color: #CC6600;">pinMode</span>(50, <span style="color: #006699;">OUTPUT</span>); <span style="color: #7E7E7E;">//sets the digital pin as output</span>
<span style="color: #CC6600;">pinMode</span>(52, <span style="color: #006699;">OUTPUT</span>); <span style="color: #7E7E7E;">//sets the digital pin as output</span>
<span style="color: #CC6600;">pinMode</span>(53, <span style="color: #006699;">OUTPUT</span>);
sbi(PORTB,program_exec_time); <span style="color: #7E7E7E;">//Sets the pin</span>
Setup_timer0();
Setup_timer1();
Setup_timer2();
Setup_timer3();
Setup_timer4();
Setup_timer5();
<span style="color: #7E7E7E;">//Disable Timer 1 interrupt to avoid any timing delays</span>
cbi (TIMSK0,TOIE0); <span style="color: #7E7E7E;">//disable Timer0 !!! delay() is now not available</span>
sbi (TIMSK2,TOIE2); <span style="color: #7E7E7E;">//enable Timer2 Interrupt</span>
tword_m=pow(2,32)*dfreq/refclk; <span style="color: #7E7E7E;">//calulate DDS new tuning word </span>
}
<span style="color: #CC6600;">void</span> <span style="color: #CC6600;"><b>loop</b></span>()
{
<span style="color: #CC6600;">while</span>(1)
{
sbi(PORTB,program_exec_time); <span style="color: #7E7E7E;">//Sets the pin </span>
<span style="color: #CC6600;">if</span> (c4ms > 0) <span style="color: #7E7E7E;">// c4ms = 4ms, thus 4ms *250 = 1 second delay</span>
{
c4ms=0; <span style="color: #7E7E7E;">//Reset c4ms</span>
<span style="color: #7E7E7E;">//dfreq=map(analogRead(0),0,1230,0,1000);</span>
dfreq=<span style="color: #CC6600;">map</span>(<span style="color: #CC6600;">analogRead</span>(0),0,1023,0,1000); <span style="color: #7E7E7E;">//Read voltage on analog 1 to see desired output frequency, 0V = 0Hz, 5V = 1.023kHz</span>
cbi (TIMSK2,TOIE2); <span style="color: #7E7E7E;">//Disable Timer2 Interrupt</span>
tword_m=pow(2,32)*dfreq/refclk; <span style="color: #7E7E7E;">//Calulate DDS new tuning word</span>
sbi (TIMSK2,TOIE2); <span style="color: #7E7E7E;">//Enable Timer2 Interrupt </span>
}
}
}
<span style="color: #CC6600;">void</span> Setup_timer0(<span style="color: #CC6600;">void</span>)
{
TCCR0B = (TCCR0B & 0b11111000) | 0x02;
<span style="color: #7E7E7E;">// Timer1 PWM Mode set to Phase Correct PWM</span>
cbi (TCCR0A, COM0A0);
sbi (TCCR0A, COM0A1);
cbi (TCCR0A, COM0B0);
sbi (TCCR0A, COM0B1);
<span style="color: #7E7E7E;">// Mode 1 / Phase Correct PWM</span>
sbi (TCCR0A, WGM00);
cbi (TCCR0A, WGM01);
}
<span style="color: #CC6600;">void</span> Setup_timer1(<span style="color: #CC6600;">void</span>)
{
TCCR1B = (TCCR1B & 0b11111000) |0x02;
<span style="color: #7E7E7E;">// Timer1 Clock Prescaler to : 8</span>
cbi (TCCR1A, COM1A0);
sbi (TCCR1A, COM1A1);
cbi (TCCR1A, COM1B0);
sbi (TCCR1A, COM1B1);
sbi (TCCR1A, WGM10);
cbi (TCCR1A, WGM11);
cbi (TCCR1B, WGM12);
cbi (TCCR1B, WGM13);
}
<span style="color: #CC6600;">void</span> Setup_timer2()
{
TCCR2B = (TCCR2B & 0b11111000) | 0x02;<span style="color: #7E7E7E;">// Timer2 Clock Prescaler to : 8</span>
cbi (TCCR2A, COM2A0); <span style="color: #7E7E7E;">// clear Compare Match</span>
sbi (TCCR2A, COM2A1);
cbi (TCCR2A, COM2B0);
sbi (TCCR2A, COM2B1);
<span style="color: #7E7E7E;">// Mode 1 / Phase Correct PWM</span>
sbi (TCCR2A, WGM20);
cbi (TCCR2A, WGM21);
cbi (TCCR2B, WGM22);
}
<span style="color: #CC6600;">void</span> Setup_timer3(<span style="color: #CC6600;">void</span>)
{
TCCR3B = (TCCR3B & 0b11111000) |0x02;<span style="color: #7E7E7E;">// Timer1 Clock Prescaler to : 8</span>
cbi (TCCR3A, COM3A0);
sbi (TCCR3A, COM3A1);
cbi (TCCR3A, COM3B0);
sbi (TCCR3A, COM3B1);
cbi (TCCR3A, COM3C0);
sbi (TCCR3A, COM3C1);
<span style="color: #7E7E7E;">// Mode 1 / Phase Correct PWM</span>
sbi (TCCR3A, WGM30);
cbi (TCCR3A, WGM31);
cbi (TCCR3B, WGM32);
cbi (TCCR3B, WGM33);
cbi (TCCR3C, WGM33);
cbi (TCCR3C, WGM33);
}
<span style="color: #CC6600;">void</span> Setup_timer4()
{
TCCR4B = (TCCR4B & 0b11111000) | 0x02;<span style="color: #7E7E7E;">// Timer2 Clock Prescaler to : 8</span>
cbi (TCCR4A, COM4A0); <span style="color: #7E7E7E;">// clear Compare Match</span>
sbi (TCCR4A, COM4A1);
cbi (TCCR4A, COM4B0);
sbi (TCCR4A, COM4B1);
cbi (TCCR4A, COM4C0);
sbi (TCCR4A, COM4C1);
sbi (TCCR4A, WGM40);
cbi (TCCR4A, WGM41);
cbi (TCCR4B, WGM42);
cbi (TCCR4C, WGM43);
cbi (TCCR4C, WGM43);
}
<span style="color: #CC6600;">void</span> Setup_timer5(<span style="color: #CC6600;">void</span>)
{
TCCR5B = (TCCR5B & 0b11111000) |0x02;<span style="color: #7E7E7E;">// Timer1 Clock Prescaler to : 8</span>
cbi (TCCR5A, COM5A0);
sbi (TCCR5A, COM5A1);
cbi (TCCR5A, COM5B0);
sbi (TCCR5A, COM5B1);
cbi (TCCR5A, COM5C0);
sbi (TCCR5A, COM5C1);
sbi (TCCR5A, WGM50);
cbi (TCCR5A, WGM51);
cbi (TCCR5B, WGM52);
cbi (TCCR5B, WGM53);
cbi (TCCR5C, WGM50);
}
ISR(TIMER2_OVF_vect)
{
cbi(PORTD,program_exec_time); <span style="color: #7E7E7E;">//Clear the pin</span>
sbi(PORTD,ISR_exec_time); <span style="color: #7E7E7E;">// Sets the pin</span>
phase_accumulator=phase_accumulator+tword_m; <span style="color: #7E7E7E;">//Adds tuning M word to previoud phase accumulator. refer to DDS_calculator (from Martin Nawrath) for explination.</span>
current_count=phase_accumulator >> 24; <span style="color: #7E7E7E;">// use upper 8 bits of phase_accumulator as frequency information </span>
<span style="color: #7E7E7E;">//motor 1</span>
OCR3B = pgm_read_byte_near(sine256 + current_count); <span style="color: #7E7E7E;">// read value fron ROM sine table and send to PWM</span>
OCR3C = pgm_read_byte_near(sine256 + (uint8_t)(current_count + offset_1)); <span style="color: #7E7E7E;">// read value fron ROM sine table and send to PWM, 120 Degree out of phase of PWM1</span>
OCR0B = pgm_read_byte_near(sine256 + (uint8_t)(current_count + offset_2));<span style="color: #7E7E7E;">// read value fron ROM sine table and send to PWM, 120 Degree out of phase of PWM2</span>
<span style="color: #7E7E7E;">//motor 2</span>
OCR3A = OCR3B;
OCR4A = OCR3C;
OCR4B = OCR0B;
<span style="color: #7E7E7E;">//motor 3</span>
OCR4C = OCR3B;
OCR2B = OCR3C;
OCR2A = OCR0B;
<span style="color: #7E7E7E;">//motor 4</span>
OCR1A = OCR3B;
OCR1B = OCR3C;
OCR0A = OCR0B;
<span style="color: #7E7E7E;">//motor 5</span>
OCR5A = OCR3B;
OCR5B = OCR3C;
OCR5C = OCR0B;
<span style="color: #7E7E7E;">//increment variable ms4_delay every 4mS/125 = milliseconds 32uS</span>
<span style="color: #CC6600;">if</span>(ms4_delay++ == 125)
{
c4ms++;
ms4_delay=0; <span style="color: #7E7E7E;">//reset count</span>
}
cbi(PORTD,ISR_exec_time); <span style="color: #7E7E7E;">//Clear the pin</span>
}
ความคิดเห็น
แสดงความคิดเห็น