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; | |
- | + | while (!(UCSRA & _BV(TXC))); | |
- | + | UCSRA |= _BV(TXC); | |
} | } | ||
Строка 34: | Строка 32: | ||
inline void led_trig (void) | inline void led_trig (void) | ||
{ | { | ||
- | + | PORTB ^= LED_PIN; | |
} | } | ||
Строка 44: | Строка 42: | ||
ISR (USART_RX_vect) | ISR (USART_RX_vect) | ||
{ | { | ||
- | + | OCR1A = ONE_SEC/UDR; | |
- | + | snd_byte('O'); | |
- | + | snd_byte('K'); | |
} | } | ||
int main () | 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; | |
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
} | } |
Текущая версия
#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; }