?? pwmsw_8c-source.html
字號(hào):
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"><title>Procyon AVRlib: pwmsw.c Source File</title><link href="dox.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.3.6 --><div class="qindex"><a class="qindex" href="main.html">Main Page</a> | <a class="qindex" href="modules.html">Modules</a> | <a class="qindex" href="annotated.html">Data Structures</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="functions.html">Data Fields</a> | <a class="qindex" href="globals.html">Globals</a></div><h1>pwmsw.c</h1><a href="pwmsw_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre>00001 <span class="comment">/*! \file pwmsw.c \brief Software interrupt-driven multi-output PWM function library. */</span>00002 <span class="comment">//*****************************************************************************</span>00003 <span class="comment">//</span>00004 <span class="comment">// File Name : 'pwmsw.c'</span>00005 <span class="comment">// Title : Software interrupt-driven multi-output PWM function library</span>00006 <span class="comment">// Author : Pascal Stang - Copyright (C) 2002</span>00007 <span class="comment">// Created : 7/20/2002</span>00008 <span class="comment">// Revised : 7/31/2002</span>00009 <span class="comment">// Version : 0.1</span>00010 <span class="comment">// Target MCU : Atmel AVR Series</span>00011 <span class="comment">// Editor Tabs : 4</span>00012 <span class="comment">//</span>00013 <span class="comment">// WARNING: this PWM library does not work perfectly. It has known and</span>00014 <span class="comment">// understood problems when two or more PWM outputs are set to nearly the</span>00015 <span class="comment">// same duty cycle. IT MAY NOT BE WORTH USING! YOU HAVE BEEN WARNED!</span>00016 <span class="comment">//</span>00017 <span class="comment">// This code is distributed under the GNU Public License</span>00018 <span class="comment">// which can be found at http://www.gnu.org/licenses/gpl.txt</span>00019 <span class="comment">//</span>00020 <span class="comment">//*****************************************************************************</span>00021 00022 <span class="preprocessor">#ifndef WIN32</span>00023 <span class="preprocessor"></span><span class="preprocessor"> #include <avr/io.h></span>00024 <span class="preprocessor">#endif</span>00025 <span class="preprocessor"></span>00026 <span class="preprocessor">#include "<a class="code" href="global_8h.html">global.h</a>"</span>00027 <span class="preprocessor">#include "<a class="code" href="pwmsw_8h.html">pwmsw.h</a>"</span>00028 00029 <span class="comment">// Program ROM constants</span>00030 00031 <span class="comment">// Global variables</span>00032 <span class="comment">// PWM channel registers</span>00033 u16 PosTics;00034 u16 PeriodTics;00035 u08 Channel;00036 SwPwmChannelType SwPwmChannels[SWPWM_NUM_CHANNELS];00037 00038 <span class="comment">// functions</span>00039 00040 <span class="comment">// initializes software PWM system</span><a name="l00041"></a><a class="code" href="pwmsw_8h.html#a4">00041</a> <span class="keywordtype">void</span> <a class="code" href="pwmsw_8h.html#a4">pwmswInit</a>(u16 periodTics)00042 {00043 u08 index;00044 00045 <span class="comment">// attach the software PWM service routine to timer1 output compare A</span>00046 <a class="code" href="timer128_8h.html#a52">timerAttach</a>(TIMER1OUTCOMPAREA_INT, <a class="code" href="pwmsw_8c.html#a7">pwmswService</a>);00047 <span class="comment">// set PeriodTics</span>00048 PeriodTics = periodTics;00049 <span class="comment">// set PosTics</span>00050 PosTics = 0;00051 <span class="comment">// clear channels</span>00052 <span class="keywordflow">for</span>(index=0; index<SWPWM_NUM_CHANNELS; index++)00053 {00054 SwPwmChannels[index].duty = 0;00055 SwPwmChannels[index].setduty = 0;00056 }00057 <span class="comment">// set initial interrupt time</span>00058 u16 OCValue;00059 <span class="comment">// read in current value of output compare register OCR1A</span>00060 OCValue = inb(OCR1AL); <span class="comment">// read low byte of OCR1A</span>00061 OCValue += inb(OCR1AH)<<8; <span class="comment">// read high byte of OCR1A</span>00062 <span class="comment">// increment OCR1A value by nextTics</span>00063 OCValue += PeriodTics; 00064 <span class="comment">// set future output compare time to this new value</span>00065 outb(OCR1AH, (OCValue>>8)); <span class="comment">// write high byte</span>00066 outb(OCR1AL, (OCValue & 0x00FF)); <span class="comment">// write low byte</span>00067 00068 <span class="comment">// enable the timer1 output compare A interrupt</span>00069 sbi(TIMSK, OCIE1A);00070 }00071 00072 <span class="comment">// turns off software PWM system</span><a name="l00073"></a><a class="code" href="pwmsw_8h.html#a5">00073</a> <span class="keywordtype">void</span> <a class="code" href="pwmsw_8c.html#a5">pwmswOff</a>(<span class="keywordtype">void</span>)00074 {00075 <span class="comment">// disable the timer1 output compare A interrupt</span>00076 cbi(TIMSK, OCIE1A);00077 <span class="comment">// detach the service routine</span>00078 <a class="code" href="timer128_8h.html#a53">timerDetach</a>(TIMER1OUTCOMPAREA_INT);00079 }00080 00081 <span class="comment">// set duty on channel</span><a name="l00082"></a><a class="code" href="pwmsw_8h.html#a6">00082</a> <span class="keywordtype">void</span> <a class="code" href="pwmsw_8h.html#a6">pwmswPWMSet</a>(u08 channel, u16 duty)00083 {00084 <span class="comment">// compare with max value of PeriodTics</span>00085 duty = MIN(duty, PeriodTics);00086 SwPwmChannels[channel].setduty = duty;00087 }00088 <a name="l00089"></a><a class="code" href="pwmsw_8h.html#a7">00089</a> <span class="keywordtype">void</span> <a class="code" href="pwmsw_8c.html#a7">pwmswService</a>(<span class="keywordtype">void</span>)00090 {00091 u16 nextTics=PeriodTics;00092 u08 index;00093 00094 <span class="comment">// check for beginning of period</span>00095 <span class="keywordflow">if</span>(PosTics == 0)00096 {00097 <span class="comment">// examine all channels</span>00098 <span class="keywordflow">for</span>(index=0; index<SWPWM_NUM_CHANNELS; index++)00099 {00100 <span class="comment">// transfer set-duty to active-duty</span>00101 SwPwmChannels[index].duty = SwPwmChannels[index].setduty;00102 <span class="comment">// find next channel event to schedule</span>00103 <span class="comment">// if no channel has a duty setting greater than 0 (PosTics);</span>00104 <span class="comment">// nextTics will remain at PeriodTics for the next cycle</span>00105 <span class="keywordflow">if</span>(SwPwmChannels[index].duty)00106 {00107 nextTics = MIN(nextTics, SwPwmChannels[index].duty);00108 <span class="comment">// set all non-zero channels to on</span>00109 outb(SWPWMPORT, inb(SWPWMPORT) | (1<<index));00110 }00111 }00112 }00113 <span class="keywordflow">else</span>00114 {00115 <span class="comment">// examine all channels</span>00116 <span class="keywordflow">for</span>(index=0; index<SWPWM_NUM_CHANNELS; index++)00117 {00118 <span class="comment">// check if we have a duty cycle match</span>00119 <span class="keywordflow">if</span>(PosTics == SwPwmChannels[index].duty)00120 {00121 <span class="comment">// clear output channel</span>00122 outb(SWPWMPORT, inb(SWPWMPORT) & ~(1<<index));00123 }00124 <span class="comment">// find next channel event to schedule</span>00125 <span class="comment">// if no channel has a duty setting greater than PosTics;</span>00126 <span class="comment">// nextTics will remain at PeriodTics and is handled below</span>00127 <span class="keywordflow">if</span>(SwPwmChannels[index].duty > PosTics)00128 nextTics = MIN(nextTics, SwPwmChannels[index].duty-PosTics);00129 }00130 <span class="keywordflow">if</span>(nextTics == PeriodTics)00131 {00132 <span class="comment">// no more channels to schedule</span>00133 <span class="comment">// schedule next cycle</span>00134 nextTics = PeriodTics - PosTics;00135 }00136 }00137 00138 <span class="comment">// schedule next interrupt</span>00139 u16 OCValue;00140 <span class="comment">// read in current value of output compare register OCR1A</span>00141 OCValue = inb(OCR1AL); <span class="comment">// read low byte of OCR1A</span>00142 OCValue += inb(OCR1AH)<<8; <span class="comment">// read high byte of OCR1A</span>00143 <span class="comment">// increment OCR1A value by nextTics</span>00144 OCValue += nextTics;00145 <span class="comment">// OCR1A+=nextTics;</span>00146 <span class="comment">// set future output compare time to this new value</span>00147 outb(OCR1AH, (OCValue>>8)); <span class="comment">// write high byte</span>00148 outb(OCR1AL, (OCValue & 0x00FF)); <span class="comment">// write low byte</span>00149 <span class="comment">// set our new tic position</span>00150 PosTics += nextTics;00151 <span class="keywordflow">if</span>(PosTics >= PeriodTics) PosTics -= PeriodTics;00152 }00153 </pre></div><hr size="1"><address style="align: right;"><small>Generated on Fri Oct 15 03:50:22 2004 for Procyon AVRlib by<a href="http://www.doxygen.org/index.html"><img src="doxygen.png" alt="doxygen" align="middle" border=0 > </a>1.3.6 </small></address></body></html>
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -