|
|
View previous topic :: View next topic |
Author |
Message |
16f628User Guest
|
16F628A |
Posted: Fri Jul 11, 2008 10:27 pm |
|
|
Hi, I have a PIC16F628A and I want to fade a LED with PWM, but I cant do it :/ any help?
Thanks |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Jul 12, 2008 12:26 am |
|
|
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
|
|
Posted: Sat Jul 12, 2008 10:01 am |
|
|
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
|
|
Posted: Sat Jul 12, 2008 10:41 am |
|
|
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
|
|
Posted: Sat Jul 12, 2008 11:58 am |
|
|
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 |
Posted: Sat Jul 12, 2008 3:37 pm |
|
|
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
|
|
Posted: Sat Jul 12, 2008 4:08 pm |
|
|
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
|
|
Posted: Sat Jul 12, 2008 11:04 pm |
|
|
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
|
|
Posted: Sat Jul 12, 2008 11:41 pm |
|
|
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
|
|
Posted: Sun Jul 13, 2008 12:08 am |
|
|
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
|
|
Posted: Sun Jul 13, 2008 12:16 am |
|
|
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
|
|
Posted: Sun Jul 13, 2008 12:57 am |
|
|
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
|
|
Posted: Sun Jul 13, 2008 2:07 am |
|
|
It has two comparators, not ADCs. |
|
|
16f628User Guest
|
|
Posted: Sun Jul 13, 2008 3:00 am |
|
|
Hmm, any example on how to use them?
Wheres the best place to buy a 16f88? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
|
|
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
|