00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef WIN32
00019 #include <avr/io.h>
00020 #endif
00021
00022 #include "global.h"
00023 #include "pwmsw.h"
00024
00025
00026
00027
00028
00029 u16 PosTics;
00030 u16 PeriodTics;
00031 u08 Channel;
00032 SwPwmChannelType SwPwmChannels[SWPWM_NUM_CHANNELS];
00033
00034
00035
00036
00037 void pwmswInit(u16 periodTics)
00038 {
00039 u08 index;
00040
00041
00042 timerAttach(TIMER1OUTCOMPAREA_INT, pwmswService);
00043
00044 PeriodTics = periodTics;
00045
00046 PosTics = 0;
00047
00048 for(index=0; index<SWPWM_NUM_CHANNELS; index++)
00049 {
00050 SwPwmChannels[index].duty = 0;
00051 SwPwmChannels[index].setduty = 0;
00052 }
00053
00054 u16 OCValue;
00055
00056 OCValue = inb(OCR1AL);
00057 OCValue += inb(OCR1AH)<<8;
00058
00059 OCValue += PeriodTics;
00060
00061 outb(OCR1AH, (OCValue>>8));
00062 outb(OCR1AL, (OCValue & 0x00FF));
00063
00064
00065 sbi(TIMSK, OCIE1A);
00066 }
00067
00068
00069 void pwmswOff(void)
00070 {
00071
00072 cbi(TIMSK, OCIE1A);
00073
00074 timerDetach(TIMER1OUTCOMPAREA_INT);
00075 }
00076
00077
00078 void pwmswPWMSet(u08 channel, u16 duty)
00079 {
00080
00081 duty = MIN(duty, PeriodTics);
00082 SwPwmChannels[channel].setduty = duty;
00083 }
00084
00085 void pwmswService(void)
00086 {
00087 u16 nextTics=PeriodTics;
00088 u08 index;
00089
00090
00091 if(PosTics == 0)
00092 {
00093
00094 for(index=0; index<SWPWM_NUM_CHANNELS; index++)
00095 {
00096
00097 SwPwmChannels[index].duty = SwPwmChannels[index].setduty;
00098
00099
00100
00101 if(SwPwmChannels[index].duty)
00102 {
00103 nextTics = MIN(nextTics, SwPwmChannels[index].duty);
00104
00105 outb(SWPWMPORT, inb(SWPWMPORT) | (1<<index));
00106 }
00107 }
00108 }
00109 else
00110 {
00111
00112 for(index=0; index<SWPWM_NUM_CHANNELS; index++)
00113 {
00114
00115 if(PosTics == SwPwmChannels[index].duty)
00116 {
00117
00118 outb(SWPWMPORT, inb(SWPWMPORT) & ~(1<<index));
00119 }
00120
00121
00122
00123 if(SwPwmChannels[index].duty > PosTics)
00124 nextTics = MIN(nextTics, SwPwmChannels[index].duty-PosTics);
00125 }
00126 if(nextTics == PeriodTics)
00127 {
00128
00129
00130 nextTics = PeriodTics - PosTics;
00131 }
00132 }
00133
00134
00135 u16 OCValue;
00136
00137 OCValue = inb(OCR1AL);
00138 OCValue += inb(OCR1AH)<<8;
00139
00140 OCValue += nextTics;
00141
00142
00143 outb(OCR1AH, (OCValue>>8));
00144 outb(OCR1AL, (OCValue & 0x00FF));
00145
00146 PosTics += nextTics;
00147 if(PosTics >= PeriodTics) PosTics -= PeriodTics;
00148 }
00149