cancel
Showing results for 
Search instead for 
Did you mean: 

PIN state change problem

Chayan Roy
Associate III
Posted on March 01, 2017 at 07:56

Hello, I   am trying to  create  100us seconds pulse which periodically triggers  a triac. Timer 1 is used for periodic delay and a while loop is under the  timer1 isr for 100us pulse. Problem is it is not working,Interrupt occurs but pin state doesn't change.  Here is the code:

 #include 'STM8S003F3P.h'

void InitialiseSystemClock(void);

unsigned int i=500;

void main(void){

                   InitialiseSystemClock();  // Internal  RC clock initialization ,frequency 16MHZ

            

                                

                 PD_DDR |=(1<<1);  //PROT D is set as output

                 PD_CR1 |=(1<<1);

                 PD_CR2 |=(1<<1);

                                

                              

                                

                                

                                 TIM1_PSCRH=0;        //timer1 setup for periodic interrupt

                                 TIM1_PSCRL=16;

                                 TIM1_ARRH=200;

                                 TIM1_ARRL=255;

                                 TIM1_IER=(1<<0);

                                 TIM1_CR1 =(1<<0);

 

                

                  

                 _asm('rim'); // Enable global interrupt

                   

                                 while(1){

              

}

}

@far @interrupt void TIMER1_INTERRUPT (void){     // timer1 isr

PD_ODR |=(1<<1);                             // PD1  high

while(i !=0){

i--;                                                 // some micro seconds delay

                     

}

PD_ODR &=~(1<<1);              // PD1  low

TIM1_SR1 =~(1<<0);

return;

}

Plese  guide me,what to do.

1 REPLY 1
Ian Legg
Senior
Posted on March 01, 2017 at 09:19

Hello,

I would suggest that a while loop delay in an isr is not good practice but I also note that you aren't re-initializing the 'i' variable so you'll get strange effects there.

It might be better to use PWM, that way the hardware will take care of toggling the pin for you at the chosen duty cycle, providing of course you configure the timer correctly.

Thanks,

Ian