Add servo control for paper cutter

This commit is contained in:
fruchti 2018-08-04 16:11:18 +02:00
parent 04bbaf8e14
commit cf1ac4b7e8
4 changed files with 91 additions and 10 deletions

View file

@ -1 +1 @@
205 220

View file

@ -18,6 +18,7 @@ static State_t State_Idle(void);
static State_t State_PaperLoad(void); static State_t State_PaperLoad(void);
static State_t State_Printing(void); static State_t State_Printing(void);
static State_t State_PaperFeed(void); static State_t State_PaperFeed(void);
static State_t State_Cutting(void);
static volatile State_t State = {State_Idle}; static volatile State_t State = {State_Idle};
static void InitStepper(void) static void InitStepper(void)
@ -70,7 +71,7 @@ static void InitDataLines(void)
RCC->APB1ENR |= RCC_APB1ENR_TIM2EN; RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;
RCC->AHBENR |= RCC_AHBENR_DMA1EN; RCC->AHBENR |= RCC_AHBENR_DMA1EN;
TIM2->PSC = 719; // Each tick corresponds to ten microseconds TIM2->PSC = 720 - 1; // Each tick corresponds to ten microseconds
TIM2->ARR = 201; // 2 milliseconds TIM2->ARR = 201; // 2 milliseconds
TIM2->CCR3 = 1; TIM2->CCR3 = 1;
TIM2->CCR4 = 1; TIM2->CCR4 = 1;
@ -114,8 +115,8 @@ static void InitThermistor(void)
RCC->APB2ENR |= RCC_APB2ENR_ADC1EN; RCC->APB2ENR |= RCC_APB2ENR_ADC1EN;
GPIOB->CRL = (GPIOB->CRL GPIOB->CRL = (GPIOB->CRL
& ~(0x0f << PIN_THERMISTOR)) & ~(0x0f << (4 * PIN_THERMISTOR)))
| (0x00 << PIN_THERMISTOR) // Analog mode | (0x00 << (4 * PIN_THERMISTOR)) // Analog mode
; ;
// Enable ADC // Enable ADC
@ -139,6 +140,27 @@ static void InitThermistor(void)
ADC1->CR2 |= ADC_CR2_ADON; ADC1->CR2 |= ADC_CR2_ADON;
} }
static void InitCutter(void)
{
RCC->APB1ENR |= RCC_APB1ENR_TIM4EN;
RCC->APB2ENR |= RCC_APB2ENR_IOPBEN;
GPIOB->CRL = (GPIOB->CRL
& ~(0xf << (4 * PIN_PAPER_CUT)))
| (0x09 << (4 * PIN_PAPER_CUT)) // Output in AF mode, max. 10 MHz
;
// Servo pulse length should be between 1 and 2 ms with a period of 20 ms
TIM4->PSC = 72 - 1; // Divide to one microsecond
TIM4->ARR = 20000; // 50 Hz frequency
TIM4->CCR2 = 1000; // 1 millisecond
TIM4->CCMR1 = TIM_CCMR1_OC2M_2 | TIM_CCMR1_OC2M_1;
TIM4->CR1 = TIM_CR1_CEN;
TIM4->CCER = TIM_CCER_CC2E;
NVIC_EnableIRQ(TIM4_IRQn);
}
static bool HasPaper(void) static bool HasPaper(void)
{ {
return !(GPIOB->IDR & (1 << PIN_PAPER)); return !(GPIOB->IDR & (1 << PIN_PAPER));
@ -253,6 +275,17 @@ static State_t State_PaperFeed(void)
return (State_t){State_PaperFeed}; return (State_t){State_PaperFeed};
} }
static State_t State_Cutting(void)
{
// The timer interrupt is deactived once a sufficient number of pulses is
// generated
if(~TIM4->DIER & TIM_DIER_UIE)
{
return (State_t){State_Idle};
}
return (State_t){State_Cutting};
}
static State_t State_Idle(void) static State_t State_Idle(void)
{ {
if(!HasPaper()) if(!HasPaper())
@ -309,8 +342,30 @@ LTP1245_Result_t LTP1245_FeedPaper(int lines)
return LTP1245_OK; return LTP1245_OK;
} }
LTP1245_Result_t LTP1245_Cut(void)
{
do
{
if(!HasPaper())
{
return LTP1245_NO_PAPER;
}
if(!HeadDown())
{
return LTP1245_HEAD_UP;
}
} while(State.fn != State_Idle);
TIM4->CCR2 = 2000;
TIM4->DIER = TIM_DIER_UIE;
State = (State_t){State_Cutting};
return LTP1245_OK;
}
void LTP1245_Init(void) void LTP1245_Init(void)
{ {
InitCutter();
InitDataLines(); InitDataLines();
InitSensors(); InitSensors();
InitStepper(); InitStepper();
@ -435,6 +490,33 @@ void ADC1_2_IRQHandler(void)
/ (int)((5 * 1.4 - 2.9) * (5 * 1.4 - 2.9)); / (int)((5 * 1.4 - 2.9) * (5 * 1.4 - 2.9));
} }
void TIM4_IRQHandler(void)
{
if(TIM4->SR & TIM_SR_UIF)
{
static int ct = 0;
if(ct == 0)
{
ct = LTP1245_CUT_DURATION;
}
else
{
ct--;
if(ct == 0)
{
TIM4->DIER &= ~TIM_DIER_UIE;
}
else if(ct == LTP1245_CUT_DURATION / 2)
{
TIM4->CCR2 = 1000; // Back to -90 deg
}
}
TIM4->SR &= ~TIM_SR_UIF;
}
}
void DMA1_Channel5_IRQHandler(void) void DMA1_Channel5_IRQHandler(void)
{ {
DMA1->IFCR = DMA_IFCR_CTCIF5; DMA1->IFCR = DMA_IFCR_CTCIF5;

View file

@ -13,6 +13,7 @@ typedef enum
} LTP1245_Result_t; } LTP1245_Result_t;
#define LTP1245_MAX_DRIVE_FREQ 473 // In Hz #define LTP1245_MAX_DRIVE_FREQ 473 // In Hz
#define LTP1245_CUT_DURATION 50 // In steps of 20 ms
#define LTP1245_BUFFER_LINES 64 #define LTP1245_BUFFER_LINES 64
#define LTP1245_LINEWIDTH 384 // In pixels #define LTP1245_LINEWIDTH 384 // In pixels
#define LTP1245_LINE_BYTES (LTP1245_LINEWIDTH / 8) #define LTP1245_LINE_BYTES (LTP1245_LINEWIDTH / 8)
@ -22,3 +23,4 @@ typedef enum
void LTP1245_Init(void); void LTP1245_Init(void);
LTP1245_Result_t LTP1245_FeedPaper(int lines); LTP1245_Result_t LTP1245_FeedPaper(int lines);
LTP1245_Result_t LTP1245_Print(uint8_t *data, int lines); LTP1245_Result_t LTP1245_Print(uint8_t *data, int lines);
LTP1245_Result_t LTP1245_Cut(void);

View file

@ -13,12 +13,9 @@ int main(void)
// LTP1245_FeedPaper(10); // LTP1245_FeedPaper(10);
// Print_Text("Testy McTestFace", &Arpegius_32); // Print_Text("Testy McTestFace", &Arpegius_32);
// Print_Text("123555bcD", &Messe_Duesseldorf_39); // Print_Text("123555bcD", &Messe_Duesseldorf_39);
Print_Text("Abcdef Ghi Jkl", &Hannover_Messe_Serif_26); Print_Text("This is a long line with very much text!", &Hannover_Messe_Serif_26);
Print_Text("Abcdef Ghi Jkl", &Hannover_Messe_Serif_26); LTP1245_FeedPaper(10);
Print_Text("Abcdef Ghi Jkl", &Hannover_Messe_Serif_26); LTP1245_Cut();
Print_Text("Abcdef Ghi Jkl", &Hannover_Messe_Serif_26);
Print_Text("Abcdef Ghi Jkl", &Hannover_Messe_Serif_26);
LTP1245_FeedPaper(100);
for(;;) for(;;)
{ {