Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] Delay inside sub procedure interrupt

Status
Not open for further replies.

kenneth031592

Newbie level 4
Joined
Jan 21, 2016
Messages
7
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
63
I encountered a problem in sub procedure interrupt.I use interrupt to detect zero cross to do a phase control using porta.B0 to trigger the triac it works like a dimmer but with some difference due to some functions like continuous monitoring of current. If ever i increase the delay as what i use above as sample 2500us everything goes wrong 2500us is fine but i need 6500us. My main code is affected and cannot read adc as what it is intended to do inside the main.


Code Basic4GL - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
sub procedure interrupt
 
     if (INTCON.INT0IF = 1) then  'INTF flag raised so external interrupt occured
 
         if (x = 1) then 'this condition came from the main
              DELAY_us(2500) 'delay for phase control of triac increasing this delay cause the problem ex. 6500us
              PORTA.B0 = 1   ' TURN ON THE TRIAC
              DELAY_us(500)  ' TIME TO  MAKE SURE THE LATCHING OF TRIAC
              PORTA.B0 = 0   ' TURN OFF THE TRIAC
    
              else
 
              PORTA.B0 = 1   'ELSE THE TRIAC IS ON          
 
          end if
      
      INTCON.INT0IF = 0      'interrupt clear
          
    end if
 
 
end sub

 
Last edited by a moderator:

If you are needing a delay in an ISR then you have designed the system incorrectly. I have never come across a situation where a delay is "required" in an ISR.
What you probably want is a state machine with a timer as well as the external interrupt to change the idle state to the first one.
Also you don't say what happens when "...everything goes wrong...".
Perhaps telling us what you are trying to do and what makes you think the code is the correct solution.
Susan
 
If you are needing a delay in an ISR then you have designed the system incorrectly. I have never come across a situation where a delay is "required" in an ISR.
What you probably want is a state machine with a timer as well as the external interrupt to change the idle state to the first one.
Also you don't say what happens when "...everything goes wrong...".
Perhaps telling us what you are trying to do and what makes you think the code is the correct solution.
Susan


Hi Susan thanks for your time. here is the setup, I use 4n25 optocoupler to detect every time zero cross occurs and use that as interrupt. I want to monitor the current reading every now and then using the adc of the pic as will as monitoring the zero cross for real time.. once I detect a big change in current I will do the phase control while reading the ADC for current change. the delay in the ISR is use to drive the triac at a certain angle(phase control), since I need tor run the program simultaneously I put the zero cross detection and phase control inside the interrupt and do the ADC reading for current detection inside the main... If I increase the delay in the interrupt the reading of my ADC current inside my main code becomes un accurate. One more question, is multi tsaking available in mikrobasic progarmming or just in C? I use PIC16F88

- - - Updated - - -

Thanks for your time, I attached here a sample of my codes cause Im still working on it, this is the concept I made before putting the phase control inside the sub procedure interrupt but I encountered problem in here, I explained it inside the code as comments.. Sir will you explain further what do you mean. this is my first programming project using pic. thanks for your help!


Code:
sub procedure interrupt

     if (INTCON.INT0IF = 1) then  'INTF flag raised so external interrupt occured
      zcnt = 1               ' zero cross indicator
      INTCON.INT0IF = 0      'interrupt flag clear manualy
    end if


main:

     while true

       if  ADCON0.B2 = 0 then
            zx1 = (ADC_Read(2)*5000)/1023
            zx2 = (abs(zx1-2500)*1000)/100
            zx3 = zx2*zx2
            zx = zx + zx3
            sample = sample + 1
         end if
         
       if (sample = 60 ) then
                       irms = sqrt(zx/20)
                       rms = irms
                       sample = 0
                       zx = 0
       end if
          
          
          

         While (rms>3000)
                             'this portion is for the phase control
              
              if zcnt = 1
                 zcnt = 0
              
                DELAY_us(6500)
                PORTA.B0 = 1
                DELAY_us(500)
                PORTA.B0 = 0
                else
                PORTA.B0 = 1
               end if
              'continous monitoring of the adc so that I can get out inside the loop once not satisfied
              ' the problem with this one is that my phase control above is affected due to the time consumes
              'in reading the adc below, the result is that my phase control is varrying due to delays added in reading
              ' thats the reason why in my post I seaparate reading the adc and phase control thinking that interrupt run separately from the main code.
              if  ADCON0.B2 = 0 then
                   zx1 = (ADC_Read(2)*5000)/1023
                   zx2 = (abs(zx1-2500)*1000)/100
                   zx3 = zx2*zx2
                   zx = zx + zx3
                   sample = sample + 1
               end if

             if (sample = 60 ) then
                       irms = sqrt(zx/20)
                       rms = irms
                       sample = 0
                       zx = 0
               end if

          wend

                
     wend

 end.
end sub
 

You need to read the actual replies you have received from Easyrider83 and me and set up a state machine that has (at least) the following states (select names for yourself):
- wait_for_zero_cross_interrupt,
- do_what_I_need_to_on_zero_cross_detect,
- wait_for_however_long,
- do_what_I_need_to_after_the_delay

The external zero-detect ISR will move you from the first state to the 2nd. When that starts it will do the ADC read (or whatever) and starts a timer before going to the 3rd state. When the timer times out, its ISR will move you to the last state. The last state doers whatever is necessary and then resets back to the first state.
This is basic state machine stuff and is how you should be handling the operation you are talking about.
To be honest I can't be bothered to read the details of what happens - if you think you need a delay in the ISR then I know you are taking the wrong approach. Therefore you might need more states and the transitions between them might need to change, but the principle is the same.
Susan
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top