Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

need some explaination about this code (PIC)

Status
Not open for further replies.

warrior_16th

Newbie level 6
Joined
Mar 28, 2011
Messages
11
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,366
i was searching about some temperature sensor code and i get one but actually i didnt understand it well iam just new at this field and this code have alot of function that i actually didn understand it so if anyone could help me and give me an explaination for each line i will be thank full
this the code :

int temp1;
char temp[10];
void adc(){
temp1=ADC_READ(0);
temp1=temp1*0.245*2;
INTToStr(temp1,temp);}
void main() {
TRISA = 0xFF;
TRISD=0;PORTD=0;
TRISB=0;PORTB=0;
TRISc=1;PORTc=0;
Uart1_init(9600);
Lcd_Init();
lcd_cmd(_LCD_CURSOR_OFF);
while (1) {
lcd_out(1,10,temp);
UART1_Write_Text("temperature");
UART1_Write_Text(temp);UART1_Write(13);UART1_Write(10);;Delay_ms(1000);;
adc();
} }
 

hexreader

Advanced Member level 2
Joined
Apr 7, 2011
Messages
535
Helped
194
Reputation
392
Reaction score
196
Trophy points
1,323
Location
England
Activity points
4,593
Re: need some explaination about this code

I have added comments according to my understanding of the code:


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
int temp1;                      // store ADC value and converted temperature
char temp[10];                  // store up to 9 ASCII characters plus null terminator
 
// function to read the voltage on port A0
void adc(){
  
    temp1=ADC_READ(0);          // call MikroC library function to read voltage on pin A0 (0V=0, VCC=1023)
    temp1=temp1*0.245*2;        // adjust the voltage reading to convert to temperature value
    INTToStr(temp1,temp);       // convert binary value to an ASCII string for printing later
}
 
void main() {
    TRISA = 0xFF;               // PORTA all inputs
    TRISD=0;                    // PORTD all outputs
    PORTD=0;                    // PORTD all low
    TRISB=0;                    // PORTB all outputs
    PORTB=0;                    // PORTB all low
    TRISc=1;                    // PORTC bit0 input, rest output
    PORTc=0;
 
    Uart1_init(9600);       // MikroC library function to initialise serial port
    Lcd_Init();                 // MikroC library function to initialise 2x16 character LCD
    lcd_cmd(_LCD_CURSOR_OFF);   // MIKROC library function to turn off the LCD flashing cursor
 
    while (1) {                              // loop forever
        lcd_out(1,10,temp);                  // send temperature to LCD
        UART1_Write_Text("temperature");     // send "temperature" to serial port
        UART1_Write_Text(temp);              // send value of temperature, converted to text
        UART1_Write(13);                     // carriage return
        UART1_Write(10);                     // line feed
        ;
        Delay_ms(1000);                      // 1000mS (1 second) delay
        ;
        adc();                               // read a new temperature value
    } 
 
}

Hope this helps
 

warrior_16th

Newbie level 6
Joined
Mar 28, 2011
Messages
11
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,366
Re: need some explaination about this code

THX BROTHER SO MUCH iam really thankful for u :D
 

alexan_e

Administrator
Joined
Mar 16, 2008
Messages
11,895
Helped
2,020
Reputation
4,156
Reaction score
2,031
Trophy points
1,393
Location
Greece
Activity points
64,377
Re: need some explaination about this code

What is the part you don't understand because even if you read it in simple English you will be able to understand what it is doing.

I assume that you know how to declare an array variable char temp[10]; or an integer int temp1;
temp1=ADC_READ(0); I think ADC_READ speaks for itself
This is also the case for INTToStr(temp1,temp); integer to string
Lcd_Init(); lcd initialize
lcd_cmd(_LCD_CURSOR_OFF); lcd send command
UART1_Write_Text("temperature"); even in this line, when you see uart write text what do you think it does?

You should be able to understand most of it

Alex
 

warrior_16th

Newbie level 6
Joined
Mar 28, 2011
Messages
11
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,366
Re: need some explaination about this code

What is the part you don't understand because even if you read it in simple English you will be able to understand what it is doing.

I assume that you know how to declare an array variable char temp[10]; or an integer int temp1;
temp1=ADC_READ(0); I think ADC_READ speaks for itself
This is also the case for INTToStr(temp1,temp); integer to string
Lcd_Init(); lcd initialize
lcd_cmd(_LCD_CURSOR_OFF); lcd send command
UART1_Write_Text("temperature"); even in this line, when you see uart write text what do you think it does?

You should be able to understand most of it

Alex
actually there r some lines i didnt understand such like this
int temp1;
char temp[10];
i dont know what does it mean store up to 9 ASCII characters plus null terminator


and INTToStr(temp1,temp); i new that it convert int to string but dont know why
the rest some how i can understand it
sorry for disturbing brother
 

hexreader

Advanced Member level 2
Joined
Apr 7, 2011
Messages
535
Helped
194
Reputation
392
Reaction score
196
Trophy points
1,323
Location
England
Activity points
4,593

alexan_e

Administrator
Joined
Mar 16, 2008
Messages
11,895
Helped
2,020
Reputation
4,156
Reaction score
2,031
Trophy points
1,393
Location
Greece
Activity points
64,377
Re: need some explaination about this code

actually there r some lines i didnt understand such like this
int temp1;
char temp[10];
i dont know what does it mean store up to 9 ASCII characters plus null terminator


and INTToStr(temp1,temp); i new that it convert int to string but dont know why
the rest some how i can understand it
sorry for disturbing brother
You don't disturb me at all but I though you might say something like that, if you are missing the basic knowledge of the C language (like declaring a variable) then you should really start by reading a C tutorial or book and start with simple blinking led code before you go to something that uses LCD and USART.

Alex
 

warrior_16th

Newbie level 6
Joined
Mar 28, 2011
Messages
11
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,366

warrior_16th

Newbie level 6
Joined
Mar 28, 2011
Messages
11
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,366
Re: need some explaination about this code

You don't disturb me at all but I though you might say something like that, if you are missing the basic knowledge of the C language (like declaring a variable) then you should really start by reading a C tutorial or book and start with simple blinking led code before you go to something that uses LCD and USART.

Alex
hye brother i read alot about these thing and to good extent i understand the good but still dont know how he used the same variable in the same equation
temp1=temp1*0.245*2 where the variable temp1 at left hand side represnet the temperature value and the temp1 in the right hand side represnt the voltage in binary w.r.t
the temp1 value at left hand side ,how they can be named like each other

another question sorry char temp[10] are we set it up by 10 character to represent just the numbers from 0 to 9 ? like decimal number ? or what
 

andre_teprom

Super Moderator
Staff member
Joined
Nov 7, 2006
Messages
9,181
Helped
1,141
Reputation
2,301
Reaction score
1,116
Trophy points
1,403
Location
Brazil
Activity points
53,327
It means that operation at this variable ( right side ) is stored at this same variable ( left side ).
After that command line, temp1 contains a value different than before.

+++
 

alexan_e

Administrator
Joined
Mar 16, 2008
Messages
11,895
Helped
2,020
Reputation
4,156
Reaction score
2,031
Trophy points
1,393
Location
Greece
Activity points
64,377
temp1=temp1*0.245*2
this actually tells to the compiler to read the value of temp1 from the ram, multiply it with 0.245 and with 2 and store the result back to the variable temp1
I don't know why you think that this would be a problem to do.

char temp[10]
this declares an array of 10 characters , temp[0], temp[1], temp[2], temp[3], temp[4], temp[5], temp[6], temp[7], temp[8], temp[9]
note that when you declare char temp[x] (or any array) you can access temp[0]..... temp[x-1]
it is just a group of 10 char type variables placed in contiguous memory space, you can assign any value to each of them but we usually use this array type as a string to hold some text (as letters in each position)
you can declare any type of array, int temp[10], long int temp[10] etc

In your code you get a result from the ADC, for example 100, then multiply it 100*0.245*2=450
and then use INTToStr function to convert the integer 450 to a string '4' '5' '0' that is stored in the temp array to be shown later in the LCD

Alex
 

warrior_16th

Newbie level 6
Joined
Mar 28, 2011
Messages
11
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,366
temp1=temp1*0.245*2
this actually tells to the compiler to read the value of temp1 from the ram, multiply it with 0.245 and with 2 and store the result back to the variable temp1
I don't know why you think that this would be a problem to do.

char temp[10]
this declares an array of 10 characters , temp[0], temp[1], temp[2], temp[3], temp[4], temp[5], temp[6], temp[7], temp[8], temp[9]
note that when you declare char temp[x] (or any array) you can access temp[0]..... temp[x-1]
it is just a group of 10 char type variables placed in contiguous memory space, you can assign any value to each of them but we usually use this array type as a string to hold some text (as letters in each position)
you can declare any type of array, int temp[10], long int temp[10] etc

In your code you get a result from the ADC, for example 100, then multiply it 100*0.245*2=450
and then use INTToStr function to convert the integer 450 to a string '4' '5' '0' that is stored in the temp array to be shown later in the LCD

Alex
thx bro so much its just a samll part of my project and i am nebwie with this field but i try to learn thx again
 

Status
Not open for further replies.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top