2013-10-12 17:05:01 +02:00
|
|
|
#include <stdint.h> /* Standard Integer Types */
|
|
|
|
#include <stdio.h> /* Standard IO facilities */
|
|
|
|
#include <stdlib.h> /* General utilities */
|
|
|
|
#include <string.h> /* Strings */
|
|
|
|
#include <avr/io.h> /* AVR device-specific IO definitions */
|
|
|
|
#include <util/delay.h> /* Convenience functions for busy-wait delay loops */
|
2013-10-13 16:47:46 +02:00
|
|
|
#include <avr/interrupt.h> /* Interrupts */
|
2013-10-12 17:05:01 +02:00
|
|
|
|
2013-10-12 18:19:39 +02:00
|
|
|
#include "main.h"
|
|
|
|
#include "uart.h"
|
2013-10-12 17:05:01 +02:00
|
|
|
|
2013-10-13 16:47:46 +02:00
|
|
|
/* global variables */
|
|
|
|
volatile uint8_t pwr_ok;
|
|
|
|
|
|
|
|
/* PWR_OK interrupt */
|
|
|
|
ISR(PCINT0_vect) { /* PCINT1 is actually triggering PCI0 Interrupt Vector */
|
|
|
|
if (pwr_ok!=(PINB&(1<<PWR_OK))) { /* did the PWR_OK pin state changed */
|
|
|
|
pwr_ok = PINB&(1<<PWR_OK); /* save new state */
|
|
|
|
if (pwr_ok) {
|
|
|
|
puts("power ok");
|
|
|
|
} else {
|
|
|
|
puts("power ko");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-12 17:05:01 +02:00
|
|
|
static void ioinit(void)
|
|
|
|
{
|
2013-10-13 11:46:53 +02:00
|
|
|
/* configure power */
|
2013-10-13 16:47:46 +02:00
|
|
|
DDRB |= (1<<nPS_ON); /* nPS_ON is output */
|
|
|
|
PORTB |= (1<<nPS_ON); /* switch off power supply */
|
|
|
|
DDRB &= ~(1<<PWR_OK); /* PWR_ON (PB1/PCINT1) is input */
|
|
|
|
pwr_ok = PINB&(1<<PWR_OK); /* save state */
|
|
|
|
PCIFR &= ~(1<<PCIF0); /* clear interrupt flag */
|
|
|
|
PCICR |= (1<<PCIE0); /* enable interrupt for PCINT[7:0] */
|
|
|
|
PCMSK0 |= (1<<PCINT1); /* enable interrupt for PCINT1 */
|
2013-10-13 11:46:53 +02:00
|
|
|
|
|
|
|
/* configure peripherals */
|
|
|
|
DDRB &= ~(1<<IR); /* IR receiver is input */
|
|
|
|
DDRC &= ~(1<<FAN); /* FAN is input */
|
|
|
|
|
|
|
|
/* configure channels (used for powering LEDs using an nMOS) */
|
|
|
|
DDRC |= (1<<CH1_1)|(1<<CH1_2)|(1<<CH1_3)|(1<<CH1_4)|(1<<CH1_5); /* CH1_x is output */
|
|
|
|
PORTC &= ~((1<<CH1_1)|(1<<CH1_2)|(1<<CH1_3)|(1<<CH1_4)|(1<<CH1_5)); /* switch off CH1_x */
|
|
|
|
DDRD |= (1<<CH2_1)|(1<<CH2_2)|(1<<CH2_3)|(1<<CH2_4)|(1<<CH2_5); /* CH2_x is output */
|
|
|
|
PORTD &= ~((1<<CH2_1)|(1<<CH2_2)|(1<<CH2_3)|(1<<CH2_4)|(1<<CH2_5)); /* switch off CH2_x */
|
|
|
|
|
|
|
|
/* use UART as terminal */
|
|
|
|
uart_init();
|
2013-10-12 18:19:39 +02:00
|
|
|
stdout = &uart_output;
|
|
|
|
stdin = &uart_input;
|
2013-10-13 11:46:53 +02:00
|
|
|
|
2013-10-13 12:19:17 +02:00
|
|
|
/* configure LED (on PD6/OC0A) */
|
|
|
|
DDRD |= (1<<LED); /* LED is output */
|
|
|
|
/* use phase correct PWM ode (because fast PWM is always on for at least 1 cycle) */
|
|
|
|
TCCR0A &= ~(1<<WGM01);
|
|
|
|
TCCR0A |= (1<<WGM00);
|
|
|
|
TCCR0B &= ~(1<<WGM02);
|
|
|
|
/* /64 prescale timer */
|
|
|
|
TCCR0B &= ~(1<<CS02);
|
|
|
|
TCCR0B |= (1<<CS01)|(1<<CS00);
|
|
|
|
/* clear on match, because the pin is used as sink */
|
|
|
|
TCCR0A |= (1<<COM0A1)|(1<<COM0A0);
|
|
|
|
/* start with LED on */
|
|
|
|
OCR0A = 0xff;
|
|
|
|
|
2013-10-13 16:47:46 +02:00
|
|
|
sei(); /* enable interrupts */
|
2013-10-12 17:05:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
ioinit(); /* initialize IOs */
|
|
|
|
|
2013-10-13 11:46:53 +02:00
|
|
|
char input;
|
2013-10-12 17:05:01 +02:00
|
|
|
|
2013-10-12 18:19:39 +02:00
|
|
|
puts("Hello world!");
|
2013-10-13 11:46:53 +02:00
|
|
|
_delay_ms(1000);
|
|
|
|
while(1) {
|
|
|
|
input = getchar();
|
|
|
|
printf("%c\n", input);
|
|
|
|
switch (input) {
|
|
|
|
case 'l':
|
|
|
|
PIND |= (1<<LED);
|
|
|
|
printf("LED: ");
|
|
|
|
if (PIND&(1<<LED)) {
|
|
|
|
puts("off");
|
|
|
|
} else {
|
|
|
|
puts("on");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
PINB |= (1<<nPS_ON);
|
|
|
|
printf("power supply: ");
|
|
|
|
if (PINB&(1<<nPS_ON)) {
|
|
|
|
puts("off");
|
|
|
|
} else {
|
|
|
|
puts("on");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
printf("power: ");
|
|
|
|
if (PINB&(1<<PWR_OK)) {
|
|
|
|
puts("ok");
|
|
|
|
} else {
|
|
|
|
puts("ko");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '1':
|
|
|
|
PINC |= (1<<CH1_1);
|
|
|
|
printf("CH1_1: ");
|
|
|
|
if (PINC&(1<<CH1_1)) {
|
|
|
|
puts("on");
|
|
|
|
} else {
|
|
|
|
puts("off");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '2':
|
|
|
|
PINC |= (1<<CH1_2);
|
|
|
|
printf("CH1_2: ");
|
|
|
|
if (PINC&(1<<CH1_2)) {
|
|
|
|
puts("on");
|
|
|
|
} else {
|
|
|
|
puts("off");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '3':
|
|
|
|
PINC |= (1<<CH1_3);
|
|
|
|
printf("CH1_3: ");
|
|
|
|
if (PINC&(1<<CH1_3)) {
|
|
|
|
puts("on");
|
|
|
|
} else {
|
|
|
|
puts("off");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '4':
|
|
|
|
PINC |= (1<<CH1_4);
|
|
|
|
printf("CH1_4: ");
|
|
|
|
if (PINC&(1<<CH1_4)) {
|
|
|
|
puts("on");
|
|
|
|
} else {
|
|
|
|
puts("off");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '5':
|
|
|
|
PINC |= (1<<CH1_5);
|
|
|
|
printf("CH1_5: ");
|
|
|
|
if (PINC&(1<<CH1_5)) {
|
|
|
|
puts("on");
|
|
|
|
} else {
|
|
|
|
puts("off");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '6':
|
|
|
|
PIND |= (1<<CH2_1);
|
|
|
|
printf("CH2_1: ");
|
|
|
|
if (PIND&(1<<CH2_1)) {
|
|
|
|
puts("on");
|
|
|
|
} else {
|
|
|
|
puts("off");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '7':
|
|
|
|
PIND |= (1<<CH2_2);
|
|
|
|
printf("CH2_2: ");
|
|
|
|
if (PIND&(1<<CH2_2)) {
|
|
|
|
puts("on");
|
|
|
|
} else {
|
|
|
|
puts("off");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '8':
|
|
|
|
PIND |= (1<<CH2_3);
|
|
|
|
printf("CH2_3: ");
|
|
|
|
if (PIND&(1<<CH2_3)) {
|
|
|
|
puts("on");
|
|
|
|
} else {
|
|
|
|
puts("off");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '9':
|
|
|
|
PIND |= (1<<CH2_4);
|
|
|
|
printf("CH2_4: ");
|
|
|
|
if (PIND&(1<<CH2_4)) {
|
|
|
|
puts("on");
|
|
|
|
} else {
|
|
|
|
puts("off");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '0':
|
|
|
|
PIND |= (1<<CH2_5);
|
|
|
|
printf("CH2_5: ");
|
|
|
|
if (PIND&(1<<CH2_5)) {
|
|
|
|
puts("on");
|
|
|
|
} else {
|
|
|
|
puts("off");
|
|
|
|
}
|
|
|
|
break;
|
2013-10-13 12:19:17 +02:00
|
|
|
case '-':
|
|
|
|
if (OCR0A>0) {
|
|
|
|
OCR0A--;
|
|
|
|
}
|
|
|
|
printf("decreasing LED: %d\n",OCR0A);
|
|
|
|
break;
|
|
|
|
case '+':
|
|
|
|
if (OCR0A<0xff) {
|
|
|
|
OCR0A++;
|
|
|
|
}
|
|
|
|
printf("increasing LED: %d\n",OCR0A);
|
|
|
|
break;
|
2013-10-13 11:46:53 +02:00
|
|
|
}
|
2013-10-12 17:05:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|