#include <16f876a.h>
#device adc =10
#use delay (clock=8M)
#fuses HS,NOWDT,NOPROTECT,NOLVP
#include <internal_eeprom.c>
#use fast_io(B)
#use fast_io(c)
#use fast_io(a)
int16 calculoBandaEr(int16 cero,int16 toper);// funcion
int16 a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0;
int16 calculoBandaBr(int16 cero, int16 topbr);//funcion
int16 n=0,z=0,o=0,p=0,q=0,r=0,s=0,t=0,u=0,v=0,w=0,x=0,y=0;
int16 cursor=0,toper=0,topbr=0,cero=0,ciclo_util;
int16 resultadoEr =0,resultadoBr =0;
void main()
{
set_tris_c(0b00000000);
output_c(0b00000000);
set_tris_b(0b11111111);
output_b(0b00000000);
//****************ADC**************************/
setup_adc_ports(AN0_AN1_AN3);//* entradas analogicas*//
setup_adc (ADC_CLOCK_INTERNAL);
setup_COUNTERS(RTCC_internal.rtcc_div_8);
//********************************************************/
// *************PWM*************************************/
setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_16,255, 1);
set_pwm1_duty(ciclo_util);
WHILE (1) {
set_pwm1_duty(ciclo_util);
set_adc_channel (0);
delay_us (20);
cursor = read_adc ();
if (input(pin_b4)==0)//configuracion
{
output_high (PIN_c0);//enciendo un led
if (input(pin_b1)==1)// configuro limite derecho
{
if (input(pin_b0)==1)//boton que graba en la eeprom
{
write_int16_eeprom(0x120,(int16) cursor);// escribe en la memoria
delay_ms(15);
toper=read_int16_eeprom(0x120);
}
}
if (input(pin_b2)==1)// configuro limite izquierdo
{
if (input(pin_b0)==1)//boton que graba en la eeprom
{
write_int16_eeprom(0x122,(int16) cursor);// escribe en la memoria
delay_ms(15);
topbr=read_int16_eeprom(0x122);
}
}
//****************CERO************************/
if (input(pin_b3)==1)// cero
{
if (input(pin_b0)==1)//boton que graba en la eeprom
{
write_int16_eeprom(0x126,(int16) cursor);// escribe en la memoria
delay_ms(15);
cero= read_int16_eeprom(0x126);
delay_ms(15);
}
}
}
//****************************************/
if (input(pin_b4)==1)//switch en posicion listo para usar
{
output_low (PIN_c0);//apago led de configuracion
cero= read_int16_eeprom(0x126);
delay_ms(15);
//******************************************************************/
topbr=read_int16_eeprom(0x122);
delay_ms(15);
resultadoBr = calculoBandaBr(topbr,cero);//( topbr -cero ) /14;
n = resultadoBr + cero;
z =cero+(resultadoBr*2);
o =cero+(resultadoBr*3);
p =cero+(resultadoBr*4);
q =cero+(resultadoBr*5);
r =cero+(resultadoBr*6);
s =cero+(resultadoBr*7);
t =cero+(resultadoBr*8);
u =cero+(resultadoBr*9);
v =cero+(resultadoBr*10);
w =cero+(resultadoBr*11);
x =cero+(resultadoBr*12);
y =cero+(resultadoBr*13);
//******************************************************************/
toper=read_int16_eeprom(0x120);
delay_ms(15);
resultadoEr = calculoBandaEr (cero, toper);
a= resultadoEr + toper;
b =( resultadoEr * 2)+toper;
c =( resultadoEr * 3)+toper;
d =( resultadoEr * 4)+toper;
e =( resultadoEr * 5)+toper;
f =( resultadoEr * 6)+toper;
g =( resultadoEr * 7)+toper;
h =( resultadoEr * 8)+toper;
i =( resultadoEr * 9)+toper;
j =( resultadoEr * 10)+toper;
k =( resultadoEr * 11)+toper;
l =( resultadoEr * 12)+toper;
m =( resultadoEr * 13)+toper;
IF (cursor >0 && cursor<= toper)
{
output_low(PIN_c6);
output_high (PIN_c5);
ciclo_util=8;
}
IF (cursor > toper && cursor <=a)
{
output_low (PIN_c5);
output_low(PIN_c6);
ciclo_util=44;
}
IF (cursor >a && cursor <b)
{
ciclo_util=80;
}
IF (cursor >b &&cursor<c)
{
ciclo_util=116;
}
IF (cursor >c && cursor<d)
{
ciclo_util=152;
}
IF (cursor >d && cursor<e)
{
ciclo_util=188;
}
IF (cursor >e && cursor<f)
{
ciclo_util=224;
}
IF (cursor>f&&cursor<g)
{
ciclo_util=260;
}
IF (cursor>g&&cursor<h)
{
ciclo_util=296;
}
IF (cursor>h&&cursor<i)
{
ciclo_util=332;
}
IF (cursor>i&&cursor<j)
{
ciclo_util=368;
}
IF (cursor>j&&cursor<k)
{
ciclo_util=404;
}
IF (cursor>k&&cursor<l)
{
ciclo_util=440;
}
IF (cursor>l&&cursor<m)
{
ciclo_util=476;
}
//************************************/
IF (cursor >m&&cursor<cero)
{
ciclo_util=512; //posicion
}
//************************************ //de la
IF (cursor >cero &&cursor<n)
{ // aguja
ciclo_util=512;
} //en el centro
//************************************/
if (cursor>n&&cursor<z)
{
ciclo_util=548;
}
if (cursor>z&&cursor<o)
{
ciclo_util=584;
}
if (cursor>o&&cursor<p)
{
ciclo_util=620;
}
if (cursor>p&&cursor<q)
{
ciclo_util=656;
}
if (cursor>q&&cursor<r)
{
ciclo_util=692;
}
if (cursor>r&&cursor<s)
{
ciclo_util=728;
}
IF (cursor>s&&cursor<t)
{
ciclo_util=764;
}
IF (cursor>t&&cursor<u)
{
ciclo_util=800;
}
IF (cursor>u&&cursor<v)
{
ciclo_util=836;
}
IF (cursor>v&&cursor<w)
{
ciclo_util=872;
}
IF (cursor>w&&cursor<x)
{
ciclo_util=908;
}
IF (cursor>x&&cursor<y)
{
ciclo_util=944;
}
if (cursor>y&&cursor<topbr)
{
output_low (PIN_c5);
output_low(PIN_c6);
ciclo_util=980;
}
if (cursor>topbr&&cursor<=1024)
{
output_high(PIN_c6);//led************************/
output_low (PIN_c5);
ciclo_util=1023;
}
}
}
}
//funciones*********************************************/
int16 calculoBandaEr(int16 cero, int16 toper)
{
toper=read_int16_eeprom(0x120);
delay_ms(15);
RETURN (cero - toper) / 14;
}
int16 calculoBandaBr(int16 topbr,int16 cero )
{
topbr=read_int16_eeprom(0x122);
delay_ms(15);
RETURN ( topbr -cero ) / 14;
}