Snippets
Created by
George T.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | #include <avr/IO.h>
#include <util/delay.h>
#include <avr/interrupt.h>
char com;
char oct;
unsigned int TM = 0;
unsigned long AN = 0;
unsigned int last = 0;
unsigned char cht = 0;
ISR(TIMER1_CAPT_vect)
{
unsigned int time;
time = TCNT1;
cht++;
if (cht%2 != 0)
last = time;
else
TM = time - last;
}
void lcd(unsigned int p)
{
_delay_ms(1);
if (com == 1)
PORTB &= ~_BV(PB0);
else
PORTB |= _BV(PB0);
unsigned int PH,PL;
PH = p / 16;
PL = p % 16;
PORTB |= _BV(PB2);
PORTD = PH;
_delay_ms(1);
PORTB &= ~_BV(PB2);
if (oct == 1)
{
_delay_ms(1);
PORTB |= _BV(PB2);
PORTD = PL;
_delay_ms(1);
PORTB &= ~_BV(PB2);
}
_delay_ms(1);
}
void lcd_init(void)
{
com = 1;
oct = 0;
lcd(0x30); _delay_ms(5);
lcd(0x30); _delay_ms(5);
lcd(0x30); _delay_ms(5);
lcd(0x28); _delay_ms(5);
oct = 1;
lcd(0x28); _delay_ms(5);
lcd(0x08); _delay_ms(5);
lcd(0x01); _delay_ms(5);
lcd(0x06); _delay_ms(5);
lcd(0x0C); _delay_ms(5);
}
void lcd_clear(void)
{
com = 1;
lcd(0x01); _delay_ms(2);
lcd(0x06);
}
void lcd_text(char *str)
{
com = 0;
unsigned int len = strlen(str);
for (int i=0; i<12; i++)
lcd(str[i]);
}
void lcd_num(unsigned long int a)
{
unsigned long int b = 0, c = a;
while (a > 0)
{
b = b*10 + (a%10);
a /= 10;
}
com = 0;
while (b > 0)
{
lcd( b%10+48 );
b = b / 10;
}
while (c%10 == 0)
{
c /= 10;
lcd('0');
}
}
void main(void)
{
DDRB = 0x05;
DDRD = 0x0F;
PORTB = 0xFF;
PORTD = 0xFF;
SREG |= 0x80;
TIMSK |= 0x08;
TCCR1B = 0xC1;
lcd_init();
lcd_text("I am ready!");
while (bit_is_set(PINB,PB4));
while(1)
{
while (TM == 0);
unsigned int time = TM;
unsigned long K = 3e7;
AN = K / TM;
if (2*(K%time) >= time) AN++;
lcd_clear();
lcd_num(AN);
_delay_ms(500);
if (bit_is_clear(PINB,PB4))
{
com = 0;
lcd('+');
_delay_ms(5);
while (bit_is_clear(PINB,PB4));
_delay_ms(5);
while (bit_is_set(PINB,PB4));
lcd_clear();
}
}
}
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.