daily pastebin goal
61%
SHARE
TWEET

pbfy

a guest Jan 27th, 2010 394 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // musicbox1.c
  2. // for NerdKits with ATtiny26L
  3. // mrobbins@mit.edu
  4. // ported by pbfy0 for ATmega168
  5. #define F_CPU 14745600
  6.  
  7. #include <stdio.h>
  8. #include <math.h>
  9.  
  10. #include <avr/io.h>
  11. #include <avr/pgmspace.h>
  12. #include <inttypes.h>
  13.  
  14. #include "../libnerdkits/delay.h"
  15. #include "../libnerdkits/lcd.h"
  16. #include "../libnerdkits/uart.h"
  17. #include <avr/interrupt.h>
  18. void lcd_clline() {
  19.         lcd_write_string(PSTR("                        "));
  20. }
  21.  
  22. void lcd_qclear_h(){
  23.         lcd_home();
  24.         lcd_clline();
  25.         lcd_line_two();
  26.         lcd_clline();
  27.         lcd_home();
  28. }
  29.  
  30. void play_tone(uint16_t delay, uint8_t duration){
  31.         uint16_t tmp = 100 * duration;
  32.         uint16_t delaysm = delay / 50;
  33.         uint16_t cycles = tmp / delaysm;
  34.         while (cycles > 0) {
  35.                 PORTB |= (1<<5);
  36.                 delay_us(delay);
  37.                 PORTB &= ~(1<<5);
  38.                 delay_us(delay);
  39.                 cycles--;
  40.         }
  41. }
  42.  
  43. void PlayAndWrite(char *strtoprint, uint16_t delay, uint8_t duration){
  44.         printf_P(PSTR("%s"), strtoprint);
  45.         play_tone(delay, duration);
  46. }
  47. #define D5 851
  48. #define E5 758
  49. #define Fsh5 675
  50. #define G5 637
  51. #define A5 568
  52. #define B5 506
  53. #define C6 477
  54. #define D6 425
  55. #define DUR 40
  56. #define DUR2 80
  57.  
  58. int main(){
  59.         lcd_init();
  60.         lcd_home();
  61.         FILE lcd_stream = FDEV_SETUP_STREAM(lcd_putchar, 0, _FDEV_SETUP_WRITE);
  62.         stdin = stdout = &lcd_stream;
  63.         DDRB |= (1<<5);
  64.         DDRC &= ~(1<<5);
  65.         PORTC |= (1<<5);
  66.         while(1){
  67.                 lcd_write_string(PSTR("Press to play a song..."));
  68.                 while(PINC & (1<<5));
  69.                 lcd_qclear_h();
  70.                 PlayAndWrite("Happy ", D5, DUR);
  71.                 PlayAndWrite("birth", E5, DUR);
  72.                 PlayAndWrite("day ", D5, DUR);
  73.                 PlayAndWrite("to ", G5, DUR);
  74.                 PlayAndWrite("you ", Fsh5, DUR2);
  75.                 lcd_line_two();
  76.                 PlayAndWrite("Happy ", D5, DUR);
  77.                 PlayAndWrite("birth", E5, DUR);
  78.                 PlayAndWrite("day ", D5, DUR);
  79.                 PlayAndWrite("to ", A5, DUR);
  80.                 PlayAndWrite("you ", G5, DUR2);
  81.                 lcd_qclear_h();
  82.                 PlayAndWrite("Happy ", D5, DUR);
  83.                 PlayAndWrite("birth", D6, DUR);
  84.                 PlayAndWrite("day ", B5, DUR);
  85.                 PlayAndWrite("dear ", G5, DUR);
  86.                 PlayAndWrite("--", Fsh5, DUR);
  87.                 PlayAndWrite("--", E5, DUR);
  88.                 lcd_line_two();
  89.                 PlayAndWrite("Happy ", C6, DUR);
  90.                 PlayAndWrite("birth", B5, DUR);
  91.                 PlayAndWrite("day ", G5, DUR);
  92.                 PlayAndWrite("to ", A5, DUR);
  93.                 PlayAndWrite("you! ", G5, DUR2);
  94.                 delay_ms(500);
  95.         }
  96.        
  97. }
RAW Paste Data
Top