View previous topic :: View next topic |
Author |
Message |
Elysion
Joined: 03 Aug 2010 Posts: 26
|
16f882 ECCP problem |
Posted: Thu Jan 20, 2011 6:13 am |
|
|
I used these codes shown below, but I couldn't use 16f882's half bridge module. Could you propose anything for this operation?
Code: |
#include <16f882.h>
#include <PIC16F882_registers.h>
#fuses HS,NOWDT,NOPROTECT,NOBROWNOUT,NOLVP,NOPUT,NOWRT,NODEBUG,NOCPD
#use delay (clock=20000000)
#use fast_io(c)
void main()
{
set_tris_c(0x00); // set PORTC as output
output_C(0x00);
T2CON=0b00000100;
PR2=4;
CCP1CON=0b10001100;
CCPR1L= 0b00000010;
PWM1CON=0b10000001;
while(1)
{
}
}
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jan 20, 2011 11:53 am |
|
|
What's your compiler version ? |
|
|
Elysion
Joined: 03 Aug 2010 Posts: 26
|
|
Posted: Fri Jan 21, 2011 2:38 am |
|
|
my compiler version is 4.108 |
|
|
Elysion
Joined: 03 Aug 2010 Posts: 26
|
|
Posted: Sat Jan 22, 2011 1:29 am |
|
|
And I want to get 1MHZ PWM at the output of the PWM. But I couldn't. Is there anyone to answer my question?
Thanks. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Jan 22, 2011 2:28 am |
|
|
I'm sorry. I forgot to work on your problem during the day. I'll try to
give you an answer now, but I can't test it in hardware so I don't
guarantee this will work.
In Half-Bridge mode, the PWM signals come out on PWM pins P1A and P1B
which are pins C2 and B2 in the 16F882. So look on those pins for the
signals. In addition to this, vs. 4.108 has a bug, where is doesn't setup
pin P1B (B2) correctly as a output pin. So I've added a line of code to
do this manually.
I think I've setup Timer2 to give you a 1 MHz PWM frequency.
For the duty cycle, you will need to use an 'int16' value to get
the maximum number of resolution steps at this frequency.
If you use a constant for the duty cycle, you need to append an 'L'
on it as I have shown below. This will invoke 10-bit PWM mode.
If you use a variable for the duty cycle, declare it as an 'int16'.
Here's the example. As I said, I can't test this in hardware at the
moment.
Code: |
#include <16F882.H>
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#define P1B PIN_B2
//==========================================
void main()
{
setup_ccp1(CCP_PWM_HALF_BRIDGE | CCP_PWM_H_H);
setup_timer_2(T2_DIV_BY_1, 4, 1);
output_low(P1B); // Initialize P1B to the inactive state (low)
set_pwm1_duty(10L); // 50% duty cycle
while(1);
} |
|
|
|
Elysion
Joined: 03 Aug 2010 Posts: 26
|
|
Posted: Sat Jan 22, 2011 3:12 am |
|
|
I tried these codes in real hardware. But the frequency of PWM is 200khz not 1Mhz. I couldn't get 1 Mhz at the output of PWMs. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Sat Jan 22, 2011 3:17 am |
|
|
Do a simple pin toggling test, and verify your chip really is running at 20MHz.
I'd suggest it is really running at 4MHz...
Best Wishes |
|
|
Elysion
Joined: 03 Aug 2010 Posts: 26
|
|
Posted: Sat Jan 22, 2011 9:17 am |
|
|
Ok thanks for your help. I solved my problem thanks to you. It is related with the crystal. |
|
|
|