Buzz Wire Code

</p>
<p>/*<br />
* File:   buzzwire.c<br />
* Author: Derek Extreme Kits<br />
* extkits.uk/buzzwire<br />
*<br />
* Created on July 26, 2016, 10:16 PM<br />
*/</p>
<p>#include &lt;stdio.h&gt;<br />
#include &lt;stdlib.h&gt;<br />
#include &lt;xc.h&gt;</p>
<p>/*<br />
*<br />
*/</p>
<p>//pin define gpio pins<br />
#define sounder2 GPIObits.GP0 //pin7<br />
#define led1     GPIObits.GP1 //pin6<br />
#define dfa1     GPIObits.GP2    //pin 5<br />
#define    wire     GPIObits.GP3 //pin4<br />
#define led2     GPIObits.GP4 //pin3<br />
#define sounder1 GPIObits.GP5 //pin2</p>
<p>//constants<br />
#define ToneOnP    0x04<br />
#define Tones 0x08</p>
<p>#define _XTAL_FREQ 4000000<br />
#pragma config FOSC = INTRCIO<br />
#pragma config WDTE = OFF<br />
#pragma config PWRTE = ON<br />
#pragma config MCLRE = OFF<br />
#pragma config BOREN = OFF<br />
#pragma config CP = OFF<br />
#pragma config CPD = OFF</p>
<p>#define SLEEP()     asm(&quot;sleep&quot;)</p>
<p>typedef union {<br />
unsigned char byte;<br />
struct {<br />
unsigned flash:1, sounder:1, b2:1, b3:1, b4:1, b5:1, b6:1, b7:1;<br />
};<br />
} t_byte;</p>
<p>//function definitions<br />
void tick(unsigned int);<br />
void tone(unsigned int ,unsigned int );<br />
void shortdelay(unsigned int);<br />
void flashled(void);<br />
t_byte status ;</p>
<p>int main(int argc, char** argv) {<br />
int cnt;</p>
<p>//port setup<br />
OPTION_REG =0b01000000; //GPWU=1. GPPU=1,T0CS=0 ToSE=0PSA=0PS=000<br />
ANSEL = 0;                     // adc off<br />
CMCON = 0b111;           //    comparator off 1 -&gt; RB0,RB1 digital<br />
WPU=0b00000010;  //pullup for prog<br />
TRISIO = 0b0; //gpo all out except gpio3 output<br />
ADCON0=0b00000000 ; //result left justified -  adc off<br />
VRCON=0;</p>
<p>//port change int setup to remove sleep<br />
IOC=0b00001000; //gpio 3 only<br />
INTCONbits.GPIE=1;<br />
INTCONbits.GIE=0;</p>
<p>while(1){<br />
led1=0;<br />
led2=0;<br />
INTCONbits.GPIF=0; //reset interrupt<br />
GPIO=0; //switch off everything<br />
SLEEP();<br />
INTCONbits.GPIF=0; //reset interrupt</p>
<p>if(wire==0){<br />
shortdelay(100); //debounce<br />
if(wire==0){<br />
cnt=10;<br />
while(cnt-- &gt;0){<br />
flashled();<br />
tone(5,640);<br />
flashled();<br />
tone(10,480);<br />
flashled();<br />
tone(15,360);<br />
flashled();<br />
tone(20,280);<br />
} // while cnt<br />
} //if wire<br />
} //if wire<br />
} //while(1)</p>
<p>return (EXIT_SUCCESS);<br />
}</p>
<p>void flashled(void){<br />
status.flash=!status.flash;<br />
led1=status.flash;<br />
led2=!status.flash;</p>
<p>}</p>
<p>void tone(unsigned int freq,unsigned int duration){<br />
int cnt=duration;<br />
while(cnt-- &gt;0){<br />
tick(freq);<br />
}<br />
}</p>
<p>void tick(unsigned int freq){<br />
status.sounder=!status.sounder;<br />
sounder1=status.sounder;<br />
sounder2=!status.sounder;<br />
shortdelay(freq);</p>
<p>}</p>
<p>void shortdelay(unsigned int time){<br />
unsigned int tmp=0;<br />
while(tmp&lt;time){<br />
tmp++;<br />
}<br />
}</p>
<p>

 

Licence

Creative Commons License
Extreme Kits Buzz Wire Kit by Extreme Kits is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.
Permissions beyond the scope of this license may be available at extkits.uk.