#include <16F88.h>
#device adc=8
//1.08 LP Low power osc < 200 khz
//1.08 XT Crystal osc <= 4mhz
#FUSES HS //1.08 HS High speed Osc (> 4mhz)
//1.08 RC Resistor/Capacitor Osc with CLKOUT
//1.08 EC External clock with CLKOUT
//1.08 EC_IO External clock
//1.08 H4 High speed osc with HW enabled 4X PLL
//1.08 RC_IO Resistor/Capacitor Osc
//1.08 INTRC Internal RC Osc
//1.08 INTRC_IO Internal RC Osc, no CLKOUT
#FUSES NOFCMEN //1.14 NOFCMEN Fail-safe clock monitor disabled
//1.14 FCMEN Fail-safe clock monitor enabled
#FUSES NOIESO //1.15 NOIESO Internal External Switch Over mode disabled
//1.15 IESO Internal External Switch Over mode enabled
//2.00 NOPUT No Power Up Timer/
#FUSES PUT //2.00 PUT Power Up Timer
//2.01 BROWNOUT Reset when brownout detected
#FUSES NOBROWNOUT //2.01 NOBROWNOUT No brownout reset
//2.01 BROWNOUT_SW Brownout controlled by configuration bit in special file register
//2.01 BROWNOUT_NOSL Brownout enabled during operation, disabled during SLEEP
//#FUSES BORV45 //2.03 BORV45 Brownout reset at 4.5V
//2.03 BORV42 Brownout reset at 4.2V
//2.03 BORV27 Brownout reset at 2.7V
//2.03 BORV25 Brownout reset at 2.5V
#FUSES NOWDT //2.08 NOWDT No Watch Dog Timer
//2.08 WDT Watch Dog Timer
//2.09 WDT128 Watch Dog Timer uses 1:128 Postscale
//2.09 WDT64 Watch Dog Timer uses 1:64 Postscale
//2.09 WDT32 Watch Dog Timer uses 1:32 Postscale
//2.09 WDT16 Watch Dog Timer uses 1:16 Postscale
//2.09 WDT8192 Watch Dog Timer uses 1:8192 Postscale
//2.09 WDT4096 Watch Dog Timer uses 1:4096 Postscale
//2.09 WDT256 Watch Dog Timer uses 1:256 Postscale
//#FUSES WDT512 //2.09 WDT512 Watch Dog Timer uses 1:512 Postscale
//2.09 WDT2048 Watch Dog Timer uses 1:2048 Postscale
//2.09 WDT1024 Watch Dog Timer uses 1:1024 Postscale
//2.09 WDT16384 Watch Dog Timer uses 1:16384 Postscale
//2.09 WDT1 Watch Dog Timer uses 1:1 Postscale
//2.09 WDT8 Watch Dog Timer uses 1:8 Postscale
//2.09 WDT4 Watch Dog Timer uses 1:4 Postscale
//2.09 WDT32768 Watch Dog Timer uses 1:32768 Postscale
//2.09 WDT2 Watch Dog Timer uses 1:2 Postscale
//3.08 CCP2C1 CCP2 input/output multiplexed with RC1
//3.08 CCP2B3 CCP2 input/output multiplexed with RB3
//#FUSES NOPBADEN //3.09 NOPBADEN PORTB pins are configured as digital I/O on RESET
//3.09 PBADEN PORTB pins are configured as analog input channels on RESET
//3.10 NOLPT1OSC Timer1 configured for higher power operation
//3.10 LPT1OSC Timer1 configured for low-power operation
#FUSES NOMCLR //3.15 NOMCLR Master Clear pin used for I/O
//3.15 MCLR Master Clear pin enabled
//#FUSES NOSTVREN //4.00 NOSTVREN Stack full/underflow will not cause reset
//4.00 STVREN Stack full/underflow will cause reset
#FUSES NOLVP //4.02 NOLVP No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
//4.02 LVP Low Voltage Programming on B3(PIC16) or B5(PIC18)
//4.06 XINST Extended set extension and Indexed Addressing mode enabled
//#FUSES NOXINST //4.06 NOXINST Extended set extension and Indexed Addressing mode disabled (Legacy mode)
//4.07 DEBUG Debug mode for use with ICD
#FUSES NODEBUG //4.07 NODEBUG No Debug mode for ICD
#FUSES PROTECT //5.00 NOPROTECT Code not protected from reading
//5.00 PROTECT Code protected from reads
//5.14 CPB Boot Block Code Protected
//#FUSES NOCPB //5.14 NOCPB No Boot Block code protection
//5.15 CPD Data EEPROM Code Protected
//#FUSES NOCPD //5.15 NOCPD No EE protection
//6.00 WRT Program Memory Write Protected
#FUSES NOWRT //6.00 NOWRT Program memory not write protected
//6.13 WRTC configuration registers write protected
//#FUSES NOWRTC //6.13 NOWRTC configuration not registers write protected
//6.14 NOWRTB Boot block not write protected
//6.14 WRTB Boot block write protected
//6.15 WRTD Data EEPROM write protected
//#FUSES NOWRTD //6.15 NOWRTD Data EEPROM not write protected
//7.00 EBTR Memory protected from table reads
//#FUSES NOEBTR //7.00 NOEBTR Memory not protected from table reads
//7.14 EBTRB Boot block protected from table reads
//#FUSES NOEBTRB //7.14 NOEBTRB Boot block not protected from table reads
#use delay(clock=10000000)
/*
PINO01 - VREF/RA2/AN2/CVREF/ - LED3
PINO02 - RA3/AN3/C1OUT - LED2
PINO03 - RA4/T0CKI/C2OUT - LED1
PINO04 - RA5/MCLR/VPP -
PINO05 - VSS
PINO06 - RB0/INT/CCP1 - INIBE ALARME
PINO07 - RB1/SDI/SDA - PRA BAIXO
PINO08 - RB2/SDO/RX/DT - A
PINO09 - RB3/PGM/CCP1 - SENSOR
PINO10 - RB4/SCK/SCL - B
PINO11 - RB5/SS/TX/CK - C
PINO12 - RB6/PGC/T1OSO/T1CKI - PRA CIMA
PINO13 - RB7/PGD/T1OSI - BEEP
PINO14 - VDD
PINO15 - RA6/OSC2/CLKO
PINO16 - RA7/OSC1/CLKI
PINO17 - RA0/AN0 - SELECIONA
PINO18 - RA1/AN1
*/
//PROTOTIPOS
void tempo_sound(int32 t);
void tantantarantan(void);
void BEEP(void);
//Pinos de Entrada
#define INIBALARME PIN_B0
#define PRBAIXO PIN_B1
#define PRACIMA PIN_B6
#define SELECIONA PIN_A0
#define SENSOR PIN_B3
//Pinos de Sa�da
//#define LED1 PIN_A4
//#define LED2 PIN_A3
//#define LED3 PIN_A2
//#define A PIN_B2
//#define B PIN_B4
//#define C PIN_B5
#define BUZZER PIN_B7
//Define as Notas
#define NOTA_DO 261
#define NOTA_DOSUS 277
#define NOTA_RE 293
#define NOTA_RESUS 311
#define NOTA_MI 329
#define NOTA_FA 350
#define NOTA_FASUS 371
#define NOTA_SOL 392
#define NOTA_SOLSUS 415
#define NOTA_LA 440
#define NOTA_LASUS 466
#define NOTA_SI 500
#define NOTA_DO2 540
//define timer scaling value for each note
#define C 255
#define D 227
#define E 204
#define F 191
#define G 170
#define A 153
#define B 136
#define C2 127
//
//D� - 263,63 Hz
//the song to be played in this demonstration is "Twinkle Twinkle Little Star"
#define x1 14 //total number of notes in song to be played - modify for specific song
int songA1[x1]={C, C, G, G, A, A, G, F, F, E, E, D, D, C}; //insert notes of song in array
int lengthB1[x1]={1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2}; //relative length of each note
int i;
#define x2 25
int songA2[x2] = {E, E, E, E, E, E, E, G, C, D, E, F, F, F, F, F, E, E, E, E, D, D, E, D, G};
int lengthB2[x2] = {1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2};
int INIBALARMEFLAG;
void main(){
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISAB