⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 utility.c

📁 avr chargeer source code
💻 C
字号:
/*-----------------------------------------------------------------
  NiMH_Charger Copyright (c) 2007 by Jack Botner

  utility.c
-----------------------------------------------------------------*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <string.h>
#include <stdlib.h>
#include "common.h"
#include "lcd.h"

// Global Variables

extern uint8_t ucFlags;					// program flags
extern uint16_t uiBtnState;				// used for button debouncing

/*---------------------------------------------------------------------
  sampleButton()
  
  Samples PB1 (Zero Button). Is called from the timer interrupt
  every 5 mS.

  The Zero button is referenced to Vcc and goes low when the
  button is pressed.
---------------------------------------------------------------------*/
void sampleButton()
{      
  // Test PB1 for Zero button state
  // The method for debouncing the switch by Jack Ganssle
  // http://www.embedded.com/showArticle.jhtml?articleID=22100235
  uiBtnState = ( uiBtnState << 1 ) | !test_bit( PINB, PB1 ) | 0xe000;
  if ( uiBtnState == 0xf000 )
    set_bit( ucFlags, ZERO_BUTTON_PRESS );
}

/*---------------------------------------------------------------------
  adc_to_mv(...)

  Converts an ADC output to MV, assuming that Aref=AVcc (5.0V)
  [Aref = 1.1V].
---------------------------------------------------------------------*/
uint16_t adc_to_mv( uint16_t uiADC, uint16_t uiVref )
{
  uint16_t	uiResult;

  uiResult = (uint16_t) ( ( (uint32_t) uiADC * (uint32_t) uiVref ) / 1024L );

  return uiResult;
}

/*---------------------------------------------------------------------
  mv_to_temp(...)

  Converts MV to temperature for the TC1047A. The transfer function
  can be found in the device specifications. Outputs a signed
  integer which could be negative.

  Returns: Tenths of degrees Celsius
---------------------------------------------------------------------*/
int16_t mv_to_temp( uint16_t uiMV )
{
  int16_t	iResult;

  //iResult = ( (int16_t) uiMV - 500 ) / 10;	// to return degrees
  iResult = (int16_t) uiMV - 500;				// to return tenths

  return iResult;
}

/*---------------------------------------------------------------------
  formatTemp(...)

  Input: temperature in tenths of degrees as signed integer

  Output: temperature formatted into ascii eg. "40.5";
  		  prepends a minus sign if negative.

  Returns: number of bytes in the output buffer
---------------------------------------------------------------------*/
int16_t formatTemp( int16_t iTemp, int8_t *pcBuf )
{
  int16_t	iN1, iN2;

  itoa( iTemp, pcBuf, 10 );
  iN1 = strlen( pcBuf );

  iN2 = iN1 - 1;
  pcBuf[iN1] = pcBuf[iN2];	// move tenths digit right
  pcBuf[iN2] = '.';			// insert the decimal
  iN2 = iN1 + 1;
  pcBuf[iN2] = '\0';		// move the null right

  return iN2;
}

/*---------------------------------------------------------------------
  beep(...)
---------------------------------------------------------------------*/
void beep( uint16_t uiMs )
{
  clear_bit( PORTB, PB2 );		// turn the beeper on
  delay_ms( uiMs );				// waste time
  set_bit( PORTB, PB2 );	    // turn the beeper off
}

/*---------------------------------------------------------------------
  delay_ms(...)

  Waste time delay loop based on clock running at 4 mHz.
---------------------------------------------------------------------*/
void delay_ms( uint16_t uiMs )
{
  uint16_t	ii;

  for (ii=0; ii<uiMs; ++ii )
  	_delay_loop_2( 1000 );		// waste 1 ms
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -