Need Help: Digitally Controlled Triac (Phase Firing Angle Application)

Status
Not open for further replies.

ArdyNT

Full Member level 2
Joined
Nov 6, 2012
Messages
126
Helped
7
Reputation
14
Reaction score
7
Trophy points
1,298
Visit site
Activity points
2,304
I built the following code to control the TRIAC's firing delay.

Code:
 #include <mega8535.h>
 #include <stdio.h>
 #include <delay.h>
 #include <stdint.h>

 unsigned char buf[10];
 volatile unsigned int ldr1;
 volatile unsigned int ldr2;
 volatile unsigned int ldr3;

 unsigned int count = 0;

 volatile unsigned int x = 2;
 volatile unsigned int y = 2;
 volatile unsigned int z = 2;
    
 // Alphanumeric LCD functions
 #include <lcd.h>
 #asm
 .equ __lcd_port=0x15 ;PORTC
 #endasm


 // External Interrupt 0 service routine
 interrupt [EXT_INT0] void ext_int0_isr(void)
 {
    // Start  Timer 0
    TCCR0 = 0x02;
 }

 // Timer 0 overflow interrupt service routine
 interrupt [TIM0_OVF] void timer0_ovf_isr(void)
 {
    // Local variables initialization
    static uint8_t pulse_flag = 0;
    
    // Reinitialize Timer 0 value
    TCNT0=0xD3;
    
    // Increment count every 50 us
    count++; 
    
    // Front lamp
    if(count == x)
        {
            PORTB.0 = 0;
        }
    else
        if(count == x + 1)
            {
                PORTB.0 = 1;
                pulse_flag++;
            }
                
    // Middle lamp        
    if(count == y)
        {
            PORTB.1 = 0;
        }
    else
        if(count == y + 1)
            {
                PORTB.1 = 1;
                pulse_flag++;
            }

    // Back lamp
    if(count == z)
        {
            PORTB.2 = 0;
        }
    else
        if(count == z + 1)
            {
                PORTB.2 = 1;
                pulse_flag++;
            }
     
    // Stop timer when all pulses have completed
    if(pulse_flag == 3)
        {
            // Clear flag
            pulse_flag = 0;
             
            // Stop the timer
            TCCR0 = 0;
                  
            // Clear count
            count = 0; 
        }  
 }


#define ADC_VREF_TYPE 0x40

 // Read the AD conversion result
 unsigned int read_adc(unsigned char adc_input)
 {
    ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);
    
    // Delay needed for the stabilization of the ADC input voltage
    delay_us(10);
    
    // Start the AD conversion
    ADCSRA|=0x40;
    
    // Wait for the AD conversion to complete
    while ((ADCSRA & 0x10)==0);
    ADCSRA|=0x10;
    return ADCW;
 }


 void main(void)
 {
    // Input/Output Ports initialization
    // Port A initialization
    // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
    // State7=P State6=P State5=P State4=P State3=P State2=P State1=P State0=P 
    PORTA=0xFF;
    DDRA=0x00;

    // Port B initialization
    // Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out 
    // State7=1 State6=1 State5=1 State4=1 State3=1 State2=1 State1=1 State0=1 
    PORTB=0xFF;
    DDRB=0xFF;

    // Port C initialization
    // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
    // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
    PORTC=0x00;
    DDRC=0x00;

    // Port D initialization
    // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
    // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
    PORTD=0x00;
    DDRD=0x00;

    // Timer/Counter 0 initialization
    // Clock source: System Clock
    // Clock value: 1500.000 kHz
    // Mode: Normal top=0xFF
    // OC0 output: Disconnected
    TCCR0=0x02;
    TCNT0=0xD3;
    OCR0=0x00;

    TCCR1A=0x00;
    TCCR1B=0x00;
    TCNT1H=0x00;
    TCNT1L=0x00;
    ICR1H=0x00;
    ICR1L=0x00;
    OCR1AH=0x00;
    OCR1AL=0x00;
    OCR1BH=0x00;
    OCR1BL=0x00;

    ASSR=0x00;
    TCCR2=0x00;
    TCNT2=0x00;
    OCR2=0x00;

    // External Interrupt(s) initialization
    // INT0: On
    // INT0 Mode: Falling Edge
    GICR|=0x40;
    MCUCR=0x02;
    MCUCSR=0x00;
    GIFR=0x40;

    // Timer(s)/Counter(s) Interrupt(s) initialization
    TIMSK=0x01;

    // Analog Comparator initialization
    // Analog Comparator: Off
    // Analog Comparator Input Capture by Timer/Counter 1: Off
    ACSR=0x80;
    SFIOR=0x00;

    // ADC initialization
    // ADC Clock frequency: 750.000 kHz
    // ADC Voltage Reference: AVCC pin
    // ADC High Speed Mode: Off
    // ADC Auto Trigger Source: ADC Stopped
    ADMUX=ADC_VREF_TYPE & 0xff;
    ADCSRA=0x84;
    SFIOR&=0xEF;

    // Alphanumeric LCD initialization
    // Connections are specified in the
    // Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu:
    // RS - PORTC Bit 0
    // RD - PORTC Bit 1
    // EN - PORTC Bit 2
    // D4 - PORTC Bit 4
    // D5 - PORTC Bit 5
    // D6 - PORTC Bit 6
    // D7 - PORTC Bit 7
    // Characters/line: 16
    lcd_init(16);

    // Global enable interrupts
    #asm("sei")

    while (1)
      {
        ldr1=read_adc(0);
        
        ldr2=read_adc(1);
        
        ldr3=read_adc(2);

        
        // Shorten the firing delay
        if((PORTB.0 == 1) && (ldr1 <= 450))
        {
            if(x > 1)
                {  
                    x = x - 1;
                }
        }
        
        // Increase the firing delay
        if((PORTB.0 == 1) && (ldr1 >= 600))
        {
            if(x < 280)
                { 
                    x = x + 1;
                }
        }
           
        // Shorten the firing delay
        if((PORTB.1 == 1) && (ldr2 <= 450))
        {
            if(y > 1)
                {
                    y = y - 1;
                }
        }
        
        // Increase the firing delay
        if((PORTB.1 == 1) && (ldr2 >= 600))
        {
            if(y < 280)
                {
                    y = y + 1;
                }
        }
          
        // Shorten the firing delay
        if((PORTB.2 == 1) && (ldr3 <= 450))
        {
            if(z > 1)
                {
                    z = z - 1;
                }
        }
           
        // Increase the firing delay
        if((PORTB.2 == 1) && (ldr3 >= 600))
        {
            if(z < 280)
                {
                    z = z + 1;
                }
        }
      }
 }


This code is working already but somehow it works for a very short period only (maybe less than one minute and then error until I RESTART/plug the power off).

I've been checked it several times but I don't know what is the mistake here???
I've re-built my hardware also, but the problem still the same.

Please help me...
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…