This commit is contained in:
fruchti 2015-05-05 18:49:42 +02:00
commit 69ea0a4518
21 changed files with 3090 additions and 0 deletions

69
src/config.h Normal file
View file

@ -0,0 +1,69 @@
// -fruchti 2015
#ifndef CONFIG_H_
#define CONFIG_H_
#define F_CPU 8000000
#include <avr/io.h>
#include <avr/interrupt.h>
// All delay times are measured in milliseconds
#define T_HALL_L 6 // Hall sensor timeout. After this time
// is elapsed without a pin change, the
// motor is considered stopped
#define T_POWRON 5000 // Delay to allow printer to start up
#define T_CLUTCH 3406 // Delay between starting print job and
// pulling in carrier/activating manual
// paper feed sensor
#define T_EXIT_H 841 // Delay between activation of paper in
// and activation of exit sensor
#define T_MNPF_L 213 // Delay before deactivation of manual
// paper feed sensor after exit sensor
// is activated
#define T_PPIN_L 311 // Delay before deactivation of paper
// in sensor
#define T_EXIT_L 849 // Delay before deactivation of exit
// sensor
#define D_LED_RD DDRD // Red status LED
#define O_LED_RD PORTD
#define P_LED_RD PD6
#define D_LED_YE DDRD // Yellow status LED
#define O_LED_YE PORTD
#define P_LED_YE PD5
#define D_LED_GN DDRD // Green status LED
#define O_LED_GN PORTD
#define P_LED_GN PD4
#define I_NPPINS PINB // New paper in sensor
#define O_NPPINS PORTB
#define P_NPPINS PB0
#define D_PPINSO DDRB // Paper in sensor output
#define O_PPINSO PORTB
#define P_PPINSO PB1
#define D_MNPFSO DDRB // Manual paper feed sensor output
#define O_MNPFSO PORTB
#define P_MNPFSO PB2
#define D_EXITSO DDRB // Exit sensor output
#define O_EXITSO PORTB
#define P_EXITSO PB3
#define D_CLUTCH DDRB // Clutch control pin
#define O_CLUTCH PORTB
#define P_CLUTCH PB4
#define I_OPANEL PIND // Operator panel
#define P_OPANEL PD2 // CAUTION: Do not change (INT0 is used
// for monitoring this input)!
#define I_HALL_1 PIND // Hall sensor from motor
#define P_HALL_1 PD3
#endif

202
src/main.c Normal file
View file

@ -0,0 +1,202 @@
// -fruchti 2015
#include "main.h"
volatile uint32_t MillisecondCounter = 0;
volatile uint16_t HallSensorTimer = T_HALL_L;
volatile State_t State = PowerOnWait;
volatile uint8_t HallSensorBuffer = 0;
int main(void)
{
//// IN- AND OUTPUTS ////
D_LED_RD |= (1 << P_LED_RD);
D_LED_YE |= (1 << P_LED_YE);
D_LED_GN |= (1 << P_LED_GN);
O_LED_RD |= (1 << P_LED_RD);
O_LED_YE |= (1 << P_LED_YE);
O_LED_GN |= (1 << P_LED_GN);
O_NPPINS |= (1 << P_NPPINS);
O_PPINSO |= (1 << P_PPINSO);
D_PPINSO |= (1 << P_PPINSO);
O_MNPFSO |= (1 << P_MNPFSO);
D_MNPFSO |= (1 << P_MNPFSO);
O_EXITSO |= (1 << P_EXITSO);
D_EXITSO |= (1 << P_EXITSO);
D_CLUTCH |= (1 << P_CLUTCH);
//// TIMER 0 ////
// Enable CTC mode
TCCR0A = (1 << WGM01);
// Select clock F_CPU/64 (125kHz)
TCCR0B = (1 << CS01) | (1 << CS00);
// 1kHz interrupt frequency
OCR0A = 124;
// Enable OC0A interrupt
TIMSK = (1 << OCIE0A);
//// EXTERNAL INTERRUPTS ////
// Trigger on the falling edge for operator panel input
MCUCR |= (1 << ISC01);
// Enable external interrupt 0
GIMSK |= (1 << INT0);
// Trigger on both edges for hall sensor input
MCUCR |= (1 << ISC10);
// Enable external interrupt 1
GIMSK |= (1 << INT1);
sei();
MillisecondCounter = 0;
State = PowerOnWait;
while(1)
{
switch(State)
{
case PowerOnWait:
// Does nothing.
// Waits for the initialization of the printer to complete
if(MillisecondCounter >= T_POWRON)
{
State = Idle;
}
break;
case Idle:
// Activate clutch and reset all sensor output just in case
// something has gone mucus
O_PPINSO |= (1 << P_PPINSO);
O_MNPFSO |= (1 << P_MNPFSO);
O_EXITSO |= (1 << P_EXITSO);
O_LED_RD |= (1 << P_LED_RD);
O_CLUTCH &= ~(1 << P_CLUTCH);
O_LED_GN &= ~(1 << P_LED_GN);
// Check if motor is running and new paper in sensor is
// blocked by carrier
if(HallSensorTimer < T_HALL_L && !(I_NPPINS & (1 << P_NPPINS)))
{
// Block carrier
O_CLUTCH |= (1 << P_CLUTCH);
O_LED_GN |= (1 << P_LED_GN);
O_LED_RD &= ~(1 << P_LED_RD);
State = ClutchDelay;
MillisecondCounter = 0;
}
break;
case ClutchDelay:
if(MillisecondCounter >= T_CLUTCH)
{
// Deactivate clutch to allow carrier to get pulled in
O_CLUTCH &= ~(1 << P_CLUTCH);
// Enable manual paper feed sensor
O_MNPFSO &= ~(1 << P_MNPFSO);
State = WaitingForPaperIn;
}
break;
case WaitingForPaperIn:
// Check if paper in sensor is triggered
if(I_NPPINS & (1 << P_NPPINS))
{
// Enable paper in sensor output
O_PPINSO &= ~(1 << P_PPINSO);
O_LED_RD |= (1 << P_LED_RD);
MillisecondCounter = 0;
State = ExitOnDelay;
}
break;
case ExitOnDelay:
if(MillisecondCounter >= T_EXIT_H)
{
// Activate exit sensor output
O_EXITSO &= ~(1 << P_EXITSO);
MillisecondCounter = 0;
State = ManualPaperFeedOffDelay;
}
break;
case ManualPaperFeedOffDelay:
if(MillisecondCounter >= T_MNPF_L)
{
// Deactivate manual paper feed sensor output
O_MNPFSO |= (1 << P_MNPFSO);
MillisecondCounter = 0;
State = PaperInOffDelay;
}
break;
case PaperInOffDelay:
if(MillisecondCounter >= T_PPIN_L)
{
// Deactivate paper in sensor output
O_PPINSO |= (1 << P_PPINSO);
MillisecondCounter = 0;
State = ExitOffDelay;
}
case ExitOffDelay:
if(MillisecondCounter >= T_EXIT_L)
{
// Deactivate exit sensor output
O_EXITSO |= (1 << P_EXITSO);
MillisecondCounter = 0;
State = Idle;
}
break;
}
}
}
ISR(TIMER0_COMPA_vect)
{
MillisecondCounter++;
if(MillisecondCounter == 0)
{
MillisecondCounter = UINT32_MAX;
}
if(State != PowerOnWait)
{
HallSensorTimer++;
if(HallSensorTimer == 0)
{
HallSensorTimer = UINT16_MAX;
}
if(HallSensorTimer < T_HALL_L)
{
O_LED_YE |= (1 << P_LED_YE);
}
else
{
O_LED_YE &= ~(1 << P_LED_YE);
}
}
}
// External interrupt. Triggered by control panel
ISR(INT0_vect)
{
// Just reset everything
// if(State == WaitingForPaperIn || State == ClutchDelay)
if(State != PowerOnWait)
{
State = Idle;
}
}
// External interrupt. Triggered by hall sensor
ISR(INT1_vect)
{
HallSensorTimer = 0;
}

25
src/main.h Normal file
View file

@ -0,0 +1,25 @@
// -fruchti 2015
#ifndef MAIN_H_
#define MAIN_H_
#include "config.h"
typedef enum
{
PowerOnWait,
Idle,
ClutchDelay,
WaitingForPaperIn,
ExitOnDelay,
ManualPaperFeedOffDelay,
PaperInOffDelay,
ExitOffDelay
} State_t;
int main(void);
ISR(TIMER0_COMPA_vect);
ISR(INT0_vect);
ISR(INT1_vect);
#endif