|
你知道什么是天狗? 它被称为一个玩具这是为了响应与它一起的音乐声音和动画。 天狗分析声音和根据它的响度或其他参数的变化LED或其他成像设备的面孔。启发亚历 天狗原克里斯宾琼斯决定建立在他的实验电路板之一。 他用一个LED点阵显示面对。 天狗心脏是一个ATMEGA48单片机在10MHz的速度运行。 声音传感器是基于从旧手机和LM386运算放大器的驻极体麦克风。 AVR ADC输入的声音检测,并根据其水平,面对的是LED矩阵更新。 简单而有趣。 要建立一个-项目文件 。
程序:
- /* -----------------------------------------------------------------------
- * Title: Tengu like face use 8x5 LED display
- * Author: Alexander Weber alex@tinkerlog.com
- * Date: 22.10.2007
- * Hardware: ATmega48
- * Software: WinAVR 20070525
- *
- */
- #include <inttypes.h>
- #include <stdlib.h>
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #include <util/delay.h>
- #define TRUE 1
- #define FALSE 0
- #define LED_BIT PD4
- #define SCROLL_COUNT_MAX 125
- #define MAX_FACES 6
- #define MAX_SAMPLES 10
- #define SCALE 18
- #define DELTA 20
- #define CHANNEL 5
- static volatile uint8_t soft_prescaler = 0; // soft prescaler
- static volatile uint8_t screen_mem[8]; // screen memory
- static volatile uint8_t active_col; // active column on screen
- static volatile uint8_t col_ptr = 0; // active column in message
- static volatile uint8_t scroll_count = 0;
- // clock
- static volatile uint16_t ms_count = 0;
- static volatile uint8_t seconds = 0;
- static volatile uint8_t minutes = 0;
- static volatile uint8_t hours = 0;
- static volatile uint8_t loudness = 0;
- static volatile uint8_t active_face = 0;
- static volatile uint8_t faces[MAX_FACES][8] = {
- // { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // empty
- {
- 0x00, // -----
- 0x0A, // -x-x-
- 0x00, // -----
- 0x0E, // -xxx-
- 0x00, // -----
- 0x00, // -----
- 0x00, // -----
- 0x00 // -----
- },
- {
- 0x00, // -----
- 0x0A, // -x-x-
- 0x00, // -----
- 0x0E, // -xxx-
- 0x0E, // -xxx-
- 0x00, // -----
- 0x00, // -----
- 0x00 // -----
- },
- {
- 0x00, // -----
- 0x0A, // -x-x-
- 0x00, // -----
- 0x0E, // -xxx-
- //0x1F, // xxxxx
- 0x11, // x---x
- //0x1F, // xxxxx
- 0x0E, // -xxx-
- 0x00, // -----
- 0x00 // -----
- },
- {
- 0x00, // -----
- 0x0A, // -x-x-
- 0x00, // -----
- 0x0E, // -xxx-
- //0x1F, // xxxxx
- 0x11, // x---x
- 0x11, // x---x
- //0x1F, // xxxxx
- 0x0E, // -xxx-
- 0x00 // -----
- },
- {
- 0x00, // -----
- 0x0A, // -x-x-
- 0x00, // -----
- 0x0E, // -xxx-
- //0x1F, // xxxxx
- 0x11, // x---x
- 0x11, // x---x
- 0x11, // x---x
- //0x1F // xxxxx
- 0x0E, // -xxx-
- },
- {
- 0x00, // -----
- 0x1B, // xx-xx
- 0x00, // -----
- 0x0E, // -xxx-
- //0x1F, // xxxxx
- 0x11, // x---x
- 0x11, // x---x
- 0x11, // x---x
- //0x1F // xxxxx
- 0x0E, // -xxx-
- }
- };
- // prototypes
- void display_active_col(void);
- void scroll_video(void);
- void clock_tick(void);
- /*
- * SIGNAL TIMER0_OVF_vect
- * Handles overflow interrupts of timer 0.
- * With prescaler 8 it is called with 4882.8125Hz at 10MHz, so the clock is
- * not exact.
- * A column update every 4 ticks = 1220.7Hz.
- * The complete display = 1220.7Hz / 8 columns = 152.6Hz
- */
- SIGNAL(TIMER0_OVF_vect) {
- if ((ms_count % 4) == 0) {
- display_active_col();
- }
- // count ms
- if (++ms_count == 4882) {
- clock_tick();
- ms_count = 0;
- }
- }
- /*
- * get_adc
- * Return the 10bit value of the selected adc channel.
- */
- uint16_t get_adc(void) {
- // ADC setup
- ADCSRA =
- (1 << ADEN) | // enable ADC
- (1 << ADPS1) | (1 << ADPS0); // set prescaler to 8
-
- // select channel
- ADMUX = CHANNEL;
-
- // warm up the ADC, discard the first conversion
- ADCSRA |= (1 << ADSC);
- while (ADCSRA & (1 << ADSC));
- return ADCW;
- }
- /*
- * clock_tick
- * Adds a second to the clock.
- */
- void clock_tick(void) {
- seconds++;
- if (seconds == 60) {
- seconds = 0;
- minutes++;
- if (minutes == 60) {
- minutes = 0;
- hours++;
- if (hours == 24) {
- hours = 0;
- }
- }
- }
- }
- /*
- * display_active_col
- * Deactivates the old column and displays the next one.
- */
- void display_active_col(void) {
-
- // shut down all rows
- PORTC |= 0x1f;
- // shut down active column
- if (active_col > 5) {
- PORTD &= ~0x0c; // switch off both pins
- }
- else {
- PORTB &= ~(1 << active_col);
- }
-
- // next column
- active_col = (active_col+1) % 8;
-
- // output all rows
- PORTC = ~faces[active_face][active_col];
-
- // activate column
- if (active_col == 6) {
- PORTD |= 0x04;
- }
- else if (active_col == 7) {
- PORTD |= 0x08;
- }
- else {
- PORTB |= (1 << active_col);
- }
- }
- int main(void) {
- uint8_t i = 0;
- uint16_t sum = 0;
- uint16_t amplitude = 0;
- // timer 0 setup, prescaler 8
- TCCR0B |= (1 << CS00);
- // enable timer 0 interrupt
- TIMSK0 |= (1 << TOIE0);
-
- DDRC |= 0x1f; // PC0-PC4: row 0-4
- DDRB |= 0x3f; // PB0-PB5: col 0-5
- DDRD |= 0x1c; // PD2-PD3: col 6-7, PD4: debug LED
-
- sei();
-
- while (1) {
- // do sampling and sum the amplitude
- for (i = 0; i < MAX_SAMPLES; i++) {
- amplitude = get_adc();
- if (amplitude > 512 + DELTA) {
- amplitude -= (512 + DELTA);
- }
- else if (amplitude < 512 - DELTA) {
- amplitude = 512 - amplitude - DELTA;
- }
- else {
- amplitude = 0;
- }
- sum += amplitude;
- _delay_ms(1);
- }
- sum = sum / MAX_SAMPLES;
- sum = sum / SCALE; // scale down to map to faces
- active_face = (sum >= MAX_FACES) ? MAX_FACES - 1 : sum;
- }
- return 0;
- }
复制代码 下载
C程序.zip
(1.84 KB, 下载次数: 12)
face.zip
(57.25 KB, 下载次数: 10)
原理图文件 需要用EAGLE 打开
|
|