/*
Progetto di scacciazanzare basato su PIC16C84
*/
#include <pic.h>
#include "delay.h"

__CONFIG(FOSC0|UNPROTECT);

static bit LED	@ ((unsigned)&PORTB*8+3);	// RB3: LED...
static bit SPK1	@ ((unsigned)&PORTB*8+4);	// RB4: Speaker +
static bit SPK2	@ ((unsigned)&PORTA*8+2);	// RA2: Speaker -
unsigned int us;
unsigned int lambda;

#define LED_ON (LED=0);
#define LED_OFF (LED=1);

void sound(void){
	unsigned int i;

	lambda=6;
	do{
		for(i=0;i<50000;i++){
			SPK1=0;
			SPK2=1;
			us=lambda;
			#asm
lo1:			decfsz	_us
				goto 		lo1
			#endasm
			SPK1=1;
			SPK2=0;
			us=lambda;
			#asm
lo2:			decfsz	_us
				goto 		lo2
			#endasm
		}
		lambda--;
	}while (lambda>1);
	SPK1=1;
	SPK2=1;
	DelayMs(250);
	DelayMs(250);
}

void main (void){
	TRISA=0x00;	// tutto output
	TRISB=0x00;
	PORTA=0xff;	// tutte le uscite ad 1
	PORTB=0xff;
	do{
		sound();
		LED_ON;
		DelayMs(50);
		LED_OFF;
	}while (1);
}
