Author Topic: how to save the value of an int on C for pic  (Read 3159 times)

0 Members and 1 Guest are viewing this topic.

Offline little_carlosTopic starter

  • Regular Contributor
  • *
  • Posts: 133
how to save the value of an int on C for pic
« on: June 11, 2016, 05:42:22 am »
hi, i made a termometer and i want an output to be ON when the temperature raises to a certain level, then  turn it off when the temperature goes down 10 degrees under the selected activation level
i tried making a statement with a int b, but it doesnt work, how can i save the value of a bit or int, so it keeps like that until something else modifies it?
here's an example of what i did on code
Code: [Select]
sbit LCD_RS at RB0_bit;
sbit LCD_EN at RB1_bit;
sbit LCD_D4 at RB2_bit;
sbit LCD_D5 at RB3_bit;
sbit LCD_D6 at RB4_bit;
sbit LCD_D7 at RB5_bit;
sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB4_bit;
sbit LCD_D7_Direction at TRISB5_bit;


unsigned int Radc = 0;     
float Tem = 0;             
char Text2[15];
char Text[15];           
char txt1[] = "Temp: ";   
bit i;
int b;
void main(){

 ANSEL  = 0x01;           
 ANSELH = 0x00;           
 TRISA0_bit = 1;         
 TRISA1_bit = 1;
 TRISA2_bit = 1;
 TRISA3_bit = 0;
 C1ON_bit = 0;           
 C2ON_bit = 0;
 i = 30;
 ADC_Init();             
 Delay_us(100);           
 PORTA = 0;
 Lcd_Init();               
 Lcd_Cmd(_LCD_CLEAR);     
 Lcd_Cmd(_LCD_CURSOR_OFF);
 Lcd_Out(1,1, txt1);     
 Lcd_Chr(1,15,223);         
 Lcd_Chr(1,16,'C');       
 Lcd_Chr(2,16,223);
 Delay_ms(50);

 while(1){
  Radc = ADC_Get_Sample(0);     
  Delay_ms(100);               
  if(RA1_bit == 1){
  Delay_ms(100);
  i++;
}
if(RA2_bit == 1){
Delay_ms(100);
i--;
}                               
if(Tem >i ){
b = 1;
}
else{
b = b;
}
if(b >= 1){
RA3_bit = 1;
if(Tem = i-10){
b = 0;
}
}

else{
RA3_bit = 0;
}                               

  Tem = (float)(Radc/2.05);
  IntToStr(i,Text2);
  FloatToStr(Tem, Text);
  Lcd_Out(2,1,"Activacion:");
  Lcd_Out(2,10,Text2);
  Lcd_Out(1,6, Text);
 
 
  Delay_ms(50);
  }
}
 

Offline rolfe

  • Newbie
  • Posts: 6
  • Country: au
  • "This is me"
Re: how to save the value of an int on C for pic
« Reply #1 on: June 11, 2016, 05:57:11 am »
This:

if(Tem = i-10){

should be this:

if(Tem == i-10){
 

Offline PartialDischarge

  • Super Contributor
  • ***
  • Posts: 1611
  • Country: 00
Re: how to save the value of an int on C for pic
« Reply #2 on: June 11, 2016, 05:59:39 am »
Tried to understand your code but couldn't since you are not saying what some variables are supposed to mean. Anyway there are some pearls like :

else{
   b = b;
}

or defining 'i' as a bit type and then comparing it to a float.
I think you should take some time to learn about types, their uses and operations.
Also try to debug your code by writing values to the display to see what they are along the routine execution
 

Offline ebclr

  • Super Contributor
  • ***
  • Posts: 2328
  • Country: 00
Re: how to save the value of an int on C for pic
« Reply #3 on: June 11, 2016, 06:28:50 am »
if(Tem >i ){
b = 1;
}
else{
b = b;


Much easier

b = (Tem >i );
 

Offline AndyC_772

  • Super Contributor
  • ***
  • Posts: 4228
  • Country: gb
  • Professional design engineer
    • Cawte Engineering | Reliable Electronics
Re: how to save the value of an int on C for pic
« Reply #4 on: June 11, 2016, 06:37:39 am »
Bug! That won't keep the value of b constant if (Tem >i) is not true.

How about:

b = (Tem > i) ? 1 : b;

Offline grumpydoc

  • Super Contributor
  • ***
  • Posts: 2905
  • Country: gb
Re: how to save the value of an int on C for pic
« Reply #5 on: June 11, 2016, 06:54:41 am »
Bug! That won't keep the value of b constant if (Tem >i) is not true.

How about:

b = (Tem > i) ? 1 : b;
How about:

if (Tem > i) {
    b=1;
}


I mean, why make it more complex than is needed - the ? operator is sometimes useful but it can make code harder to read.

That said if you want a one-liner for this I quite like the form:

(Tem > i) && b = 1;
 
The following users thanked this post: newbrain

Offline PartialDischarge

  • Super Contributor
  • ***
  • Posts: 1611
  • Country: 00
Re: how to save the value of an int on C for pic
« Reply #6 on: June 11, 2016, 07:03:19 am »
guys, you seem to be worried about code etiquette when hes substracting 10 to a bit data type and then comparing it to a float ??WTF
 

Offline AndyC_772

  • Super Contributor
  • ***
  • Posts: 4228
  • Country: gb
  • Professional design engineer
    • Cawte Engineering | Reliable Electronics
Re: how to save the value of an int on C for pic
« Reply #7 on: June 11, 2016, 07:11:48 am »
the ? operator is sometimes useful but it can make code harder to read.

I like the ? operator  8)

Offline grumpydoc

  • Super Contributor
  • ***
  • Posts: 2905
  • Country: gb
Re: how to save the value of an int on C for pic
« Reply #8 on: June 11, 2016, 10:05:46 am »
guys, you seem to be worried about code etiquette when hes substracting 10 to a bit data type and then comparing it to a float ??WTF
Well, the whole thing is too horrible to contemplate so we might as well look at he minutiae :)

hi, i made a termometer and i want an output to be ON when the temperature raises to a certain level, then  turn it off when the temperature goes down 10 degrees under the selected activation level
Your code is hard to read, confuses data types and (as posted) has lots of redundant variables EDIT: and undeclared variables. To do what you have stated (a temperature controller with hysteresis), you need something like (a bit pseudo-codeish):

/* temp_t holds temperature. Float if you want but scaled integer would be better.
   Probably needs to be a signed type */

/* Threshold to turn on. For example 20 degrees C */
temp_t threshold =  DegreesCToTempT(20);

/* Hysteresis, 10 degrees C  */
temp_t hysteresis = DegreesCToTempT(10);

/* Current state of output device */
enum {on, off} state = off;

InitialiseOutputThing();

while(1){
    temp_t cur_temp = AdcToTempT(ADC_Get_Sample(0));
    /* At or above upper threshold and currently off - turn on */
    if (cur_temp >= threshold && state == off) {
        state = on;
        SwitchOutputThingON();
    }
    /* At or below lower threshold and on - turn off */
    if (cur_temp <= threshold - hysteresis && state == on) {
        state = off;
        SwitchOutputThingOFF();
    }
}



I like the ? operator  8)

I didn't say that I didn't like it :)
« Last Edit: June 11, 2016, 01:27:51 pm by grumpydoc »
 

Online newbrain

  • Super Contributor
  • ***
  • Posts: 1719
  • Country: se
Re: how to save the value of an int on C for pic
« Reply #9 on: June 11, 2016, 01:16:55 pm »
Well, the whole thing is too horrible to contemplate so we might as well look at he minutiae :)

This.

@little_carlos:
[patronizing graybeard mode=on]
I have seen some questions posted by you, and the efforts you put in is a good thing.
Still, it seems to me that you're often programming in a not well structured way and you have a fuzzy grasp of the language.

There are some steps, I would think, you could take to improve your results to effort ratio:

1. Learn to properly indent your code, it helps a lot in understanding the relationships between the various parts and the logic behind them, both for you and for the others reading it: consider that many, me included, are put off by a "wall of code" and less inclined to spend time cleaning it up to answer your doubts.
Indentation styles are very personal, like the choice of editors, but any is better than none (I happen to like and use Allman, and can't stand GNU).
All the modern editors/IDEs support indentation and can do the work for you (Eclipse, emacs, Visual Studio, you name it, there is an ample choice of free but excellent programming environments).

2. Even more important: learn the C language from the basics. Write simple programs working from the command line (they are nearly the same in Windows, Linux and OS X as long as you remain inside standard C), work your way through the examples on a good C programming book.
When you have mastered the control flow, the different datatypes and the memory handling (scoping, variables life, dynamic memory, pointers etc.) you'll be able to tackle your embedded programs with better understanding, and not as a series of trials and errors.
If this is your first programming language, a general algorithm and data structure book can also be useful (I would not know what to advise, though, Knuth is the definitive bible but a bit "heavy").

3. A side note (very personal opinion): PICs are fine and all, but the odd language (as far as I can see: #pragmas, void main()* etc) is not helping a learner towards good style. Other systems let you use a language much closer to standard C.

* Yes, I know, void main() is perfectly fine and contemplated by the standard in an embedded environment, but let an old(ish) man some pet peeves.
[patronizing graybeard mode=off, but I'll keep my beard, thank you very much]

That said, have fun!

the ? operator is sometimes useful but it can make code harder to read.
I like the ? operator  8)
I really love it, as I love the value returning rust or python ifs, but, as with a lot of what we love, care is needed. ;)
Nandemo wa shiranai wa yo, shitteru koto dake.
 

Offline little_carlosTopic starter

  • Regular Contributor
  • *
  • Posts: 133
Re: how to save the value of an int on C for pic
« Reply #10 on: June 11, 2016, 01:57:11 pm »
Tried to understand your code but couldn't since you are not saying what some variables are supposed to mean. Anyway there are some pearls like :

else{
   b = b;
}

or defining 'i' as a bit type and then comparing it to a float.
I think you should take some time to learn about types, their uses and operations.
Also try to debug your code by writing values to the display to see what they are along the routine execution
no pudiste entender un codigo tan simple como ese? lol no debes ser buen programador
Por cierto, al comparar i con la variable flotante si da el resultado deseado
 

Offline grumpydoc

  • Super Contributor
  • ***
  • Posts: 2905
  • Country: gb
Re: how to save the value of an int on C for pic
« Reply #11 on: June 11, 2016, 02:35:51 pm »
Por cierto, al comparar i con la variable flotante si da el resultado deseado
Comparing "bit" and float types will "work" but, in general you should ask whether comparing two different types is meaningful - as one of these is numeric and one logical I would suggest that comparison is not meaningful.

Also the line
bit i;

followed by
i = 30;

Looks as though you are confused.

To quote the microchip compiler manual - "Single bit variables can be declared using the keyword bit (or __bit)"

You cannot store the value "30" in a single bit.
« Last Edit: June 11, 2016, 02:39:26 pm by grumpydoc »
 

Offline PartialDischarge

  • Super Contributor
  • ***
  • Posts: 1611
  • Country: 00
Re: how to save the value of an int on C for pic
« Reply #12 on: June 11, 2016, 02:36:28 pm »
Tried to understand your code but couldn't since you are not saying what some variables are supposed to mean. Anyway there are some pearls like :

else{
   b = b;
}

or defining 'i' as a bit type and then comparing it to a float.
I think you should take some time to learn about types, their uses and operations.
Also try to debug your code by writing values to the display to see what they are along the routine execution
no pudiste entender un codigo tan simple como ese? lol no debes ser buen programador
Por cierto, al comparar i con la variable flotante si da el resultado deseado
Little_carlos is telling me that I must be a bad programmer  :-DD :-DD :-DD
Keep it up kid!
 

Online wraper

  • Supporter
  • ****
  • Posts: 16863
  • Country: lv
Re: how to save the value of an int on C for pic
« Reply #13 on: June 11, 2016, 02:50:22 pm »
Tried to understand your code but couldn't since you are not saying what some variables are supposed to mean. Anyway there are some pearls like :

else{
   b = b;
}


or defining 'i' as a bit type and then comparing it to a float.
I think you should take some time to learn about types, their uses and operations.
Also try to debug your code by writing values to the display to see what they are along the routine execution
no pudiste entender un codigo tan simple como ese? lol no debes ser buen programador
Por cierto, al comparar i con la variable flotante si da el resultado deseado
LOL, we understand that code. Also we understand you have no clue what you are doing by that, because you are doing nothing except wasting CPU clock cycles.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf