Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics 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.

explain this code please-- mikroc pic

Status
Not open for further replies.

mailus

Full Member level 4
Full Member level 4
Joined
Mar 6, 2012
Messages
234
Helped
7
Reputation
16
Reaction score
7
Trophy points
1,308
Location
-
Activity points
2,706
please explain this code....


program......

Code:
*/
// LCD module connections
 sbit LCD_RS at RC4_bit;
 sbit LCD_EN at RC5_bit;
 sbit LCD_D4 at RC0_bit;
 sbit LCD_D5 at RC1_bit;
 sbit LCD_D6 at RC2_bit;
 sbit LCD_D7 at RC3_bit;
 sbit LCD_RS_Direction at TRISC4_bit;
 sbit LCD_EN_Direction at TRISC5_bit;
 sbit LCD_D4_Direction at TRISC0_bit;
 sbit LCD_D5_Direction at TRISC1_bit;
 sbit LCD_D6_Direction at TRISC2_bit;
 sbit LCD_D7_Direction at TRISC3_bit;
// End LCD module connections
// Define Messages
 char message1[] = "ADC Value= ";
 char *temp = "0000";
 unsigned int ADC_Value;
 void main() {
  ANSEL = 0b00000100; // RA2/AN2 is analog input
  ADCON0 = 0b00001000; // Analog channel select @ AN2
  CMCON0 = 0x07 ; // Disbale comparators
  TRISC = 0b00000000; // PORTC All Outputs
  TRISA = 0b00001100; // PORTA All Outputs, Except RA3 and RA2
  Lcd_Init();                      // Initialize LCD

  Lcd_Cmd(_LCD_CLEAR);             // CLEAR display
  Lcd_Cmd(_LCD_CURSOR_OFF);        // Cursor off
  Lcd_Out(1,1,message1);           // Write message1 in 1st row
  do {
   adc_value = ADC_Read(2);
   temp[0] = adc_value/1000 + 48; // Add 48 to get the ASCII character value
   temp[1] = (adc_value/100)%10 + 48;
   temp[2] = (adc_value/10)%10 + 48;
   temp[3] = adc_value%10 + 48;
   Lcd_Out(1,11,temp);
   Delay_ms(2000);
  } while(1);
 }


need explanation for these....

Code:
char *temp = "0000";

Code:
 temp[0] = adc_value/1000 + 48; // Add 48 to get the ASCII character value
   temp[1] = (adc_value/100)%10 + 48;
   temp[2] = (adc_value/10)%10 + 48;
   temp[3] = adc_value%10 + 48;
   Lcd_Out(1,11,temp);


here "(2)" indicates channel number is it correct...
Code:
  adc_value = ADC_Read(2);
 

in micro c there are a large variety of libraries.
adc_value = ADC_Read(2) assigns the adc_value variable with the analogue value on the third analogue channel AN0.it is already commented in the program at the first line of main function.
micro c converts the value in to an integer. we can't print an integer directly on lcd.so we have to convert it in to asci values or text.there are lots of conversion libraries in micro c.but the simple conversion is to extract each digit and convert in to text.
here we use a character array temp and initialized the four members to zero by char *temp = "0000"
now assume the four digit value hold by the variable adc_value as 1234.
the first digit is extracted by dividing the value by 1000.then we get '1' and remainder 234.
1 is converted in to displayable form by adding 48 with it (text).
and the remainders first digit '2' is extracted by dividing it with 100 and so on.
finally integer 1024 assigned in adc_value is converted in to text "1024" and stored in the character array temp.
it is printed using lcd library function. read the parameter type of library functions first.



try c if you are a biginner.
 
  • Like
Reactions: mailus

    mailus

    Points: 2
    Helpful Answer Positive Rating
explain this....what is the function of char*temp="0000" and temp[1]
Code:
char *temp = "0000";
temp[0]
temp[1]
temp[2]
temp[3]
 

What's the function on c. is this pointer and array function.
 

Code:
 char *temp = "0000";
means a pointer to a string variable is declared and it is pointing to a string "0000". If you do not assign "0000" to the pointer then it doesn't work. Instead of
Code:
 char *temp = "0000"
you can try
Code:
 char temp[5];
By defining "0000" you are actually defining the length of the pointer. If you do not declare "0000" then the pointers lenght will be zero and so it cannot contain any value.
 
Where I can reference for this?
 

it's ok....
but my doubt is
what the relation between char *temp = "0000"; and temp[0] = adc_value/1000 + 48;


Code:
 char message1[] = "ADC Value= ";
 char *temp = "0000";
 unsigned int ADC_Value;
 void main() {
  ANSEL = 0b00000100; // RA2/AN2 is analog input
  ADCON0 = 0b00001000; // Analog channel select @ AN2
  CMCON0 = 0x07 ; // Disbale comparators
  TRISC = 0b00000000; // PORTC All Outputs
  TRISA = 0b00001100; // PORTA All Outputs, Except RA3 and RA2
  Lcd_Init();                      // Initialize LCD

  Lcd_Cmd(_LCD_CLEAR);             // CLEAR display
  Lcd_Cmd(_LCD_CURSOR_OFF);        // Cursor off
  Lcd_Out(1,1,message1);           // Write message1 in 1st row
  do {
   adc_value = ADC_Read(2);
   temp[0] = adc_value/1000 + 48; // Add 48 to get the ASCII character value
   temp[1] = (adc_value/100)%10 + 48;
   temp[2] = (adc_value/10)%10 + 48;
   temp[3] = adc_value%10 + 48;
   Lcd_Out(1,11,temp);
   Delay_ms(2000);
  } while(1);
 }
 

Code:
char *temp = "0000";
the memory space is assigned.
Code:
temp[0] = adc_value/1000 + 48
the first value calculated is saved in the initial location of the assigned memory block and so on..
 

Code:
 char *temp = "0000"
reserves 4 bytes of memory which are temp[0], temp[1], temp[2], and temp[3].

Code:
 temp[0] = adc_value/1000 + 48;
extracts the digit in thousandth place and assigns it to temp[0] location. Likewise 100th place digit is extracted and assigned to temp[1], 10s digit is extracted and assigned to temp[2] and units place digit is extracted and assigned to temp[3].
 
  • Like
Reactions: mailus

    mailus

    Points: 2
    Helpful Answer Positive Rating
thank you for your valuable reply....

"%"operator performs modulus operation and it find the prim number....
but for example

ADC_value =1234; performs the operation ADC_value/1000 + 48;

final answer is 1 in ASCII;

similarly
(adc_value/100)%10 + 48;
it's output is "2" in ASCII;
is it correct or wrong?
 

1234/100 =12.34 but the result gets truncated to an integer so you get 12

12%10 gives a result of 2 because 10 fits in 12 once and the remainder is 2

2+ 48 results in ASCII char '2'

- - - Updated - - -

Code:
char *temp = "0000";

My suggestion would be to avoid this kind of declaration because depending on the compiler it will be a read/write or read only and may cause problems

https://www.lysator.liu.se/c/c-faq/c-17.html#17-20
https://stackoverflow.com/questions/2589949/c-string-literals-where-do-they-go

use

Code:
char temp[] = "0000";
 
  • Like
Reactions: mailus

    mailus

    Points: 2
    Helpful Answer Positive Rating
thank you....

- - - Updated - - -

how to disable ccp module in pic 16f88. i use mikroc pic pro.
 

Code:
CCP1CON = 0x00;

or

Code:
CCP1CON.CCP1M3 = 0;
CCP1CON.CCP1M2 = 0;
CCP1CON.CCP1M1 = 0;
CCP1CON.CCP1M0 = 0;

disables the CCP (Capture Compare PWM) module.
 
Last edited:

yes. I do this already ccp1con=0x00;
But RB3 pin always in gray colour in proteus simulation.y?
 

yes i reinstalled...but same problem...
 

Sorry sorry.... I am wrong. It's works fine...
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top