CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Question

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
argo



Joined: 11 May 2016
Posts: 2

View user's profile Send private message

Question
PostPosted: Wed May 11, 2016 1:51 pm     Reply with quote

Hi all

I would like to know if this code uses (is based) "timer interrupts"?
Thank you in advance!

Code:

#include<12f683.h>

#fuses INTRC_IO,NOWDT,PUT,NOPROTECT,NOMCLR,NOBROWNOUT

#use delay(CLOCK=8000000)  //8MHz
#byte  GP=5
 #bit IRSIG=GP.3

#define ON 1
#define OFF 0
#define HI 1
#define LOW 0
#define el_on 0x31   
#define el_off 0x0e
#define rd_on 0x02
#define rd_off 0x35

signed long th_count,rd_count,el_count;
signed long rev_mix,rd_duty,al_count,rd_trim;
int cycl_count,el_duty;
byte gp_now;

// PWM
#int_rtcc
void soft_pwm()
{
  set_timer0(156);

  if(cycl_count < rd_duty){   // ON
    gp_now=gp_now | rd_on;
    GP=gp_now;
  }
  else{   // OFF
    gp_now=gp_now & rd_off;
    GP=gp_now;
  }
 
  if(cycl_count < el_duty){  // ON
    gp_now=gp_now | el_on;
    GP=gp_now;
  }
  else{   // OFF
    gp_now=gp_now & el_off;
    GP=gp_now;
  }

  cycl_count++;
  cycl_count %=99;
}



main(){
  int gcount;
 
  setup_oscillator(OSC_8MHz);
  setup_comparator(NC_NC); 
  setup_ccp1(CCP_PWM);     
  setup_adc_ports(NO_ANALOGS);

  setup_timer_0(RTCC_INTERNAL | RTCC_DIV_4);
  set_timer0(246);
       //0.5us*4*(255-246+1)=0.00002s(=0.02ms)
  setup_timer_2(T2_DIV_BY_4,255,1);   
       
  enable_interrupts(INT_RTCC);     // Timer0
  enable_interrupts(GLOBAL);               

  set_tris_a(0x08);  // GP3
/*  GP   5   4   3   2   1   0
        el3 el2  SG  MTR MTT el1
         0   0   1   0   0   0
*/

  cycl_count=rd_duty=el_duty=0;
  th_count=0;
  rd_trim=0;
  gp_now=OFF;

  while(1){
    gcount=0;
    while(gcount < 30){
       if(IRSIG==HI){
         delay_us(100);
         gcount++;
       }
       else{
         gcount=0;
       }
    }
   
    While(IRSIG==HI){
    }

//**************** Timer0 ******************
    disable_interrupts(INT_RTCC);

// 1ch
    al_count=0;
    while(IRSIG==LOW){ // Low
      delay_us(6);
      al_count=al_count+1;
    }
    while(IRSIG==HI){
      delay_us(6);
      al_count=al_count+1;
    }
   
// 2ch
    el_count=0;
    while(IRSIG==LOW){
      delay_us(6);
      el_count=el_count+1;
    }
    while(IRSIG==HI){
      delay_us(6);
      el_count=el_count+1;
    }
   
// 3ch
    th_count=0;
    while(IRSIG==LOW){
      delay_us(6);
      th_count=th_count+1;
    }
    while(IRSIG==HI){
      delay_us(6);
      th_count=th_count+1;
    }

// 4ch
    rd_count=0;
    while(IRSIG==LOW){
      delay_us(6);
      rd_count=rd_count+1;
    }
    while(IRSIG==HI){
      delay_us(6);
      rd_count=rd_count+1;
    }

//************** Timer0 ******************
    enable_interrupts(INT_RTCC);
   
    if(al_count > 180){
      delay_ms(100);
      rd_trim=rd_trim+2;
    }
    if(al_count < 130){
      delay_ms(100);
      rd_trim=rd_trim-2;
    }

    if(el_count<105){
      el_count=100;
    }
    if(el_count>180){
      el_count=205;
    }
    if(el_count > 160){
      el_duty=(el_count-155)*2;
    }
    else{
      el_duty=0;
    }

    rev_mix=th_count;
    if(th_count<110){ 
      th_count=0;
    }
    else if(th_count>190){
      th_count=1023;
    }
    else{
      th_count=(th_count-100)*10;     }

    set_pwm1_duty(th_count);

    rev_mix=rev_mix-100;
    if(rd_count<110){
      rd_count=100;
    }
    if(rd_count>200){
      rd_count=200;
    }

    if(150 <= rd_count && rd_count <=160){
      rd_count=155;
    }
    if(rev_mix < 45){
      rd_duty=rev_mix+(rd_count-155)+rd_trim;
    }
    else{
      rd_duty=rev_mix/10*7+14+(rd_count-155)+rd_trim;
    }

  }

}


Last edited by argo on Wed May 11, 2016 2:21 pm; edited 1 time in total
Ttelmah



Joined: 11 Mar 2010
Posts: 19477

View user's profile Send private message

PostPosted: Wed May 11, 2016 1:53 pm     Reply with quote

Yes.

#int_rtcc

This defines the timer0 (rtcc) interrupt handler.
argo



Joined: 11 May 2016
Posts: 2

View user's profile Send private message

PostPosted: Wed May 11, 2016 2:35 pm     Reply with quote

Thanks for the reply, Ttelmah.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group