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.

MikroC LCD problem !!

Status
Not open for further replies.

mr_byte31

Full Member level 5
Joined
Oct 19, 2005
Messages
295
Helped
10
Reputation
20
Reaction score
8
Trophy points
1,298
Activity points
3,241
Hi All,

I made a small project to read ADC results and get their difference.

My MCU Atmega328p. Compiler MikroC 7

the problem is with LCD :

Code:
sbit LCD_RS at PORTD0_bit;
sbit LCD_EN at PORTD1_bit;
sbit LCD_D4 at PORTD2_bit;
sbit LCD_D5 at PORTD3_bit;
sbit LCD_D6 at PORTD4_bit;
sbit LCD_D7 at PORTD5_bit;

sbit LCD_RS_Direction at DDD0_bit;
sbit LCD_EN_Direction at DDD1_bit;
sbit LCD_D4_Direction at DDD2_bit;
sbit LCD_D5_Direction at DDD3_bit;
sbit LCD_D6_Direction at DDD4_bit;
sbit LCD_D7_Direction at DDD5_bit;

void main()
{
 unsigned int Adc_0,Adc_1;
 int Adc_diff=0;
 char strArray[16]= "0123456789";

 LCD_Init();
 ADC_Init();
 Lcd_Cmd(_LCD_CLEAR);
 LCD_Cmd(_LCD_CURSOR_OFF);

 while(1)
 {
	LCD_Out(1,1,"Moh v3 :");
	Adc_0 = ADC_Read(0);
	Adc_1 = ADC_Read(1);
	Adc_diff = Adc_0 - Adc_1;
	if(Adc_diff < 0)
	{
	  Adc_diff = Adc_diff *-1;
	}
	 LCD_Chr(2,1,strArray[Adc_diff/1000%10]);
	 LCD_Chr(2,2,strArray[Adc_diff/100%10]);
	 LCD_Chr(2,3,strArray[Adc_diff/10%10]);
	 LCD_Chr(2,4,strArray[Adc_diff/1%10]);
 }
 
}
the output looks like :
Capture.png


when I initialize the first variables, it works fine
Code:
unsigned int Adc_0=0,Adc_1=0;

any comment ?
 
Last edited:

hello,

unsigned int Adc_0,Adc_1;

use signed int to get negative value ...
 

the adc will not provide negative values !
 

True, but when you subtract the two positive ADC results you can get a negative result.
If you only want to see the difference, use abs() to make everything positive.

Brian.
 

According to C implicit type conversion rules, the code should work, though. I'm not sure if mikro C implements C language rules strictly. You better debug the code execution with suitable means (simulator, in-circuit debugger).
 

This code seems to work for me:
Code:
void main()
{
signed int Adc_0;
signed int Adc_1;
signed int Adc_diff;
unsigned char strArray[8];

    LCD_Init();
    ADC_Init();
    Lcd_Cmd(_LCD_CLEAR);
    LCD_Cmd(_LCD_CURSOR_OFF);

    while(1)
    {
        LCD_Out(1,1,"Moh v3 :");
        Adc_0 = ADC_Read(0);
        Adc_1 = ADC_Read(1);
        Adc_diff = Adc_0 - Adc_1;
        IntToStr(Adc_diff, strArray);
        LCD_Out(2,1,strArray);
    }
}
Be sure to enable conversions library
 

@ hexreader ,Be sure to enable conversions library

how to do so ?

- - - Updated - - -

True, but when you subtract the two positive ADC results you can get a negative result.
If you only want to see the difference, use abs() to make everything positive.

Brian.

this is already done in my code !

- - - Updated - - -

According to C implicit type conversion rules, the code should work, though. I'm not sure if mikro C implements C language rules strictly. You better debug the code execution with suitable means (simulator, in-circuit debugger).

i tried it on avr and it gave the same results as proteus :)
 

Be sure to enable conversions library

how to do so ?
Click on the library manager tab and make sure that there is a tick against "conversions"

It may be that it is already selected - if the code compiles, then all is well
 

it is already selected. thanks

it is really a strange thing.

I compared the asm file for both changes and they look different in 3 lines that caused this problem !!!

super strange !
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top