Gerard-Meier

IVIbot fur elise

Feb 17th, 2011
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.22 KB | None | 0 0
  1.  
  2. #include <p18f4550.h>
  3. #include <delays.h>
  4.  
  5. typedef unsigned char byte;
  6.  
  7. // Bunch of prototypes:
  8. extern void InitializeSystem(void); // Declared in inits.c
  9.        void testRoutine(void);
  10.  
  11.  
  12. // Point of entry:
  13. void main(void) {
  14.     InitializeSystem();
  15.     testRoutine();
  16. }
  17.  
  18.  
  19. // Struct outline for a music note:
  20. typedef struct {
  21.     unsigned int ledbit;
  22.     unsigned int delay;
  23. } Note;
  24.  
  25.  
  26. // Declare some music notes:
  27. Note A = {1, 40};
  28. Note B = {2, 20};
  29. Note C = {4, 40};
  30. Note D = {8, 80};
  31. Note E = {16, 160};
  32. Note F = {32, 320};
  33. Note G = {33, 640};
  34.  
  35.  
  36. void testRoutine(void) {
  37.     Note songNotes[] = {&A, &B};//,E,B,D,C,A,D,F,A,B};
  38.     Note* notePlaying;
  39.  
  40.     byte i = 0, j = 0;
  41.     for(;;) {
  42.        
  43.         notePlaying = &songNotes[i];       
  44.  
  45.         // Repeat a few times to create a persistent tone.
  46.         for(j = 0; j < 200; ++j) {
  47.             PORTAbits.RA5 = ~PORTAbits.RA5; // Toggle between speaker on, speaker off.
  48.    
  49.             PORTB = notePlaying->ledbit;
  50.             Delay100TCYx(notePlaying->delay);
  51.         }
  52.        
  53.         // Disable all leds and the speaker after the tone has been played long enough.
  54.         PORTAbits.RA5 = 0;
  55.         PORTB         = 0;
  56.  
  57.         Delay10KTCYx(1000);
  58.        
  59.         // Jump back to the start of the array, if the end has been reached.
  60.         if(++i > 2) i = 0;
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment