Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <p18f4550.h>
- #include <delays.h>
- typedef unsigned char byte;
- // Bunch of prototypes:
- extern void InitializeSystem(void); // Declared in inits.c
- void testRoutine(void);
- // Point of entry:
- void main(void) {
- InitializeSystem();
- testRoutine();
- }
- // Struct outline for a music note:
- typedef struct {
- unsigned int ledbit;
- unsigned int delay;
- } Note;
- // Declare some music notes:
- Note A = {1, 40};
- Note B = {2, 20};
- Note C = {4, 40};
- Note D = {8, 80};
- Note E = {16, 160};
- Note F = {32, 320};
- Note G = {33, 640};
- void testRoutine(void) {
- Note songNotes[] = {&A, &B};//,E,B,D,C,A,D,F,A,B};
- Note* notePlaying;
- byte i = 0, j = 0;
- for(;;) {
- notePlaying = &songNotes[i];
- // Repeat a few times to create a persistent tone.
- for(j = 0; j < 200; ++j) {
- PORTAbits.RA5 = ~PORTAbits.RA5; // Toggle between speaker on, speaker off.
- PORTB = notePlaying->ledbit;
- Delay100TCYx(notePlaying->delay);
- }
- // Disable all leds and the speaker after the tone has been played long enough.
- PORTAbits.RA5 = 0;
- PORTB = 0;
- Delay10KTCYx(1000);
- // Jump back to the start of the array, if the end has been reached.
- if(++i > 2) i = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment