If you post the BoostC file that flowcode produces I may be able to figure out what is going wrong.
Mike.
Go on then, you've tempted me.
This code, is not my pd controller code. All this code does is pass integer of value '255' to the 10-bit pwm controller code (flowcode bug then maybe?)
The result is that flowcode simulates an approx 25% duty signal, where measuring it using a software simulator with scope actually gives me 100%, and measuring the voltage out with an analogue meter gives me full 12v (I'm using it with a mosfet for solenoid driving).
If I get rid of the 10-bit controller completely, and bit bang a 25% signal at any particular frequency, I get an analogue reading of 3v from the multimeter.
I don't think the pic configuration of ccp frequency settings are of any importance as the problem can be repeated for any pic, and any ccp setup. (definitely a flowcode bug then?
)
anyway, without further ado here's the compiled code:
Code:
[b]//This is the configuration code generated by flowcode:[/b]
#define MX_PIC
//Defines for microcontroller
#define P12F683
#define MX_EE
#define MX_EE_TYPE1
#define MX_EE_SIZE 256
#define MX_PWM
#define MX_PWM_CNT 1
#define MX_PWM_TRIS1 trisio
#define MX_PWM_1 2
//Functions
#define MX_CLK_SPEED 19660800
#ifdef _BOOSTC
#include <system.h>
#endif
#ifdef HI_TECH_C
#include <pic.h>
#endif
//Configuration data
//Internal functions
#include "C:\Program Files\Matrix Multimedia\Flowcode V4\FCD\internals.h"
//Macro function declarations
//Variable declarations
//Defines:
/**** Macro Substitutions ****
255 = Timer 2 Rollover Value
0x04 = Timer 2 Prescaler Value
%c = Unused
0 = Alternate Pin FCD_PWM0_Enable
******************************/
//PWM0: //Macro function declarations
void FCD_PWM0_Enable(char nIdx);
void FCD_PWM0_Disable(char nIdx);
void FCD_PWM0_SetDutyCycle(char nIdx, char nDuty);
void FCD_PWM0_ChangePeriod(char nPeriodVal, char nPrescalerVal);
void FCD_PWM0_SetDutyCycle10bit(char nIdx, short nDuty);
//PWM0: //Macro implementations
void FCD_PWM0_Enable(char nIdx)
{
//error checking
#ifndef MX_PWM
#warning "This chip does not have PWM capability"
#else
#ifndef MX_PWM_CNT
#pragma error FCD file error (no MX_PWM_CNT)
#endif
#if (MX_PWM_CNT < 1)
#pragma error FCD file error (MX_PWM_CNT < 1)
#endif
#if (MX_PWM_CNT > 2)
#pragma error FCD file error (MX_PWM_CNT > 2)
#endif
#ifndef MX_PWM_TRIS1
#pragma error FCD file error (no MX_PWM_TRIS1)
#endif
#ifndef MX_PWM_1
#pragma error FCD file error (no MX_PWM_1)
#endif
#if (MX_PWM_CNT == 2)
#ifndef MX_PWM_TRIS2
#pragma error FCD file error (no MX_PWM_TRIS2)
#endif
#ifndef MX_PWM_2
#pragma error FCD file error (no MX_PWM_2)
#endif
#endif
#if (0 == 1)
#ifndef MX_PWM_TRIS1a
#pragma error PWM component error (using alternative, but no MX_PWM_TRIS1a)
#define MX_PWM_ALT_ERROR
#endif
#ifndef MX_PWM_1a
#pragma error PWM component error (using alternative, but no MX_PWM_1a)
#define MX_PWM_ALT_ERROR
#endif
#endif
#if (0 == 2)
#ifndef MX_PWM_TRIS2a
#pragma error PWM component error (using alternative, but no MX_PWM_TRIS2a)
#define MX_PWM_ALT_ERROR
#endif
#ifndef MX_PWM_2a
#pragma error PWM component error (using alternative, but no MX_PWM_2a)
#define MX_PWM_ALT_ERROR
#endif
#endif
#endif
#ifdef MX_PWM
pr2 = 255;
t2con = 0x04;
#if (MX_PWM_CNT >= 1)
if (nIdx == 1)
{
ccp1con = 0x0C;
#if (0 == 1)
#ifndef MX_PWM_ALT_ERROR
clear_bit(MX_PWM_TRIS1a, MX_PWM_1a);
#endif
#else
clear_bit(MX_PWM_TRIS1, MX_PWM_1);
#endif
}
#endif
#if (MX_PWM_CNT >= 2)
if (nIdx == 2)
{
ccp2con = 0x0C;
#if (0 == 2)
#ifndef MX_PWM_ALT_ERROR
clear_bit(MX_PWM_TRIS2a, MX_PWM_2a);
#endif
#else
clear_bit(MX_PWM_TRIS2, MX_PWM_2);
#endif
}
#endif
#endif
#ifdef MX_PWM_ALT_ERROR
#undef MX_PWM_ALT_ERROR
#endif
}
void FCD_PWM0_Disable(char nIdx)
{
//error checking
#ifndef MX_PWM
#warning "This chip does not have PWM capability"
#endif
#ifdef MX_PWM
#if (MX_PWM_CNT >= 1)
if (nIdx == 1)
{
ccp1con = 0x00;
}
#endif
#if (MX_PWM_CNT >= 2)
if (nIdx == 2)
{
ccp2con = 0x00;
}
#endif
#endif
}
void FCD_PWM0_SetDutyCycle(char nIdx, char nDuty)
{
//error checking
#ifndef MX_PWM
#warning "This chip does not have PWM capability"
#endif
#ifdef MX_PWM
#if (MX_PWM_CNT >= 1)
if (nIdx == 1)
{
ccpr1l = nDuty;
}
#endif
#if (MX_PWM_CNT >= 2)
if (nIdx == 2)
{
ccpr2l = nDuty;
}
#endif
#endif
}
void FCD_PWM0_ChangePeriod(char nPeriodVal, char nPrescalerVal)
{
//error checking
#ifndef MX_PWM
#warning "This chip does not have PWM capability"
#endif
#ifdef MX_PWM
pr2 = nPeriodVal;
switch (nPrescalerVal)
{
case 1:
t2con = 0x04;
break;
case 4:
t2con = 0x05;
break;
case 16:
t2con = 0x06;
break;
}
#endif
}
[b]void FCD_PWM0_SetDutyCycle10bit(char nIdx, short nDuty)
{
char nCCPxCONtemp;
//error checking
#ifndef MX_PWM
#warning "This chip does not have PWM capability"
#endif
#ifdef MX_PWM
#if (MX_PWM_CNT >= 1)
if (nIdx == 1)
{
ccpr1l = (nDuty & 0x3FC) >> 2;
nDuty = (nDuty & 0x03) << 4;
nCCPxCONtemp = (ccp1con & 0xCF) | nDuty;
ccp1con = nCCPxCONtemp;
}
#endif
#if (MX_PWM_CNT >= 2)
if (nIdx == 2)
{
ccpr2l = (nDuty & 0x3FC) >> 2;
nDuty = (nDuty & 0x03) << 4;
nCCPxCONtemp = (ccp2con & 0xCF) | nDuty;
ccp2con = nCCPxCONtemp;
}
#endif
#endif
}[/b]
//Macro implementations
void main()
{
//Initialisation
ansel = 0x00;
cmcon0 = 0x07;
//Interrupt initialisation code
option_reg = 0xC0;
[b]//My program ACTUALLY STARTS here: (end of configuration)[/b]
[b]// three steps to my program: Initialise pwm -> set pwm to 255(10 bit) -> endless loop[/b]
//Call Component Macro
//Call Component Macro: PWM(0)::Enable(1)
FCD_PWM0_Enable(1);
[b]
//Call Component Macro
//Call Component Macro: PWM(0)::SetDutyCycle10bit(1, 255)
FCD_PWM0_SetDutyCycle10bit(1, 255);
[/b]
//Loop
//Loop: While 1
while (1)
{
}
mainendloop: goto mainendloop;
}
void MX_INTERRUPT_MACRO(void)
{
}
I'm not that C minded (tried to learn it a few times, I use assembly for very short projects, and flowcode for anything bigger) C would probably be a good intermediate step for me,
anywho, that code is a bit easier to read through than I expected. There's pages of configuration to look through, but when it comes to the actual program code at the bottom, it's only two function calls and an endless loop!
Anyway to make your life easier ive emboldened anything that i think will be of particular interest, added a couple of comments too to try and make life easier...try anyway.
If I understood the code at the pwm_setduty10bit function, personally i'd step through the code with paper and a pen seeing the result if the input integer was 256 (which works - approx 25%) then i'd do the same for 255 (which doesn't) and see where the math is crapping up....
cheers!
Kris