|
|
View previous topic :: View next topic |
Author |
Message |
Stig
Joined: 13 May 2014 Posts: 13 Location: Belgium
|
Flowchart to program with ccs |
Posted: Tue May 13, 2014 2:39 am |
|
|
Hello, i'm a beginner in this field, someone helps me...
here you're my stupied program
Code: |
#include <16f877.h>
#use delay (clock=4000000)
#include "lcd.c"
unsigned int D, Vmax, Vmin, Imin, Vfloat;
void main()
{
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
// TODO: USER CODE!!
float Lecture, Vbat, read, Ibat;
short int charge;
charge=0;
Imin=0.8;
Vmax=14.28;
Vfloat=13.4;
Vmin=11;
lcd_init();
lcd_putc("Lecture & envoie\n a lcd");
delay_ms(1000);
lcd_putc("\f");
setup_adc_ports(AN0_AN1_AN3);
//setup_adc(ADC_CLOCK_DIV_2);
setup_adc(ADC_CLOCK_internal);
//setup_adc_ports(ALL_ANALOG);
while(TRUE) {
setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_by_16, 200, 1);
set_adc_channel(0);
delay_us(50);
Lecture=read_adc();
Vbat=((Lecture*5)/1023.0)+9.4;
set_adc_channel(3);
delay_us(50);
read=read_adc();
Ibat=((read*4.2)/1023.0);
lcd_gotoxy(1,1);
printf(lcd_putc,"V,I Batterie:\n %fV %fA ", Vbat, Ibat);
delay_us(50);
while(Vbat < 10.5) {output_high(pin_b1);
lcd_gotoxy(1,4);
printf(lcd_putc,"Discharged batt");
delay_ms(500);
lcd_putc("\f");
}
if(Ibat > 0.1) {printf(lcd_putc," Battery charger");lcd_putc("\f");}
else if (Vbat < Vmin) { charge=1;
D=170;
set_pwm1_duty(D);
delay_ms(120000);
break; }
else if (Vbat < Vmax) { if (charge==1) {
for(D=63;D<85;D++)
set_pwm1_duty(D);
delay_ms(1000); }
else { printf(lcd_putc,"Floating");
if (Vbat > Vfloat)
{
for(D=85;D>63;D--)
set_pwm1_duty(D);
delay_ms(1000); }
else {
for(D=63;D<85;D++)
set_pwm1_duty(D);
delay_ms(1000); } } }
else {
for(D=98;D>85;D--)
set_pwm1_duty(D);
delay_ms(1000);
if (Ibat > Imin && charge==1)
{ printf(lcd_putc,"Absorption");
Delay_ms(1000); }
else { printf(lcd_putc,"Floating");
if (Vbat > Vfloat)
{
for(D=85;D>63;D--)
set_pwm1_duty(D);
delay_ms(1000); }
else {
for(D=63;D<85;D++)
set_pwm1_duty(D);
delay_ms(1000);} } }
}
}
|
I want to rectify myself Any help !!![/url][/list]
Last edited by Stig on Wed May 14, 2014 5:23 am; edited 3 times in total |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Tue May 13, 2014 4:15 am |
|
|
I can't even start.
1) Use the code button to make code readable.
2) Explain what you are trying to do.
3) Tell us what your code should do.
4) Tell us what your code does/doesn't do.
5) Read the forum guidelines.
............................
Mike |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue May 13, 2014 4:53 am |
|
|
Your code is not that bad for a beginner, we all had trouble in doing the first steps.
I have to agree with Mike: if you don't tell us what the code is supposed to do and how it fails, then how are we to know?
A few things I noticed: Code: | setup_spi(SPI_SS_DISABLED); | This is an error in the old CCS program wizard. Change to:
Code: | if(Ibat > 0.1) {printf(lcd_putc," Battery charger");lcd_putc("\f");} | If I remember correctly, the command '\f' will clear the display. This means that the first text will be erased immediately before anyone can read it.
Vfloat is declared as an integer but you are using it as a float.
The variable 'charge' is changing from 0 to 1, but never back again? Perhaps that is what you want but seems strange.
Also the name could be more logical by changing it into a verb. Good practice with booleans like this is to add the word 'is' in front. For example it could become 'isCharging' or 'isCharged' (you have to choose the right one because I have no clue as to what your program is doing).
Personal preference: don't use 'short int', but use 'int1' instead. This is more portable between other compiler vendors. Then also use 'true' and 'false' instead of 0 and 1.
Please fix the errors and post the new version using the 'Code' buttons, it will preserve formatting and makes it a lot easier to read. Now it is so difficult to follow that you will get very little help. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9245 Location: Greensville,Ontario
|
|
Posted: Tue May 13, 2014 5:14 am |
|
|
gee guys I figured out his program is a 'smart battery charger' ! Keep saying I gotta build 10-12 of them as I have lots of tractors here but never seem to get around to it...
along with the other comments though
1) insert a
delay_ms(500);
before the lcd_init(); call
this gives the LCD module time to powerup in default mode before the PIC talks to it.
2) delay_ms(120000); can't work
largest delay is 65535, a bit over 60 seconds( 1 minute)
if you want a 2 minute delay just
delay_ms(60000); //delay 1 minute
delay_ms(60000); //delay another minute
it works and is a LOT easier to understand
note the different comments NOT just a copy and paste.This trick SHOWS you the 2 lines of code are different,not a type from cut/paste'
3) if you've got a 2x16 LCD( any 2 line display) consider putting 'titles' on the top line, actual 'data' on the 2nd.....
recode/recompile/retest/report back
always happy to help
Jay |
|
|
Stig
Joined: 13 May 2014 Posts: 13 Location: Belgium
|
|
Posted: Wed May 14, 2014 5:06 am |
|
|
Hey guys, thank you for your answers.
So, you want to understand my project, because I haven't illuminate those things. Sorry ... I was confused, and I need your help ... I really appreciate your jobs... that was an awesome forum.
I will introduce to you the algorithm.
and if any questions you ask, I'll be there.
Happy to be with you, in real life or Network.
Stig
Last edited by Stig on Sun May 18, 2014 1:46 am; edited 1 time in total |
|
|
Stig
Joined: 13 May 2014 Posts: 13 Location: Belgium
|
|
Posted: Wed May 14, 2014 5:15 am |
|
|
more explanation,
I'll make a solar controller based on a PIC16F877, which controlled the state of charge of the battery, and protect against deep discharge and overcharge in order to increase its life. |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Wed May 14, 2014 7:13 am |
|
|
Hi,
Learn to help yourself. We aren't here to do your project for you, only to help guide you when you get stuck!
On a project like this, you've got to break it down into small, manageable 'chunks', and only start to put it all together once the pieces are demonstrated to work.
First, is this a *real* project, with real hardware, or is it a simulation using proteus?
Is your PIC actually running? You have no #fuses declaration, so I doubt it! Connect an LED to an I/O pin (with a suitable current limiting resistor) and flash it On for 1 second, and Off for 1 second repeatedly. Does this work? Don't proceed until it does.
You've got an LCD connected to your project. Does it work? Can you see your 'sign-on' message being displayed? Don't proceed until you can.
You're attempting to take A/D readings, and display them on the LCD. Can you see reasonable values being displayed? Does this work? Don't proceed until it does.
Finally, once all this is working, is the logic of your solar controller doing what you expect? Devise a method to test and verify that it is, and implement your test plan in a thoughtful and logical manner.
John |
|
|
Stig
Joined: 13 May 2014 Posts: 13 Location: Belgium
|
|
Posted: Thu May 15, 2014 9:35 am |
|
|
# Hello All
Could someone that see with me this program, I made it and doesn't work like it should be ... the problem is that I'm not expert in codes, need some help from you or others .. any question I'll be here, actually, I'm designing a solar smart charge controller, which controlled the state of charge of the battery "deep cycle lead acid", and protect against deep discharge and overcharge in order to increase its life, to charge a battery ... we must cross those three stages, showing below in figure. "1)Bulk, 2)Absorption, 3)Floating". I used CCS compiler.
Code: |
#include <16f877.h>
#use delay (clock=4000000)
#include "lcd.c"
unsigned int D, Vmax, Vmin, Imin, Vfloat;
void main()
{
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
// TODO: USER CODE!!
float Lecture, Vbat, read, Ibat;
int1 charge;
charge=0;
Imin=0.8;
Vmax=14.28;
Vfloat=13.4;
Vmin=11;
lcd_init();
lcd_putc("Lecture & envoie\n a lcd");
delay_ms(1000);
lcd_putc("\f");
setup_adc_ports(AN0_AN1_AN3);
setup_adc(ADC_CLOCK_DIV_2);
//setup_adc(ADC_CLOCK_internal);
//setup_adc_ports(ALL_ANALOG);
while(TRUE) {
setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_by_16, 100, 1);
set_adc_channel(0);
delay_us(50);
Lecture=read_adc();
Vbat=((Lecture*5)/1023.0)+9.4;
set_adc_channel(3);
delay_us(50);
read=read_adc();
Ibat=((read*4.2)/1023.0);
lcd_gotoxy(1,1);
printf(lcd_putc,"V,I Batterie:\n %fV %fA ", Vbat, Ibat);
delay_us(50);
while(Vbat < 10.5) {
output_high(pin_b1); // if Vbat < 10.5V turn on the red Led
lcd_gotoxy(1,4);
printf(lcd_putc,"Discharged batt"); // & display (Discharged battery)
delay_ms(500);
}
if(Ibat > 0.1) {
lcd_putc("\f");
lcd_init();
printf(lcd_putc," Battery charger");
}
else if (Vbat < Vmin) {
charge=1;
D=170;
set_pwm1_duty(D);
delay_ms(6000);
lcd_putc("\f");
lcd_init();
printf(lcd_putc,"Bulk");
break;
}
else if (Vbat < Vmax) {
if (charge==1) {
for(D=63;D<85;D++)
set_pwm1_duty(D);
delay_ms(1000);
}
else {
lcd_putc("\f");
lcd_init();
printf(lcd_putc,"Floating");
if (Vbat > Vfloat) {
for(D=85;D>63;D--)
set_pwm1_duty(D);
delay_ms(1000);
}
else {
for(D=63;D<85;D++)
set_pwm1_duty(D);
delay_ms(1000);
}
}
}
else {
for(D=98;D>85;D--)
set_pwm1_duty(D);
delay_ms(1000);
if (Ibat > Imin && charge==1) {
lcd_putc("\f");
lcd_init();
printf(lcd_putc,"Absorption");
Delay_ms(1000);
}
else {
lcd_putc("\f");
lcd_init();
printf(lcd_putc,"Floating");
if (Vbat > Vfloat) {
for(D=85;D>63;D--)
set_pwm1_duty(D);
delay_ms(1000);
}
else {
for(D=63;D<85;D++)
set_pwm1_duty(D);
delay_ms(1000);
}
}
}
}
}
|
Last edited by Stig on Sun May 18, 2014 1:49 am; edited 1 time in total |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Thu May 15, 2014 10:42 am |
|
|
ezflyr has pointed you in the right direction.
Mike |
|
|
Stig
Joined: 13 May 2014 Posts: 13 Location: Belgium
|
|
Posted: Thu May 15, 2014 12:22 pm |
|
|
# come on dude, give me hand of help |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu May 15, 2014 12:42 pm |
|
|
You have blocks of code like this all over your program:
Quote: |
if (charge==1) {
for(D=63;D<85;D++)
set_pwm1_duty(D);
delay_ms(1000);
{
|
The code will run like this:
Code: |
if(charge==1)
{
for(D=63;D<85;D++) // Buzz through this loop
{
set_pwm1_duty(D);
}
delay_ms(1000); // Then do a 1 second delay (only once)
}
|
Is that really what you want ?
Or do you really want it like this ?
Code: | if (charge==1)
{
for(D=63;D<85;D++)
{
set_pwm1_duty(D); // Set a new duty cycle
delay_ms(1000); // Run it that way for 1 second
}
} |
|
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Thu May 15, 2014 1:54 pm |
|
|
Stig,
Yesterday, I laid out a concise, methodical pathway to help get your project up off the ground. Today, you said this:
Stig wrote: | # come on dude, give me hand of help |
Like I said, we aren't going to do your project for you, you've got to do it with our help! It appears that you just want to dump a large chunk of code on us and have us debug it for you. If that is the case, I say 'get lost', and find another hobby/profession, 'cause you ain't cutting it with this one!
Telling us "I made it and doesn't work like it should be ..." is almost totally meaningless to us. My suspicion is that you copied that code from somewhere, and you have no idea how it is supposed to work. Now, you want us to make it work for you, right?
Cheers,
John |
|
|
Stig
Joined: 13 May 2014 Posts: 13 Location: Belgium
|
|
Posted: Thu May 15, 2014 2:01 pm |
|
|
thank you PCM Programmer, the compiler doesn't warn me, there is no mistake this is why I didn't take the attention. |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Thu May 15, 2014 2:10 pm |
|
|
Stig wrote: | # come on dude, give me hand of help |
I do not wish to appear rude. I have ignored your PM, on purpose.
I am not your dude. Even if I were to understand what you mean.
Please play ball, otherwise you will alienate all those prepared to help.
Mike |
|
|
Stig
Joined: 13 May 2014 Posts: 13 Location: Belgium
|
|
Posted: Thu May 15, 2014 2:15 pm |
|
|
ezflyr
I really worked hard and the program made by me, that's what the cow could give... sorry |
|
|
|
|
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
|