00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef _PWM_NG_H_
00022 #define _PWM_NG_H_
00023
00024 #include <aversive.h>
00025 #include <aversive/timers.h>
00026 #include <aversive/parts.h>
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #define PWM_NG_MODE_NORMAL 0x00
00042 #define PWM_NG_MODE_REVERSE 0x01
00043 #define PWM_NG_MODE_SIGN_INVERTED 0x02
00044 #define PWM_NG_MODE_SIGNED 0x04
00045 #define PWM_NG_MODE_SPECIAL_SIGN 0x08
00046
00047
00048
00049
00050 #define PWM_NG_TIMER_0_MODE
00051
00052 struct pwm_ng {
00053 uint8_t mode:4,
00054 type:1,
00055 nbits:2,
00056 reserved:1;
00057 union {
00058 volatile uint8_t *ocr8;
00059 volatile uint16_t *ocr16;
00060 } u;
00061 volatile uint8_t *sign_port;
00062 uint8_t sign_bit;
00063 };
00064
00075 #define PWM_NG_TIMER_8BITS_INIT(n, t_mode, prsc) \
00076 do { \
00077 TCCR##n = ( ((t_mode & 0x01) ? (1 << WGM##n##0):0) | \
00078 ((t_mode & 0x02) ? (1 << WGM##n##1):0) | \
00079 ((prsc << CS##n##0)) ); \
00080 } while(0)
00081
00085 #define PWM_NG_TIMER_8BITS_INIT_B(n, t_mode, prsc) \
00086 do { \
00087 TCCR##n##A = ( ((t_mode & 0x01) ? (1 << WGM##n##0):0) | \
00088 ((t_mode & 0x02) ? (1 << WGM##n##1):0) | \
00089 ((prsc << CS##n##0)) ); \
00090 TCCR##n##B = (prsc << CS##n##0); \
00091 } while(0)
00092
00103 #define PWM_NG_TIMER_16BITS_INIT(n, t_mode, prsc) do { \
00104 TCCR##n##A &= ~((1 << WGM##n##0) | (1 << WGM##n##1)); \
00105 TCCR##n##A |= ((t_mode & 0x01) ? (1 << WGM##n##0):0) | \
00106 ((t_mode & 0x02) ? (1 << WGM##n##1):0); \
00107 TCCR##n##B = ((t_mode & 0x04) ? (1 << WGM##n##2):0) | \
00108 ((t_mode & 0x08 ) ? (1 << WGM##n##3):0) | \
00109 (prsc << CS##n##0); \
00110 } while(0)
00111
00112
00139 void pwm_ng_init(struct pwm_ng *pwm, uint8_t timer_nbits,
00140 uint8_t pwm_nbits, uint8_t pwm_mode,
00141 volatile void *ocrn,
00142 uint8_t com0, volatile uint8_t *tccrn,
00143 volatile uint8_t *pwm_port, uint8_t pwm_bit,
00144 volatile uint8_t *sign_port, uint8_t sign_bit);
00145
00146 #define PWM_NG_INIT8(pwm, n, pwm_nbits, pwm_mode, sign_port, sign_bit) \
00147 do { \
00148 pwm_ng_init(pwm, 8, pwm_nbits, pwm_mode, &OCR##n, \
00149 COM##n##0, &TCCR##n, &OC##n##_PORT, \
00150 OC##n##_BIT, sign_port, sign_bit); \
00151 } while(0)
00152
00153 #define PWM_NG_INIT8_B(pwm, n, m, pwm_nbits, pwm_mode, sign_port, sign_bit) \
00154 do { \
00155 pwm_ng_init(pwm, 8, pwm_nbits, pwm_mode, &OCR##n##m, \
00156 COM##n##m##0, &TCCR##n##A, &OC##n##m##_PORT, \
00157 OC##n##m##_BIT, sign_port, sign_bit); \
00158 } while(0)
00159
00160 #define PWM_NG_INIT16(pwm, n, m, pwm_nbits, pwm_mode, sign_port, sign_bit) \
00161 do { \
00162 pwm_ng_init(pwm, 16, pwm_nbits, pwm_mode, &OCR##n##m, \
00163 COM##n##m##0, &TCCR##n##A, \
00164 &OC##n##m##_PORT, OC##n##m##_BIT, \
00165 sign_port, sign_bit); \
00166 } while(0)
00167
00173 void pwm_ng_set(void *pwm, int32_t value);
00174
00175 #endif // _PWM_H_