Problems in understanding PIC TRIAC dimmer code

RAHAMANSA's picture

Offline

Joined: Sep 15, 2017

Replies: 9
Problems in understanding PIC TRIAC dimmer code
October 6, 2018 - 6:26pm

Hi,

http://ww1.microchip.com/downloads/en/AppNotes/40171a.pdf

As I said earlier in here I am trying to develop complete ckt with the link above with MCU 12f675 to control ON /OFF and then DIMMER with the ckt similar to link.---

I will be using that 2 button for ON/OFF and 600E SENSITIVE TRIAC with the modification of code written in the sheet.

For that  Please let me know, 1) how do I calculate timer value with  frequency 50hz to enter at main after configured of timer at 8BIT/ PS64.

2) why 'Count' is monitored upto 60 in for loop.

3) what is actually happening in the code with the variable used  'TestCount, TestCheck, DelayCount, OutCount .

Please suggest sir..

Tnx..

 

Hiro_Hamada's picture

Offline

Joined: Apr 17, 2018

Replies: 82
PIC012F675 Dimmer
October 8, 2018 - 2:13pm

It took a lot of time to clear up your code from PDF it was all messy with assembly instructions in between. If you are interested in it get it from here https://files.fm/u/mw7tbufp

Actually the logic behind the code is very simple it is made to look complicated since the author has also added test mode in his code. To put it simple you have to monitor the GP4 pin as input to detect the zero cross. This pin will go high and then go low during one full AC cycle. Once you have detected the zero crossing (Author calls it syncing) based on the required brightness (value stored in PercentOn variable) create delay and then fire the TRIAC. This delay is created using Timer0. How long is the delay, has to be is decided by the value in PercentOn variable which can be adjusted by pressing the push buttons.

1) how do I calculate timer value with  frequency 50hz to enter at main after configured of timer at 8BIT/ PS64.

There is nothing like calculating the Timer value for 50Hz, the same program will also work for 50Hz and 60HZ. It is just that the code is not yet tested with 50Hz. The timer configured is just be used to create a delay based in the value in PercentOn varibable and then fire the TRIAC.

why 'Count' is monitored upto 60 in for loop.

As soon as the program is booted it is made to wait for 1sec. The author is working with 60Hz frequency so by making the program to wait for on GP4 to get high and low for 60 times the program is held there for 1sec. This is done only once before the control reaches the infinite loop. This part of code is just a safety measure and is optional. 

what is actually happening in the code with the variable used  'TestCount, TestCheck, DelayCount, OutCount .

The answer for this given at page 5 of your datasheet. The author has a test mode in his code, by pressing both the pushbuttons together the code will enter into test mode and The test will run for 255 cycles or until the DIM button is pressed. The test runs in a cycle of brightening to full bright, dimming to full dim and then flashing full bright twice the variables are used just to execute these auto dimming feature 

Hope this helps!, Hiro Out

RAHAMANSA's picture

Offline

Joined: Sep 15, 2017

Replies: 9
Very very thanks Hiro Sir.
October 10, 2018 - 6:24pm

Very very thanks Hiro Sir.

I stuck in the infinite loop and  could not understand how do i overcome from there. My motive is simple to use of 3 switch, one OnOff and remaing 2 DIM and BRT. When the o/p is ON by pressing the OnOff switch then only be DIM/BRT to be work for BRT/DIM controlling. For that I have written c code as pasted which is incompleted in WHILE(1) Section and request to help me for the remaining part for the code.

/* DIRECT CONTROL TRIAC WITH push to ON and DIMING and push to OFF control WITH 12F675 MCU//
 * File:   675 TRIAC.c
 
 */

// PIC12F675 Configuration Bit Settings

// 'C' source line config statements

// CONFIG
#pragma config FOSC = INTRCIO   // Oscillator Selection bits (INTOSC oscillator: I/O function on GP4/OSC2/CLKOUT pin, I/O function on GP5/OSC1/CLKIN)
#pragma config WDTE = ON        // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = ON      // Power-Up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF      // GP3/MCLR pin function select (GP3/MCLR pin function is digital I/O, MCLR internally tied to VDD)
#pragma config BOREN = ON      // Brown-out Detect Enable bit (BOD disabled)
#pragma config CP = OFF         // Code Protection bit (Program Memory code protection is disabled)
#pragma config CPD = OFF        // Data Code Protection bit (Data memory code protection is disabled)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>
#define _XTAL_FREQ 4000000 // 4MHz This is the speed the controller is running at
        
//********************************************************/
#define BRTbut      GP0 //Brighten button/will work after PUSH-ON by GP3
#define DIMbut      GP1 //Dim button/will work after PUSH-ON by GP3
#define Output      GP2 //Output to TRIAC
#define OnOff       GP3 //Push to ON and Push to OFF
#define LineInput   GP4 //AC line zero crossing sense

unsigned int PercentOn;   
unsigned int Count;

void InitIO ()
{
   CMCON = 0x07;         // disable comparator//SHUT OFF COMPARATOR
   ANSEL = 0x00;         // disble analog functions//SET PORT AS DIGITAL I/O
   ADCON0 = 0x00;        //SHUT OFF THE A/D CONVERTER
   VRCON = 0x00;         //SHUT OFF THE VOLTAGE REFERENCE
   TRISIO = 0b00010111;  // 000-ZC-0-TRIAC-DIMbut-BRTbut //
         
}

void Timer0 ()
{
    PS0 = 1;
    PS1 = 0;
    PS2 = 1;   //101 = 1:64/ tmr0 prescaler 
    PSA = 0;   //0= PS assign to the tmr0 module//1= assign to WDT
    T0CS = 0;  // TMR0 Clock Source from Internal instruction cycle clock (CLKOUT)   
}

void main()
{
PercentOn   = 128; //OnOff GP3 toggle, if off after pressing GP3, it will come up with 50% brightness.
Count = 0;                     //General counter

 

for(Count=0; Count < 50; Count++) //Allow power supply to stabilize
        {
        while(LineInput == 1);
        while(LineInput == 0);
        CLRWDT();           
        }
        
InitIO ();       
Timer0 ();
OPTION_REG &=0x55 ; //0 = GPIO pull-ups are enabled by individual port latch values
WPU = 0b00000011; //WPU=weak pull-up Resister
 
GPIO = 0x04;  //GP2=TRIAC PIN HIGH//set triac output latch high       

while (LineInput==1) //Synch to line phase//ZC detected
//CLRWDT();

TMR0 = PercentOn;  //Get Delay time

while(TMR0 >= 3 && LineInput == 0) //Delay to enter main at proper point
CLRWDT();

while(1) // stay in this loop
{
    ---

   ----
}

RAHAMANSA's picture

Offline

Joined: Sep 15, 2017

Replies: 9
how to complete balance part of code under while(1) loop
October 16, 2018 - 6:29pm

Hi,

Is there any body can correct and complete the balance part of code for above operation to get ON/OFF and DIM control.

please help me out.

Tnx

Bradyen's picture

Offline

Joined: Aug 14, 2018

Replies: 8
People here can help you with
October 17, 2018 - 5:53pm

People here can help you with problems, I don't think anyone has enough time to spare on providing you codes