MyTetra Share
Делитесь знаниями!
functionz.c
Время создания: 30.11.2017 14:56
Раздел: Electronics - Microcontrollers - AVR8 - AVR GCC - C_PROJ - Clock 7sgm (red) - ver.4

#ifndef __FUNCTIONZ_C__

#define __FUNCTIONZ_C__


#include "functionz.h"


void clearRegState(void)

{

CLEAR_ON;

LOCK_HI;

CLEAR_OFF;

LOCK_LOW;

}


void sendRegData(const uint8_t data)

{

uint8_t bit_counter = RAZR;

do

{

DATA_LOW;

CLK_LOW;

if( CHECKBIT(data, --bit_counter) ) //if bit in data == 1

DATA_HI;

CLK_HI;

} while(bit_counter > 0);


CLK_LOW;

DATA_LOW;

}


uint8_t assignDigit(const uint8_t val)

{

//_____________________________________________

//START of CODE SELECTING block for number:

switch(val)

{

case 0: return DIG0;

case 1: return DIG1;

case 2: return DIG2;

case 3: return DIG3;

case 4: return DIG4;

case 5: return DIG5;

case 6: return DIG6;

case 7: return DIG7;

case 8: return DIG8;

case 9: return DIG9;

default: return val;

}

//END of CODE SELECTING block for number

}


//Assign LED segment with the right data

uint8_t assignSegment(const uint8_t segNu, const uint8_t v1, const uint8_t v2, const uint8_t v3, const uint8_t v4)

{

switch(segNu)

{

case 1: return v1;

case 2: return v2;

case 3: return v3;

case 4: return v4;

default: return 0;

}


}


/*

remainder will be returned through the pointer

integer part returns normaly

*/

uint8_t calcRemainder(uint8_t val, uint8_t * const rem)

{

uint8_t temp = 0;


while(val > 9)

{

val -= 10;

++temp;

}

*rem = val;

return temp;

}


/*

Increment for high or low part of the value

amount must be 10 for high part

or 1 for low part

*/

uint8_t incrValue(uint8_t val, const uint8_t limit, const uint8_t amount)

{

uint8_t remaind = 0; //remainder

uint8_t * const pRemaind = &remaind;

uint8_t result; //integer part


val = calcRemainder(val, pRemaind);


if(amount == 10)

{

result = (((++val) * 10) + remaind);

if( result >= limit )

return (0 + remaind);

else return result;

}

else// if(amount == 1)

{

result = ((val * 10) + (++remaind));

if( result >= limit || remaind == 10 )

return (val * 10);

else return result;

}

// else return 0;

}


void EEPROM_write(uint16_t uiAddress, uint8_t ucData)

{

while(EECR & (1<<EEWE)) // Wait for completion of previous write

{}

EEAR = uiAddress; //Set up address register

EEDR = ucData; //Set up data register

EECR |= (1<<EEMWE); //Write logical one to EEMWE

CLI;

EECR |= (1<<EEWE); //Start eeprom write by setting EEWE

SEI;

}


uint8_t EEPROM_read(uint16_t uiAddress)

{

while(EECR & (1<<EEWE)) // Wait for completion of previous write

{}

EEAR = uiAddress; //Set up address register

CLI;

EECR |= (1<<EERE); //Start eeprom read by writing EERE

SEI;

return EEDR; //Return data from data register

}


//365 or 366 days in current year?

uint8_t leapYearFoo(const uint8_t year_h, const uint8_t year_l)

{

uint16_t yr = (year_h << 8) + year_l;

if(yr % 400 == 0) return 1; //yes, it's a leap year

else if(yr % 100 == 0) return 0; //no, it's not a leap year

else if(yr % 4 == 0) return 1; //yes, it's a leap year

else return 0;

}


uint8_t findMaxDay(const uint8_t month, const uint8_t leapYear)

{

if(month == 2)

{

if(leapYear) return 29;

else return 28;

}

else

{

switch(month)

{

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12: return 31;

case 4:

case 6:

case 9:

case 11:

default: return 30;

}

}

}


#endif

Так же в этом разделе:
 
MyTetra Share v.0.53
Яндекс индекс цитирования