Está en la página 1de 8

************************************************ULTRASONICO******************

***********************

#include <16F877A.h> // busca en el directorio include del compilador

#fuses HS,NOWDT,PUT,NODEBUG,NOPROTECT,NOBROWNOUT,NOLVP,NOCPD,NOWRT

#use delay(clock = 4M) //CRISTAL DEL PIC = 20MHz

//#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8) //uart rs232

#BYTE pta = 0x5 // dirección en RAM

#BYTE ptb = 0x6 // dirección en RAM

#BYTE ptc = 0x7 // dirección en RAM

#BYTE ptd = 0x8 // dirección en RAM

#BYTE pte = 0x9 // dirección en RAM

#BYTE trisa = 0x85 // dirección en RAM

#BYTE trisb = 0x86 // dirección en RAM

#BYTE trisc = 0x87 // dirección en RAM

#BYTE trisd = 0x88 // dirección en RAM

#BYTE trise = 0x89 // dirección en RAM

#BIT led_state = ptc.0 //bit 1 del puerto c

int16 tiempo,distancia=0,valor_leido=0;

#define trig pin_B1

#define echo pin_B0

#USE standard_io(b)

void main(){

setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);

output_low(pin_c0);

set_tris_d(0b00000000);

set_tris_c(0b00000000);
output_low(pin_e1);

while(true){

led_state=!led_state;

output_high(trig);

delay_us(20);

output_low(trig);

while(!input(echo))

{}

set_timer1(0);

while(input(echo))

{}

tiempo=get_timer1();

distancia=(tiempo*10)/(59.0);

if(distancia<=10){

valor_leido=0;

output_d(valor_leido);

else if(distancia>=100){

valor_leido=169;

output_d(valor_leido);

else{

valor_leido=((169/95.0)*((distancia)-10));

output_d(valor_leido);
}

************************************************COMPENSADOR****************
*******************

#include <16f877A.h> // busca en el directorio include del compilador

#device adc=10

#fuses HS,NOWDT,PUT,NODEBUG,NOPROTECT,NOBROWNOUT,NOLVP,NOCPD,NOWRT

#use delay(clock = 20M) // CRISTAL DEL PIC = 20MHz

#BYTE pta = 0x5 // dirección en RAM

#BYTE ptb = 0x6 // dirección en RAM

#BYTE ptc = 0x7 // dirección en RAM

#BYTE ptd = 0x8 // dirección en RAM

#BYTE pte = 0x9 // dirección en RAM

#BYTE trisa = 0x85 // dirección en RAM

#BYTE trisb = 0x86 // dirección en RAM

#BYTE trisc = 0x87 // dirección en RAM

#BYTE trisd = 0x88 // dirección en RAM

#BYTE trise = 0x89 // dirección en RAM

#BIT led_state = pta.5 //bit 1 del puerto c

#BIT led_1 = pte.0

#BIT led_2 = pte.1

#BIT led_3 = pte.2

int8 f=0;

#bit p1 =f.0

#bit p2 =f.1

#bit p3 =f.2
int16 Dat=0;

int16 cnt_led=0;

float value=0;

float vi[22]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

float vo[4]={0,0,0,0};

float xn=0,rn=0;

#int_TIMER1

void TIMER1_isr(void){

// tiempo de muestreo de 0.037

//T=(4*8*(65536-42411))/(20M)=0.037

set_TIMER1(42411);

set_adc_channel(0); //the next read_adc call will read channel 0

delay_us(20); //a small delay is required after setting the channel and before
read

value=read_adc(); //starts the conversion and reads the result and store it in value

rn=value/1023.0;

set_TIMER1(42411);

set_adc_channel(1); //the next read_adc call will read channel 0

delay_us(20); //a small delay is required after setting the channel and before
read

value=read_adc(); //starts the conversion and reads the result and store it in value

xn=value/1023.0; //starts the conversion and reads the result and store it in value

vi[0]= rn - xn;

//vo[2] = 0.001127*vi[0]+0.002254*vi[1]+0.001127*vi[2]+1.923*vo[1]-0.9295*vo[0];

vo[1] = 0.0246*vi[1]+0.0246*vi[0]+vo[0];

if(vo[1]<0){vo[1]=0;}

else if(vo[1]>1){vo[1]=1;}
Dat=vo[1]*1023;

output_d(make8(Dat,0));

output_c(make8(Dat,1));

vi[2]=vi[1];

vi[1]=vi[0];

vo[0]=vo[1];

vo[1]=vo[2];

vo[2]=vo[3];

#int_TIMER0

void TIMER0_isr(void)

cnt_led++;

if(cnt_led==100){

led_state=!led_state;

cnt_led=0;}

set_TIMER0(120);

void main(){

set_tris_d(0b00000000);

set_tris_c(0b00000000);

PORT_B_PULLUPS(true);

enable_interrupts(INT_RB);

output_float(pin_b4);

output_float(pin_b5);

output_low(pin_a5);
output_low(pin_e0);

output_low(pin_e1);

//output_low(pin_e2);

setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256); //configuro preescala y oscilador interno

enable_interrupts(int_timer0); // habilito int timer0

set_TIMER0(120); //inicializa el timer0

setup_timer_1(T1_INTERNAL|T1_DIV_BY_8); //configuro preescala y oscilador interno

enable_interrupts(int_timer1); // habilito int timer1

set_TIMER1(42411); //inicializa el timer1 GET_TIMER1()

setup_adc(ADC_CLOCK_INTERNAL); //enables the a/d module

setup_adc_ports(AN0_AN1_AN3); //sets all the adc pins to analog

enable_interrupts(global); // ENABLE_INTERRUPTS(GLOBAL)

do

while(TRUE);

************************************************PWM*************************
******************

const int adc1 = A3; // select the input pin for the potentiometer

const int pwm1 = 14; // select the pin for the LED

const int ledPin1 = 12; // select the pin for the LED

int sal1 = 0; // variable to store the value coming from the sensor

int sal2 = 0; // variable to store the value coming from the sensor

int datoAdc1 =0;

int min1 =0;

void setup() {

// inicilizamos el pin de sensor como una entrada

pinMode(adc1,INPUT);
// declare the ledPin as an OUTPUT:

pinMode(pwm1, OUTPUT);

// declare the ledPin as an OUTPUT:

pinMode(ledPin1, OUTPUT);

analogWrite(pwm1, 22);

delay(500);

analogWrite(pwm1, 23);

delay(500);

analogWrite(pwm1, 24);

delay(500);

analogWrite(pwm1, 35);

delay(500);

analogWrite(pwm1, 26);

delay(500);

analogWrite(pwm1, 34);

delay(1000);

analogWrite(pwm1, 38);

delay(500);

analogWrite(pwm1, 39);

delay(500);

analogWrite(pwm1, 40);

delay(500);

void loop()

// leemos el adc 1

datoAdc1 = analogRead(adc1);

// scalizamos el pwm1 con el minimo 1 y el dato 1

sal1 = map(datoAdc1, 0, 1023, 45, 70);

// los sacmos como salidas cada uno de los pwm por los pines escogidos al principio

analogWrite(pwm1, sal1);
digitalWrite(ledPin1, HIGH);

delay(50);

digitalWrite(ledPin1, LOW);

delay(50);

También podría gustarte