summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Pointner <equinox@spreadspace.org>2021-09-12 21:38:38 +0200
committerChristian Pointner <equinox@spreadspace.org>2021-09-12 21:38:38 +0200
commitb7e9b1a01b9bfdaa8487071ef8f13353bb1a76fa (patch)
tree16e0525b7925a9873cc1892222fb3c5f7966fee0
parentadd project: table-fan (diff)
basic PWM works now
-rw-r--r--table-fan/table-fan.c71
1 files changed, 40 insertions, 31 deletions
diff --git a/table-fan/table-fan.c b/table-fan/table-fan.c
index bb5fa29..2cf9a32 100644
--- a/table-fan/table-fan.c
+++ b/table-fan/table-fan.c
@@ -31,42 +31,47 @@
#include "led.h"
#include "usbio.h"
-/* #define PWM_VAL OCR1BL */
-
-/* void pwm_init(void) */
-/* { */
-/* DDRB |= (1<<PB6); */
-/* TCCR1B = 0; */
-/* TCNT1 = 0; */
-/* OCR1B = 0; */
-/* TCCR1A = (1<<COM1B1) | (1<<WGM10); */
-/* TCCR1B = (1<<WGM12) | (1<<CS10); */
-/* } */
-
-/* inline void pwm_set(uint8_t val) */
-/* { */
-/* PWM_VAL = val; */
-/* } */
-
-/* inline void pwm_inc(void) */
-/* { */
-/* if(PWM_VAL < 255) */
-/* PWM_VAL++; */
-/* } */
-
-/* inline void pwm_dec(void) */
-/* { */
-/* if(PWM_VAL > 0) */
-/* PWM_VAL--; */
-/* } */
+#define PWM_VAL OCR4B
+#define PWM_MAX 160
+
+void pwm_init(void)
+{
+ DDRB |= (1<<PB6);
+ TCCR4B = 0; // make sure timer is stopped
+
+ TCCR4A = (1<<COM4B1) | (1<<PWM4B);
+ TCCR4D = (1<<WGM40);
+ TCNT4 = 0;
+ OCR4C = 160;
+
+ OCR4B = 80; // set duty cycle to 50:50
+ TCCR4B = (1<<CS41); // start timer
+}
+
+inline void pwm_set(uint8_t val)
+{
+ PWM_VAL = val;
+}
+
+inline void pwm_inc(void)
+{
+ if(PWM_VAL < PWM_MAX)
+ PWM_VAL++;
+}
+
+inline void pwm_dec(void)
+{
+ if(PWM_VAL > 0)
+ PWM_VAL--;
+}
void handle_cmd(uint8_t cmd)
{
switch(cmd) {
case '0': led_off(); printf("ok\r\n"); break;
case '1': led_on(); printf("ok\r\n"); break;
- /* case '+': pwm_inc(); printf("pwm = %d\r\n", PWM_VAL); break; */
- /* case '-': pwm_dec(); printf("pwm = %d\r\n", PWM_VAL); break; */
+ case '+': pwm_inc(); printf("pwm = %d\r\n", PWM_VAL); break;
+ case '-': pwm_dec(); printf("pwm = %d\r\n", PWM_VAL); break;
case 'r': reset2bootloader(); break;
default: printf("error\r\n"); return;
}
@@ -83,13 +88,17 @@ int main(void)
led_init();
usbio_init();
- // pwm_init();
+ pwm_init();
sei();
// FAN tacho input (enable Pull-Up)
DDRD &= ~(1<<PD4);
PORTD |= (1<<PD4);
+ // rotary encoder
+ DDRB &= ~((1<<PB4) | (1<<PB5));
+ PORTB |= (1<<PB4) | (1<<PB5);
+
led_on();
for(;;) {
int16_t BytesReceived = usbio_bytes_received();