|
|
View previous topic :: View next topic |
Author |
Message |
aliaj00
Joined: 11 May 2011 Posts: 24
|
need help with PWM |
Posted: Thu Jun 20, 2013 9:37 am |
|
|
hi guys
I have put up a small frequency generator using the pwm of the pic16f887. I have tested small code to check the generation of different PWM from serial input. But i want to input the frequency to the PIC via serial and to let the pic calculate and generate the signal.
The issue i am facing is that i have put the variables on a structure and i can calculate the data from it. But when i need to assign the value of a struct variable to a global variable or another struct variable it does not work. The gets(b) is to halt the program so i can see the output on the terminal
The code that is not working is this:
Calc_PR2_DUTY_nga_DIV( &FRQ ,Prescaler_var_1);
printf("\f FRQ.PR2 %lu \r",FRQ.PR2);
gets(DIV);
printf("\f tani duhet qe FRQ_var.PR2_1 te marr vleren e FRQ.PR2: %lu \r",FRQ.PR2);
gets(DIV);
FRQ_var.PR2_1 = FRQ.PR2; here i cannot assign the value from FRQ.PR2 to FRQ_var.PR2_1 PLEASE HELP
printf("\f FRQ_var.PR2_1 %lu \r",FRQ_var.PR2_1);
gets(DIV);
Code is attached maybe someone will help me in figuring out what i am missing. be aware the code is very messy and without cleaning
Code: |
#include <LCD-rev2.h>
#include "lcdrabah.c"
#include <STDLIB.H>
#include <string.h>
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
/////////////
struct frequency_data
{
int32 frequency;
int32 frequenca_mesatare;
int16 PWM_F;
int32 PR2;
int16 Duty_Cycle;
};
struct frequency_var
{
int32 frequency_1;
int16 PWM_F_1;
int32 PR2_1;
int16 Duty_Cycle_1;
int16 prescaler_1;
//
int32 frequency_2;
int16 PWM_F_2;
int32 PR2_2;
int16 Duty_Cycle_2;
int16 prescaler_2;
//
int32 frequency_3;
int32 PWM_F_3;
int32 PR2_3;
int16 Duty_Cycle_3;
int16 prescaler_3;
};
typedef struct frequency_data Frequency_data;
typedef struct frequency_var Frequency_var;
/////////////
/////////////
void Calc_PR2_DUTY_nga_DIV( Frequency_data FRQ , int16 Prescaler)
{
char b;
printf("\f Frekuenca e paracaktuar e PIC16F887 ne kod eshte: %lu \r"FRQ.frequency);
gets(b);
printf("\f Pwm qe kerkuam %lu \r",FRQ.PWM_F);
gets(b);
FRQ.frequenca_mesatare = FRQ.frequency/4;
printf("\f calc FRQ.frequenca_mesatare faza fillestare %lu \r",FRQ.frequenca_mesatare);
gets(b);
switch (Prescaler)
{
//printf("\f prescaler i funksionit Calc_PR2_DUTY_nga_DIV : %lu \r",Prescaler);
//gets(DIV);
case 1:
FRQ.PR2 = FRQ.frequenca_mesatare /FRQ.PWM_F - 1;
printf("\f calc FRQ.PR2 per prescaler 1: %lu \r",FRQ.PR2);
gets(b);
break;
case 4:
FRQ.PR2 = FRQ.frequenca_mesatare /FRQ.PWM_F * 4;
printf("\f calc FRQ.PR2 per prescaler 4: %lu \r",FRQ.PR2);
gets(b);
break;
case 16:
FRQ.PR2 = FRQ.frequenca_mesatare /FRQ.PWM_F * 16;
printf("\f calc FRQ.PR2 per prescaler 16: %lu \r",FRQ.PR2);
gets(b);
delay_ms(1000);
default:
printf("\f Default nuk u pranua: %lu \r",FRQ.PR2);
gets(b);
FRQ.PR2 = 1;
break;
}
FRQ.DUTY_CYcle = (FRQ.PR2 + 1) * Prescaler;
}
/////////////
void Setup_PWM (int16 i, int16 y)
{
printf("\f Setup_PWM: DIV: %lu PR2: %lu \r",i,y);
setup_timer_2(i,y,1);
}
void main()
{
int16 PR2_Final;
int16 Duty_cycle_final;
int16 Prescaler_Final;
int16 Prescaler_var_1 = 1;
int16 Prescaler_var_4 = 4;
int16 Prescaler_var_16 = 16;
frequency_data FRQ;
frequency_var FRQ_var;
setup_ccp1(CCP_PWM);
lcd_init();
lcd_gotoxy(1,1);
lcd_putc("OK u ndez");
delay_ms(1000);
char DIV[30],PR2[30],Duty[30],Frekuenca[30];
unsigned int16 a=0,b=0;//frekuenca=0;
while(true)
{
printf("\f Jepni vleren e Frekuences qe doni \r");
lcd_putc("\f Jepni vleren e Frekuence qe doni\r");
gets(Frekuenca);
printf(lcd_putc,"\f%4s \r",Frekuenca);
gets(DIV);
FRQ.PWM_F=atol(Frekuenca);
FRQ.frequency = 4000000;
printf("\f vendoset Frekuencen %lu \r",FRQ.PWM_F);
gets(DIV);
/////////////
/////////////
Calc_PR2_DUTY_nga_DIV( &FRQ ,Prescaler_var_1);
printf("\f FRQ.PR2 %lu \r",FRQ.PR2);
gets(DIV);
printf("\f tani duhet qe FRQ_var.PR2_1 te marr vleren e FRQ.PR2: %lu \r",FRQ.PR2);
gets(DIV);
FRQ_var.PR2_1 = FRQ.PR2;
printf("\f FRQ_var.PR2_1 %lu \r",FRQ_var.PR2_1);
gets(DIV);
FRQ_var.Duty_Cycle_1 = FRQ.Duty_Cycle;
FRQ_var.prescaler_1 = 1;
printf("\f FRQ_var.PR2_1 %lu \r",FRQ_var.PR2_1);
gets(DIV);
printf("\f FRQ_var.Duty_Cycle_1 %lu \r",FRQ_var.Duty_Cycle_1);
gets(DIV);
printf("\f FRQ_var.prescaler_1 %lu \r",FRQ_var.prescaler_1);
gets(DIV);
/////////////
/////////////
Calc_PR2_DUTY_nga_DIV( &FRQ ,Prescaler_var_4);
FRQ_var.PR2_2 = FRQ.PR2;
printf("\f FRQ_var.PR2_2 %lu \r",FRQ_var.PR2_2);
gets(DIV);
FRQ_var.Duty_Cycle_2 = FRQ.Duty_Cycle;
printf("\f FRQ_var.Duty_Cycle_2 %lu \r",FRQ_var.Duty_Cycle_2);
gets(DIV);
FRQ_var.prescaler_2 = 4;
printf("\f FRQ_var.prescaler_2 %lu \r",FRQ_var.prescaler_2);
gets(DIV);
/////////////
/////////////
Calc_PR2_DUTY_nga_DIV(&FRQ ,Prescaler_var_16);
FRQ_var.PR2_3 = FRQ.PR2;
printf("\f FRQ_var.PR2_3 %lu \r",FRQ_var.PR2_3);
gets(DIV);
FRQ_var.Duty_Cycle_3 = FRQ.Duty_Cycle;
FRQ_var.prescaler_3 = 16;
printf("\f FRQ_var.prescaler_2 %lu \r",FRQ_var.prescaler_3);
gets(DIV);
if (FRQ_var.Duty_Cycle_1 > FRQ_var.Duty_Cycle_2 && FRQ_var.Duty_Cycle_1 > FRQ_var.Duty_Cycle_2)
PR2_Final = FRQ_var.Duty_Cycle_1;
Duty_cycle_final = FRQ_var.Duty_Cycle_1;
Prescaler_Final = FRQ_var.prescaler_1;
if (FRQ_var.Duty_Cycle_2 > FRQ_var.Duty_Cycle_1 && FRQ_var.Duty_Cycle_2 > FRQ_var.Duty_Cycle_3)
PR2_Final = FRQ_var.Duty_Cycle_2;
Duty_cycle_final = FRQ_var.Duty_Cycle_2;
Prescaler_Final = FRQ_var.prescaler_2;
if (FRQ_var.Duty_Cycle_3 > FRQ_var.Duty_Cycle_1 && FRQ_var.Duty_Cycle_3 > FRQ_var.Duty_Cycle_2)
PR2_Final = FRQ_var.Duty_Cycle_3;
Duty_cycle_final = FRQ_var.Duty_Cycle_3;
Prescaler_Final = FRQ_var.prescaler_3;
///////////
Setup_PWM (Prescaler_Final,PR2_Final);
set_pwm1_duty(30);
////////
gets(DIV);
}
} |
|
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Thu Jun 20, 2013 11:09 am |
|
|
For me to even consider this one you have to do some work.
Reduce your code to some 10 lines which show the issue.
Then you've got more of a chance.
Mike |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jun 20, 2013 11:12 am |
|
|
Quote: | when i need to assign the value of a struct variable to a global variable or
another struct variable it does not work |
That isn't a pwm problem. It's a structure variable access problem.
Make a test program that can be run in MPLAB simulator. The test
program should only contain code necessary to test the problem.
If I compile the program below with CCS vs. 4.141 and run it in MPLAB
Simulator, I get this result:
Quote: | FRQ_var.PR2_1 12345678 |
It's correct.
What is your compiler version ? Please don't say 5.006.
Also, you are doing some things below that are not supported, at least
in vs. 4.xxx. You are declaring local variables in mid-code. Quote: | lcd_gotoxy(1,1);
lcd_putc("OK u ndez");
delay_ms(1000);
char DIV[30],PR2[30],Duty[30],Frekuenca[30];
unsigned int16 a=0,b=0;//frekuenca=0; |
You should declare them at the start of the function:
Test program:
Code: |
#include <16F887.H>
#fuses INTRC_IO, NOWDT, BROWNOUT, PUT, NOLVP
#use delay(clock=4M)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
struct frequency_data
{
int32 frequency;
int32 frequenca_mesatare;
int16 PWM_F;
int32 PR2;
int16 Duty_Cycle;
};
struct frequency_var
{
int32 frequency_1;
int16 PWM_F_1;
int32 PR2_1;
int16 Duty_Cycle_1;
int16 prescaler_1;
//
int32 frequency_2;
int16 PWM_F_2;
int32 PR2_2;
int16 Duty_Cycle_2;
int16 prescaler_2;
//
int32 frequency_3;
int32 PWM_F_3;
int32 PR2_3;
int16 Duty_Cycle_3;
int16 prescaler_3;
};
typedef struct frequency_data Frequency_data;
typedef struct frequency_var Frequency_var;
//==========================================
void main()
{
frequency_data FRQ;
frequency_var FRQ_var;
FRQ.PR2 = 12345678;
FRQ_var.PR2_1 = FRQ.PR2;
printf("FRQ_var.PR2_1 %lu \r",FRQ_var.PR2_1);
while(1);
} |
|
|
|
aliaj00
Joined: 11 May 2011 Posts: 24
|
thanks |
Posted: Thu Jun 20, 2013 11:29 am |
|
|
hi the issue that i got on compiling is the following:
error 167 test-struct.c line26 assignment invalid: lValue is READ ONLY
i made a test program as the following to check but it has the same issue. i know i dont know much but any help is very much appreciated:
Code: |
#include <test-struct.h>
#include "lcdrabah.c"
#include <STDLIB.H>
#include <string.h>
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
struct frequency_data
{
char frequency[10];
};
void display(struct frequency_data FRQ)
{
printf(" Output\nName: %s",FRQ.frequency);
}
void main()
{
char koli[10];
struct frequency_data *test; //pointer to structure
//setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
struct frequency_data s1;//structure
printf( "Enter Frequency: ");
gets(&s1.frequency);// getting serial in to structure member
test =&s1;// getting pointer to structure
//koli = test->frequency; //failing to get the value from test.frequency to koli if i comment it out the code works
printf("koli eshte : %s",test->frequency ); //working it print it on the serial
//printf("koli eshte : %s",koli );
//test = (char*)s1.frequency;
//printf ("-.....-: %s",test);
//printf(" Output\nName: %s",&my_struct_pointer.frequency);
//display(s1);
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jun 20, 2013 11:53 am |
|
|
I have to go work on company stuff for a few hours, so I can't help until
later in the afternoon. Maybe someone else can help for a while. |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Thu Jun 20, 2013 12:02 pm |
|
|
You're still not telling us your compiler version.
Also, you need to post short complete, compilable code we can copy and paste to test.
Mike |
|
|
|
|
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
|