127 lines
3.1 KiB
C
127 lines
3.1 KiB
C
/* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*
|
|
*/
|
|
#include <stdint.h> // Standard Integer Types
|
|
#include <stdio.h> // Standard IO facilities
|
|
#include <stdlib.h> // General utilities
|
|
#include <stdbool.h> // Boolean
|
|
#include <string.h> // Strings
|
|
|
|
#include <avr/io.h> // AVR device-specific IO definitions
|
|
#include <util/delay.h> // Convenience functions for busy-wait delay loops
|
|
#include <avr/interrupt.h> // Interrupts
|
|
#include <avr/wdt.h> // Watchdog timer handling
|
|
#include <avr/pgmspace.h> // Program Space Utilities
|
|
#include <avr/sleep.h> // Power Management and Sleep Modes
|
|
|
|
|
|
#include "uart.h" // basic UART functions
|
|
#include "main.h" // main definitions
|
|
|
|
/* variables */
|
|
volatile uint8_t stateD; // the state of port D
|
|
|
|
/* switch off LED */
|
|
void led_off(void)
|
|
{
|
|
PORTB &= ~(1<<LED); // remove power to LED
|
|
}
|
|
|
|
/* switch on LED */
|
|
void led_on(void)
|
|
{
|
|
PORTB |= (1<<LED); // provide power to LED
|
|
}
|
|
|
|
/* toggle LED */
|
|
void led_toggle(void)
|
|
{
|
|
PINB |= (1<<LED);
|
|
}
|
|
|
|
/* scale on interrupt
|
|
PCI2 Interrupt Vector for PCINT[23:16]/PORTD
|
|
*/
|
|
ISR(PCINT2_vect)
|
|
{
|
|
if ((stateD&(1<<SCALE_ON))!=(PIND&(1<<SCALE_ON))) { // scale on state changed
|
|
if (PIND&(1<<SCALE_ON)) {
|
|
led_on();
|
|
} else {
|
|
led_off();
|
|
}
|
|
}
|
|
stateD = PIND; // save new state
|
|
}
|
|
|
|
/* timer 1 OCR1A match interrupt
|
|
* use it to toogle LED
|
|
*/
|
|
ISR(TIMER1_COMPA_vect)
|
|
{
|
|
/*
|
|
if (blink_flag) {
|
|
led_toggle();
|
|
}
|
|
*/
|
|
}
|
|
|
|
/* disable watchdog when booting */
|
|
void wdt_init(void) __attribute__((naked)) __attribute__((section(".init3")));
|
|
void wdt_init(void)
|
|
{
|
|
MCUSR = 0;
|
|
wdt_disable();
|
|
|
|
return;
|
|
}
|
|
|
|
/* initialize GPIO */
|
|
void io_init(void)
|
|
{
|
|
/* use UART as terminal */
|
|
uart_init();
|
|
stdout = &uart_output;
|
|
stdin = &uart_input;
|
|
|
|
/* gpio */
|
|
/* LED */
|
|
DDRB |= (1<<LED); // LED is driven by pin (set as output)
|
|
led_off();
|
|
/* scale on signal */
|
|
DDRD &= ~(1<<SCALE_ON); // SIGNAL_ON is input (should be per default)
|
|
PCIFR &= ~(1<<PCIF2); // clear interrupt flag for SCALE_ON on PCINT[23:16]/PORTD
|
|
PCICR |= (1<<PCIE2); // enable interrupt for PCINT[23:16]/PORTD
|
|
PCMSK2 |= (1<<PCINT21); // enable interrupt for SCALE_ON
|
|
stateD = PIND; // save state to detect changes
|
|
/* PWM scale weight signal on analog comparator */
|
|
DDRD &= ~((1<<SCALE_PWM)||(1<<REF_PWM)); // analog comparator is input (should be per default)
|
|
|
|
sei(); /* enable interrupts */
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
io_init(); // initialize IOs
|
|
|
|
printf(PSTR("welcome to the body scale weight reader\n")); // print welcome message
|
|
while (true) { // endless loop for micro-controller
|
|
/* go to sleep and wait for next interrupt */
|
|
set_sleep_mode(SLEEP_MODE_IDLE);
|
|
sleep_mode();
|
|
}
|
|
return 0;
|
|
}
|
|
|