# Complex type to simple type error in Mikrobasic with programming PIC16F676

#### ChrisChemist116

Joined Mar 13, 2009
79
I am working in a project pretty simple, what it does it is to take analog signal through ADC channel 0 from PIC16F676, and the result to be displayed in a LCD HITACHI44780. However Mikrobasic keeps telling me it is not possible, i get the error, Incompatible types ("complex type" to "simple type"). Any clues what am i doing wrong?

Rich (BB code):
program Fifth_Funct
' Lcd module connections
dim LCD_RS as sbit at RC4_bit
LCD_EN as sbit at RC5_bit
LCD_D4 as sbit at RC0_bit
LCD_D5 as sbit at RC1_bit
LCD_D6 as sbit at RC2_bit
LCD_D7 as sbit at RC3_bit
LCD_RS_Direction as sbit at TRISC4_bit
LCD_EN_Direction as sbit at TRISC5_bit
LCD_D4_Direction as sbit at TRISC0_bit
LCD_D5_Direction as sbit at TRISC1_bit
LCD_D6_Direction as sbit at TRISC2_bit
LCD_D7_Direction as sbit at TRISC3_bit
' End LCD module connections
main:
CMCON = 7             'Comparators turned off
trisa0_bit=1
ANSEL=0
Trisc=0
while (true)
Lcd_init()
wend
end.

#### thatoneguy

Joined Feb 19, 2009
6,359

You need to get the high and low bytes into a single number to display.

Does it work in simulation with a potentiometer? Have you looked at the sample code for ADC that came with the compiler?

#### ChrisChemist116

Joined Mar 13, 2009
79

You need to get the high and low bytes into a single number to display.

Does it work in simulation with a potentiometer? Have you looked at the sample code for ADC that came with the compiler?
I have simulation on ISIS Pro and yes i had a look at the sample code which is in Mikrobasic manual, and it does work on the simulator with a potentiometer as you described, however i tried to adapt such code so it can display the result on the LCD screen, but it is not possible to run it as hex code isn't generated since can't compile from "complex" to "simple".

#### thatoneguy

Joined Feb 19, 2009
6,359
char chVAL[16];
unsigned int VAL=0;
.
.
.
WordToStr(VAL,chVAL);
Lcd_Out(1,7,chVAL);

-- From here

#### ChrisChemist116

Joined Mar 13, 2009
79
char chVAL[16];
unsigned int VAL=0;
.
.
.
WordToStr(VAL,chVAL);
Lcd_Out(1,7,chVAL);

-- From here
Hello again, thanks but unfortunatelly, the code presented there it is for MikroC, it is not working as it should on mikrobasic, tons of errors...

#### thatoneguy

Joined Feb 19, 2009
6,359
Dim chVAL As String[4]
Dim VAL As Word
.
.
WordToStr(VAL,chVAL)
Lcd_Out(1,7,chVAL)
.
.
.

That should work for Mikro BASIC, though you may need a delay before or after the read if it doesn't seem to be working right, maybe 500uS

#### ChrisChemist116

Joined Mar 13, 2009
79
Dim chVAL As String[4]
Dim VAL As Word
.
.
WordToStr(VAL,chVAL)
Lcd_Out(1,7,chVAL)
.
.
.

That should work for Mikro BASIC, though you may need a delay before or after the read if it doesn't seem to be working right, maybe 500uS
I made the following changes, so the code overall looks like this
Rich (BB code):
program Fifth_Funct
' Lcd module connections
dim LCD_RS as sbit at RC4_bit
LCD_EN as sbit at RC5_bit
LCD_D4 as sbit at RC0_bit
LCD_D5 as sbit at RC1_bit
LCD_D6 as sbit at RC2_bit
LCD_D7 as sbit at RC3_bit
LCD_RS_Direction as sbit at TRISC4_bit
LCD_EN_Direction as sbit at TRISC5_bit
LCD_D4_Direction as sbit at TRISC0_bit
LCD_D5_Direction as sbit at TRISC1_bit
LCD_D6_Direction as sbit at TRISC2_bit
LCD_D7_Direction as sbit at TRISC3_bit
' End LCD module connections
Dim chVAL As String[4]
Dim value As Word
main:
CMCON = 7             'Comparator off
trisa0_bit=1
Trisc=0
while (true)
WordToStr(value,chVAL)
Lcd_init()
Lcd_Out(1,1,chVAL)
delay_ms(500)
wend
end.
Val had to be changed to value since Val is a registered word on Mikrobasic.
Although it is compiled successfuly it doesn't work so fine on the simulator, as the LCD screen it is blank.

#### Attachments

• 69.6 KB Views: 28

#### thatoneguy

Joined Feb 19, 2009
6,359
Don't put the LCDInit inside the loop.

LCDInit only needs to be called once, in the main setup of the program, where you are setting the TRIS bits.

Move that and it should work OK, the simulator is simulating the 100mS-ish "blank" time it takes for an LCD to change to the mode that LCDInit specifies.

You can use LCDClear to erase the display if that is the reason you put init inside the loop.

#### ChrisChemist116

Joined Mar 13, 2009
79
Don't put the LCDInit inside the loop.

LCDInit only needs to be called once, in the main setup of the program, where you are setting the TRIS bits.

Move that and it should work OK, the simulator is simulating the 100mS-ish "blank" time it takes for an LCD to change to the mode that LCDInit specifies.

You can use LCDClear to erase the display if that is the reason you put init inside the loop.
I figured out i missed to add ANSEL registry to the code, as it is described in the microcontroller datasheet, after doing so it works showing the result but with some errors. The number displayed is not aligned to the left side of the screen, although it is configured on the code to do so. (first pic)

However if i move LCDInit out of the loop as this way

Rich (BB code):
program Fifth_Funct
' Lcd module connections
....
' End LCD module connections
Dim chVAL As String[4]
Dim value As word
main:
CMCON = 7             'Comparators off
trisa0_bit=1
Trisc=0
Lcd_init()
while (true)
WordToStr(value,chVAL)
Lcd_Out(1,1,chVAL)
delay_ms(50)
wend
end.
the LCD screen goes nuts and shows the result each six blocks as in the second picture and i did used Lcd_Cmd(_LCD_CLEAR), still nothing changed.

#### Attachments

• 65.8 KB Views: 20
• 67 KB Views: 44

#### thatoneguy

Joined Feb 19, 2009
6,359
Ok, try a 500 millisecond delay between clear and sending the data, keep init outside the loop, it should only be called once so that lcdcmd and lcdout know which ports to use.

I'd like to see what the results are on the actual board, rather than the simulator, as the simulator isn't perfect (which is what started this thread).

--ETA: Left aligned set in ADC just means the high byte of ADRESH holds the high 8 bits, with the low ADRESL holding the remainder in Most significant bits. Right Justified means ADRESH holds the most signifcant 2 bits as the least significant bits in ADRESH, while the remaining 8 bits are in ADRESHL

The postion/alignment on the screen is your 1,1, which means first column, first row.

Try writing it to1,7 to center it, or 2,7 to put it on the second line.

Last edited:

#### ChrisChemist116

Joined Mar 13, 2009
79
Ok, try a 500 millisecond delay between clear and sending the data, keep init outside the loop, it should only be called once so that lcdcmd and lcdout know which ports to use.

I'd like to see what the results are on the actual board, rather than the simulator, as the simulator isn't perfect (which is what started this thread).

--ETA: Left aligned set in ADC just means the high byte of ADRESH holds the high 8 bits, with the low ADRESL holding the remainder in Most significant bits. Right Justified means ADRESH holds the most signifcant 2 bits as the least significant bits in ADRESH, while the remaining 8 bits are in ADRESHL

The postion/alignment on the screen is your 1,1, which means first column, first row.

Try writing it to1,7 to center it, or 2,7 to put it on the second line.
I did as mentioned, but the delay only affects the displaying time between each result, and it does appear next to the previous one, as in the second picture posted above. I also found the AD convertion it misses the left character...

of course zeroes to the left are meaningless but why is it missing in the screen?

i understand the stuff related with left and right alignment on ADC controls, however to make myself clear, the position alignment on the screen it is not working as it should based on the code, because it is written to appear on the first row first column, but the result appears beginning in the first row and second column instead.

True, simulators are not accurate, but on this kind of things i found this one (ISIS) has very little deviations when put under test in the board, 100% of the time the errors i found on the simulator appeared on the board as well.

#### thatoneguy

Joined Feb 19, 2009
6,359
What is happening when it fills the display is 910 is written to the display, the cursor is after the 0 in 910. When another write is made, the cursor starts from there and writes 910 again, when it gets to the end of the line, it wraps around and continues.

Try an lcdcmd goto 1,1 between writes to get the cursor back where you want it to be before you write to it again, and put the delay back to 1/10th second. LCD needs a short delay between command and input mode, forgot the exact number, but it's under 20mS.

#### ChrisChemist116

Joined Mar 13, 2009
79
What is happening when it fills the display is 910 is written to the display, the cursor is after the 0 in 910. When another write is made, the cursor starts from there and writes 910 again, when it gets to the end of the line, it wraps around and continues.

Try an lcdcmd goto 1,1 between writes to get the cursor back where you want it to be before you write to it again, and put the delay back to 1/10th second. LCD needs a short delay between command and input mode, forgot the exact number, but it's under 20mS.
Alright, but how to do that.? I dont see that command on mikrobasic manual.
I tried this... but the result now it is shifted four spaces to the right.

Rich (BB code):
program Fifth_Funct
' Lcd module connections
...
' End LCD module connections
Dim chVAL As String[4]
Dim value As word
main:
....
Trisc=0
Lcd_init()
while (true)
WordToStr(value,chVAL)
Lcd_Cmd(_LCD_CLEAR)
Lcd_Out(1,1,chVAL)
lcd_cmd(_LCD_RETURN_HOME)
delay_ms(100)
wend
end.

#### thatoneguy

Joined Feb 19, 2009
6,359
Is it at least updating and in the same spot each time, although in the wrong spot?

The code for the delay for the LCD to update and change modes from command to write is just delay_ms(20)

I'd put the home command right after the clear command, then delay 20mS, then write value

#### ChrisChemist116

Joined Mar 13, 2009
79
Is it at least updating and in the same spot each time, although in the wrong spot?

The code for the delay for the LCD to update and change modes from command to write is just delay_ms(20)

I'd put the home command right after the clear command, then delay 20mS, then write value
Yes it is updating in the same spot, but as mentioned above not in the right place. I pulled all the tricks out of the hat (user's manual), but no avail. Any other ideas?.
The result displayed blinks a lot, i guess this is related to refresh rate or something like this, but it doesn't seem to work this way as i tried using PicBasicPro.

#### thatoneguy

Joined Feb 19, 2009
6,359
Removing the Clear command, and leaving just the cursor_home command should work fine.

Actually, the coodinates out in the LCDout command should work find, leaving no need for the LCD_CURSOR_HOME command.

Have you posted this on MikroC's forum? What you have should be working fine, the two commands we have around the LCDOut are basically hacks to see if it is the way your code works or the simulator.

You should be able to write text to the top line, then only update the second line, for example.