<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="zh-cn">
		<id>http://wiki.ywrobot.net/index.php?action=history&amp;feed=atom&amp;title=Wiring.c</id>
		<title>Wiring.c - 版本历史</title>
		<link rel="self" type="application/atom+xml" href="http://wiki.ywrobot.net/index.php?action=history&amp;feed=atom&amp;title=Wiring.c"/>
		<link rel="alternate" type="text/html" href="http://wiki.ywrobot.net/index.php?title=Wiring.c&amp;action=history"/>
		<updated>2026-05-14T11:43:09Z</updated>
		<subtitle>本wiki的该页面的版本历史</subtitle>
		<generator>MediaWiki 1.26.2</generator>

	<entry>
		<id>http://wiki.ywrobot.net/index.php?title=Wiring.c&amp;diff=71&amp;oldid=prev</id>
		<title>YWrobot CYB：创建页面，内容为“&lt;pre style=&quot;color:blue&quot;&gt; /*   wiring.c - Partial implementation of the Wiring API for the ATmega8.   Part of Arduino - http://www.arduino.cc/    Copyright (c) 2005-2...”</title>
		<link rel="alternate" type="text/html" href="http://wiki.ywrobot.net/index.php?title=Wiring.c&amp;diff=71&amp;oldid=prev"/>
				<updated>2016-04-25T02:08:41Z</updated>
		
		<summary type="html">&lt;p&gt;创建页面，内容为“&amp;lt;pre style=&amp;quot;color:blue&amp;quot;&amp;gt; /*   wiring.c - Partial implementation of the Wiring API for the ATmega8.   Part of Arduino - http://www.arduino.cc/    Copyright (c) 2005-2...”&lt;/p&gt;
&lt;p&gt;&lt;b&gt;新页面&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&amp;lt;pre style=&amp;quot;color:blue&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
  wiring.c - Partial implementation of the Wiring API for the ATmega8.&lt;br /&gt;
  Part of Arduino - http://www.arduino.cc/&lt;br /&gt;
&lt;br /&gt;
  Copyright (c) 2005-2006 David A. Mellis&lt;br /&gt;
&lt;br /&gt;
  This library is free software; you can redistribute it and/or&lt;br /&gt;
  modify it under the terms of the GNU Lesser General Public&lt;br /&gt;
  License as published by the Free Software Foundation; either&lt;br /&gt;
  version 2.1 of the License, or (at your option) any later version.&lt;br /&gt;
&lt;br /&gt;
  This library is distributed in the hope that it will be useful,&lt;br /&gt;
  but WITHOUT ANY WARRANTY; without even the implied warranty of&lt;br /&gt;
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU&lt;br /&gt;
  Lesser General Public License for more details.&lt;br /&gt;
&lt;br /&gt;
  You should have received a copy of the GNU Lesser General&lt;br /&gt;
  Public License along with this library; if not, write to the&lt;br /&gt;
  Free Software Foundation, Inc., 59 Temple Place, Suite 330,&lt;br /&gt;
  Boston, MA  02111-1307  USA&lt;br /&gt;
&lt;br /&gt;
  $Id$&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
#include &amp;quot;wiring_private.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
// the prescaler is set so that timer0 ticks every 64 clock cycles, and the&lt;br /&gt;
// the overflow handler is called every 256 ticks.&lt;br /&gt;
#define MICROSECONDS_PER_TIMER0_OVERFLOW (clockCyclesToMicroseconds(64 * 256))&lt;br /&gt;
&lt;br /&gt;
// the whole number of milliseconds per timer0 overflow&lt;br /&gt;
#define MILLIS_INC (MICROSECONDS_PER_TIMER0_OVERFLOW / 1000)&lt;br /&gt;
&lt;br /&gt;
// the fractional number of milliseconds per timer0 overflow. we shift right&lt;br /&gt;
// by three to fit these numbers into a byte. (for the clock speeds we care&lt;br /&gt;
// about - 8 and 16 MHz - this doesn't lose precision.)&lt;br /&gt;
#define FRACT_INC ((MICROSECONDS_PER_TIMER0_OVERFLOW % 1000) &amp;gt;&amp;gt; 3)&lt;br /&gt;
#define FRACT_MAX (1000 &amp;gt;&amp;gt; 3)&lt;br /&gt;
&lt;br /&gt;
volatile unsigned long timer0_overflow_count = 0;&lt;br /&gt;
volatile unsigned long timer0_millis = 0;&lt;br /&gt;
static unsigned char timer0_fract = 0;&lt;br /&gt;
&lt;br /&gt;
#if defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)&lt;br /&gt;
ISR(TIM0_OVF_vect)&lt;br /&gt;
#else&lt;br /&gt;
ISR(TIMER0_OVF_vect)&lt;br /&gt;
#endif&lt;br /&gt;
{&lt;br /&gt;
	// copy these to local variables so they can be stored in registers&lt;br /&gt;
	// (volatile variables must be read from memory on every access)&lt;br /&gt;
	unsigned long m = timer0_millis;&lt;br /&gt;
	unsigned char f = timer0_fract;&lt;br /&gt;
&lt;br /&gt;
	m += MILLIS_INC;&lt;br /&gt;
	f += FRACT_INC;&lt;br /&gt;
	if (f &amp;gt;= FRACT_MAX) {&lt;br /&gt;
		f -= FRACT_MAX;&lt;br /&gt;
		m += 1;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	timer0_fract = f;&lt;br /&gt;
	timer0_millis = m;&lt;br /&gt;
	timer0_overflow_count++;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
unsigned long millis()&lt;br /&gt;
{&lt;br /&gt;
	unsigned long m;&lt;br /&gt;
	uint8_t oldSREG = SREG;&lt;br /&gt;
&lt;br /&gt;
	// disable interrupts while we read timer0_millis or we might get an&lt;br /&gt;
	// inconsistent value (e.g. in the middle of a write to timer0_millis)&lt;br /&gt;
	cli();&lt;br /&gt;
	m = timer0_millis;&lt;br /&gt;
	SREG = oldSREG;&lt;br /&gt;
&lt;br /&gt;
	return m;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
unsigned long micros() {&lt;br /&gt;
	unsigned long m;&lt;br /&gt;
	uint8_t oldSREG = SREG, t;&lt;br /&gt;
	&lt;br /&gt;
	cli();&lt;br /&gt;
	m = timer0_overflow_count;&lt;br /&gt;
#if defined(TCNT0)&lt;br /&gt;
	t = TCNT0;&lt;br /&gt;
#elif defined(TCNT0L)&lt;br /&gt;
	t = TCNT0L;&lt;br /&gt;
#else&lt;br /&gt;
	#error TIMER 0 not defined&lt;br /&gt;
#endif&lt;br /&gt;
&lt;br /&gt;
  &lt;br /&gt;
#ifdef TIFR0&lt;br /&gt;
	if ((TIFR0 &amp;amp; _BV(TOV0)) &amp;amp;&amp;amp; (t &amp;lt; 255))&lt;br /&gt;
		m++;&lt;br /&gt;
#else&lt;br /&gt;
	if ((TIFR &amp;amp; _BV(TOV0)) &amp;amp;&amp;amp; (t &amp;lt; 255))&lt;br /&gt;
		m++;&lt;br /&gt;
#endif&lt;br /&gt;
&lt;br /&gt;
	SREG = oldSREG;&lt;br /&gt;
	&lt;br /&gt;
	return ((m &amp;lt;&amp;lt; 8) + t) * (64 / clockCyclesPerMicrosecond());&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void delay(unsigned long ms)&lt;br /&gt;
{&lt;br /&gt;
	uint16_t start = (uint16_t)micros();&lt;br /&gt;
&lt;br /&gt;
	while (ms &amp;gt; 0) {&lt;br /&gt;
		if (((uint16_t)micros() - start) &amp;gt;= 1000) {&lt;br /&gt;
			ms--;&lt;br /&gt;
			start += 1000;&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/* Delay for the given number of microseconds.  Assumes a 8 or 16 MHz clock. */&lt;br /&gt;
void delayMicroseconds(unsigned int us)&lt;br /&gt;
{&lt;br /&gt;
	// calling avrlib's delay_us() function with low values (e.g. 1 or&lt;br /&gt;
	// 2 microseconds) gives delays longer than desired.&lt;br /&gt;
	//delay_us(us);&lt;br /&gt;
#if F_CPU &amp;gt;= 20000000L&lt;br /&gt;
	// for the 20 MHz clock on rare Arduino boards&lt;br /&gt;
&lt;br /&gt;
	// for a one-microsecond delay, simply wait 2 cycle and return. The overhead&lt;br /&gt;
	// of the function call yields a delay of exactly a one microsecond.&lt;br /&gt;
	__asm__ __volatile__ (&lt;br /&gt;
		&amp;quot;nop&amp;quot; &amp;quot;\n\t&amp;quot;&lt;br /&gt;
		&amp;quot;nop&amp;quot;); //just waiting 2 cycle&lt;br /&gt;
	if (--us == 0)&lt;br /&gt;
		return;&lt;br /&gt;
&lt;br /&gt;
	// the following loop takes a 1/5 of a microsecond (4 cycles)&lt;br /&gt;
	// per iteration, so execute it five times for each microsecond of&lt;br /&gt;
	// delay requested.&lt;br /&gt;
	us = (us&amp;lt;&amp;lt;2) + us; // x5 us&lt;br /&gt;
&lt;br /&gt;
	// account for the time taken in the preceeding commands.&lt;br /&gt;
	us -= 2;&lt;br /&gt;
&lt;br /&gt;
#elif F_CPU &amp;gt;= 16000000L&lt;br /&gt;
	// for the 16 MHz clock on most Arduino boards&lt;br /&gt;
&lt;br /&gt;
	// for a one-microsecond delay, simply return.  the overhead&lt;br /&gt;
	// of the function call yields a delay of approximately 1 1/8 us.&lt;br /&gt;
	if (--us == 0)&lt;br /&gt;
		return;&lt;br /&gt;
&lt;br /&gt;
	// the following loop takes a quarter of a microsecond (4 cycles)&lt;br /&gt;
	// per iteration, so execute it four times for each microsecond of&lt;br /&gt;
	// delay requested.&lt;br /&gt;
	us &amp;lt;&amp;lt;= 2;&lt;br /&gt;
&lt;br /&gt;
	// account for the time taken in the preceeding commands.&lt;br /&gt;
	us -= 2;&lt;br /&gt;
#else&lt;br /&gt;
	// for the 8 MHz internal clock on the ATmega168&lt;br /&gt;
&lt;br /&gt;
	// for a one- or two-microsecond delay, simply return.  the overhead of&lt;br /&gt;
	// the function calls takes more than two microseconds.  can't just&lt;br /&gt;
	// subtract two, since us is unsigned; we'd overflow.&lt;br /&gt;
	if (--us == 0)&lt;br /&gt;
		return;&lt;br /&gt;
	if (--us == 0)&lt;br /&gt;
		return;&lt;br /&gt;
&lt;br /&gt;
	// the following loop takes half of a microsecond (4 cycles)&lt;br /&gt;
	// per iteration, so execute it twice for each microsecond of&lt;br /&gt;
	// delay requested.&lt;br /&gt;
	us &amp;lt;&amp;lt;= 1;&lt;br /&gt;
    &lt;br /&gt;
	// partially compensate for the time taken by the preceeding commands.&lt;br /&gt;
	// we can't subtract any more than this or we'd overflow w/ small delays.&lt;br /&gt;
	us--;&lt;br /&gt;
#endif&lt;br /&gt;
&lt;br /&gt;
	// busy wait&lt;br /&gt;
	__asm__ __volatile__ (&lt;br /&gt;
		&amp;quot;1: sbiw %0,1&amp;quot; &amp;quot;\n\t&amp;quot; // 2 cycles&lt;br /&gt;
		&amp;quot;brne 1b&amp;quot; : &amp;quot;=w&amp;quot; (us) : &amp;quot;0&amp;quot; (us) // 2 cycles&lt;br /&gt;
	);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void init()&lt;br /&gt;
{&lt;br /&gt;
	// this needs to be called before setup() or some functions won't&lt;br /&gt;
	// work there&lt;br /&gt;
	sei();&lt;br /&gt;
	&lt;br /&gt;
	// on the ATmega168, timer 0 is also used for fast hardware pwm&lt;br /&gt;
	// (using phase-correct PWM would mean that timer 0 overflowed half as often&lt;br /&gt;
	// resulting in different millis() behavior on the ATmega8 and ATmega168)&lt;br /&gt;
#if defined(TCCR0A) &amp;amp;&amp;amp; defined(WGM01)&lt;br /&gt;
	sbi(TCCR0A, WGM01);&lt;br /&gt;
	sbi(TCCR0A, WGM00);&lt;br /&gt;
#endif  &lt;br /&gt;
&lt;br /&gt;
	// set timer 0 prescale factor to 64&lt;br /&gt;
#if defined(__AVR_ATmega128__)&lt;br /&gt;
	// CPU specific: different values for the ATmega128&lt;br /&gt;
	sbi(TCCR0, CS02);&lt;br /&gt;
#elif defined(TCCR0) &amp;amp;&amp;amp; defined(CS01) &amp;amp;&amp;amp; defined(CS00)&lt;br /&gt;
	// this combination is for the standard atmega8&lt;br /&gt;
	sbi(TCCR0, CS01);&lt;br /&gt;
	sbi(TCCR0, CS00);&lt;br /&gt;
#elif defined(TCCR0B) &amp;amp;&amp;amp; defined(CS01) &amp;amp;&amp;amp; defined(CS00)&lt;br /&gt;
	// this combination is for the standard 168/328/1280/2560&lt;br /&gt;
	sbi(TCCR0B, CS01);&lt;br /&gt;
	sbi(TCCR0B, CS00);&lt;br /&gt;
#elif defined(TCCR0A) &amp;amp;&amp;amp; defined(CS01) &amp;amp;&amp;amp; defined(CS00)&lt;br /&gt;
	// this combination is for the __AVR_ATmega645__ series&lt;br /&gt;
	sbi(TCCR0A, CS01);&lt;br /&gt;
	sbi(TCCR0A, CS00);&lt;br /&gt;
#else&lt;br /&gt;
	#error Timer 0 prescale factor 64 not set correctly&lt;br /&gt;
#endif&lt;br /&gt;
&lt;br /&gt;
	// enable timer 0 overflow interrupt&lt;br /&gt;
#if defined(TIMSK) &amp;amp;&amp;amp; defined(TOIE0)&lt;br /&gt;
	sbi(TIMSK, TOIE0);&lt;br /&gt;
#elif defined(TIMSK0) &amp;amp;&amp;amp; defined(TOIE0)&lt;br /&gt;
	sbi(TIMSK0, TOIE0);&lt;br /&gt;
#else&lt;br /&gt;
	#error	Timer 0 overflow interrupt not set correctly&lt;br /&gt;
#endif&lt;br /&gt;
&lt;br /&gt;
	// timers 1 and 2 are used for phase-correct hardware pwm&lt;br /&gt;
	// this is better for motors as it ensures an even waveform&lt;br /&gt;
	// note, however, that fast pwm mode can achieve a frequency of up&lt;br /&gt;
	// 8 MHz (with a 16 MHz clock) at 50% duty cycle&lt;br /&gt;
&lt;br /&gt;
#if defined(TCCR1B) &amp;amp;&amp;amp; defined(CS11) &amp;amp;&amp;amp; defined(CS10)&lt;br /&gt;
	TCCR1B = 0;&lt;br /&gt;
&lt;br /&gt;
	// set timer 1 prescale factor to 64&lt;br /&gt;
	sbi(TCCR1B, CS11);&lt;br /&gt;
#if F_CPU &amp;gt;= 8000000L&lt;br /&gt;
	sbi(TCCR1B, CS10);&lt;br /&gt;
#endif&lt;br /&gt;
#elif defined(TCCR1) &amp;amp;&amp;amp; defined(CS11) &amp;amp;&amp;amp; defined(CS10)&lt;br /&gt;
	sbi(TCCR1, CS11);&lt;br /&gt;
#if F_CPU &amp;gt;= 8000000L&lt;br /&gt;
	sbi(TCCR1, CS10);&lt;br /&gt;
#endif&lt;br /&gt;
#endif&lt;br /&gt;
	// put timer 1 in 8-bit phase correct pwm mode&lt;br /&gt;
#if defined(TCCR1A) &amp;amp;&amp;amp; defined(WGM10)&lt;br /&gt;
	sbi(TCCR1A, WGM10);&lt;br /&gt;
#elif defined(TCCR1)&lt;br /&gt;
	#warning this needs to be finished&lt;br /&gt;
#endif&lt;br /&gt;
&lt;br /&gt;
	// set timer 2 prescale factor to 64&lt;br /&gt;
#if defined(TCCR2) &amp;amp;&amp;amp; defined(CS22)&lt;br /&gt;
	sbi(TCCR2, CS22);&lt;br /&gt;
#elif defined(TCCR2B) &amp;amp;&amp;amp; defined(CS22)&lt;br /&gt;
	sbi(TCCR2B, CS22);&lt;br /&gt;
#else&lt;br /&gt;
	#warning Timer 2 not finished (may not be present on this CPU)&lt;br /&gt;
#endif&lt;br /&gt;
&lt;br /&gt;
	// configure timer 2 for phase correct pwm (8-bit)&lt;br /&gt;
#if defined(TCCR2) &amp;amp;&amp;amp; defined(WGM20)&lt;br /&gt;
	sbi(TCCR2, WGM20);&lt;br /&gt;
#elif defined(TCCR2A) &amp;amp;&amp;amp; defined(WGM20)&lt;br /&gt;
	sbi(TCCR2A, WGM20);&lt;br /&gt;
#else&lt;br /&gt;
	#warning Timer 2 not finished (may not be present on this CPU)&lt;br /&gt;
#endif&lt;br /&gt;
&lt;br /&gt;
#if defined(TCCR3B) &amp;amp;&amp;amp; defined(CS31) &amp;amp;&amp;amp; defined(WGM30)&lt;br /&gt;
	sbi(TCCR3B, CS31);		// set timer 3 prescale factor to 64&lt;br /&gt;
	sbi(TCCR3B, CS30);&lt;br /&gt;
	sbi(TCCR3A, WGM30);		// put timer 3 in 8-bit phase correct pwm mode&lt;br /&gt;
#endif&lt;br /&gt;
&lt;br /&gt;
#if defined(TCCR4A) &amp;amp;&amp;amp; defined(TCCR4B) &amp;amp;&amp;amp; defined(TCCR4D) /* beginning of timer4 block for 32U4 and similar */&lt;br /&gt;
	sbi(TCCR4B, CS42);		// set timer4 prescale factor to 64&lt;br /&gt;
	sbi(TCCR4B, CS41);&lt;br /&gt;
	sbi(TCCR4B, CS40);&lt;br /&gt;
	sbi(TCCR4D, WGM40);		// put timer 4 in phase- and frequency-correct PWM mode	&lt;br /&gt;
	sbi(TCCR4A, PWM4A);		// enable PWM mode for comparator OCR4A&lt;br /&gt;
	sbi(TCCR4C, PWM4D);		// enable PWM mode for comparator OCR4D&lt;br /&gt;
#else /* beginning of timer4 block for ATMEGA1280 and ATMEGA2560 */&lt;br /&gt;
#if defined(TCCR4B) &amp;amp;&amp;amp; defined(CS41) &amp;amp;&amp;amp; defined(WGM40)&lt;br /&gt;
	sbi(TCCR4B, CS41);		// set timer 4 prescale factor to 64&lt;br /&gt;
	sbi(TCCR4B, CS40);&lt;br /&gt;
	sbi(TCCR4A, WGM40);		// put timer 4 in 8-bit phase correct pwm mode&lt;br /&gt;
#endif&lt;br /&gt;
#endif /* end timer4 block for ATMEGA1280/2560 and similar */	&lt;br /&gt;
&lt;br /&gt;
#if defined(TCCR5B) &amp;amp;&amp;amp; defined(CS51) &amp;amp;&amp;amp; defined(WGM50)&lt;br /&gt;
	sbi(TCCR5B, CS51);		// set timer 5 prescale factor to 64&lt;br /&gt;
	sbi(TCCR5B, CS50);&lt;br /&gt;
	sbi(TCCR5A, WGM50);		// put timer 5 in 8-bit phase correct pwm mode&lt;br /&gt;
#endif&lt;br /&gt;
&lt;br /&gt;
#if defined(ADCSRA)&lt;br /&gt;
	// set a2d prescale factor to 128&lt;br /&gt;
	// 16 MHz / 128 = 125 KHz, inside the desired 50-200 KHz range.&lt;br /&gt;
	// XXX: this will not work properly for other clock speeds, and&lt;br /&gt;
	// this code should use F_CPU to determine the prescale factor.&lt;br /&gt;
	sbi(ADCSRA, ADPS2);&lt;br /&gt;
	sbi(ADCSRA, ADPS1);&lt;br /&gt;
	sbi(ADCSRA, ADPS0);&lt;br /&gt;
&lt;br /&gt;
	// enable a2d conversions&lt;br /&gt;
	sbi(ADCSRA, ADEN);&lt;br /&gt;
#endif&lt;br /&gt;
&lt;br /&gt;
	// the bootloader connects pins 0 and 1 to the USART; disconnect them&lt;br /&gt;
	// here so they can be used as normal digital i/o; they will be&lt;br /&gt;
	// reconnected in Serial.begin()&lt;br /&gt;
#if defined(UCSRB)&lt;br /&gt;
	UCSRB = 0;&lt;br /&gt;
#elif defined(UCSR0B)&lt;br /&gt;
	UCSR0B = 0;&lt;br /&gt;
#endif&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>YWrobot CYB</name></author>	</entry>

	</feed>