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

16F628A
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
16f628User
Guest







16F628A
PostPosted: Fri Jul 11, 2008 10:27 pm     Reply with quote

Hi, I have a PIC16F628A and I want to fade a LED with PWM, but I cant do it :/ any help? Smile


Thanks
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Jul 12, 2008 12:26 am     Reply with quote

See this post:
http://www.ccsinfo.com/forum/viewtopic.php?t=35191&start=3
This will ramp the PWM duty cycle up from 0% to 100%.
You might want to change the delay to 10 ms, instead of 100 ms.
That way, it will take 2.5 seconds to ramp up the brightness,
instead of 25 seconds. Make sure you use a series resistor
with the LED. You can use 220 to 470 ohms.
16f628User
Guest







PostPosted: Sat Jul 12, 2008 10:01 am     Reply with quote

Still not working :/
Where should I see the PWM?
I tried all pins and I my LED always stays OFF..

Also cant we use internal clock?
16f628User
Guest







PostPosted: Sat Jul 12, 2008 10:41 am     Reply with quote

Ok, my PIC must have some problem I cant run this simple code:

Code:
#include <16F628A.h>
#use delay(clock=4000000)
#fuses NOWDT, HS, PUT, NOPROTECT, BROWNOUT, MCLR, NOLVP, NOCPD
//————————————————————————————————————————————————————————————
// PWM signal to be generated on pin9 -> RB3/CCP1
//————————————————————————————————————————————————————————————
void main(void)
{
 // Value = 0 Duty-Cycle (pulse width) = 0
 // Value = 64 Duty-Cycle (pulse width) = 50%
 // Value = 128 Duty-Cycle (pulse width) = 100% -> DC Signal
 int8 value = 0; delay_ms(100); // power up delay
 
 // will hang on debug but necessary to turn of comparators
 setup_comparator(NC_NC_NC_NC);
 setup_vref(FALSE); // turn of ADC channels
 set_tris_b(0b11110000);
 setup_ccp1(CCP_PWM);   // Configure CCP1 as a PWM
 //     (1/clock_freq)*4*Divider*128 = cycle time
 //     (1/4000000)*4*16*128= 2.048 ms or 488.281 Hz
 setup_timer_2(T2_DIV_BY_16, 127, 1);
 
 while( TRUE )
 {
  set_pwm1_duty(value);
  // only for debug - will blink led every 0.5 second
  // and increment Duty Cycle every second
  output_low(PIN_B0);
  delay_ms(500);
  output_high(PIN_B0);
  delay_ms(500);
  value = value + 1;
 }
}


I think its because of the configuration, looks like it must have a specific configuration, otherwise it wont do nothing :/

Not even this simple code works:
Code:
#include <16F628A.h>
#use delay(clock=4000000)
#fuses NOWDT, HS, PUT, NOPROTECT, BROWNOUT, MCLR, NOLVP, NOCPD

void main(void)
{
 
 while( TRUE )
 {
  output_low(PIN_B0);
  delay_ms(500);
  output_high(PIN_B0);
  delay_ms(500);

 }
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Jul 12, 2008 11:58 am     Reply with quote

1. Have you ever made any program work with this PIC ?

2. Is the PIC installed in a board, with the required power and ground
connections, and an MCLR circuit, and a crystal circuit ?

3. Did you buy the board or did you make it yourself ?

4. What is your compiler version ? It's given at the top of the .LST file,
which is in your project directory.
16f628User
Guest







XOCTTQ4
PostPosted: Sat Jul 12, 2008 3:37 pm     Reply with quote

1: Yes
2: Yes
3: I made the programmer
4: CCS PCM C Compiler, Version 4.017, 42528


I got it to work, but I lost the program, so I lost the FUSES...and now its not working anymore, I have to find the program again, I was using Internal Clock..
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Jul 12, 2008 4:08 pm     Reply with quote

The fuse settings are listed at the top of the 16F628A.H file.
It's in this directory:
Quote:
c:\Program Files\Picc\Devices

Delete the HS fuse and replace it with INTRC_IO.
16f628User
Guest







PostPosted: Sat Jul 12, 2008 11:04 pm     Reply with quote

Ok, this code works:

Code:
#include <16F628A.h>
#use delay (clock=4000000)
#fuses INTRC_IO, NOWDT, NOPUT, NOBROWNOUT, NOMCLR, NOLVP, NOPROTECT

void main()
{
   while (true)
   {
      output_high (pin_b0);
      delay_ms(200);
      output_low (pin_b0);
      delay_ms(200);
   }
}



Still the PWM does not work:

Code:
#include <16F628A.h>
#use delay (clock=4000000)
#fuses INTRC_IO, NOWDT, NOPUT, NOBROWNOUT, NOMCLR, NOLVP, NOPROTECT

void main()
 {
byte value;

setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_4, 255, 1);
set_pwm1_duty(0); // PWM off

delay_ms(1000);

while(1)
  {
      output_high(pin_b0);
      value++;
      set_pwm1_duty(value);
      delay_ms(100);
  }

}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Jul 12, 2008 11:41 pm     Reply with quote

Your version is very buggy with PWM for this PIC. The generated code
is incorrect. You need to substitute the macros shown below, for the
built-in CCS functions. Then it will work.

Code:

#include <16F628A.h>
#fuses INTRC_IO, NOWDT, NOPUT, NOBROWNOUT, MCLR, NOLVP, NOPROTECT
#use delay (clock=4000000)


#byte CCPR1L = 0x15
#define my_set_pwm1_duty(x) CCPR1L = x

#byte CCP1CON = 0x17
#define my_setup_ccp1(x)  output_low(PIN_B3); CCP1CON = value


Notice the lines marked below. They use the new functions.
Code:
void main()
{
byte value;

my_setup_ccp1(CCP_PWM);    // *** CHANGED ***
setup_timer_2(T2_DIV_BY_4, 255, 1);
my_set_pwm1_duty(0);    // *** CHANGED ***

delay_ms(1000);

while(1)
  {
     output_high(PIN_B0);
      value++;
      my_set_pwm1_duty(value);  // *** CHANGED ***
      delay_ms(10);
  }

}

Note that I also changed the 100 ms delay to 10 ms. That's because
with 100 ms, it will take over 25 seconds to ramp up the brightness.
That's too long for a test program. I don't want to wait that long.


I have two other comments. In CCS, you should put the #fuses
statement after the main #include statement. Then next comes the
#use delay() statement. The #use delay() should come after the
#fuses so the compiler can see it and put in the proper start-up code
for the INTRC_IO mode. The way you originally did it caused the
compiler to leave out this code. By chance, it still works. But in the
general case it might not, so it's better to use the standard line order.

The 2nd thing is that when you type in constants such as PIN_B0, they
should be kept in upper case. Most compilers are case sensitive by
default, and you would get an error based on the way you did it.
The 16F628A.H file has the pin number constants in upper case.
Using upper case for constants is the C standard.
16f628User
Guest







PostPosted: Sun Jul 13, 2008 12:08 am     Reply with quote

Finnaly working, thanks alot ;)

Also, do you know how can I do an AD without an AD chip? I saw some ways using a RC circuit..
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jul 13, 2008 12:16 am     Reply with quote

I haven't done it. I always use a chip that has an A/D. The easiest way
in your case is to change to a 16F88. It's the same package as the
16F628A, but has more features, including an A/D.
16f628User
Guest







PostPosted: Sun Jul 13, 2008 12:57 am     Reply with quote

I dont know where I can buy a 16f88 here in portugal :/
Ive to buy it from USA or China wich will cost me more in taxes than the actual PIC :/

Also, I see in the 16f628A datasheet that it has two ADC's how can I use them?

Shouldnt this be the same as the AD?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jul 13, 2008 2:07 am     Reply with quote

It has two comparators, not ADCs.
16f628User
Guest







PostPosted: Sun Jul 13, 2008 3:00 am     Reply with quote

Hmm, any example on how to use them?

Wheres the best place to buy a 16f88?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jul 13, 2008 10:22 am     Reply with quote

Here are two suppliers in the UK:
http://www.farnell.com/
http://dkc1.digikey.com/us/en/mkt/International.html
Carefully check the shipping charges and the VAT before you order.
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