View previous topic :: View next topic |
Author |
Message |
mcataldo
Joined: 04 Jul 2010 Posts: 11 Location: Santiago, Chile
|
Zero crossing detector problem |
Posted: Sun Jul 04, 2010 2:43 pm |
|
|
Hello everybody, i'm new here and i need help, please. I'm trying to do a temperature control system for a water heater through power control with triac.
My problem is i need generate a control signal from PIC to indicate triac when open and close, but PIC doesn't it.
To design code i based on Microchip apps document using directly mains voltage connected to RA0 and using external interruption, but it doesn't work.
Some data are:
Voltage amplitude: 12 Vrms.
Voltage frequency: 50 Hz.
Temperatures range amplitude: 4
I attach source code. Thank you very much.
Code: |
#include <16F877A.h>
#device ADC=10
#fuses XT,NOWDT,NOPROTECT,PUT,BROWNOUT
#use delay(clock=4000000)
#priority rb,ext,timer1,timer0
int16 temp_analogo_f;
int temp_f;
int temp_min;
int temp_max;
int temp_sp;
int temp_sp_ee;
int d_temp_f;
int retardo = 10;
int contador = 0;
short flanco = 0;
short sp = 0;
#INT_TIMER0
void control_flanco()
{
contador++;
if(contador == retardo)
{
output_high(pin_C3);
disable_interrupts(INT_TIMER1);
contador = 0;
if(flanco == 0)
{
ext_int_edge(H_TO_L);
flanco = 1;
}
else
{
ext_int_edge(L_TO_H);
flanco = 0;
}
}
}
// El timer 0 es de 8 bits, eso significa 0 a 255
#INT_TIMER1
void lectura_temperatura()
{
// Agua fría
set_adc_channel(0);
temp_analogo_f=(read_adc()-559L)/2; //10 bits
temp_f=(int)temp_analogo_f; //conversion a °C (LM335) se mejora con Vref A/D
temp_min = temp_f+1;
temp_max = temp_f+4;
}
#INT_EXT
void control()
{
output_low(pin_C3);
enable_interrupts(INT_TIMER1);
set_timer1(6);
}
#INT_RB
void cambio_temperatura()
{
if(!input(pin_B4))
{
if(temp_sp < temp_max)
{
temp_sp++;
sp = 1;
}
}
if(!input(pin_B5))
{
if(temp_sp > temp_min)
{
temp_sp--;
sp = 1;
}
}
}
void main()
{
set_tris_a(0b00111111);
set_tris_b(0xFF);
set_tris_c(0x00);
setup_adc(ADC_CLOCK_DIV_32);
setup_adc_ports(ALL_ANALOG);
// setup_spi(FALSE);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);
setup_timer_1(T1_DIV_BY_1);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
// setup_vref(VREF_LOW|-2);
ext_int_edge(L_TO_H);
enable_interrupts(GLOBAL);
enable_interrupts(INT_EXT);
enable_interrupts(INT_RB);
enable_interrupts(INT_TIMER0);
enable_interrupts(INT_TIMER1);
output_high(pin_C3);
// Agua fría
set_adc_channel(0);
temp_analogo_f=(read_adc()-559L)/2; //10 bits
temp_f=(int)temp_analogo_f; //conversion a °C (LM335) se mejora con Vref A/D
temp_sp_ee = read_eeprom(0);
if(temp_sp_ee != 255)
{
temp_sp = temp_sp_ee;
}
else
{
temp_sp = 15;
}
set_timer0(6);
set_timer1(0);
do
{
d_temp_f = temp_sp - temp_f;
// hay que ponerle un cálculo acá para decirle al contador cuantas veces enteras y "decimales" debe contar
switch(d_temp_f)
{
case 1:retardo = 30;
break;
case 2:retardo = 20;
break;
case 3:retardo = 10;
break;
default:retardo = 0;
break;
}
if(sp == 1)
{
sp = 0;
write_eeprom(0,temp_sp);
}
}
while(true);
} |
Last edited by mcataldo on Sun Jul 04, 2010 5:18 pm; edited 1 time in total |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Sun Jul 04, 2010 6:00 pm |
|
|
Quote: |
To design code i based on Microchip apps document using directly mains voltage connected to RA0 and
using external interruption, but it doesn't work.
|
Comments:
1) RA0 with EXT Interrupt ???
2) You are doing an AD conversion inside an interrupt.
3) To handle temperatures, you do not need to do multiple AD conversion per second.
4) It is impossible to run all these stuff with FOUR interrupts activated, at least in this way.
Humberto |
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1634 Location: Perth, Australia
|
Re: Zero crossing detector problem |
Posted: Mon Jul 05, 2010 2:19 am |
|
|
mcataldo wrote: |
Some data are:
Voltage amplitude: 12 Vrms. |
If you are putting 12V RMS into the PIC and haven't yet killed the PIC (give it time) then you may have probably put the PIC in latch-up mode. _________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
|
mcataldo
Joined: 04 Jul 2010 Posts: 11 Location: Santiago, Chile
|
|
Posted: Mon Jul 05, 2010 4:18 am |
|
|
Following Microchip app directions, i've placed a 500M resistor to limit current. |
|
|
bungee-
Joined: 27 Jun 2007 Posts: 206
|
|
Posted: Mon Jul 05, 2010 2:57 pm |
|
|
Why don't you use opto-coupled triac driver with zero cross detection like MOC3061.
All problems with cross detection are solved and you have only to deal with the logic part of problem. |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Mon Jul 05, 2010 4:50 pm |
|
|
mcataldo wrote: | Following Microchip app directions, i've placed a 500M resistor to limit current. |
If you only have 12VAC then 500M is awfully big. AN521 suggests 5 Meg for 110VAC RMS to get 32uA peak. For 12VAC RMS you would need a resistor of about 500k. It a lot cheaper than an opto isolator if you have your grounds right. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
mcataldo
Joined: 04 Jul 2010 Posts: 11 Location: Santiago, Chile
|
|
Posted: Mon Jul 05, 2010 4:59 pm |
|
|
I'm sorry, I mistook, i used a 500k resistor. Regarding opto-couple, i didn't think it before, but unfortunately it's not long until i have show project to professor.
Thanks |
|
|
|