diff --git a/PT6302.c b/PT6302.c index 89f4abe..dde8710 100644 --- a/PT6302.c +++ b/PT6302.c @@ -34,6 +34,30 @@ inline void PT6302_startup (void) _delay_us (TWRSTB); } +inline void transmit_bit(const uint8_t data) +{ + PORTB &= ~CLKBpin; + if ((data & 0x01) == 0) + PORTB &= ~DINpin; + else + PORTB |= DINpin; + _delay_us (TCW); + PORTB |= CLKBpin; + _delay_us (TCW); +} + +void transmit_byte (uint8_t data) +{ + _delay_us (1); + PORTB &= ~CSBpin; + for (int j = 0; j < 8; ++j, data = (data >> 1)) + { + transmit_bit (data); + } + _delay_us (TCSH); + PORTB |= CSBpin; +} + void transmit_bytes (const uint8_t *payload, const uint8_t size) { _delay_us (1); @@ -43,14 +67,7 @@ void transmit_bytes (const uint8_t *payload, const uint8_t size) uint8_t data = payload[i]; for (int j = 0; j < 8; ++j, data = (data >> 1)) { - PORTB &= ~CLKBpin; - if ((data & 0x01) == 0) - PORTB &= ~DINpin; - else - PORTB |= DINpin; - _delay_us (TCW); - PORTB |= CLKBpin; - _delay_us (TCW); + transmit_bit (data); } _delay_us (TDOFF); } @@ -65,7 +82,7 @@ void set_ports (uint8_t gp1, uint8_t gp2) command |= 0x01; if (gp2 != 0) command |= 0x02; - transmit_bytes (&command, 1); + transmit_byte (command); } void set_duty (uint8_t brightness) @@ -75,7 +92,7 @@ void set_duty (uint8_t brightness) brightness = 7; } const uint8_t command = 0x50 | brightness; - transmit_bytes (&command, 1); + transmit_byte (command); } void set_digits (uint8_t digit_count) @@ -86,7 +103,7 @@ void set_digits (uint8_t digit_count) digit_count = 9; digit_count -= 8; const uint8_t command = 0x60 | digit_count; - transmit_bytes (&command, 1); + transmit_byte (command); } void set_ADRAM (const uint8_t address, const uint8_t *data, uint8_t size) @@ -119,7 +136,6 @@ void set_CGRAM (const uint8_t address, const uint8_t *data, uint8_t size) } transmit_bytes (payload, size); } - void set_DCRAM (const uint8_t address, const uint8_t *cg_address, uint8_t size) { if (address > DISPLAY_DIGITS) @@ -133,4 +149,15 @@ void set_DCRAM (const uint8_t address, const uint8_t *cg_address, uint8_t size) payload[i] = cg_address[i - 1]; } transmit_bytes (payload, size); -} \ No newline at end of file +} +void all_on (void) +{ + const uint8_t command = 0x72; + transmit_byte (command); +} + +void all_off (void) +{ + const uint8_t command = 0x71; + transmit_byte (command); +} diff --git a/PT6302.h b/PT6302.h index dcc491e..2021291 100644 --- a/PT6302.h +++ b/PT6302.h @@ -1,3 +1,4 @@ +#pragma once /* * AT-VFD * Copyright (C) 2022 László Párkányi @@ -18,15 +19,13 @@ * USA */ -#ifndef _PT6302_H_ -#define _PT6302_H_ - #define DISPLAY_DIGITS 12 /* PT6302 timing constants, all in usec */ static const uint8_t TCW = 1; //CLKB pulse width static const uint8_t TDOFF = 16; //Data processing time static const uint8_t DTCSH = 16; //time difference between tDOFF and tCSH +static const uint8_t TCSH = 32; //Chip select hold time static const uint8_t TPRZ = 100; //VDD rise time static const uint8_t TWRSTB = 1; //RSTB pulse width @@ -49,6 +48,19 @@ enum RAM_types{ * ------------------------------------------------------------- */ void PT6302_startup (void); +/* -------------------------------- + * Transmits the LSB of data + * Handles communication except CSB + * --------------------------------- */ +static inline void transmit_bit(uint8_t data); + +/* ------------------------------------------------ + * Transmits one character (data) + * Handles the entire communication + * this is (mostly) intended to be used by wrappers + * ------------------------------------------------ */ +void transmit_byte(uint8_t data); + /* ------------------------------------------------------------------------------- * Transmits size byte of data through the VFD controller's interface from payload * Handles CLKB, CSB and timing constraints @@ -59,14 +71,14 @@ void transmit_bytes (const uint8_t *payload, uint8_t size); * Sets GP1 and GP2 to the values given in the function call * Handles the entire communication * --------------------------------------------------------- */ -void set_ports (uint8_t gp1, uint8_t gp2); +inline void set_ports (uint8_t gp1, uint8_t gp2); /* --------------------------------------------------------------------------------------- * Sets the number of digit_count the VFD has * digit_count below 9 are interpreted as 9 and digit_count above 16 are interpreted as 16 * Handles the entire communication * --------------------------------------------------------------------------------------- */ -void set_digits(uint8_t digit_count); +inline void set_digits(uint8_t digit_count); /* ----------------------------------------------------------- * Sets duty cycle of controlled VFD @@ -74,7 +86,7 @@ void set_digits(uint8_t digit_count); * duty = (brightness + 8)/16 (valid between 8/16 and 15/16) * Handles the entire communication * ----------------------------------------------------------- */ -void set_duty (uint8_t brightness); +inline void set_duty (uint8_t brightness); /* ------------------------------------------------------------------------------------------ * Set the content of the DCRAM - this will display a character from the CGROM/RAM at address @@ -108,4 +120,15 @@ void set_CGRAM (uint8_t address, const uint8_t *data, uint8_t size); * --------------------------------------------------------------------------------------- */ void set_ADRAM(uint8_t address, const uint8_t* data, uint8_t size); -#endif //_PT6302_H_ +/* ----------------------------------------- + * Turn all outputs of the VFD controller on + * This is primarily used for testing + * Handles the entire communication + * ----------------------------------------- */ +inline void all_on(void); + +/* ------------------------------------------ + * Turn all outputs of the VFD controller off + * Handles the entire communication + * ------------------------------------------ */ +inline void all_off(void); \ No newline at end of file diff --git a/main.c b/main.c index c0b0ede..caf9d94 100644 --- a/main.c +++ b/main.c @@ -25,10 +25,6 @@ #include #include "PT6302.h" -/* PT6302 commands */ -static const uint8_t lighton = 0x72; -static const uint8_t lightoff = 0x71; - int main () { @@ -46,9 +42,9 @@ int main () set_DCRAM (0, characters, DISPLAY_DIGITS); while (1) { - transmit_bytes (&lighton, 1); + all_on(); _delay_us (1000); - transmit_bytes (&lightoff, 1); + all_off(); _delay_us (1000); } }