View previous topic :: View next topic |
Author |
Message |
geshsoft
Joined: 01 May 2007 Posts: 14 Location: California
|
PWM 16F685 Problem |
Posted: Fri May 04, 2007 7:08 pm |
|
|
I was trying the PWM module of the PIC16F685 but so far I have no success. The code I used is:
#include "C:\Program Files\PICC\Projects\DCMControl\main.h"
void main()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DIV_BY_1,79,1);
setup_ccp1(CCP_PWM);
setup_ccp1(CCP_PWM_L_L);
setup_ccp1(CCP_PWM_FULL_BRIDGE);
setup_ccp1(CCP_SHUTDOWN_AC_H);
set_pwm1_duty(160);
setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
setup_oscillator(OSC_8MHZ);
// TODO: USER CODE!!
}
I was expecting to see 25kHz square wave with 50% duty cycle, but so far I see no output at all. I wonder if I did something wrong or there is a bug of some sort? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
geshsoft
Joined: 01 May 2007 Posts: 14 Location: California
|
|
Posted: Fri May 04, 2007 10:08 pm |
|
|
Do you mean to try compiling it the way it is there as for PIC18F4550 or to modify it to match PIC16F685? For once 16F685 has only CCP1 so it will not work in its original form. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri May 04, 2007 10:22 pm |
|
|
Yes, skip the 2nd CCP. Just try to make it work in Full Bridge mode. |
|
|
geshsoft
Joined: 01 May 2007 Posts: 14 Location: California
|
|
Posted: Fri May 04, 2007 10:32 pm |
|
|
Here we go:
Code: |
#fuses XT,NOWDT,BROWNOUT,PUT
#use delay (clock=4000000)
//=================================
void main()
{
// Setup the ECCP for PWM in full bridge mode.
setup_ccp1(CCP_PWM_FULL_BRIDGE | CCP_PWM_H_H);
setup_timer_2(T2_DIV_BY_1, 255, 1);
set_pwm1_duty(100);
// Setup CCP2 for PWM output on pin C1.
setup_ccp1(CCP_PWM);
set_pwm1_duty(50);
// Switch the H-Bridge outputs between forward
// and reverse every 5 seconds.
while(1)
{
// Run forward for 5 seconds.
setup_ccp1(CCP_PWM_FULL_BRIDGE | CCP_PWM_H_H);
delay_ms(5000);
// Run in reverse for 5 seconds.
setup_ccp1(CCP_PWM_FULL_BRIDGE_REV | CCP_PWM_H_H);
delay_ms(5000);
}
|
It did not work at all. Compiler version as seen in LST file is:
CCS PCM C Compiler, Version 4.033 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri May 04, 2007 10:42 pm |
|
|
The CCP2 stuff must be deleted, not turned into CCP1.
Make the changes shown in bold below.
Quote: |
#include <16F685.h> // Add this line
#fuses XT,NOWDT,BROWNOUT,PUT
#use delay (clock=4000000)
//=================================
void main()
{
// Setup the ECCP for PWM in full bridge mode.
setup_ccp1(CCP_PWM_FULL_BRIDGE | CCP_PWM_H_H);
setup_timer_2(T2_DIV_BY_1, 255, 1);
set_pwm1_duty(100);
// Delete the lines shown in bold below.
// Setup CCP2 for PWM output on pin C1.
setup_ccp1(CCP_PWM);
set_pwm1_duty(50);
// Switch the H-Bridge outputs between forward
// and reverse every 5 seconds.
while(1)
{
// Run forward for 5 seconds.
setup_ccp1(CCP_PWM_FULL_BRIDGE | CCP_PWM_H_H);
delay_ms(5000);
// Run in reverse for 5 seconds.
setup_ccp1(CCP_PWM_FULL_BRIDGE_REV | CCP_PWM_H_H);
delay_ms(5000);
}
} |
|
|
|
geshsoft
Joined: 01 May 2007 Posts: 14 Location: California
|
|
Posted: Fri May 04, 2007 10:58 pm |
|
|
Ok, I compiled the last code that you posted and now PIN_C5 goes on for 5s and off for 5s. No other pins are affected. There is no PWM output on any of the pins either. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat May 05, 2007 1:00 am |
|
|
With respect pin to C5, it's behaving correctly for the Full Bridge mode.
It's the P1A pin in that mode. According to Figure 11-6 in the 16F685
data sheet, pin C5 will be either a constant high or a constant low,
depending upon whether you're going in forward or reverse.
So pin C5 is OK.
There is a note in Section 11.5 of the data sheet (PWM Enhanced mode).
It says:
Quote: | The TRIS register value for each PWM output must be configured appropriately. |
However, the ASM code generated by the compiler doesn't do this.
It only sets pin C5 as a low-level output pin. It leaves the other pins
(C2, C3, C4) in the default state, which is as input pins.
Here's something you could try. Add a line of code after each
setup_ccp1() function, to manually force pins C2-C5 to be outputs.
Add the lines shown in bold below.
Quote: |
#include <16F685.h>
#fuses XT,NOWDT,BROWNOUT,PUT
#use delay (clock=4000000)
//=================================
void main()
{
// Setup the ECCP for PWM in full bridge mode.
setup_ccp1(CCP_PWM_FULL_BRIDGE | CCP_PWM_H_H);
set_tris_c(0xC3); // Set pins C2-C5 as outputs
setup_timer_2(T2_DIV_BY_1, 255, 1);
set_pwm1_duty(100);
// Switch the H-Bridge outputs between forward
// and reverse every 5 seconds.
while(1)
{
// Run forward for 5 seconds.
setup_ccp1(CCP_PWM_FULL_BRIDGE | CCP_PWM_H_H);
set_tris_c(0xC3); // Set pins C2-C5 as outputs
delay_ms(5000);
// Run in reverse for 5 seconds.
setup_ccp1(CCP_PWM_FULL_BRIDGE_REV | CCP_PWM_H_H);
set_tris_c(0xC3); // Set pins C2-C5 as outputs
delay_ms(5000);
}
} |
Then compile it and program it. Then when it's running, look at pin C2
in forward mode, and pin C4 in reverse mode. You should see PWM
pulses on them. |
|
|
geshsoft
Joined: 01 May 2007 Posts: 14 Location: California
|
|
Posted: Sat May 05, 2007 2:14 am |
|
|
Thanks, this worked
Why was not the compiller setting the TRIS registers anyways? I thought that this is the advantage of the standard io... |
|
|
|