led.c

来自「AVR单片机的C语言源程序,包括按键扫描EEPROM通讯LCD显示数码管显示等等」· C语言 代码 · 共 78 行

C
78
字号
/* Moving LED example

   CodeVisionAVR C Compiler
   (C) 2000-2002 HP InfoTech S.R.L.
   www.hpinfotech.ro

   Chip: AT90S8515
   Memory Model: SMALL
   Data Stack Size: 128 bytes

   8 LEDs are connected between the PORTC
   outputs and +5V using 1K current
   limiting resistors
   The LEDs anodes are connected to +5V
   
   On the STK500 it's only necessary to
   connect the PORTC and LEDS headers
   together with a 10-wire cable
*/

// I/O register definitions for AT90S8515
#include <90s8515.h>

// quartz crystal frquency [Hz]
#define xtal 3686400
// moving LED frequency [Hz]
#define fmove 2

// the LED on PORTC output 0 will be on
unsigned char led_status=0xfe;

// TIMER1 overflow interrupt service routine
// occurs every 0.5 seconds

interrupt [TIM1_OVF] void timer1_overflow(void)
{
// preset again TIMER1
TCNT1=0x10000-(xtal/1024/fmove);
// move the LED
led_status<<=1;
led_status|=1;
if (led_status==0xff) led_status=0xfe;
// turn on the LED
PORTC=led_status;
}

void main(void)
{
// set the I/O ports
// all PORTC pins are outputs
DDRC=0xff;
// turn on the first LED
PORTC=led_status;

// init TIMER1
// TIMER1 is disconnected from pin OC1
// no PWM
TCCR1A=0;
// TIMER1 clock is xtal/1024
TCCR1B=5;
// preset TIMER1
TCNT1=0x10000-(xtal/1024/fmove);
// clear TIMER1 interrupts flags
TIFR=0;
// enable TIMER1 overflow interrupt
TIMSK=0x80;
// all other interrupt sources are disabled
GIMSK=0;

// global enable interrupts
#asm
    sei
#endasm

// the rest is done by TIMER1 overflow interrupts
while (1);
}

⌨️ 快捷键说明

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