Led + Button + Timer + UART

Материал из Automata.

(Различия между версиями)
Перейти к: навигация, поиск
(Новая: #include <stdint.h> #include <avr/io.h> #include <avr/interrupt.h> #include <util/delay.h> #define BUT_PIN 0x10 #define LED_PIN 0x01 #define TST_MES 0xA5 #define ONE_SEC 31250 ...)
м
 
Строка 11: Строка 11:
const char msg[] = "set Hz freq";
const char msg[] = "set Hz freq";
- 
char led_stat;
char led_stat;
-
char led_freq;
 
void snd_byte (uint8_t byte)
void snd_byte (uint8_t byte)
{
{
-
UDR = byte;
+
UDR = byte;
-
while (!(UCSRA & _BV(TXC)));
+
while (!(UCSRA & _BV(TXC)));
-
UCSRA |= _BV(TXC);
+
UCSRA |= _BV(TXC);
}
}
Строка 34: Строка 32:
inline void led_trig (void)
inline void led_trig (void)
{
{
-
PORTB ^= LED_PIN;
+
PORTB ^= LED_PIN;
}
}
Строка 44: Строка 42:
ISR (USART_RX_vect)
ISR (USART_RX_vect)
{
{
-
OCR1A = ONE_SEC/UDR;
+
OCR1A = ONE_SEC/UDR;
-
snd_byte('O');
+
snd_byte('O');
-
snd_byte('K');
+
snd_byte('K');
}
}
int main ()
int main ()
{
{
-
//pin 0 - led act GND, pin 4 - button act GND
+
//pin 0 - led act GND, pin 4 - button act GND
-
DDRB = 0x01;
+
DDRB = 0x01;
-
PORTB = 0x10;
+
PORTB = 0x10;
-
//CTC, presc 256
+
//CTC, presc 256
-
TCCR1B = _BV(WGM12) | _BV(CS12);
+
TCCR1B = _BV(WGM12) | _BV(CS12);
-
TIMSK = _BV(OCIE1A);
+
TIMSK = _BV(OCIE1A);
-
OCR1A = ONE_SEC;
+
OCR1A = ONE_SEC;
-
//19.2k , 8 db, 1sb, no parity
+
//19.2k , 8 db, 1sb, no parity
-
UBRRH = 0;
+
UBRRH = 0;
-
UBRRL = 25;
+
UBRRL = 25;
-
UCSRB = _BV(RXCIE) | _BV(RXEN) | _BV(TXEN);
+
UCSRB = _BV(RXCIE) | _BV(RXEN) | _BV(TXEN);
-
UCSRC = _BV(URSEL) | _BV(USBS) | _BV(UCSZ0) | _BV(UCSZ1);
+
UCSRC = _BV(URSEL) | _BV(USBS) | _BV(UCSZ0) | _BV(UCSZ1);
-
for (int i = 0; i < MSG_LEN; ++i)
+
for (int i = 0; i < MSG_LEN; ++i)
-
snd_byte(msg[i]);
+
snd_byte(msg[i]);
 +
 +
sei();
-
led_freq = 100;
+
while (1) {
-
+
but_test();
-
sei();
+
TIMSK = led_stat ? _BV(OCIE1A) : 0;
-
+
}
-
while (1) {
+
-
but_test();
+
return 0;
-
if (led_stat)
+
-
TIMSK |= _BV(OCIE1A);
+
-
else
+
-
TIMSK &= ~_BV(OCIE1A);
+
-
}
+
-
+
-
return 0;
+
}
}

Текущая версия

#include <stdint.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#define BUT_PIN 0x10
#define LED_PIN 0x01
#define TST_MES 0xA5
#define ONE_SEC 31250
#define MSG_LEN 12
const char msg[] = "set Hz freq";
char led_stat;
void snd_byte (uint8_t byte)
{
    UDR = byte;
    while (!(UCSRA & _BV(TXC)));
    UCSRA |= _BV(TXC);
}

void but_test (void)
{ 
    if (!(PINB & BUT_PIN)) { 
        _delay_ms(5); 
        led_stat ^= 1; 
        while (!(PINB & BUT_PIN)); 
        _delay_ms(5); 
   } 
} 

inline void led_trig (void)
{ 
    PORTB ^= LED_PIN;
}
ISR (TIMER1_COMPA_vect)
{
    led_trig();
}

ISR (USART_RX_vect)
{
    OCR1A = ONE_SEC/UDR; 
    snd_byte('O');
    snd_byte('K');
}
int main ()
{
    //pin 0 - led act GND, pin 4 - button act GND
    DDRB = 0x01;
    PORTB = 0x10;
	
    //CTC, presc 256
    TCCR1B = _BV(WGM12) | _BV(CS12);
    TIMSK  = _BV(OCIE1A);
    OCR1A  = ONE_SEC;

    //19.2k , 8 db, 1sb, no parity
    UBRRH = 0;
    UBRRL = 25;	
    UCSRB = _BV(RXCIE) | _BV(RXEN) | _BV(TXEN);
    UCSRC = _BV(URSEL) | _BV(USBS) | _BV(UCSZ0) | _BV(UCSZ1);	

    for (int i = 0; i < MSG_LEN; ++i)
        snd_byte(msg[i]);

    sei();
	
    while (1) {
        but_test();
	 TIMSK = led_stat ? _BV(OCIE1A) : 0;
    } 

    return 0;
}
Личные инструменты
Микроконтроллеры