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

Any other way
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
pdl



Joined: 01 Jun 2005
Posts: 45
Location: UK

View user's profile Send private message

Any other way
PostPosted: Sun Jul 13, 2008 2:58 pm     Reply with quote

Hi guys

is there any other way of doing this program.
Code:

#include <12F629.h>
#FUSES NOWDT                    //No Watch Dog Timer
#FUSES INTRC_IO                 //Internal RC Osc, no CLKOUT
#FUSES NOCPD                    //No EE protection
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOMCLR                   //Master Clear pin used for I/O
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOBROWNOUT               //No brownout reset
#FUSES BANDGAP_HIGH         
#use delay(clock=4000000)

#define PWM_PIN       Pin_A2
#define Rx_Input      Pin_A3
#define Min_Width     0x07      // 1.0mS / 128uS = 07dec = 07h.
#define Max_Width     0x0F      // 2.0mS / 128uS = 16dec = 0Fh.
#define Low_Trip      0x0A      // 1.4mS / 128uS = 10dec = 0Ah.
#define High_Trip     0x0C      // 1.6mS / 128uS = 12dec = 0Ch.
#define Max_Frame     0x9C      //  20mS / 128us = 156dec = 9Ch.

int i;

void lighton(){

for(i=0; i<30; i++){
output_high(PWM_PIN);
delay_us(5500);

output_low(PWM_PIN);
delay_us(4500);

   }
}

   
void wait_for_low_to_high() {

   while(input(Rx_Input)) ;     

   delay_us(3);               
   while(!input(Rx_Input));       
}

void wait_for_low() {

   delay_us(3);               
   while(input(Rx_Input));     
}



void main(void)
{
   int PulseWidth;
   int BeginPulse;

   setup_counters( RTCC_INTERNAL, RTCC_DIV_128 );
 

   while(1)
   {


    wait_for_low_to_high();
    BeginPulse = get_rtcc();
    wait_for_low();
    PulseWidth = (get_rtcc() - BeginPulse);

    if (PulseWidth >= High_Trip)
      {
         lighton();
      }


   } 

}

thanks

Pete
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Sun Jul 13, 2008 3:47 pm     Reply with quote

Quote:
is there any other way of doing this program.
Yes.

But for us to give a hint to a solution it would be very helpful if you can tell us why your implementation isn't good for the job.
pdl



Joined: 01 Jun 2005
Posts: 45
Location: UK

View user's profile Send private message

PostPosted: Mon Jul 14, 2008 12:42 am     Reply with quote

I am trying to light an led with out a resistor and with out it flickering and turning ever now and again

Pete
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Mon Jul 14, 2008 1:28 am     Reply with quote

I could give you ten possible suggestions for implementing such a function and than have you choose the single suggestion fitting your needs ...
... or you give more details about your system and requirements.

Because of my limited (spare) time you'll have to do with the last option. Also explain _why_ your example code is not sufficient.
pdl



Joined: 01 Jun 2005
Posts: 45
Location: UK

View user's profile Send private message

PostPosted: Mon Jul 14, 2008 2:01 am     Reply with quote

Thank you for you help ckielstra as you can see by my code i am new to C.

The Led Flickers ever now and again

What i would like when the program sees 1.6ms and a bove the Led stays on until the pluse is no longer 1.6ms or greater.

its just not stable a the minute

Pete
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Mon Jul 14, 2008 3:27 am     Reply with quote

I don't know the characteristics of your input signal, but most likely the waiting for a pulse is what causes your LED to flicker.

The easiest method to solve this is by removing the LED modulation out of the main loop. Create a timer driver interrupt which will drive your LED. Right now I don't have time to post example code but if you search this forum with the keywords 'soft PWM' you will find some related posts.
pdl



Joined: 01 Jun 2005
Posts: 45
Location: UK

View user's profile Send private message

PostPosted: Mon Jul 14, 2008 3:43 am     Reply with quote

I have tried that

I tried to use some of this code but could not get it to work properly.

The input pulse is a 1ms to 2ms pulse the same type of pluse that drives a model servo.

thanks

Pete

#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)

#define PWM_PIN PIN_B0

#define LOOPCNT 39

int8 width;

//-------------------------------
#INT_RTCC
void tick_interrupt(void);

//====================================
main()
{
width = 0;

setup_counters(RTCC_INTERNAL, RTCC_DIV_1);
enable_interrupts(INT_RTCC);
enable_interrupts(GLOBAL);

// Step through all values for width, from 0 to 39.
// This will slowly increase the duty cycle from 0 to 100%,
// by doing one step every 100 ms.
while(1)
{
for(width = 0; width <= LOOPCNT; width++)
delay_ms(100);
}

}

//====================================
#INT_RTCC
void tick_interrupt(void)
{
static int8 loop = LOOPCNT;
static int8 pulse;

if(--loop == 0)
{
loop = LOOPCNT;
pulse = width;
}

if(pulse)
{
output_high(PWM_PIN);
pulse--;
}
else
{
output_low(PWM_PIN);
}

}
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Mon Jul 14, 2008 5:12 am     Reply with quote

Code:
#include <16F877.H>
Are you sure this is correct? Your first post was using a PIC12F629
Code:
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
The XT fuse is for an external crystal. Your first post was using the internal clock generator.
pdl



Joined: 01 Jun 2005
Posts: 45
Location: UK

View user's profile Send private message

PostPosted: Mon Jul 14, 2008 5:19 am     Reply with quote

Yes i changed the pic i was using

i tried to use some of this code with the pulse measurement code and i could not get it working

Pete
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Mon Jul 14, 2008 6:36 am     Reply with quote

Try to be more detailed in your descriptions, just saying 'it's not working' is too limited. What do you expect to see, what is different and what behaviour do you actually see?

Are you using the PIC16F877 or the PIC16F877A? The first is very old...

Note that the PIC16F877 has two internal PWM hardware modules which are easier to use, will work more accurate and have less impact on the CPU usage than the soft PWM based on interrupts.
pdl



Joined: 01 Jun 2005
Posts: 45
Location: UK

View user's profile Send private message

PostPosted: Mon Jul 14, 2008 7:44 am     Reply with quote

Hi ckielstra

I am using the 12f629

When i say it did not work i was putting a 1ms to 2 ms pulse in on PIN_A3 and was not getting anything out of PIN_A2 but looking at it now i think that the ICD was setup for MCLR and not I/O.


I expect For the LED to light when i put a 1.6ms pulse on PIN_A3
PIN_A2 should have a 100hz PWM output a 50% duty

When the input pulse is below 1.6ms the LED should stay turned off and not come on until the 1.6ms pulse is back on the input pin.

hope this a bit more clear

Pete
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Mon Jul 14, 2008 9:18 am     Reply with quote

pdl wrote:
I am trying to light an led with out a resistor and with out it flickering and turning ever now and again

Pete


So what sets the current during the ON time? Are you relying on the over-current protection of the PIC pin logic? That is going to be very temperature sensitive. This may be OK for a home project, but I would not recommend it for a commercial product!
_________________
The search for better is endless. Instead simply find very good and get the job done.
pdl



Joined: 01 Jun 2005
Posts: 45
Location: UK

View user's profile Send private message

PostPosted: Mon Jul 14, 2008 9:47 am     Reply with quote

I am using a Mosfet as the LEDs i am using draw 350ma at 3.6v

Pete
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Mon Jul 14, 2008 3:52 pm     Reply with quote

Thanks for explaining your circuit in more detail. Now it makes more sense.

Quote:
When i say it did not work i was putting a 1ms to 2 ms pulse in on PIN_A3 and was not getting anything out of PIN_A2 but looking at it now i think that the ICD was setup for MCLR and not I/O.
Does it work after fixing the MCLR setup?

If not, please post your code for the PIC12F629 and tell us what behaviour you see.
pdl



Joined: 01 Jun 2005
Posts: 45
Location: UK

View user's profile Send private message

PostPosted: Mon Jul 14, 2008 6:42 pm     Reply with quote

Here is my code so far i now can turn on some sort of PWM with my input which is 1ms to 2ms and the trip should be 1.6ms but i can not get the
timing right to get 100Hz 50% duty from the PWM pin


I have a Mosfet which i supply 7.4v to and with the 50%duty from the PWM i was hoping to get 3.7v output from the Mosfet.

thanks

Pete

Code:

#include <12F629.h>
#FUSES NOWDT, INTRC_IO, NOCPD, NOPROTECT, NOMCLR, NOPUT, NOBROWNOUT, BANDGAP_HIGH
#use delay(clock=4000000)




#define PWM_PIN           PIN_A2
#define Rx_Input          PIN_A3
#define Min_Width         0x07      // 1.0mS / 128uS = 07dec = 07h.
#define Max_Width         0x0F      // 2.0mS / 128uS = 16dec = 0Fh.
#define Low_Trip          0x0A      // 1.4mS / 128uS = 10dec = 0Ah.
#define High_Trip         0x0C      // 1.6mS / 128uS = 12dec = 0Ch.
#define Max_Frame         0x9C      //  20mS / 128us = 156dec = 9Ch.

#define LOOPCNT 39

int8 width;

#INT_RTCC
void tick_interrupt(void)
{
static int8 loop = LOOPCNT;
static int8 pulse;

if(--loop == 0)
  {
   loop = LOOPCNT;
   pulse = width;
  }

if(pulse)
  {
   output_high(PWM_PIN);
   pulse--;
  }
else
  {
   output_low(PWM_PIN);
  }

}

void wait_for_low_to_high() {

   while(input(Rx_Input)) ;

   delay_us(3);                 
   while(!input(Rx_Input));     
}

void wait_for_low() {

   delay_us(3);             
   while(input(Rx_Input));       
}


void main()
{

   int PulseWidth;
   int BeginPulse;
width =19;
setup_counters( RTCC_INTERNAL, RTCC_DIV_128 );

   while(1)
   {
      wait_for_low_to_high();
         BeginPulse = get_rtcc();
         wait_for_low();

         PulseWidth = (get_rtcc() - BeginPulse);

      if (PulseWidth >= High_Trip)
      {
      enable_interrupts(INT_RTCC);
      enable_interrupts(GLOBAL);
     
      }
     
      if (PulseWidth <= Low_Trip)
      {
         clear_interrupt(INT_RTCC);
         disable_interrupts(INT_RTCC);
         disable_interrupts(GLOBAL);
                  output_low(PIN_A2);

      }

      }
}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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