Follow along with the video below to see how to install our site as a web app on your home screen.
Nota: This feature currently requires accessing the site using the built-in Safari browser.
Entonces me sorprende que digas que TRISA = 1 coloca todo el registro en 0b11111111Muchas gracias y disculpe si no me di a entender bien... Okay
Sé bien como usar y conozco el sistema binario
Conozco sus valores de decimal a binario´, así como 2 en decimal es en binario 10
3 es 11
4 es 100... etc.
Yo no tengo dudas.Ahora, no sé si su duda surja por el compilador que uso... Uso el compilador MikroC, lo he estudiado y he aprendido a programar en ese compilador, y principalmente esas instrucciones principales, son las esenciales de aprender para la programación en MikroC.
El código de esa imagen tiene lo siguiente:Ahora pues no me queda más que justificarle mi respuesta con esta captura que realicé de un ejemplo de un programa hecho por la misma empresa que creo el compilador, disculpe pero si esto no es suficiente justificar pues.... Gracias
ANSEL = 0 ; // = 0b00000000 (Todo el registro en 0)
ANSELH = 0; // = 0b00000000 (Todo el registro en 0)
PORTB = 0b01010101; // RB7 = 0, RB6 = 1, RB5 = 0, RB4 = 1, RB3 = 0, RB2 = 1, RB1 = 0, RB0 = 1
TRISB = 0; // = 0b00000000 // Todo el puerto B cómo salidas.
sbit LCD_RS at RD4_bit;
sbit LCD_EN at RD5_bit;
sbit LCD_D4 at RD0_bit;
sbit LCD_D5 at RD1_bit;
sbit LCD_D6 at RD2_bit;
sbit LCD_D7 at RD3_bit;
sbit LCD_RS_Direction at TRISD4_bit;
sbit LCD_EN_Direction at TRISD5_bit;
sbit LCD_D4_Direction at TRISD0_bit;
sbit LCD_D5_Direction at TRISD1_bit;
sbit LCD_D6_Direction at TRISD2_bit;
sbit LCD_D7_Direction at TRISD3_bit;
char txt1[]="Distancia:";
int time_us;
int distance;
char length_cm[4];
void main() {
ADCON1 = 0X06; //confg an as digital
ANSEL = 0x00; // Configure
ANSELH = 0x00; // Configure other AN pins as digital I/O
C1ON_bit = 0; // Deshabilitar comparadores
C2ON_bit = 0;
TRISE.RE0 = 0;
TRISE.RE1 = 1;
INTCON = 0x00; //Disable interrupts
T1CON = 0x00;
Lcd_Init(); // Inicialización del visualizador LCD
Lcd_Cmd(_LCD_CURSOR_OFF); // Comando LCD (apagar el cursor)
Lcd_Cmd(_LCD_CLEAR); // Comando LCD (borrar el LCD)
Lcd_Out(1,2,txt1);
while(1)
{
PORTE.RE0=0;//initialize trig to inactive level
delay_ms(1000);
PORTE.RE0=1; //ping the sonar
delay_us(10); //sending 10us pulse
PORTE.RE0=0;
while (PORTE.RE1 =0); //wait for high state of echo pin
{}
TMR1L = 0; //Clear the TMR1L value
TMR1H = 0 ; //Clear the TMR1H value
T1CON = 0x21; //Enable TMR1
while (PORTE.RE1=1); //While echo received, let the TMR1 count
{}
T1CON=0;
time_us = TMR1H + TMR1L; //Retain TMR1 value = ON pulse
distance = ((time_us/58)*34000/2); //distance in cm
intTostr(distance, length_cm) ;
Lcd_Out(2,3,length_cm); //Write text in 2nd row
Lcd_Out(2,7,"cm"); // Write text in 2nd row
delay_ms(500);
}
}
¿Seguro que estás usando un PIC16F877?el pic16f877 es el que estoy utilizando y estoy programando en MikoC
¿Por qué no tomas como referencia el código del post #34?
Ahí hay un enlace con un programa escrito en MikroC.
¿Seguro que estás usando un PIC16F877?
Porque los registros ANSEL, ANSELH, C1ON y C2ON, no son registros de ese PIC.
sbit LCD_RS at RD4_bit;
sbit LCD_EN at RD5_bit;
sbit LCD_D4 at RD0_bit;
sbit LCD_D5 at RD1_bit;
sbit LCD_D6 at RD2_bit;
sbit LCD_D7 at RD3_bit;
sbit LCD_RS_Direction at TRISD4_bit;
sbit LCD_EN_Direction at TRISD5_bit;
sbit LCD_D4_Direction at TRISD0_bit;
sbit LCD_D5_Direction at TRISD1_bit;
sbit LCD_D6_Direction at TRISD2_bit;
sbit LCD_D7_Direction at TRISD3_bit;
void main()
{
int a;
char txt[7];
OSCCON=0b0110110;
ADCON1 = 0X06; //confg an as digital
ANSEL = 0x00; // Configure
ANSELH = 0x00; // Configure other AN pins as digital I/O
C1ON_bit = 0; // Deshabilitar comparadores
C2ON_bit = 0;
TRISE.RE0 = 0; //Trigger
TRISE.RE1 = 1; //RE1 as Input PIN (ECHO)
Lcd_Init(); // Inicialización del visualizador LCD
Lcd_Cmd(_LCD_CURSOR_OFF); // Comando LCD (apagar el cursor)
Lcd_Cmd(_LCD_CLEAR); // Comando LCD (borrar el LCD)
Lcd_Out(1,1,"Developed By");
Lcd_Out(2,1,"electroSome");
Delay_ms(3000);
Lcd_Cmd(_LCD_CLEAR);
T1CON = 0x10; //Initialize Timer Module
while(1)
{
TMR1H = 0; //Sets the Initial Value of Timer
TMR1L = 0; //Sets the Initial Value of Timer
PORTE.F0 = 1; //TRIGGER HIGH
Delay_us(10); //10uS Delay
PORTE.F0 = 0; //TRIGGER LOW
while(!PORTE.F1); //Waiting for Echo
T1CON.F0 = 1; //Timer Starts
while(PORTE.F1); //Waiting for Echo goes LOW
T1CON.F0 = 0; //Timer Stops
a = (TMR1L | (TMR1H<<8)); //Reads Timer Value
a = a/58.82; //Converts Time to Distance
a = a + 1; //Distance Calibration
if(a>=2 && a<=400) //Check whether the result is valid or not
{
IntToStr(a,txt);
Ltrim(txt);
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,"Distance = ");
Lcd_Out(1,12,txt);
Lcd_Out(1,15,"cm");
}
else
{
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,"Out of Range");
}
Delay_ms(400);
}
}
if (a>7){
Lcd_Cmd(_LCD_CLEAR); // Comando LCD (borrar el LCD)
Lcd_Out(2,1,"Nivel Bajo");
Lcd_Out(1,1,"Distance = ");
Lcd_Out(1,12,txt);
Lcd_Out(1,15,"cm");
UART1_Write_Text("Nivel Bajo");
UART1_Write(0x0D); // ASCII - Retorno de Carro
UART1_Write(0x0A); // ASCII - Avance de Linea
Delay_ms(400);
}
if (a>=4 && a<=7){
Lcd_Cmd(_LCD_CLEAR); // Comando LCD (borrar el LCD)
Lcd_Out(2,1,"Nivel Medio");
Lcd_Out(1,1,"Distance = ");
Lcd_Out(1,12,txt);
Lcd_Out(1,15,"cm");
UART1_Write_Text("Nivel Medio");
UART1_Write(0x0D); // ASCII - Retorno de Carro
UART1_Write(0x0A); // ASCII - Avance de Linea
Delay_ms(400);
}
if (a>=0 && a<4){
Lcd_Cmd(_LCD_CLEAR); // Comando LCD (borrar el LCD)
Lcd_Out(2,1,"Nivel Alto");
Lcd_Out(1,1,"Distance = ");
Lcd_Out(1,12,txt);
Lcd_Out(1,15,"cm");
UART1_Write_Text("Nivel Alto");
UART1_Write(0x0D); // ASCII - Retorno de Carro
UART1_Write(0x0A); // ASCII - Avance de Linea
Delay_ms(400);
}
int a;
char txt[7];
void medir(void);
void main()
{
ADCON1 = 0X06; //confg an as digital
ANSEL = 0x00; // Configure
ANSELH = 0x00; // Configure other AN pins as digital I/O
C1ON_bit = 0; // Deshabilitar comparadores
C2ON_bit = 0;
TRISE = 0b00000010;
PORTE = 0;
Lcd_Init(); // Inicialización del visualizador LCD
Lcd_Cmd(_LCD_CURSOR_OFF); // Comando LCD (apagar el cursor)
Lcd_Cmd(_LCD_CLEAR); // Comando LCD (borrar el LCD)
Lcd_Out(1,1,"Sensando");
UART1_Init(9600); // Iniciar comunicacion Serial
Delay_us(100); // Retardo para estabilizar el ADC
T1CON = 0x10; //Initialize Timer Module
while (1) {
void medir();
if (a>=7){
Lcd_Cmd(_LCD_CLEAR); // Comando LCD (borrar el LCD)
Lcd_Out(2,1,"Nivel Bajo");
Lcd_Out(1,1,"Distance = ");
Lcd_Out(1,12,txt);
Lcd_Out(1,15,"cm");
UART1_Write_Text("Nivel Bajo");
UART1_Write(0x0D); // ASCII - Retorno de Carro
UART1_Write(0x0A); // ASCII - Avance de Linea
Delay_ms(400);
}
if (4<=a<=7){
Lcd_Cmd(_LCD_CLEAR); // Comando LCD (borrar el LCD)
Lcd_Out(2,1,"Nivel Medio");
Lcd_Out(1,1,"Distance = ");
Lcd_Out(1,12,txt);
Lcd_Out(1,15,"cm");
UART1_Write_Text("Nivel Medio");
UART1_Write(0x0D); // ASCII - Retorno de Carro
UART1_Write(0x0A); // ASCII - Avance de Linea
Delay_ms(400);
}
if (0<=a<=4){
Lcd_Cmd(_LCD_CLEAR); // Comando LCD (borrar el LCD)
Lcd_Out(2,1,"Nivel Alto");
Lcd_Out(1,1,"Distance = ");
Lcd_Out(1,12,txt);
Lcd_Out(1,15,"cm");
UART1_Write_Text("Nivel Alto");
UART1_Write(0x0D); // ASCII - Retorno de Carro
UART1_Write(0x0A); // ASCII - Avance de Linea
Delay_ms(400);
}
}
}
void medir(void)
{
TMR1H = 0; //Sets the Initial Value of Timer
TMR1L = 0; //Sets the Initial Value of Timer
PORTE.F0 = 1; //TRIGGER HIGH
Delay_us(10); //10uS Delay
PORTE.F0 = 0; //TRIGGER LOW
while(!PORTE.F1); //Waiting for Echo
T1CON.F0 = 1; //Timer Starts
while(PORTE.F1); //Waiting for Echo goes LOW
T1CON.F0 = 0; //Timer Stops
a = (TMR1L | (TMR1H<<8)); //Reads Timer Value
a = a/58.82; //Converts Time to Distance
a = a + 1; //Distance Calibration
IntToStr(a,txt);
Ltrim(txt);
}
OK. Es lo que te iba a mencionar acerca del "void medir();" dentro del bucle.Ya me dí cuenta del problema, es que estaba declarando mal los void(funciones) y como no me marcaba el error al compilar, no me daba cuenta de mi enorme error.
Se me ocurre que puedes usar la interrupción por cambio de estado en el puerto B. <RB7:RB4>¿Me puedes dar una idea de cómo hacer que cuando presione una tecla en especial del teclado matricial, me active una salida para un pequeño motor?
Necesariamente tengo que utilizar interrupciones. ¿O lo puedo hacer sin utilizarlas?
Lo que quiero hacer, es que cuando presione un botón, se active el motor y se detenga hasta que llegue al nivel deseado.
#include<16F876A.h>
#fuses XT,NOPUT,NOBROWNOUT,NOPROTECT
#use delay(clock=4Mhz)
#include "flex_lcd 4x20.c"
#define trig pin_C0 // Disparo HC-SR04
#define echo pin_C1 // Receptor HC-SR04
#define motor pin_C2 // Pin de Motor
#use fast_io(B)
#use fast_io(C)
int16 toma[5];
float distancia=0;
int16 Longitud(void) {
int16 centimetros=0;
output_high(trig);
delay_us(10);
output_low(trig);
while(!input(echo)){}
while(input(echo)) {
centimetros++;
delay_us(52);
}
delay_ms(50);
return(centimetros);
}
void main( void ) {
set_tris_B(0X00);
set_tris_C(0X02);
output_B(0);
output_C(0);
lcd_init();
lcd_putc('\f');
lcd_gotoxy(1,1);
lcd_putc(" DISTANCIA MEDIDA ");
lcd_gotoxy(1,2);
printf(lcd_putc," MEDIDA: %03.1f cmts ",distancia);
while (true) {
toma[0]= Longitud();
delay_ms(3000);
toma[1]= Longitud();
delay_ms(3000);
toma[2]= Longitud();
delay_ms(3000);
toma[3]= Longitud();
delay_ms(3000);
toma[4]= Longitud();
distancia=(toma[0]+toma[1]+toma[2]+toma[3]+toma[4])/5.0; // instruccion que funciona
// distancia=toma[5]/5.0; // instruccion que no funciona
if(distancia>15) { // Si la distancia es mayor de 10cm
lcd_gotoxy(1,2);
printf(lcd_putc,"%3lu%3lu%3lu%3lu%3lu %3.2f",toma[0],toma[1],toma[2],toma[3],toma[4],distancia);
output_high(motor); // El Motor esta encendido
}
if(distancia<=15) { // si la distancia es menor de 10cm
lcd_gotoxy(1,2);
printf(lcd_putc,"%3lu%3lu%3lu%3lu%3lu %3.2f",toma[0],toma[1],toma[2],toma[3],toma[4],distancia);
output_low(motor); // El Motor esta apagado
}
delay_ms(500);
}
}
Hola, si lo que buscas es la suma de todos los elementos puedes crear una funcion que realice esta accion, la instruccion "distancia=toma[5]/5.0" no es de ningun modo valida, aqui estas dividiendo el elemento 5 del vector entre cinco, elemento que por cierto no existe.Esta es mi pregunta: como debo poner la instruccion para evitar que sea tan larga?
Para eso debes realizar un promedio de las lecturas.lo que quiero es sacar la media de las 5 lecturas.
void medir(void) {
for(i=0;i<5;i++) {
toma=Longitud();
total+=toma;
delay_ms(1500);
}
distancia=total/5.0;
}
void medir(void) {
for(i=0;i<5;i++) {
total+=Longitud();
delay_ms(1500);
}
distancia=total/5.0;
}