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.
Hola,buenas, necesito ayuda para encender un led con un switch programado con mikroc en un pic 18f4550 esto es lo que llevo pero no se por que no funciona, gracias por la colaboración que me pueda ser brindada.
sbit LCD_RS at RC2_bit;
sbit LCD_EN at RC3_bit;
sbit LCD_D4 at RC4_bit;
sbit LCD_D5 at RC5_bit;
sbit LCD_D6 at RC6_bit;
sbit LCD_D7 at RC7_bit;
sbit LCD_RS_Direction at TRISC2_bit;
sbit LCD_EN_Direction at TRISC3_bit;
sbit LCD_D4_Direction at TRISC4_bit;
sbit LCD_D5_Direction at TRISC5_bit;
sbit LCD_D6_Direction at TRISC6_bit;
sbit LCD_D7_Direction at TRISC7_bit;
// end LCD module connections dim as byte tem
int raw_temp;
char *temp = "000.00 C";
void main() {
ADCON1 = 0x07; //Configura puerto A como digitales
PORTB = 0xFF;
INTCON = 0;
TRISB = 0xff;
Lcd_Init(); // Initialize LCD module
Lcd_Cmd(_LCD_CURSOR_OFF); // cursor off
Lcd_Cmd(_LCD_CLEAR); // clear LCD
lcd_out(1, 3, "Temperatura:");
while(1) {
Ow_Reset(&PORTA, 1); // Onewire reset signal
Ow_Write(&PORTA, 1, 0xCC); // Issue command SKIP_ROM
Ow_Write(&PORTA, 1, 0x44); // Issue command CONVERT_T
while(Ow_Read(&PORTA, 1) == 0) ;
Ow_Reset(&PORTA, 1); // Onewire reset signal
Ow_Write(&PORTA, 1, 0xCC); // Issue command SKIP_ROM
Ow_Write(&PORTA, 1, 0xBE); // Issue command READ_SCRATCHPAD
raw_temp = Ow_Read(&PORTA, 1); // Read temperature LSB byte
raw_temp |= (Ow_Read(&PORTA, 1) << 8); // Read temperature MSB byte
if(raw_temp & 0x8000) { // If the temperature is negative
temp[0] = '-'; // Put minus sign (-)
raw_temp = ~raw_temp + 1; // Change temperature value to positive form
}
else {
if((raw_temp >> 4) >= 100) // If the temperatue >= 100 °C
temp[0] = '1'; // Put 1 of hundreds
else // otherwise
temp[0] = ' '; // put space ' '
}
// Put the first two digits ( for tens and ones)
temp[1] = ( (raw_temp >> 4) / 10 ) % 10 + 48; // Put tens digit
temp[2] = (raw_temp >> 4) % 10 + 48; // Put ones digit
// Put the 4 fraction digits (digits after the point)
// Why 625: because we're working with 12-bit resolution
temp[4] = ( (raw_temp & 0x0F) * 625) / 1000 + 48; // Put thousands digit
temp[5] = (((raw_temp & 0x0F) * 625) / 100 ) % 10 + 48; // Put hundreds digit
temp[6] = 223; // Put degree symbol ( ° )
lcd_out(2, 4, temp); // Display temperature
delay_ms(1000); // Wait 1 second
}
if(raw_temp>40) // cuando llegue a mas de 40
{
PORTB.rb0 = 0; // se activa portb .0
}
if(raw_temp<40) // cuando es menor de 40
{
PORTB.rb0 = 1; // se apaga portb .0
}
}
// End of code
// Lcd pinout settings
sbit LCD_RS at RC2_bit;
sbit LCD_EN at RC3_bit;
sbit LCD_D7 at RC7_bit;
sbit LCD_D6 at RC6_bit;
sbit LCD_D5 at RC5_bit;
sbit LCD_D4 at RC4_bit;
sbit led at RB0_bit;
sbit mas at RB1_bit;
sbit menos at RB2_bit;
sbit men at RB3_bit;
// Pin direction
sbit LCD_RS_Direction at TRISC2_bit;
sbit LCD_EN_Direction at TRISC3_bit;
sbit LCD_D7_Direction at TRISC7_bit;
sbit LCD_D6_Direction at TRISC6_bit;
sbit LCD_D5_Direction at TRISC5_bit;
sbit LCD_D4_Direction at TRISC4_bit;
sbit led_dir at TRISB0_bit;
char tem1;
int raw_temp;
char *temp = "000.00 C";
void main() {
ADCON1 = 0x07; //Configura puerto A como digitales
led_dir = 0;
Lcd_Init(); // Initialize LCD module
Lcd_Cmd(_LCD_CURSOR_OFF); // cursor off
Lcd_Cmd(_LCD_CLEAR); // clear LCD
lcd_out(1, 3, "TEMPERATURA");
led = 0;
tem1 = 35;
men = 0;
mas = 0;
menos = 0;
while(1) {
Ow_Reset(&PORTA, 1); // Onewire reset signal
Ow_Write(&PORTA, 1, 0xCC); // Issue command SKIP_ROM
Ow_Write(&PORTA, 1, 0x44); // Issue command CONVERT_T
while(Ow_Read(&PORTA, 1) == 0) ;
Ow_Reset(&PORTA, 1); // Onewire reset signal
Ow_Write(&PORTA, 1, 0xCC); // Issue command SKIP_ROM
Ow_Write(&PORTA, 1, 0xBE); // Issue command READ_SCRATCHPAD
raw_temp = Ow_Read(&PORTA, 1); // Read temperature LSB byte
raw_temp |= (Ow_Read(&PORTA, 1) << 8); // Read temperature MSB byte
if(raw_temp & 0x8000) { // If the temperature is negative
temp[0] = '-'; // Put minus sign (-)
raw_temp = ~raw_temp + 1; // Change temperature value to positive form
}else {
if((raw_temp >> 4) <= tem1){
Lcd_Cmd(_LCD_CLEAR); // clear LCD
lcd_out(1, 3, tem1);
led = 1;
}else {
Lcd_Cmd(_LCD_CURSOR_OFF); // cursor off
Lcd_Cmd(_LCD_CLEAR); // clear LCD
lcd_out(1, 3, "Tem Estable:");
led = 0;
}
if((raw_temp >> 4) >= 100) // If the temperatue >= 100 °C
temp[0] = '1'; // Put 1 of hundreds
else // otherwise
temp[0] = ' '; // put space ' '
}
// Put the first two digits ( for tens and ones)
temp[1] = ( (raw_temp >> 4) / 10 ) % 10 + 48; // Put tens digit
temp[2] = (raw_temp >> 4) % 10 + 48; // Put ones digit
// Put the 4 fraction digits (digits after the point)
// Why 625: because we're working with 12-bit resolution
temp[4] = ( (raw_temp & 0x0F) * 625) / 1000 + 48; // Put thousands digit
temp[5] = (((raw_temp & 0x0F) * 625) / 100 ) % 10 + 48; // Put hundreds digit
temp[6] = 223; // Put degree symbol ( ° )
lcd_out(2, 4, temp); // Display temperature
delay_ms(500);
}
}
// Lcd pinout setting FUNCIONA SIN ERRORES EN PROTEUS funcionando al 100 en proto con sensor
#include "floattostr_.h"
sbit CALEFACTOR at RB0_bit;
int tem1;
char Text[10]; //Cadena de caracteres para impresión de datos.
char fracc;
char hume;
char texto[30];
unsigned int HUM1;
unsigned char Check, T_byte1, T_byte2, RH_byte1, RH_byte2, Ch ;
unsigned Temp, RH, Sum ;
unsigned be, me,ne, Sum2,h1,h2 ;
//////////////////////////////
void Uart1_write_text_const(const char *info)
{
while(*info) UART1_Write(*info++);
}
//////////////////////////////
void StartSignal(){
TRISA.F0 = 0; //Configure RD0 as output
PORTA.F0 = 0; //RD0 sends 0 to the sensor
delay_ms(18);
PORTA.F0 = 1; //RD0 sends 1 to the sensor
delay_us(30);
TRISA.F0 = 1; //Configure RD0 as input
}
//////////////////////////////
void CheckResponse(){
Check = 0;
delay_us(40);
if (PORTA.F0 == 0){
delay_us(80);
if (PORTA.F0 == 1) Check = 1; delay_us(40);}
}
//////////////////////////////
char ReadData(){
char i, j;
for(j = 0; j < 8; j++){
while(!PORTA.F0); //Wait until PORTD.F0 goes HIGH
delay_us(30);
if(PORTA.F0 == 0)
i&= ~(1<<(7 - j)); //Clear bit (7-b)
else {i|= (1 << (7 - j)); //Set bit (7-b)
while(PORTA.F0);} //Wait until PORTD.F0 goes LOW
}
return i;
}
//////////////////////////////
void main() {
UART1_Init(9600);
Uart1_write_text_const("INICIANDO\r\n");
TEM1=350;
ADCON1 = 0x07; //Configura puerto A como digitales
while(1){
StartSignal();
CheckResponse();
if(Check == 1){
RH_byte1 = ReadData();
RH_byte2 = ReadData();
T_byte1 = ReadData();
T_byte2 = ReadData();
Sum = ReadData();
if(Sum == ((RH_byte1+RH_byte2+T_byte1+T_byte2) & 0XFF)){
Temp = T_byte1;
Temp = (Temp << 8) | T_byte2;
RH = RH_byte1;
RH = (RH << 8) | RH_byte2;
delay_ms(500);
sprinti(texto,"HUMEDAD=%u \r\n",rh);
UART1_Write_Text(texto);
sprinti(texto,"Temperatura=%u \r\n",TEMP);
UART1_Write_Text(texto);
delay_ms(500);
} else {
CALEFACTOR=0;
}
}
}
#define pwm PORTC.F2
char txt[6]=" ";
unsigned char kp;
int i=0;
int ciclo=0;
// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections
char keypadPort at PORTD;
char teclado(){
kp = 0;
do{
kp = Keypad_Key_Click();
}
while (kp==0);
switch(kp){
case 1: kp = 49; break; // 1
case 2: kp = 50; break; // 2
case 3: kp = 51; break; // 3
case 4: kp = 65; break; // A
case 5: kp = 52; break; // 4
case 6: kp = 53; break; // 5
case 7: kp = 54; break; // 6
case 8: kp = 66; break; // B
case 9: kp = 55; break; // 7
case 10: kp = 56; break; // 8
case 11: kp = 57; break; // 9
case 12: kp = 67; break; // C
case 13: kp = 42; break; // *
case 14: kp = 48; break; // 0
case 15: kp = 35; break; // #
case 16: kp = 68; break; // D
}
return kp;
}
void main(){
ANSEL=0;
ANSELH=0;
c1on_bit=0;
c1on_bit=0;
TRISA=0X00;
PORTA=0X00;
TRISB=0X00;
PORTB=0X00;
TRISC=0X00;
PORTC=0X00;
TRISD=0X00;
PORTD=0X00;
TRISE=0X00;
PORTE=0X00;
Lcd_init();
Lcd_cmd(_LCD_CLEAR);
Lcd_cmd(_LCD_CURSOR_OFF);
Keypad_Init();
Lcd_out(1,1,"Valor: ");
while(1){
txt=teclado();
Lcd_chr_cp(txt);
i++;
PWM1_Set_Duty(ciclo);
if(i==3){
delay_ms(500);
Lcd_cmd(1);
Lcd_Out(1, 1, "Ciclo de trabajo:");
Lcd_Out(2, 1, txt);
Lcd_Out(2, 4, "%");
}
}
}
Adjunto programa modificado para que pruebesBuen día.
Soy principiante en programación con MikroC, tengo que controlar la velocidad de un motor de 12V cambiando el ciclo de trabajo ingresando el ciclo de 0 a 255 por teclado matricial, como el que está en el diagrama de Proteus.
Estoy utilizando un PIC16F887.
El porcentaje correspondiente ingresado debe aparecer en el LCD y por consiguiente variar la velocidad del motor.
Lo que me falta lograr es hacer que dicha cantidad ingresada, se transforme a porcentaje y asignarla a la variable de ciclo de trabajo de PWM. Estoy utilizando las librerías de Keypad, PWM y LCD que vienen en MikroC.
Por ejemplo: (dutycycle/100)*255, pero no sé cómo implementar la fórmula dentro del código.
¿Me pueden ayudar a checarlo y decirme cómo corregirlo? Gracias de antemano.
Aquí esta mi código:
Y adjunto el diagrama en Proteus 8.9C:#define pwm PORTC.F2 char txt[6]=" "; unsigned char kp; int i=0; int ciclo=0; // LCD module connections sbit LCD_RS at RB4_bit; sbit LCD_EN at RB5_bit; sbit LCD_D4 at RB0_bit; sbit LCD_D5 at RB1_bit; sbit LCD_D6 at RB2_bit; sbit LCD_D7 at RB3_bit; sbit LCD_RS_Direction at TRISB4_bit; sbit LCD_EN_Direction at TRISB5_bit; sbit LCD_D4_Direction at TRISB0_bit; sbit LCD_D5_Direction at TRISB1_bit; sbit LCD_D6_Direction at TRISB2_bit; sbit LCD_D7_Direction at TRISB3_bit; // End LCD module connections char keypadPort at PORTD; char teclado(){ kp = 0; do{ kp = Keypad_Key_Click(); } while (kp==0); switch(kp){ case 1: kp = 49; break; // 1 case 2: kp = 50; break; // 2 case 3: kp = 51; break; // 3 case 4: kp = 65; break; // A case 5: kp = 52; break; // 4 case 6: kp = 53; break; // 5 case 7: kp = 54; break; // 6 case 8: kp = 66; break; // B case 9: kp = 55; break; // 7 case 10: kp = 56; break; // 8 case 11: kp = 57; break; // 9 case 12: kp = 67; break; // C case 13: kp = 42; break; // * case 14: kp = 48; break; // 0 case 15: kp = 35; break; // # case 16: kp = 68; break; // D } return kp; } void main(){ ANSEL=0; ANSELH=0; c1on_bit=0; c1on_bit=0; TRISA=0X00; PORTA=0X00; TRISB=0X00; PORTB=0X00; TRISC=0X00; PORTC=0X00; TRISD=0X00; PORTD=0X00; TRISE=0X00; PORTE=0X00; Lcd_init(); Lcd_cmd(_LCD_CLEAR); Lcd_cmd(_LCD_CURSOR_OFF); Keypad_Init(); Lcd_out(1,1,"Valor: "); while(1){ txt=teclado(); Lcd_chr_cp(txt); i++; PWM1_Set_Duty(ciclo); if(i==3){ delay_ms(500); Lcd_cmd(1); Lcd_Out(1, 1, "Ciclo de trabajo:"); Lcd_Out(2, 1, txt); Lcd_Out(2, 4, "%"); } } }