Made pullup setting mandatory in order to avoid errors and refactored some code

This commit is contained in:
BlackMark 2016-02-25 22:20:24 +01:00
parent e2e45becc0
commit 6b44b1868c
4 changed files with 65 additions and 75 deletions

View File

@ -7,7 +7,7 @@
#ifndef CLOCK_H #ifndef CLOCK_H
#define CLOCK_H #define CLOCK_H
#define F_CPU 16000000 #define F_CPU 20000000
#include <util/delay.h> #include <util/delay.h>

View File

@ -3,7 +3,7 @@
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
volatile uint8_t* InOut::getPort( Pin enmPin, Type enmType ) volatile uint8_t* InOut::getPort( Pin enmPin, Type enmType )
{ {
volatile uint8_t* vpui8Port = nullptr; volatile uint8_t *vpui8Port = nullptr;
if( enmPin == Pin::P_NONE ) 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 ) if( enmPin == Pin::P_NONE )
{ {
return; return;
} }
volatile uint8_t* vpui8PortDir = getPort( enmPin, Type::T_DDR ); volatile uint8_t *vpui8PortDir = getPort( enmPin, Type::T_DDR );
volatile uint8_t* vpui8PortOut = getPort( enmPin, Type::T_PORT ); volatile uint8_t *vpui8PortOut = getPort( enmPin, Type::T_PORT );
uint8_t ui8Pin = getPin( enmPin ); uint8_t ui8Pin = getPin( enmPin );
setPinDirection( vpui8PortDir, ui8Pin, enmDir ); setPinDirection( vpui8PortDir, ui8Pin, enmDir );
if( enmDir == Dir::D_IN && bPullup ) if( enmDir == Dir::D_IN )
{ {
writePin( vpui8PortOut, ui8Pin, true ); writePin( vpui8PortOut, ui8Pin, bPullup );
}
else if( enmDir == Dir::D_IN && !bPullup )
{
writePin( vpui8PortOut, ui8Pin, false );
} }
} }
@ -124,7 +120,7 @@ bool InOut::readPin( Pin enmPin )
return false; 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 ); uint8_t ui8Pin = getPin( enmPin );
return readPin( vpui8Port, ui8Pin ); return readPin( vpui8Port, ui8Pin );
@ -138,7 +134,7 @@ void InOut::writePin( Pin enmPin, bool bValue )
return; return;
} }
volatile uint8_t* vpui8Port = getPort( enmPin, Type::T_PORT ); volatile uint8_t *vpui8Port = getPort( enmPin, Type::T_PORT );
uint8_t ui8Pin = getPin( enmPin ); uint8_t ui8Pin = getPin( enmPin );
writePin( vpui8Port, ui8Pin, bValue ); writePin( vpui8Port, ui8Pin, bValue );
@ -152,7 +148,7 @@ uint8_t InOut::readPort( Pin enmPortPin )
return 0; return 0;
} }
volatile uint8_t* vpui8Port = getPort( enmPortPin, Type::T_PIN ); volatile uint8_t *vpui8Port = getPort( enmPortPin, Type::T_PIN );
return readPort( vpui8Port ); return readPort( vpui8Port );
} }
@ -165,7 +161,7 @@ void InOut::writePort( Pin enmPortPin, uint8_t ui8Value )
return; return;
} }
volatile uint8_t* vpui8Port = getPort( enmPortPin, Type::T_PORT ); volatile uint8_t *vpui8Port = getPort( enmPortPin, Type::T_PORT );
writePort( vpui8Port, ui8Value ); writePort( vpui8Port, ui8Value );
} }

View File

@ -1,7 +1,7 @@
/* /*
* Copyright (c) by BlackMark 2015-2016 * Copyright (c) by BlackMark 2015-2016
* Date 05/01/2016 * Date 25/02/2016
* Version 2.3 * Version 2.4
*/ */
#ifndef INOUT_H #ifndef INOUT_H
@ -120,7 +120,7 @@ public:
static volatile uint8_t* getPort( Pin enmPin, Type enmType ); static volatile uint8_t* getPort( Pin enmPin, Type enmType );
static uint8_t getPin( Pin enmPin ); 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 bool readPin( Pin enmPin );
static void writePin( Pin enmPin, bool bValue ); static void writePin( Pin enmPin, bool bValue );
@ -128,7 +128,7 @@ public:
static void writePort( Pin enmPortPin, uint8_t ui8Value ); 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 ) 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 ) 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 ) 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 ) ); *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; 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; *vpui8Port = ui8Value;
} }
@ -188,9 +188,9 @@ public:
class InOutPin class InOutPin
{ {
private: private:
volatile uint8_t* m_vpui8Input; volatile uint8_t *m_vpui8Input;
volatile uint8_t* m_vpui8Dir; volatile uint8_t *m_vpui8Dir;
volatile uint8_t* m_vpui8Output; volatile uint8_t *m_vpui8Output;
uint8_t m_ui8Pin; uint8_t m_ui8Pin;
@ -202,7 +202,7 @@ public:
void setPin( InOut::Pin enmPin ); 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 ) if( !m_vpui8Dir || !m_vpui8Output )
{ {
@ -211,13 +211,9 @@ public:
InOut::setPinDirection( m_vpui8Dir, m_ui8Pin, enmDir ); 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 ); InOut::writePin( m_vpui8Output, m_ui8Pin, bPullup );
}
else if( enmDir == InOut::Dir::D_IN && !bPullup )
{
InOut::writePin( m_vpui8Output, m_ui8Pin, false );
} }
} }
@ -249,9 +245,9 @@ public:
class InOutPort class InOutPort
{ {
private: private:
volatile uint8_t* m_vpui8Input; volatile uint8_t *m_vpui8Input;
volatile uint8_t* m_vpui8Dir; volatile uint8_t *m_vpui8Dir;
volatile uint8_t* m_vpui8Output; volatile uint8_t *m_vpui8Output;
public: public:
InOutPort(); InOutPort();
@ -261,7 +257,7 @@ public:
void setPort( InOut::Pin enmPortPin ); 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 ) if( !m_vpui8Dir || !m_vpui8Output )
{ {
@ -270,13 +266,9 @@ public:
InOut::setPortDirection( m_vpui8Dir, enmDir ); InOut::setPortDirection( m_vpui8Dir, enmDir );
if( enmDir == InOut::Dir::D_IN && bPullup ) if( enmDir == InOut::Dir::D_IN )
{ {
InOut::writePort( m_vpui8Output, 0xFF ); InOut::writePort( m_vpui8Output, ( ( bPullup ) ? ( 0xFF ) : ( 0x00 ) ) );
}
else if( enmDir == InOut::Dir::D_IN && !bPullup )
{
InOut::writePort( m_vpui8Output, 0x00 );
} }
} }

View File

@ -1,7 +1,7 @@
/* /*
* Copyright (c) by BlackMark 2015-2016 * Copyright (c) by BlackMark 2015-2016
* Date 02/01/2016 * Date 25/02/2016
* Version 1.2 * Version 1.3
*/ */
#include "Clock.h" #include "Clock.h"
@ -9,40 +9,42 @@
int main() int main()
{ {
InOutPin cPinRed( InOut::Pin::P_B5 ); InOutPin cLED( InOut::Pin::P_D7 );
InOutPin cPinGreen( InOut::Pin::P_B4 );
InOutPin cPinBlue( InOut::Pin::P_B3 );
cPinRed.setDirection( InOut::Dir::D_OUT ); cLED.setDirection( InOut::Dir::D_OUT, false );
cPinGreen.setDirection( InOut::Dir::D_OUT );
cPinBlue.setDirection( InOut::Dir::D_OUT );
cPinRed.write( false ); cLED.write( false );
cPinGreen.write( false );
cPinBlue.write( false );
while( true ) while( true )
{ {
cPinRed.write( true ); for( uint8_t i = 0; i < 3; ++i )
_delay_ms( 1000 ); {
cPinRed.write( false ); cLED.write( true );
_delay_ms( 100 );
cPinGreen.write( true ); cLED.write( false );
_delay_ms( 1000 ); _delay_ms( 100 );
cPinGreen.write( false ); }
cPinBlue.write( true ); _delay_ms( 300 );
_delay_ms( 1000 );
cPinBlue.write( false ); for( uint8_t i = 0; i < 3; ++i )
{
cPinRed.write( true ); cLED.write( true );
cPinGreen.write( true ); _delay_ms( 300 );
cPinBlue.write( true ); cLED.write( false );
_delay_ms( 5000 ); _delay_ms( 300 );
}
cPinRed.write( false );
cPinGreen.write( false ); _delay_ms( 100 );
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( 1000 ); _delay_ms( 1000 );
} }