Complex type to simple type error in Mikrobasic with programming PIC16F676

Discussion in 'Embedded Systems and Microcontrollers' started by ChrisChemist116, Dec 2, 2011.

  1. ChrisChemist116

    Thread Starter Active Member

    Mar 13, 2009
    78
    1
    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?

    Code ( (Unknown Language)):
    1.  
    2. program Fifth_Funct
    3. ' Lcd module connections
    4.     dim LCD_RS as sbit at RC4_bit
    5.         LCD_EN as sbit at RC5_bit
    6.         LCD_D4 as sbit at RC0_bit
    7.         LCD_D5 as sbit at RC1_bit
    8.         LCD_D6 as sbit at RC2_bit
    9.         LCD_D7 as sbit at RC3_bit
    10.         LCD_RS_Direction as sbit at TRISC4_bit
    11.         LCD_EN_Direction as sbit at TRISC5_bit
    12.         LCD_D4_Direction as sbit at TRISC0_bit
    13.         LCD_D5_Direction as sbit at TRISC1_bit
    14.         LCD_D6_Direction as sbit at TRISC2_bit
    15.         LCD_D7_Direction as sbit at TRISC3_bit
    16.         ' End LCD module connections
    17.   dim adc_rd as word
    18. main:
    19. CMCON = 7             'Comparators turned off
    20.          ADCON0= %00000011   'AN0 turned on
    21.          ADCON1= %00000000
    22.          trisa0_bit=1
    23.          ADCON0= 0  
    24.         ADCON1= 0  
    25.          ANSEL=0
    26.       Trisc=0
    27.   while (true)
    28.           adc_rd = ADC_read(0)
    29.           Lcd_init()
    30.           Lcd_Out(1,1,adc_rd)
    31.   wend
    32. end.
    33.  
     
  2. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    ADC is 10 bit

    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?
     
  3. ChrisChemist116

    Thread Starter Active Member

    Mar 13, 2009
    78
    1
    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".
     
  4. thatoneguy

    AAC Fanatic!

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

    -- From here
     
  5. ChrisChemist116

    Thread Starter Active Member

    Mar 13, 2009
    78
    1
    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... :confused:
     
  6. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    Dim chVAL As String[4]
    Dim VAL As Word
    .
    .
    VAL=Adc_Read(0)
    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
     
  7. ChrisChemist116

    Thread Starter Active Member

    Mar 13, 2009
    78
    1
    I made the following changes, so the code overall looks like this
    Code ( (Unknown Language)):
    1.  
    2. program Fifth_Funct
    3. ' Lcd module connections
    4.     dim LCD_RS as sbit at RC4_bit
    5.         LCD_EN as sbit at RC5_bit
    6.         LCD_D4 as sbit at RC0_bit
    7.         LCD_D5 as sbit at RC1_bit
    8.         LCD_D6 as sbit at RC2_bit
    9.         LCD_D7 as sbit at RC3_bit
    10.         LCD_RS_Direction as sbit at TRISC4_bit
    11.         LCD_EN_Direction as sbit at TRISC5_bit
    12.         LCD_D4_Direction as sbit at TRISC0_bit
    13.         LCD_D5_Direction as sbit at TRISC1_bit
    14.         LCD_D6_Direction as sbit at TRISC2_bit
    15.         LCD_D7_Direction as sbit at TRISC3_bit
    16.         ' End LCD module connections
    17. Dim chVAL As String[4]
    18. Dim value As Word
    19. main:
    20. CMCON = 7             'Comparator off
    21.          ADCON0= %00000011   'Channel AN0 on
    22.          ADCON1= %00000000
    23.          trisa0_bit=1
    24.       Trisc=0
    25.        while (true)
    26.            value=Adc_Read(0)
    27.            WordToStr(value,chVAL)
    28.            Lcd_init()
    29.           Lcd_Out(1,1,chVAL)
    30.           delay_ms(500)
    31.   wend
    32. end.
    33.  
    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.
     
  8. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    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.
     
  9. ChrisChemist116

    Thread Starter Active Member

    Mar 13, 2009
    78
    1
    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

    Code ( (Unknown Language)):
    1.  
    2. program Fifth_Funct
    3. ' Lcd module connections
    4.     ....
    5.         ' End LCD module connections
    6. Dim chVAL As String[4]
    7. Dim value As word
    8. main:
    9. CMCON = 7             'Comparators off
    10.          ADCON0= %00000011   'Channel AN0 on
    11.          ADCON1= %00000000
    12.          ANSEL=%00000001 'missed this one AN0 set as analog input
    13.          trisa0_bit=1
    14.       Trisc=0
    15.       Lcd_init()
    16.        while (true)
    17.            value=Adc_Read(0)
    18.            WordToStr(value,chVAL)
    19.            Lcd_Out(1,1,chVAL)
    20.           delay_ms(50)
    21.   wend
    22. end.
    23.  
    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.
     
  10. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    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: Dec 4, 2011
  11. ChrisChemist116

    Thread Starter Active Member

    Mar 13, 2009
    78
    1
    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...

    it appears 910, instead 0910.

    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. :confused:

    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.
     
  12. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    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.
     
  13. ChrisChemist116

    Thread Starter Active Member

    Mar 13, 2009
    78
    1
    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.

    Code ( (Unknown Language)):
    1.  
    2. program Fifth_Funct
    3. ' Lcd module connections
    4.    ...
    5.         ' End LCD module connections
    6. Dim chVAL As String[4]
    7. Dim value As word
    8. main:
    9. ....
    10.       Trisc=0
    11.       Lcd_init()
    12.       while (true)
    13.            value=Adc_Read(0)
    14.            WordToStr(value,chVAL)
    15.            Lcd_Cmd(_LCD_CLEAR)
    16.            Lcd_Out(1,1,chVAL)
    17.            lcd_cmd(_LCD_RETURN_HOME)
    18.           delay_ms(100)
    19.   wend
    20. end.
    21.  
     
  14. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    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
     
  15. ChrisChemist116

    Thread Starter Active Member

    Mar 13, 2009
    78
    1
    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?. :confused:
    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.
     
  16. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    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.
     
  17. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
Loading...