To moze podam jako przyklad jakies programy w C bez plusow i mysle, ze bedziesz w stanie wyciagnac wniosek
Odgrywanie dzwieku
#include <avr/pgmspace.h>
#include <inttypes.h>
#define MELODY_SAMPLE(x) (pgm_read_byte(&melody[x]))
PROGMEM const uint8_t melody[] = {
0x00, 0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0xfe, 0x02, 0x00, 0x00, 0x02, 0x00, 0x01, 0x02, 0x01, 0x02, 0x02, 0x01, 0x02, 0x00, 0x01, 0x03, 0x02, 0x02, 0x02, 0x02, 0x03, 0x02, 0x01, 0x03, 0x01, 0x03, 0x02, 0x01, 0x02, 0x01, 0x00, 0xfe, 0x01, 0xff, 0x01, 0xfe, 0x01, 0x01, 0xff, 0x02, 0x02, 0x03, 0x03, 0x02, 0x02, 0x07, 0x08, 0x05, 0xfa, 0x00, 0x00, 0x08, 0x02, 0x06, 0x0a, 0x0a, 0x10, 0x08, 0x10, 0x0e, 0x1a, 0x10, 0x19, 0x16, 0x20, 0x1e, 0x14, 0x1f, 0x1e, 0x1f, 0x21, 0x22, 0x27, 0x27, 0x26, 0x1f, 0x22, 0x1e, 0x22, 0x1f, 0x1a, 0x22, 0x1f, 0x0e, 0x15, 0x0f, 0x0a, 0x0c, 0x09, 0x0c, 0x02, 0x0c, 0x03, 0x0b, 0x03, 0x05, 0xff, 0x02, 0x02, 0x01, 0x06, 0xfd, 0x01, 0xff, 0x00, 0x06, 0x0a, 0x0a, 0x03, 0x10, 0x12, 0x12, 0x10, 0x16, 0x18, 0x17, 0x20, 0x1d, 0x19, 0x15, 0x19, 0x1d, 0x25, 0x1e, 0x1c, 0x17, 0x22, 0x20, 0x1e, 0x1c, 0x25, 0x26, 0x1a, 0x1e, 0x1e, 0x29, 0x1c, 0x19, 0x18, 0x15, 0x13, 0x08, 0x03, 0x0b, 0x06, 0x04, 0x00, 0x09, 0x0c, 0x08, 0x06, 0x02, 0x0b, 0xfc, 0xfb, 0x02, 0xff, 0x09, 0x02, 0x09, 0x09, 0x08, 0x09, 0x07, 0x06, 0x0d, 0x15, 0x11, 0x1c, 0x1a, 0x16, 0x1a, 0x23, 0x1c, 0x1e, 0x21, 0x1d, 0x1e, 0x1f, 0x23, 0x36, 0x3a, 0x35, 0x2b, 0x1d, 0x1d, 0x0e, 0x11, 0x0d, 0x08, 0x13, 0x14, 0x19, 0x1c, 0x1a, 0x14, 0x0f, 0x24, 0x08, 0x05, 0x0d, 0x00, 0x14, 0x08, 0x13, 0x08, 0x0f, 0xfa, 0xeb, 0xec, 0xe7, 0xea, 0xe8, 0xf2, 0xfd, 0x09, 0x08, 0x12, 0x11, 0x23, 0x14, 0x0a, 0x10, 0x12, 0x14, 0x1a, 0x21, 0x28, 0x2f, 0x21, 0x1a, 0x26, 0x1f, 0x19, 0x0e, 0x16, 0x18, 0x19, 0x1a, 0x2a, 0x2c, 0x2c, 0x2d, 0x24, 0x1f, 0x1c, 0x19, 0x17, 0x1e, 0x14, 0x14, 0x0b, 0x09, 0x05, 0x00, 0x06, 0x07, 0xfe, 0xff, 0xf8, 0xf3, 0xfa, 0x0c, 0x13, 0x12, 0x0f, 0x0a, 0x14, 0x13, 0xfe, 0x07, 0x0a, 0x04, 0xfe, 0xfd, 0x0a, 0x0c, 0x16, 0x16, 0x1c, 0x1a, 0x13, 0x0c, 0x1f, 0x25, 0x21, 0x1d, 0x2d, 0x34, 0x2e, 0x38, 0x32, 0x30, 0x27, 0x13, 0x12, 0x12, 0x0a, 0x0d, 0x1c, 0x19, 0x19, 0x1d, 0x13, 0x12, 0x1b, 0x11, 0x0b, 0x10, 0x0b, 0x02 /* ciach bo za duzo bajtow na to forum */
};
const uint16_t MELODY_LEN = sizeof(melody);
#include <avr/io.h>
#include <util/delay.h>
#include "melody.c"
#include "spi.c"
#define SLAVE_SELECT PB2
#define SS_PORT PORTB
int main( void ) {
spi_init();
uint16_t pos = 0;
while(1) {
SS_PORT &= ~ _BV(SLAVE_SELECT);
uint8_t sample = MELODY_SAMPLE(pos++);
spi_trade( 0x70 | (sample >> 4) );
spi_trade( (sample << 4) & 0xFF );
SS_PORT |= _BV(SLAVE_SELECT);
if (pos >= MELODY_LEN) {
pos = 0;
}
_delay_us(125);
}
return 0;
}
Timer
#include <avr/io.h>
#include <inttypes.h>
#include "spi.c"
#include <util/delay.h>
#define LA PB1
#define OE PB2
static uint8_t segments[10] = {
_BV(0) | _BV(1) | _BV(2) | _BV(3) | _BV(4) | _BV(5) | _BV(7), // 0
_BV(1) | _BV(2), // 1
_BV(0) | _BV(1) | _BV(3) | _BV(4) | _BV(6), // 2
_BV(0) | _BV(1) | _BV(2) | _BV(3) | _BV(6), // 3
_BV(1) | _BV(2) | _BV(5) | _BV(6), // 4
_BV(0) | _BV(2) | _BV(3) | _BV(5) | _BV(6), // 5
_BV(0) | _BV(2) | _BV(3) | _BV(4) | _BV(5) | _BV(6), // 6
_BV(0) | _BV(1) | _BV(2), // 7
_BV(0) | _BV(1) | _BV(2) | _BV(3) | _BV(4) | _BV(5) | _BV(6), // 8
_BV(0) | _BV(1) | _BV(2) | _BV(3) | _BV(5) | _BV(6) // 9
};
static void show(uint8_t digit) {
PORTB |= _BV(OE);
PORTB &= ~ _BV(LA);
spi_trade( segments[digit] );
PORTB |= _BV(LA);
PORTB &= ~ _BV(OE);
}
int main( void ) {
spi_init();
uint8_t s = 0;
while(1) {
show(s++);
if (s >= 10) {
s = 0;
}
_delay_ms(1000);
}
return 0;
}