Subversion Repositories group.NITPanels

Rev

Blame | Last modification | View Log | RSS feed

#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/interrupt.h>
#include <string.h>
#include <util/delay.h>
#include <stdlib.h>

#include "avrutil.h"

uint32_t millis(void) {
        return systime;
}

inline uint8_t map_8(uint8_t x, uint8_t in_min, uint8_t in_max, uint8_t out_min, uint8_t out_max) {
        return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

inline void delay_ms(uint16_t count) {
  while(count--)
    _delay_ms(1);
}

void pulsePin(uint8_t port, uint8_t pin, uint16_t count) {
  sbi(port, pin);
  delay_ms(count);
  cbi(port, pin);
}

/*
void systemLed(uint8_t state) {
        if (state)
                sbi(PORTA, PA0);
        else
                cbi(PORTA, PA0);
}
*/

char* tick_addr;

void sysclockInit(void) {
        TIMSK = (1<<TOIE0);             // Eable timer overflow for Timer0
        TCCR0B = (1 << CS00) ;          // /1 prescaler
        TCNT0 = 0x00;                   // Set Timer0 to 0
}

void swap_u8(uint8_t* a, uint8_t* b) {
        uint8_t t = *b;
        *b = *a;
        *a = t;
}

void swap_u16(uint16_t* a, uint16_t* b) {
        uint16_t t = *b;
        *b = *a;
        *a = t;
}

/*
 * Convert from a uint32 to BCD
 * Use like:
 *      uint8_t[10] values; itobcd(ui32num, values);
 *      values will be an array with BCD digits
 *      returned value will be how many digits used
 */
uint8_t itobcd32(uint32_t val, uint8_t *result) {
  char BCD_text[10]; // max 10 digits on uint32
  uint8_t i = 0;

  itoa(val, BCD_text, 10);
  while(BCD_text[i]) {
    result[i] = BCD_text[i]-'0';
    i++;
  }
  return i;
}

/*
 * Convert from a uint16 to BCD
 * Use like:
 *      uint8_t[5] values; itobcd(ui16num, values);
 *      values will be an array with BCD digits
 *      returned value will be how many digits used
 */
uint8_t itobcd16(uint16_t val, uint8_t *result) {
  char BCD_text[6]; // max 10 digits on uint32
  uint8_t i = 0;

  itoa(val, BCD_text, 10);
  while(BCD_text[i]) {
    result[i] = BCD_text[i]-'0';
    i++;
  }
  return i;
}

/*
 * Convert from a uint8 to BCD
 * Use like:
 *      uint8_t[3] values; itobcd(ui8num, values);
 *      values will be an array with BCD digits
 *      returned value will be how many digits used
 */
uint8_t itobcd8(uint8_t val, uint8_t *result) {
  char BCD_text[3]; // max 10 digits on uint32
  uint8_t i = 0;

  itoa(val, BCD_text, 10);
  while(BCD_text[i]) {
    result[i] = BCD_text[i]-'0';
    i++;
  }
  return i;
}