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 support@ccsinfo.com

PWM as an input to another digital input pin

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



Joined: 08 Mar 2021
Posts: 3

View user's profile Send private message

PWM as an input to another digital input pin
PostPosted: Mon Mar 08, 2021 8:47 pm     Reply with quote

Hi,
I'm trying to set the PWM signal generated by the CCP1 pin in the PIC16F887 as an input to the PIN_A4, in order to then use it as an output of different pins of ports B and D.
I'm using CCS Compiler and I've read the manual, however I can't get the pins of ports B and D to output the same frequency as the PWM signal (2.5kHz) input in A4, as for now, the output of those ports are HIGHs and LOWs with a frequency of 1-2Hz.

Code:
#include <16f887.h>
#fuses XT,NOWDT,NOPROTECT,NOLVP,PUT,BROWNOUT,INTRC_IO 
#use delay(clock=4M) 

#byte PORTA=0x05 
#byte PORTB=0x06 
#byte PORTD=0x08
#byte TRISA=0x85 
#byte TRISB=0x86
#byte TRISD=0x88

void main() {
   TRISA=0b00001111;
   TRISB=0;
   TRISD=0;
   PORTA=1;
   PORTB=0;
   PORTD=0;

   //To set the PWM with a f=2500Hz:
   //PWM = (PR2+1) * 4 * TOsc * Preescaler
   //PR2= PWM / (4 * Preescaler * TOsc) -1
   //= 0.0004/ (4 * 4 * 0.000000250) -1
   //PR2=99

   Timer2=99;
   Poscaler=1;
   setup_timer_2(t2_div_by_4,Timer2,Poscaler);
   setup_ccp1(ccp_pwm); //CCP1 as PWM

   //duty cycle = (PR2+1)(4)
   //=(99+1)*4
   //=400
   //400 --> 100%
   // x  --> 50%,   x=200
   set_pwm1_duty((int16)200);

      while(true){     
         if(input(PIN_A0)==0 && (input(PIN_A1))==0 && (input(PIN_A2))==0
      && (input(PIN_A3))==0){
         delay_ms(300);
         output_bit(PIN_B0, input(PIN_A4));
         }
         else{
            output_low(PIN_B0);
         }
         if(input(PIN_A0)==0 && (input(PIN_A1))==0 && (input(PIN_A2))==1
      && (input(PIN_A3))==0){
            delay_ms(300);
            output_bit(PIN_B1, input(PIN_A4));
         }
         else{
            output_low(PIN_B1);
         }
    }
}
alan



Joined: 12 Nov 2012
Posts: 349
Location: South Africa

View user's profile Send private message

PostPosted: Mon Mar 08, 2021 11:58 pm     Reply with quote

Well 1st glaring problem are the 2 delay_ms(300), which will result in a freq of approx 1.6Hz if you just toggle the 2 pins.
temtronic



Joined: 01 Jul 2010
Posts: 9102
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Mar 09, 2021 8:11 am     Reply with quote

hmm... from another website....
Quote:

//Timer2
//Prescaler 1:1; Postscaler 1:2; TMR2 Preload = 199; Actual Interrupt Time : 400 us

Might be worth checking the 'math'.

Also is this a real PIC and NOT a 'simulation' ?
If real, did you compile in 'debug' or 'release' mode (if using MPLAB).
Ttelmah



Joined: 11 Mar 2010
Posts: 19219

View user's profile Send private message

PostPosted: Tue Mar 09, 2021 8:39 am     Reply with quote

His PWM maths is right. As Alan points out, it It is just that he is sampling
the input signal only once every 300mSec. Result very slow actual update...

The figure you were looking at was for an interrupt rate from the timer
not a PWM. Since there is no postscaler when used as a PWM...
Amend



Joined: 08 Mar 2021
Posts: 3

View user's profile Send private message

PostPosted: Wed Mar 10, 2021 10:29 am     Reply with quote

alan wrote:
Well 1st glaring problem are the 2 delay_ms(300), which will result in a freq of approx 1.6Hz if you just toggle the 2 pins.


Thank you Alan, you were right, the delay was causing problems. By removing it the signal comes out right now!
Amend



Joined: 08 Mar 2021
Posts: 3

View user's profile Send private message

PostPosted: Wed Mar 10, 2021 10:30 am     Reply with quote

Ttelmah wrote:
His PWM maths is right. As Alan points out, it It is just that he is sampling
the input signal only once every 300mSec. Result very slow actual update...

The figure you were looking at was for an interrupt rate from the timer
not a PWM. Since there is no postscaler when used as a PWM...


You're right, I removed the delay and now the signal is accurate. Thank you very much
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