Buzz Wire Code


/*
* File:   buzzwire.c
* Author: Derek Extreme Kits
* extkits.uk/buzzwire
*
* Created on July 26, 2016, 10:16 PM
*/

#include <stdio.h>
#include <stdlib.h>
#include <xc.h>


/*
*
*/


//pin define gpio pins
#define sounder2 GPIObits.GP0 //pin7
#define led1     GPIObits.GP1 //pin6
#define dfa1     GPIObits.GP2    //pin 5
#define    wire     GPIObits.GP3 //pin4
#define led2     GPIObits.GP4 //pin3
#define sounder1 GPIObits.GP5 //pin2


//constants
#define ToneOnP    0x04
#define Tones 0x08

#define _XTAL_FREQ 4000000
#pragma config FOSC = INTRCIO
#pragma config WDTE = OFF
#pragma config PWRTE = ON
#pragma config MCLRE = OFF
#pragma config BOREN = OFF
#pragma config CP = OFF
#pragma config CPD = OFF

#define SLEEP()     asm("sleep")

typedef union {
unsigned char byte;
struct {
unsigned flash:1, sounder:1, b2:1, b3:1, b4:1, b5:1, b6:1, b7:1;
};
} t_byte;

//function definitions
void tick(unsigned int);
void tone(unsigned int ,unsigned int );
void shortdelay(unsigned int);
void flashled(void);
t_byte status ;

int main(int argc, char** argv) {
int cnt;

//port setup
OPTION_REG =0b01000000; //GPWU=1. GPPU=1,T0CS=0 ToSE=0PSA=0PS=000
ANSEL = 0;                     // adc off
CMCON = 0b111;           //    comparator off 1 -> RB0,RB1 digital
WPU=0b00000010;  //pullup for prog
TRISIO = 0b0; //gpo all out except gpio3 output
ADCON0=0b00000000 ; //result left justified -  adc off
VRCON=0;

//port change int setup to remove sleep
IOC=0b00001000; //gpio 3 only
INTCONbits.GPIE=1;
INTCONbits.GIE=0;

while(1){
led1=0;
led2=0;
INTCONbits.GPIF=0; //reset interrupt
GPIO=0; //switch off everything
SLEEP();
INTCONbits.GPIF=0; //reset interrupt

if(wire==0){
shortdelay(100); //debounce
if(wire==0){
cnt=10;
while(cnt-- >0){
flashled();
tone(5,640);
flashled();
tone(10,480);
flashled();
tone(15,360);
flashled();
tone(20,280);
} // while cnt
} //if wire
} //if wire
} //while(1)

return (EXIT_SUCCESS);
}

void flashled(void){
status.flash=!status.flash;
led1=status.flash;
led2=!status.flash;

}

void tone(unsigned int freq,unsigned int duration){
int cnt=duration;
while(cnt-- >0){
tick(freq);
}
}

void tick(unsigned int freq){
status.sounder=!status.sounder;
sounder1=status.sounder;
sounder2=!status.sounder;
shortdelay(freq);


}

void shortdelay(unsigned int time){
unsigned int tmp=0;
while(tmp<time){
tmp++;
}
}

 

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.