All pastes #948150 Raw Edit

Kopias Csaba

public c v1 · immutable
·published 2008-03-18 22:34 UTC
rendered paste body
pwm.h:----------------------------------#ifndef PWM_#define PWM_#include <stdint.h>#define NOFPWMS 1  // number of pwm channelsvoid pwmned();void InitPwm();void setPwmValue(int clr, int value);// A pwm channel is one pin, on a port, with it's own pwm value. struct pwm_channel {uint8_t *port;uint8_t pin;volatile uint8_t state;uint8_t value; // 0-255 brightness};#endif /*PWM_*/pwm.c:-------------------------------------------------#include "pwm.h"#include <avr/io.h>static volatile struct pwm_channel pwms[NOFPWMS];static struct pwm_channel default_pwmc;void InitPwm() { // initialize the pwm channels	default_pwmc.port = &PORTB;	default_pwmc.pin = PB0;	default_pwmc.state = 0;	default_pwmc.value = 0;	for (int a = 0; a<NOFPWMS; a++)		pwms[a] = default_pwmc;	pwms[0].pin=PB0;	}void pwmned() {	struct pwm_channel *most;	for (int i = 0; i<NOFPWMS; i++) {		most = &pwms[i];		if (most->state==255) {			*most->port |= (1<<most->pin);			most->state=0;		}				if (most->value==most->state) {			*most->port &= ~(1<<most->pin);		}		most->state++;	}}void setPwmValue(int clr, int value) {	pwms[clr].value = value;}usage:--------------------------------pwms is an array of  "pwm_channel"s. Each item of it needs to be set after InitPwm().