how to save the value of an int on C for pic - Page 1
Forum section: electronics / beginners

author: little_carlos
author_rank: Regular Contributor
timestamp: June 11, 2016, 05:42:22 am
content: 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 leveli 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 codeCode: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;char Text;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);}}

author: rolfe
author_rank: Newbie
timestamp: June 11, 2016, 05:57:11 am
content: This:if(Tem = i-10){should be this:if(Tem == i-10){

author: PartialDischarge
author_rank: Super Contributor
timestamp: June 11, 2016, 05:59:39 am
content: 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

author: ebclr
author_rank: Super Contributor
timestamp: June 11, 2016, 06:28:50 am
content: if(Tem >i ){b = 1;}else{b = b;Much easierb = (Tem >i );

author: AndyC_772
author_rank: Super Contributor
timestamp: June 11, 2016, 06:37:39 am
content: Bug! That won't keep the value of b constant if (Tem >i) is not true.How about:b = (Tem > i) ? 1 : b;

author: grumpydoc
author_rank: Super Contributor
timestamp: June 11, 2016, 06:54:41 am
content: Quote from: AndyC_772 on June 11, 2016, 06:37:39 amHow 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;

author: PartialDischarge
author_rank: Super Contributor
timestamp: June 11, 2016, 07:03:19 am
content: 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

author: AndyC_772
author_rank: Super Contributor
timestamp: June 11, 2016, 07:11:48 am
content: Quote from: grumpydoc on June 11, 2016, 06:54:41 amI like the ? operator

author: grumpydoc
author_rank: Super Contributor
timestamp: June 11, 2016, 10:05:46 am
content: Quote from: MasterTech on June 11, 2016, 07:03:19 amWell, the whole thing is too horrible to contemplate so we might as well look at he minutiaeQuote from: little_carlos on June 11, 2016, 05:42:22 amYour 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();}}Quote from: AndyC_772 on June 11, 2016, 07:11:48 amI didn't say that I didn't like it

author: newbrain
author_rank: Super Contributor
timestamp: June 11, 2016, 01:16:55 pm
content: Quote from: grumpydoc on June 11, 2016, 10:05:46 amThis.@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 stylesare 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 agood 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!Quote from: AndyC_772 on June 11, 2016, 07:11:48 amI 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.

author: little_carlos
author_rank: Regular Contributor
timestamp: June 11, 2016, 01:57:11 pm
content: Quote from: MasterTech on June 11, 2016, 05:59:39 amno pudiste entender un codigo tan simple como ese? lol no debes ser buen programadorPor cierto, al comparar i con la variable flotante si da el resultado deseado

author: grumpydoc
author_rank: Super Contributor
timestamp: June 11, 2016, 02:35:51 pm
content: Quote from: little_carlos on June 11, 2016, 01:57:11 pmComparing "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 isnotmeaningful.Also the linebit i;followed byi = 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.

author: PartialDischarge
author_rank: Super Contributor
timestamp: June 11, 2016, 02:36:28 pm
content: Quote from: little_carlos on June 11, 2016, 01:57:11 pmLittle_carlos is telling me that I must be a bad programmerKeep it up kid!

author: wraper
author_rank: Unknown
timestamp: June 11, 2016, 02:50:22 pm
content: Quote from: little_carlos on June 11, 2016, 01:57:11 pmLOL, 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.