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

Problem using PWM and other Port C outputs - 16f916

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



Joined: 08 Jul 2007
Posts: 15
Location: Cordoba - Argentina

View user's profile Send private message Visit poster's website ICQ Number

Problem using PWM and other Port C outputs - 16f916
PostPosted: Tue Aug 07, 2012 7:13 am     Reply with quote

Well, I can't figure out how to make PWM and PORTC outputs work together.

This is the code
Code:

void main()
{
 
   int var, i, Start_Iniciado, var_input, var_mostrar;
   int1 var1;
   int1 arranque_inicial;

   setup_adc_ports(sAN0|sAN1|VSS_VDD);
   setup_adc(ADC_CLOCK_INTERNAL);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_8); //1 ms
   //setup_wdt(WDT_2304MS|WDT_DIV_16);
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_1); //32 ms
   setup_timer_2(T2_DIV_BY_1,255,1); //15 Khz
   setup_ccp1(CCP_PWM);
   setup_comparator(NC_NC_NC_NC);
   
   //set_tris_c(0b00010000); I also try fast input output
   
   //TODO: User Code

 // INICIALIZACION DE VARIABLES, SALIDAS Y ENTRADAS IMPORTANTES
 
   output_high (PIN_OUT_STOP);
   var_decremeta_cuenta_xa_1_seg = cuenta_xa_1_seg;
   
   flag_blink_Power_LED = FALSE;
   flag_blink_Stop_LED = FALSE;
   flag_blink_Sensor_LED = FALSE;
   flag_blink_S_Corr_LED = FALSE;
   
   flag_STOP = TRUE;
   flag_START = FALSE;
   flag_Sobre_Corr = FALSE;
   
   Start_Iniciado = FALSE;
   sent_giro = sent_enroscando;


   //enable_interrupts(INT_AD);
   enable_interrupts (INT_EXT);
   enable_interrupts (INT_TIMER1);
   //enable_interrupts (INT_TIMER0);
   
   stop();

   
   enable_interrupts(GLOBAL);

   



   flag_SENS_PUNTA = TRUE;
   arranque_inicial = TRUE;
   for(;;)
   {
   restart_wdt();

   if ( flag_START == FALSE && flag_STOP == FALSE){
   
      //pot timer
   set_adc_channel (1);
   delay_us(30);
   timer_segs = read_adc();
   delay_us(30);
   
   var_decrementa_timer_segs = timer_segs;
   

   }

//THIS IS THE GREAT PROBLEM with Output and PWM
//IF I take out this output_c code, everything works fine

   if (var_BCD_timer_nums != var_BCD_timer_nums_anterior){     
      //var_decrementa_timer_segs--;     
      //var_input = input_c();
      var_mostrar = var_BCD_timer_nums;
      output_c(BCD_numbers[var_mostrar]);   
      var_BCD_timer_nums_anterior = var_BCD_timer_nums;
   }

   //
   if (var_decrementa_timer_segs == 0 && flag_START == TRUE){
      flag_STOP = TRUE;
      //flag_START = FALSE;
      //var_input = input_c();
      var_mostrar = 14; // Letra t = Terminado
      output_c(BCD_numbers[var_mostrar]); 
      delay_ms(1000);
   }
     
   
// Boton Stop
      if (flag_STOP == TRUE || (var_veloc_motor_pote > 120 && var_veloc_motor_pote < 130)){
         
         flag_START = FALSE;
         Start_Iniciado = FALSE;
         arranque_inicial = FALSE;
         stop();
      }

   
      if (Start_Iniciado == TRUE && ( (var_veloc_motor_pote_anterior != var_veloc_motor_pote) || arranque_inicial == TRUE ) ){

      //disable_interrupts(INT_TIMER1);

         arranque_inicial = FALSE;
         
         if (var_veloc_motor_pote > 130){
            sent_giro = sent_enroscando;
            var_veloc_motor = var_veloc_motor_pote * 2;
            veloc_motor();
         }
     
         if (var_veloc_motor_pote < 120){
            sent_giro = sent_desenroscando;
            var_veloc_motor = 255 - var_veloc_motor_pote * 2;
            veloc_motor();
         }
         
         var_veloc_motor_pote_anterior = var_veloc_motor_pote;
      }
     
     // Start

      if (flag_START == TRUE && Start_Iniciado == FALSE && flag_STOP == FALSE && flag_SENS_PUNTA == TRUE){
     
         flag_STOP = FALSE;
         Start_Iniciado = TRUE;
         arranque_inicial = TRUE;
         
         output_LOW(PIN_OUT_STOP_LED);
         output_HIGH(PIN_OUT_Power_LED); 
         flag_blink_Power_LED = TRUE;
      }
     
////// Control de Sobre Corriente ///////////

      if (flag_S_Corr == TRUE){
         flag_STOP = TRUE;
         flag_PITIDO = TRUE;
         output_HIGH(PIN_OUT_S_Corr_LED);
         flag_blink_Stop_LED = TRUE;
         cuenta_xa_S_Corr = 0;
         }
      if (flag_S_Corr == FALSE && flag_PITIDO == TRUE){
         flag_PITIDO = FALSE;
 
      }
 



   }



}




void stop (void){

int var_input, var_mostrar;
   
   var_veloc_motor = 0 ;
   veloc_motor();   
   output_LOW(PIN_OUT_STOP); //ENABLE of L293
   flag_STOP = FALSE;
   
}

void veloc_motor (void)
{
   if (sent_giro == sent_enroscando)
      output_low(PIN_OUT_Sent_Giro);
   if (sent_giro == sent_desenroscando)
      output_high(PIN_OUT_Sent_Giro);
     

   if (sent_giro == sent_enroscando)
      set_pwm1_duty (var_veloc_motor);
   if (sent_giro == sent_desenroscando)
      set_pwm1_duty (255 - var_veloc_motor); 

   output_HIGH(PIN_OUT_STOP); //el ENABLE del Integrado L293
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19520

View user's profile Send private message

PostPosted: Tue Aug 07, 2012 11:16 am     Reply with quote

With standard_io selected, 'input_c' says set the TRIS for the whole of port C, to input. This immediately stops the PWM (which requires the ccp pin set as an output), from working.

Choices:

1) Switch to using fast_io, and set the tris yourself.
2) Switch to using fixed_io, and set the CCP pin as an output.
3) Immedately after the input line, add 'output_drive',on the CCP pin.

Best Wishes
zagoaristides



Joined: 08 Jul 2007
Posts: 15
Location: Cordoba - Argentina

View user's profile Send private message Visit poster's website ICQ Number

PostPosted: Tue Aug 07, 2012 12:19 pm     Reply with quote

Ttelmah wrote:
With standard_io selected, 'input_c' says set the TRIS for the whole of port C, to input. This immediately stops the PWM (which requires the ccp pin set as an output), from working.

Choices:

1) Switch to using fast_io, and set the tris yourself.
2) Switch to using fixed_io, and set the CCP pin as an output.
3) Immedately after the input line, add 'output_drive', on the CCP pin.

Best Wishes


Thanks a lot, I never used that output_drive function. By the way I figured out how to make it work by making an output bit by bit, like this.

Code:

      for(i = 56; i<60;i++) //PIN_C0 al PIN_C3
         output_bit(i,shift_right(&var_mostrar,1, pin_C0));
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