Ð˜Ð½Ð´Ð¸ÐºÐ°Ñ†Ð¸Ñ Ð½Ð° двух Ñеми Ñегментных двух знаковых Ñветодиодных индикаторах через SPI
Материал из Automata.
(Различия между версиями)
м (Новая: #include <stdint.h> #include <avr/io.h> #include <avr/interrupt.h> //MISO - RCK; #define LATCH 0x40 - →0 - /...) |
(Отмена правки № 1786 участника Kern (обсуждение)) |
||
(4 промежуточные версии не показаны) | |||
Строка 1: | Строка 1: | ||
- | #include <stdint.h> | + | #include <stdint.h> |
- | #include <avr/io.h> | + | #include <avr/io.h> |
- | #include <avr/interrupt.h> | + | #include <avr/interrupt.h> |
- | //MISO - RCK; | + | //MISO - RCK; |
- | #define LATCH 0x40 | + | #define LATCH 0x40 |
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | void Indicate(uint16_t data) | + | /*0*/ /*1*/ /*2*/ /*3*/ /*4*/ /*5*/ /*6*/ /*7*/ /*8*/ /*9*/ /*.*/ |
- | { | + | static const uint8_t LEDS[11] = { 0x03, 0x9F, 0x25, 0x0D, 0x99, 0x49, 0x41, 0x1F, 0x01, 0x09, 0xFE }; |
+ | static volatile uint8_t counts[4]; | ||
+ | static volatile uint16_t data = 0; | ||
+ | |||
+ | void Indicate(uint16_t data) | ||
+ | { | ||
counts[3] = data % 10; | counts[3] = data % 10; | ||
data /= 10; | data /= 10; | ||
Строка 20: | Строка 20: | ||
data /= 10; | data /= 10; | ||
counts[0] = data % 10; | counts[0] = data % 10; | ||
- | data /= 10; | + | data /= 10; |
- | + | ||
SPCR |= _BV(SPE); | SPCR |= _BV(SPE); | ||
- | + | ||
register uint8_t j; | register uint8_t j; | ||
for (j = 0; j < 4; j++) | for (j = 0; j < 4; j++) | ||
Строка 30: | Строка 30: | ||
while (!(SPSR & _BV(SPIF))); | while (!(SPSR & _BV(SPIF))); | ||
} | } | ||
- | + | ||
SPCR &= ~_BV(SPE); | SPCR &= ~_BV(SPE); | ||
- | + | ||
PORTB |= LATCH; | PORTB |= LATCH; | ||
PORTB &= ~LATCH; | PORTB &= ~LATCH; | ||
- | } | + | } |
- | ISR (TIMER0_COMP_vect) | + | |
- | { | + | ISR (TIMER0_COMP_vect) |
- | + | { | |
- | + | ||
- | + | ||
- | + | ||
- | + | ||
if (data == 10000) | if (data == 10000) | ||
data = 0; | data = 0; | ||
+ | |||
Indicate(data++); | Indicate(data++); | ||
- | } | + | } |
- | int main (void) | + | int main (void) |
- | { | + | { |
DDRB = 0xff; | DDRB = 0xff; | ||
PORTB = 0x00; | PORTB = 0x00; | ||
- | + | ||
SPCR = _BV(SPE) | _BV(MSTR) | _BV(SPR1); | SPCR = _BV(SPE) | _BV(MSTR) | _BV(SPR1); | ||
- | + | ||
TCCR0 = _BV(WGM01) | _BV(CS02) | _BV(CS00); | TCCR0 = _BV(WGM01) | _BV(CS02) | _BV(CS00); | ||
- | OCR0 = 78; //78.125; | ||
TIMSK = _BV(OCIE0); | TIMSK = _BV(OCIE0); | ||
- | + | OCR0 = 78; | |
+ | //78'''.125''' t = 0.01 s * 8/1024 MHz; | ||
+ | |||
+ | |||
sei(); | sei(); | ||
- | + | ||
while (1); | while (1); | ||
- | } | + | } |
Текущая версия
#include <stdint.h> #include <avr/io.h> #include <avr/interrupt.h>
//MISO - RCK; #define LATCH 0x40
/*0*/ /*1*/ /*2*/ /*3*/ /*4*/ /*5*/ /*6*/ /*7*/ /*8*/ /*9*/ /*.*/ static const uint8_t LEDS[11] = { 0x03, 0x9F, 0x25, 0x0D, 0x99, 0x49, 0x41, 0x1F, 0x01, 0x09, 0xFE }; static volatile uint8_t counts[4]; static volatile uint16_t data = 0;
void Indicate(uint16_t data) { counts[3] = data % 10; data /= 10; counts[2] = data % 10; data /= 10; counts[1] = data % 10; data /= 10; counts[0] = data % 10; data /= 10; SPCR |= _BV(SPE); register uint8_t j; for (j = 0; j < 4; j++) { SPDR = ( j == 1 ) ? ( LEDS[counts[j]] & LEDS[10] ) : ( LEDS[counts[j]] ); while (!(SPSR & _BV(SPIF))); } SPCR &= ~_BV(SPE); PORTB |= LATCH; PORTB &= ~LATCH; }
ISR (TIMER0_COMP_vect) { if (data == 10000) data = 0; Indicate(data++); }
int main (void) { DDRB = 0xff; PORTB = 0x00; SPCR = _BV(SPE) | _BV(MSTR) | _BV(SPR1); TCCR0 = _BV(WGM01) | _BV(CS02) | _BV(CS00); TIMSK = _BV(OCIE0); OCR0 = 78; //78.125 t = 0.01 s * 8/1024 MHz; sei(); while (1); }