Ir ao conteúdo
  • Cadastre-se

Ajuda com programacao PIC C


alexura

Posts recomendados

Estou fazendo um programa para controle de nivel e temperatura, utilizando o PIC16F873A.

Basicamente é o seguinte, para medir nivel utilizo um potenciometro, quando esta no nivel 0ml = 1Volt e nivel 2000ml = 5Volts.

Para temperatura utilizo um LM35 onde a temperatura minima é 30ºC e

maxima 60ºC

O projeto possui dois tanques, sendo, o primeiro de aproximadamente 5L, e o segundo, de aproximadamente 3L.

O tanque de 5L irá armazenar água fria e vai possuir uma bomba D' água, já o

segundo irá possuir uma resistencia (ebulidor) para elevar a temperatura da água, o LM35, petenciometro e uma bomba D'agua

O tanque de 5L é apenas para fazer a troca de calor e para mandar agua ou receber agua do outro tanque

Agora vamos a programacao :P

Eu tenho 3 botões

Menu -> para alterna os sub menus ou voltar a tela inicial

+ -> para ajustar meu set point e entrar nos sub menus

- -> para ajustar set point

na tela inicial vai mostrar a temperatura atual e o nivel atual ( no caso 0ml e 30ºC)

quando apertar o botao menu ele vai para o sub menu Nivel onde iremos ajustar o nivel desejado

nesta tela ira mostrar Set point e Nivel atual ( caso set point maior que nivel atual vai ligar a bomba ate chegar nivel atual)

voltano a tela inicial vamos ajustar a temperatura mesma coisa do nivel, vai mostrar temperatura atual e vai ajustar ao set

pensano em segurança o projeto tem caso de emergencia, se o nivel subir muito a bomba vai jogar o excesso de agua no tanque de 5L e se ela for menor que o 0ml ele vai encher

e tambem possui leds para tudo, bombas ligada, resistencia ligada, nivel critico, temperatura critica

vixeee fico tenso eu falando esse tanto, vou deixar as fotos para entender melhor...

ate o momento eu so consegui faze um pedaco do menu, ele nao esta entrando nos sub menus

#include "projeto.h"
#include "LCD.C"

int16 adc_nivel, adc_temp, nivel, temp, spt, menu;
int1 flag=1;

#int_RTCC
void RTCC_isr(void)
{



}



void main()
{

setup_adc_ports(AN0_AN1_AN3);
setup_adc(ADC_CLOCK_DIV_16);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_4);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DIV_BY_16,255,1);
setup_ccp1(CCP_PWM);
setup_ccp2(CCP_PWM);
set_pwm1_duty(0);
set_pwm2_duty(0);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
lcd_init();
enable_interrupts(INT_RTCC);
enable_interrupts(GLOBAL);

while(true){

set_adc_channel(0);
delay_ms(15);
adc_temp = read_adc();
temp = adc_temp * 0.4918; // 60 / 122 = 0.4918 // 1023*0,6 / 5 = 122 (0,6 = 60 (temperatura maxima)



set_adc_channel(1);
delay_ms(15);
adc_nivel = read_adc();
nivel = adc_nivel * 3,491620 ; // 2500 / 716 = 3,4916 // 1023 * 3,5 / 5 = 716 (3,5 tensão medida maxima )


if(flag==1){
lcd_gotoxy(1,1);
printf(lcd_putc, "TEMPERATURA %2lu C ",temp);

lcd_gotoxy(1,2);
printf(lcd_putc, "NIVEL %4lu ml ",nivel);

}

if(input(pin_b3)){
delay_ms(300);
flag=0;
menu++;

}

switch(menu){

case 1:
lcd_gotoxy(1,1);
printf(lcd_putc, "TEMPERATURA ");

lcd_gotoxy(1,2);
printf(lcd_putc, " ");

break;

case 2:
lcd_gotoxy(1,1);
printf(lcd_putc, "NIVEL ");

lcd_gotoxy(1,2);
printf(lcd_putc, " ");

break;

case 3:
menu=0;

case 4:
flag=1;



}
}
}

Sem-t--tulo.png.html

Link para o comentário
Compartilhar em outros sites

obrigado a ajuda e atencao, mas foi resolvido o problema, pra quem quiser da uma olhada vou deixar o codigo abaixo e comentado valeu :)

#include "Projeto.h"
#include "LCD.C"

int menu=1, set_temp=30, temp;
int1 flag1;
int16 set_nivel=2000, nivel, adc_temp, adc_nivel, erro;
float pwm;

void main()
{

setup_adc_ports(AN0_AN1_AN3);
setup_adc(ADC_CLOCK_DIV_16);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DIV_BY_16,255,1);
setup_ccp1(CCP_PWM);
setup_ccp2(CCP_PWM);
set_pwm1_duty(0);
set_pwm2_duty(0);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
lcd_init();

// TODO: USER CODE!!

while(true){

set_adc_channel(0); //TEMPERATURA
delay_ms(15);
adc_temp = read_adc();
temp = adc_temp * 0.4918; // 60 / 122 = 0.4918 // 1023*0,6 / 5 = 122 (0,6 = 60 (TEMPERATURA MAXIMA)

set_adc_channel(1); //nivel
delay_ms(15);
adc_nivel = read_adc();
nivel = adc_nivel * 13.671567923767337257073327454559; // 4000 / 292,578 = ???? // 1023 * 1.43 / 5 = 292,578 ??? = Tensão max lida pelo pot

nivel=nivel/25;
nivel=nivel*25;

if((flag1)==0){
if(input(pin_b3)==1){
delay_ms(200);
menu++;
if((menu)==4){
menu=1;
}
}
}
switch(menu){

case(1):
lcd_gotoxy(1,1);
printf (lcd_putc,"NIVEL ATUAL %4lu ",nivel); // MOSTRAR NIVEL
lcd_gotoxy(1,2);
printf(lcd_putc,"TEMP ATUAL %4u ", temp); // MOSTRAR TEMPERATURA
break;

case(2):
if(input(pin_a4)==1 && flag1==0){
delay_ms(200);
flag1=1; // BOTÃO DE SELECIONAR OPÇÃO (+)
}
if(input(pin_b3)==1 && flag1==1){
delay_ms(200);
flag1=0; // BOTÃO DE SAIR DA OPÇÃO (MENU)
}

if((flag1)==0){ // MENU MOSTRADO QUANDO N SELECIONADO
lcd_gotoxy(1,1);
printf(lcd_putc,"AJUSTAR NIVEL? "); // MENU DE AJUSTAR NIVEL(PERGUNTA)
lcd_gotoxy(1,2);
printf(lcd_putc,"NIVEL ATUAL %4lu ",nivel);// VISUALIZAÇÃO DO NIVEL ATUAL
}
if((flag1)==1){
lcd_gotoxy(1,1);
printf(lcd_putc,"NIVEL: %4lu ",nivel);// VISUALIZAÇÃO DO NIVEL ATUAL
lcd_gotoxy(1,2);
printf(lcd_putc,"SET-NIVEL: %4lu ",set_nivel);// VISUALIZAÇÃO DO NIVEL DESEJADO

if(input(pin_a4)==1 && set_nivel < 4000){ // AUMENTAR VOLUME DE 100 EM 100ML
delay_ms(200);
set_nivel=set_nivel+100;
}
if(input(pin_a5)==1 &&set_nivel > 2000){ // DECREMENTAR VOLUME DE 100 EM 100ML
delay_ms(200);
set_nivel=set_nivel-100;
}


}
break;

case(3):
if(input(pin_a4)==1 && flag1==0){ // BOTÃO DE SELECIONAR OPÇÃO (+)
delay_ms(200);
flag1=1;
}
if(input(pin_b3)==1 && flag1==1){ // BOTÃO DE SAIR DA OPÇÃO (MENU)
delay_ms(200);
flag1=0;
}

if((flag1)==0){ // MENU MOSTRADO QUANDO N SELECIONADO
lcd_gotoxy(1,1);
printf(lcd_putc,"AJUSTAR TEMP? "); // MENU DE AJUSTAR TEMPERATURA(PERGUNTA)
lcd_gotoxy(1,2);
printf(lcd_putc,"TEMP ATUAL %4u ",temp);// VIZUALIZAÇÃO DA TEMPERATURA ATUAL
}
if((flag1)==1){
lcd_gotoxy(1,1);
printf(lcd_putc,"TEMPERATURA:%2u ",temp);// VIZUALIZAÇÃO DO TEMPERATURA ATUAL
lcd_gotoxy(1,2);
printf(lcd_putc,"SET-TEMP:%2u ",set_temp);// VISUALIZAÇÃO DA TEMPERATURA DESEJADA

if(input(pin_a4)==1 && set_temp < 60){ // AUMENTAR TEMPERATURA
delay_ms(200);
set_temp++;
}
if(input(pin_a5)==1 && set_temp > 30){ // DECREMENTAR TEMPERATURA
delay_ms(200);
set_temp--;
}

}
break;
}

if(nivel>4000){ // LIGA LED NIVEL CRITICO
output_high(pin_c6);
}

if(nivel<=4000){ //DESLIGA LED
output_low(pin_c6);
}

if(temp>60){ //LIGA LED TEMPERATURA CRITICA
output_high(pin_c7);
}

if(temp<=60){ //DESLIGA LED
output_low(pin_c7);
}

if(set_nivel == nivel){
output_low(pin_c1);
output_low(pin_c2);
}

if(set_temp < temp ){ // ESFRIAR AGUA, DESLIGA RES, E LIGA AS 2 BOMBAS E OS 2 LEDS DAS BOMBAS
output_low(pin_c0);
output_low(pin_c5);
output_high(pin_c1);
output_high(pin_c2);
output_high(pin_c3);
output_high(pin_c4);
}
if(set_temp > temp ){ //ESQUENTAR AGUA, LIGA RES E LED RES, E DESLIGA AS 2BOMBAS E SEUS RESPECTIVOS LEDS
output_high(pin_c0);
output_high(pin_c5);
output_low(pin_c1);
output_low(pin_c2);
output_low(pin_c3);
output_low(pin_c4);
}
if(set_temp == temp ){ //TEMPERATURA IGUAL A DESEJADA, DESLIGAR TUDO REFERENTE A RES
output_low(pin_c0);
output_low(pin_c5);
output_low(pin_c1);
output_low(pin_c2);
output_low(pin_c3);
output_low(pin_c4);
}

if(set_nivel < nivel){
erro=(nivel - set_nivel);
pwm= (erro * 0.5115);
set_pwm1_duty(pwm); // 0,5115 = (1023/2000) encher (2000 = range nivel) "ESVAZIAR"
output_high(pin_c3);
}

if(set_nivel == nivel){
set_pwm1_duty(0);
erro=0;
}


if(set_nivel > nivel){
erro=(set_nivel - nivel);
pwm = (erro * 0.5115);
set_pwm2_duty(pwm); // 0.5115 = (1023/2000) esvasiar (2000 = range nivel) "ENCHER"
output_high(pin_c4);
}

if(nivel == set_nivel){
set_pwm2_duty(0);
erro=0;
}

}
}

Link para o comentário
Compartilhar em outros sites

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...

 

GRÁTIS: ebook Redes Wi-Fi – 2ª Edição

EBOOK GRÁTIS!

CLIQUE AQUI E BAIXE AGORA MESMO!