圣源电子制作

 找回密码
 立即注册
查看: 7648|回复: 0

ATMEGA48天狗项目的乐趣-天狗分析声音和根据它的响度-C程序-原理图文件-转载自国外网

[复制链接]
发表于 2011-12-12 20:49:06 | 显示全部楼层 |阅读模式
你知道什么是天狗? 它被称为一个玩具这是为了响应与它一起的音乐声音和动画。 天狗分析声音和根据它的响度或其他参数的变化LED或其他成像设备的面孔。启发亚历 天狗原克里斯宾琼斯决定建立在他的实验电路板之一。 他用一个LED点阵显示面对。
AVR_tengu.jpg
天狗心脏是一个ATMEGA48单片机在10MHz的速度运行。 声音传感器是基于从旧手机和LM386运算放大器的驻极体麦克风。  AVR ADC输入的声音检测,并根据其水平,面对的是LED矩阵更新。 简单而有趣。 要建立一个-项目文件 。
12.png
程序:
  1. /* -----------------------------------------------------------------------
  2. * Title:    Tengu like face use 8x5 LED display
  3. * Author:   Alexander Weber alex@tinkerlog.com
  4. * Date:     22.10.2007
  5. * Hardware: ATmega48
  6. * Software: WinAVR 20070525
  7. *
  8. */

  9. #include <inttypes.h>
  10. #include <stdlib.h>
  11. #include <avr/io.h>
  12. #include <avr/interrupt.h>
  13. #include <util/delay.h>

  14. #define TRUE 1
  15. #define FALSE 0
  16. #define LED_BIT PD4

  17. #define SCROLL_COUNT_MAX 125
  18. #define MAX_FACES 6
  19. #define MAX_SAMPLES 10
  20. #define SCALE 18
  21. #define DELTA 20
  22. #define CHANNEL 5

  23. static volatile uint8_t soft_prescaler = 0;                // soft prescaler
  24. static volatile uint8_t screen_mem[8];                        // screen memory
  25. static volatile uint8_t active_col;                        // active column on screen
  26. static volatile uint8_t col_ptr = 0;                        // active column in message
  27. static volatile uint8_t scroll_count = 0;               

  28. // clock
  29. static volatile uint16_t ms_count = 0;
  30. static volatile uint8_t seconds = 0;
  31. static volatile uint8_t minutes = 0;
  32. static volatile uint8_t hours = 0;
  33. static volatile uint8_t loudness = 0;

  34. static volatile uint8_t active_face = 0;
  35. static volatile uint8_t faces[MAX_FACES][8] = {
  36.   // { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},        // empty
  37.   {
  38.     0x00,        // -----
  39.     0x0A,         // -x-x-
  40.     0x00,         // -----
  41.     0x0E,         // -xxx-
  42.     0x00,         // -----
  43.     0x00,         // -----
  44.     0x00,         // -----
  45.     0x00         // -----
  46.   },       
  47.   {
  48.     0x00,        // -----
  49.     0x0A,         // -x-x-
  50.     0x00,         // -----
  51.     0x0E,         // -xxx-
  52.     0x0E,         // -xxx-
  53.     0x00,         // -----
  54.     0x00,         // -----
  55.     0x00         // -----
  56.   },       
  57.   {
  58.     0x00,        // -----
  59.     0x0A,         // -x-x-
  60.     0x00,         // -----
  61.     0x0E,         // -xxx-
  62.     //0x1F,         // xxxxx
  63.     0x11,         // x---x
  64.     //0x1F,         // xxxxx
  65.     0x0E,         // -xxx-
  66.     0x00,         // -----
  67.     0x00         // -----
  68.   },       
  69.   {
  70.     0x00,        // -----
  71.     0x0A,         // -x-x-
  72.     0x00,         // -----
  73.     0x0E,         // -xxx-
  74.     //0x1F,         // xxxxx
  75.     0x11,         // x---x
  76.     0x11,         // x---x
  77.     //0x1F,         // xxxxx
  78.     0x0E,         // -xxx-
  79.     0x00         // -----
  80.   },       
  81.   {
  82.     0x00,        // -----
  83.     0x0A,         // -x-x-
  84.     0x00,         // -----
  85.     0x0E,         // -xxx-
  86.     //0x1F,         // xxxxx
  87.     0x11,         // x---x
  88.     0x11,         // x---x
  89.     0x11,         // x---x
  90.     //0x1F         // xxxxx
  91.     0x0E,         // -xxx-
  92.   },       
  93.   {
  94.     0x00,        // -----
  95.     0x1B,         // xx-xx
  96.     0x00,        // -----
  97.     0x0E,         // -xxx-
  98.     //0x1F,         // xxxxx
  99.     0x11,         // x---x
  100.     0x11,         // x---x
  101.     0x11,         // x---x
  102.     //0x1F         // xxxxx
  103.     0x0E,         // -xxx-
  104.   }       
  105. };


  106. // prototypes
  107. void display_active_col(void);
  108. void scroll_video(void);
  109. void clock_tick(void);


  110. /*
  111. * SIGNAL TIMER0_OVF_vect
  112. * Handles overflow interrupts of timer 0.
  113. * With prescaler 8 it is called with 4882.8125Hz at 10MHz, so the clock is
  114. * not exact.
  115. * A column update every 4 ticks = 1220.7Hz.
  116. * The complete display = 1220.7Hz / 8 columns = 152.6Hz
  117. */
  118. SIGNAL(TIMER0_OVF_vect) {       
  119.   if ((ms_count % 4) == 0) {
  120.     display_active_col();
  121.   }
  122.   // count ms
  123.   if (++ms_count == 4882) {
  124.     clock_tick();
  125.     ms_count = 0;
  126.   }       
  127. }



  128. /*
  129. * get_adc
  130. * Return the 10bit value of the selected adc channel.
  131. */
  132. uint16_t get_adc(void) {

  133.   // ADC setup
  134.   ADCSRA =
  135.     (1 << ADEN) |                        // enable ADC
  136.     (1 << ADPS1) | (1 << ADPS0);        // set prescaler to 8       
  137.                
  138.   // select channel
  139.   ADMUX = CHANNEL;
  140.        
  141.   // warm up the ADC, discard the first conversion
  142.   ADCSRA |= (1 << ADSC);
  143.   while (ADCSRA & (1 << ADSC));

  144.   return ADCW;
  145. }



  146. /*
  147. * clock_tick
  148. * Adds a second to the clock.
  149. */
  150. void clock_tick(void) {
  151.   seconds++;
  152.   if (seconds == 60) {
  153.     seconds = 0;
  154.     minutes++;
  155.     if (minutes == 60) {
  156.       minutes = 0;
  157.       hours++;
  158.       if (hours == 24) {
  159.         hours = 0;
  160.       }
  161.     }
  162.   }
  163. }



  164. /*
  165. * display_active_col
  166. * Deactivates the old column and displays the next one.
  167. */
  168. void display_active_col(void) {
  169.        
  170.   // shut down all rows
  171.   PORTC |= 0x1f;

  172.   // shut down active column
  173.   if (active_col > 5) {
  174.     PORTD &= ~0x0c;        // switch off both pins
  175.   }
  176.   else {
  177.     PORTB &= ~(1 << active_col);               
  178.   }
  179.        
  180.   // next column
  181.   active_col = (active_col+1) % 8;
  182.        
  183.   // output all rows
  184.   PORTC = ~faces[active_face][active_col];
  185.        
  186.   // activate column
  187.   if (active_col == 6) {
  188.     PORTD |= 0x04;       
  189.   }
  190.   else if (active_col == 7) {
  191.     PORTD |= 0x08;       
  192.   }
  193.   else {
  194.     PORTB |= (1 << active_col);
  195.   }

  196. }



  197. int main(void) {

  198.   uint8_t i = 0;
  199.   uint16_t sum = 0;
  200.   uint16_t amplitude = 0;

  201.   // timer 0 setup, prescaler 8
  202.   TCCR0B |= (1 << CS00);
  203.   // enable timer 0 interrupt
  204.   TIMSK0 |= (1 << TOIE0);       
  205.        
  206.   DDRC |= 0x1f;        // PC0-PC4: row 0-4
  207.   DDRB |= 0x3f;        // PB0-PB5: col 0-5
  208.   DDRD |= 0x1c;        // PD2-PD3: col 6-7, PD4: debug LED
  209.                        
  210.   sei();
  211.        
  212.   while (1) {
  213.     // do sampling and sum the amplitude
  214.     for (i = 0; i < MAX_SAMPLES; i++) {
  215.       amplitude = get_adc();
  216.       if (amplitude > 512 + DELTA) {
  217.         amplitude -= (512 + DELTA);
  218.       }
  219.       else if (amplitude < 512 - DELTA) {
  220.         amplitude = 512 - amplitude - DELTA;
  221.       }
  222.       else {
  223.         amplitude = 0;
  224.       }
  225.       sum += amplitude;
  226.       _delay_ms(1);
  227.     }
  228.     sum = sum / MAX_SAMPLES;
  229.     sum = sum / SCALE;             // scale down to map to faces
  230.     active_face = (sum >= MAX_FACES) ? MAX_FACES - 1 : sum;
  231.   }

  232.   return 0;

  233. }

复制代码
下载
C程序.zip (1.84 KB, 下载次数: 12)
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|联系我们|闽公网安备 35012102000020号|圣源电子 ( 闽ICP备11020110号 )

GMT+8, 2024-3-29 07:22 , Processed in 0.050415 second(s), 21 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表