From 6b44b1868cbdbf9cc0d75c89d1cdb21263a6c787 Mon Sep 17 00:00:00 2001 From: BlackMark Date: Thu, 25 Feb 2016 22:20:24 +0100 Subject: [PATCH] Made pullup setting mandatory in order to avoid errors and refactored some code --- InOut/Clock.h | 2 +- InOut/InOut.cpp | 24 ++++++++----------- InOut/InOut.h | 50 ++++++++++++++++---------------------- InOut/main.cpp | 64 +++++++++++++++++++++++++------------------------ 4 files changed, 65 insertions(+), 75 deletions(-) diff --git a/InOut/Clock.h b/InOut/Clock.h index cbdc6c5..4b8cbff 100644 --- a/InOut/Clock.h +++ b/InOut/Clock.h @@ -7,7 +7,7 @@ #ifndef CLOCK_H #define CLOCK_H -#define F_CPU 16000000 +#define F_CPU 20000000 #include diff --git a/InOut/InOut.cpp b/InOut/InOut.cpp index 8367531..ca576e5 100644 --- a/InOut/InOut.cpp +++ b/InOut/InOut.cpp @@ -3,7 +3,7 @@ ////////////////////////////////////////////////////////////////////////// volatile uint8_t* InOut::getPort( Pin enmPin, Type enmType ) { - volatile uint8_t* vpui8Port = nullptr; + volatile uint8_t *vpui8Port = nullptr; if( enmPin == Pin::P_NONE ) { @@ -93,26 +93,22 @@ uint8_t InOut::getPin( Pin enmPin ) } ////////////////////////////////////////////////////////////////////////// -void InOut::setPinDirection( Pin enmPin, Dir enmDir, bool bPullup /* = true */ ) +void InOut::setPinDirection( Pin enmPin, Dir enmDir, bool bPullup ) { if( enmPin == Pin::P_NONE ) { return; } - volatile uint8_t* vpui8PortDir = getPort( enmPin, Type::T_DDR ); - volatile uint8_t* vpui8PortOut = getPort( enmPin, Type::T_PORT ); + volatile uint8_t *vpui8PortDir = getPort( enmPin, Type::T_DDR ); + volatile uint8_t *vpui8PortOut = getPort( enmPin, Type::T_PORT ); uint8_t ui8Pin = getPin( enmPin ); setPinDirection( vpui8PortDir, ui8Pin, enmDir ); - if( enmDir == Dir::D_IN && bPullup ) + if( enmDir == Dir::D_IN ) { - writePin( vpui8PortOut, ui8Pin, true ); - } - else if( enmDir == Dir::D_IN && !bPullup ) - { - writePin( vpui8PortOut, ui8Pin, false ); + writePin( vpui8PortOut, ui8Pin, bPullup ); } } @@ -124,7 +120,7 @@ bool InOut::readPin( Pin enmPin ) return false; } - volatile uint8_t* vpui8Port = getPort( enmPin, Type::T_PIN ); + volatile uint8_t *vpui8Port = getPort( enmPin, Type::T_PIN ); uint8_t ui8Pin = getPin( enmPin ); return readPin( vpui8Port, ui8Pin ); @@ -138,7 +134,7 @@ void InOut::writePin( Pin enmPin, bool bValue ) return; } - volatile uint8_t* vpui8Port = getPort( enmPin, Type::T_PORT ); + volatile uint8_t *vpui8Port = getPort( enmPin, Type::T_PORT ); uint8_t ui8Pin = getPin( enmPin ); writePin( vpui8Port, ui8Pin, bValue ); @@ -152,7 +148,7 @@ uint8_t InOut::readPort( Pin enmPortPin ) return 0; } - volatile uint8_t* vpui8Port = getPort( enmPortPin, Type::T_PIN ); + volatile uint8_t *vpui8Port = getPort( enmPortPin, Type::T_PIN ); return readPort( vpui8Port ); } @@ -165,7 +161,7 @@ void InOut::writePort( Pin enmPortPin, uint8_t ui8Value ) return; } - volatile uint8_t* vpui8Port = getPort( enmPortPin, Type::T_PORT ); + volatile uint8_t *vpui8Port = getPort( enmPortPin, Type::T_PORT ); writePort( vpui8Port, ui8Value ); } diff --git a/InOut/InOut.h b/InOut/InOut.h index 20a0efe..61de1e0 100644 --- a/InOut/InOut.h +++ b/InOut/InOut.h @@ -1,7 +1,7 @@ /* * Copyright (c) by BlackMark 2015-2016 -* Date 05/01/2016 -* Version 2.3 +* Date 25/02/2016 +* Version 2.4 */ #ifndef INOUT_H @@ -120,7 +120,7 @@ public: static volatile uint8_t* getPort( Pin enmPin, Type enmType ); static uint8_t getPin( Pin enmPin ); - static void setPinDirection( Pin enmPin, Dir enmDir, bool bPullup = true ); + static void setPinDirection( Pin enmPin, Dir enmDir, bool bPullup ); static bool readPin( Pin enmPin ); static void writePin( Pin enmPin, bool bValue ); @@ -128,7 +128,7 @@ public: static void writePort( Pin enmPortPin, uint8_t ui8Value ); ////////////////////////////////////////////////////////////////////////// - static inline void setPinDirection( volatile uint8_t* vpui8Port, uint8_t ui8Pin, Dir enmDir ) + static inline void setPinDirection( volatile uint8_t *vpui8Port, uint8_t ui8Pin, Dir enmDir ) { if( enmDir == Dir::D_OUT ) { @@ -141,7 +141,7 @@ public: } ////////////////////////////////////////////////////////////////////////// - static inline bool readPin( volatile uint8_t* vpui8Port, uint8_t ui8Pin ) + static inline bool readPin( volatile uint8_t *vpui8Port, uint8_t ui8Pin ) { if( ( ( *vpui8Port ) >> ui8Pin ) & 1 ) { @@ -152,7 +152,7 @@ public: } ////////////////////////////////////////////////////////////////////////// - static inline void writePin( volatile uint8_t* vpui8Port, uint8_t ui8Pin, bool bValue ) + static inline void writePin( volatile uint8_t *vpui8Port, uint8_t ui8Pin, bool bValue ) { if( bValue ) { @@ -165,19 +165,19 @@ public: } ////////////////////////////////////////////////////////////////////////// - static inline void setPortDirection( volatile uint8_t* vpui8Port, Dir enmDir ) + static inline void setPortDirection( volatile uint8_t *vpui8Port, Dir enmDir ) { *vpui8Port = ( ( enmDir == InOut::Dir::D_OUT ) ? ( 0xFF ) : ( 0x00 ) ); } ////////////////////////////////////////////////////////////////////////// - static inline uint8_t readPort( volatile uint8_t* vpui8Port ) + static inline uint8_t readPort( volatile uint8_t *vpui8Port ) { return *vpui8Port; } ////////////////////////////////////////////////////////////////////////// - static inline void writePort( volatile uint8_t* vpui8Port, uint8_t ui8Value ) + static inline void writePort( volatile uint8_t *vpui8Port, uint8_t ui8Value ) { *vpui8Port = ui8Value; } @@ -188,9 +188,9 @@ public: class InOutPin { private: - volatile uint8_t* m_vpui8Input; - volatile uint8_t* m_vpui8Dir; - volatile uint8_t* m_vpui8Output; + volatile uint8_t *m_vpui8Input; + volatile uint8_t *m_vpui8Dir; + volatile uint8_t *m_vpui8Output; uint8_t m_ui8Pin; @@ -202,7 +202,7 @@ public: void setPin( InOut::Pin enmPin ); ////////////////////////////////////////////////////////////////////////// - inline void setDirection( InOut::Dir enmDir, bool bPullup = true ) + inline void setDirection( InOut::Dir enmDir, bool bPullup ) { if( !m_vpui8Dir || !m_vpui8Output ) { @@ -211,13 +211,9 @@ public: InOut::setPinDirection( m_vpui8Dir, m_ui8Pin, enmDir ); - if( enmDir == InOut::Dir::D_IN && bPullup ) + if( enmDir == InOut::Dir::D_IN ) { - InOut::writePin( m_vpui8Output, m_ui8Pin, true ); - } - else if( enmDir == InOut::Dir::D_IN && !bPullup ) - { - InOut::writePin( m_vpui8Output, m_ui8Pin, false ); + InOut::writePin( m_vpui8Output, m_ui8Pin, bPullup ); } } @@ -249,9 +245,9 @@ public: class InOutPort { private: - volatile uint8_t* m_vpui8Input; - volatile uint8_t* m_vpui8Dir; - volatile uint8_t* m_vpui8Output; + volatile uint8_t *m_vpui8Input; + volatile uint8_t *m_vpui8Dir; + volatile uint8_t *m_vpui8Output; public: InOutPort(); @@ -261,7 +257,7 @@ public: void setPort( InOut::Pin enmPortPin ); ////////////////////////////////////////////////////////////////////////// - inline void setDirection( InOut::Dir enmDir, bool bPullup = true ) + inline void setDirection( InOut::Dir enmDir, bool bPullup ) { if( !m_vpui8Dir || !m_vpui8Output ) { @@ -270,13 +266,9 @@ public: InOut::setPortDirection( m_vpui8Dir, enmDir ); - if( enmDir == InOut::Dir::D_IN && bPullup ) + if( enmDir == InOut::Dir::D_IN ) { - InOut::writePort( m_vpui8Output, 0xFF ); - } - else if( enmDir == InOut::Dir::D_IN && !bPullup ) - { - InOut::writePort( m_vpui8Output, 0x00 ); + InOut::writePort( m_vpui8Output, ( ( bPullup ) ? ( 0xFF ) : ( 0x00 ) ) ); } } diff --git a/InOut/main.cpp b/InOut/main.cpp index d16e46a..233fbc7 100644 --- a/InOut/main.cpp +++ b/InOut/main.cpp @@ -1,7 +1,7 @@ /* * Copyright (c) by BlackMark 2015-2016 -* Date 02/01/2016 -* Version 1.2 +* Date 25/02/2016 +* Version 1.3 */ #include "Clock.h" @@ -9,40 +9,42 @@ int main() { - InOutPin cPinRed( InOut::Pin::P_B5 ); - InOutPin cPinGreen( InOut::Pin::P_B4 ); - InOutPin cPinBlue( InOut::Pin::P_B3 ); + InOutPin cLED( InOut::Pin::P_D7 ); - cPinRed.setDirection( InOut::Dir::D_OUT ); - cPinGreen.setDirection( InOut::Dir::D_OUT ); - cPinBlue.setDirection( InOut::Dir::D_OUT ); + cLED.setDirection( InOut::Dir::D_OUT, false ); - cPinRed.write( false ); - cPinGreen.write( false ); - cPinBlue.write( false ); + cLED.write( false ); while( true ) { - cPinRed.write( true ); - _delay_ms( 1000 ); - cPinRed.write( false ); - - cPinGreen.write( true ); - _delay_ms( 1000 ); - cPinGreen.write( false ); - - cPinBlue.write( true ); - _delay_ms( 1000 ); - cPinBlue.write( false ); - - cPinRed.write( true ); - cPinGreen.write( true ); - cPinBlue.write( true ); - _delay_ms( 5000 ); - - cPinRed.write( false ); - cPinGreen.write( false ); - cPinBlue.write( false ); + for( uint8_t i = 0; i < 3; ++i ) + { + cLED.write( true ); + _delay_ms( 100 ); + cLED.write( false ); + _delay_ms( 100 ); + } + + _delay_ms( 300 ); + + for( uint8_t i = 0; i < 3; ++i ) + { + cLED.write( true ); + _delay_ms( 300 ); + cLED.write( false ); + _delay_ms( 300 ); + } + + _delay_ms( 100 ); + + for( uint8_t i = 0; i < 3; ++i ) + { + cLED.write( true ); + _delay_ms( 100 ); + cLED.write( false ); + _delay_ms( 100 ); + } + _delay_ms( 1000 ); }