Zahl | grüne LED | rote LED
---------------------------
1 aus aus
2 aus ein
3 blinkt aus
4 blinkt ein
5 ein aus
6 ein ein
/*******************************************************
CodeVisionAVR V3.10 Evaluation
http://www.hpinfotech.com
Chip type : ATtiny13
AVR Core Clock frequency: 1,200000 MHz
Memory model : Tiny
External RAM size : 0
Data Stack size : 16
*******************************************************/
#include <tiny13.h>
#include <delay.h>
#define P_LED_GREEN PORTB.1
#define DP_LED_GREEN DDRB.1
#define P_LED_RED PORTB.3
#define DP_LED_RED DDRB.3
#define KEY_S1 PINB.0
#define KEY_S2 PINB.2
#define LED_OFF 0
#define LED_ON 1
#define LED_BLINK 2
volatile unsigned char cubeValue = 1;
volatile unsigned char ledGreen;
volatile unsigned char ledRed;
volatile unsigned char blinkTime;
// Timer 0 output compare A interrupt service routine
interrupt [TIM0_COMPA] void timer0_compa_isr(void)
{
static unsigned char blinkCnt = 0;
static unsigned char blinkState = 0;
cubeValue++;
if(cubeValue > 6)
{
cubeValue = 1;
}
if(blinkCnt == 0)
{
blinkState ^= 0x01;
blinkCnt = blinkTime;
}
else
{
blinkCnt--;
}
if(ledGreen == LED_OFF)
{
P_LED_GREEN = 0;
}
else if(ledGreen == LED_ON)
{
P_LED_GREEN = 1;
}
else if(ledGreen == LED_BLINK)
{
P_LED_GREEN = blinkState;
}
if(ledRed == LED_OFF)
{
P_LED_RED = 0;
}
else if(ledRed == LED_ON)
{
P_LED_RED = 1;
}
else if(ledRed == LED_BLINK)
{
P_LED_RED = blinkState;
}
}
void initTimer(void)
{
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 150,000 kHz
// Mode: CTC top=OCR0A
// OC0A output: Disconnected
// OC0B output: Disconnected
// Timer Period: 1 ms
TCCR0A=(0<<COM0A1) | (0<<COM0A0) | (0<<COM0B1) | (0<<COM0B0) | (1<<WGM01) | (0<<WGM00);
TCCR0B=(0<<WGM02) | (0<<CS02) | (1<<CS01) | (0<<CS00);
TCNT0=0x00;
OCR0A=0x95;
OCR0B=0x00;
// Timer/Counter 0 Interrupt(s) initialization
TIMSK0=(0<<OCIE0B) | (1<<OCIE0A) | (0<<TOIE0);
}
void main(void)
{
unsigned char count;
// Crystal Oscillator division factor: 8
#pragma optsize-
CLKPR=(1<<CLKPCE);
CLKPR=(0<<CLKPCE) | (0<<CLKPS3) | (0<<CLKPS2) | (1<<CLKPS1) | (1<<CLKPS0);
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif
// Analog Comparator initialization
// Analog Comparator: Off
// The Analog Comparator's positive input is
// connected to the AIN0 pin
// The Analog Comparator's negative input is
// connected to the AIN1 pin
ACSR=(1<<ACD) | (0<<ACBG) | (0<<ACO) | (0<<ACI) | (0<<ACIE) | (0<<ACIS1) | (0<<ACIS0);
ADCSRB=(0<<ACME);
initTimer();
DP_LED_GREEN = 1;
DP_LED_RED = 1;
// Global enable interrupts
#asm("sei")
while (1)
{
while(KEY_S1 != 0);
delay_ms(20);
blinkTime = 70;
ledRed = LED_BLINK;
ledGreen = LED_BLINK;
while(KEY_S1 == 0);
delay_ms(500);
count = cubeValue;
blinkTime = 250;
switch(count)
{
case 1:
ledGreen = LED_OFF;
ledRed = LED_OFF;
break;
case 2:
ledGreen = LED_OFF;
ledRed = LED_ON;
break;
case 3:
ledGreen = LED_BLINK;
ledRed = LED_OFF;
break;
case 4:
ledGreen = LED_BLINK;
ledRed = LED_ON;
break;
case 5:
ledGreen = LED_ON;
ledRed = LED_OFF;
break;
case 6:
ledGreen = LED_ON;
ledRed = LED_ON;
break;
}
}
}