1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

urgent help in Code!!!!

Discussion in 'Microcontrollers' started by engkhlaif, Mar 15, 2008.

  1. engkhlaif

    engkhlaif New Member

    Joined:
    Nov 26, 2007
    Messages:
    67
    Likes:
    1
    ok i will change my connections -RB0RB3
    realy iam very happy for ur help
    thank u very much
    i will test it in the university on monday
    i want to ask u
    how can i but a programmer for pic 16f877a ???
     
  2. blueroomelectronics

    blueroomelectronics Well-Known Member

    Joined:
    Jan 21, 2007
    Messages:
    12,536
    Likes:
    170
    Location:
    Toronto, Canada
    Wouldn't it be easier just to fix the code to work with your setup?

    Do you mean build a programmer for the 16F877A?
     
  3. engkhlaif

    engkhlaif New Member

    Joined:
    Nov 26, 2007
    Messages:
    67
    Likes:
    1
    i think it will be easier to change the connection rather that change code
    about the programmer i mean that i want to buy some one or if not how can i build my own one!!!!
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. futz

    futz Active Member

    Joined:
    Sep 15, 2007
    Messages:
    2,043
    Likes:
    24
    Location:
    Vancouver, B.C.

    Did you mean to say, "How can I buy a programmer for PIC 16F877A?"

    Get a Microchip PICkit2 or a PICkit2 clone, like a Blueroom Electronics' Junebug. They're cheap and very, very good programmer/debuggers. They're extremely well supported by Microchip and work with pretty much all the PICs (the Junebug won't do 3.3V PICs though). You can buy direct from Microchip too. There may be a Microchip Direct dealer in your country. If not, any place where you can buy a PIC chip should be able to order you a PICkit2.

    Don't bother building a crappy JDM programmer. You'll spend all your time fighting with it to try to make it work.
     
    Last edited: Mar 22, 2008
  6. José Carlos Pinto Miranda

    José Carlos Pinto Miranda New Member

    Joined:
    Mar 22, 2008
    Messages:
    3
    Likes:
    0
    Hello there,
    No mather what the compiler is, or what microcontroller you use (and even what language you use), when you perform (x/255.0)*(5.0) since variavel x is an unsigned char and since it comes in first place, when you divide it by a float, the result still comes in an unsigned char and so it will always be zero (unless x=255) since it has no decimal part. If you want to perform such a calculation you should start with a float:
    voltage=x;
    voltage/=255.0;
    voltage*=5.0;

    (why diviging by 255 and multiplying by 5 though??? why not simply dividing by 51?)
    Hope this helps...
     
  7. blueroomelectronics

    blueroomelectronics Well-Known Member

    Joined:
    Jan 21, 2007
    Messages:
    12,536
    Likes:
    170
    Location:
    Toronto, Canada
    That means you don't understand the code. Letting Futz write your code won't help you if you don't know what he's doing. I'm learning C too and I can see where to make the changes required to move the pin assignments. You should take the time to understand what is happening in your program, what's the point of submitting something that's not your work?
     
  8. futz

    futz Active Member

    Joined:
    Sep 15, 2007
    Messages:
    2,043
    Likes:
    24
    Location:
    Vancouver, B.C.
    That's VERY simple code. In each of lcd_char and lcd_cmd you have to delete one line and add one line. And in lcd_init there are three lines to change. Very simple stuff. That's all the hint you're getting from me :p LOL
     
  9. engkhlaif

    engkhlaif New Member

    Joined:
    Nov 26, 2007
    Messages:
    67
    Likes:
    1
    first thanx Futz
    i get it what i want to change
    i have to change PORTB to modify it to output to RB4-RB7 and so the rest
    thanx again!!!
    yaaa!!! when i compile ur code i get the message from the programmer that the size of the HEX exceed the size of PIC (8kb) so i copy some of ur code and paste it in mine !!!
    is there any way to skip this issue !!!!
     
  10. José Carlos Pinto Miranda

    José Carlos Pinto Miranda New Member

    Joined:
    Mar 22, 2008
    Messages:
    3
    Likes:
    0
    By the way...
    I don't know the compiler you use, but usually in C language when You print a float one uses %f instead of %d. However I can't be sure about your compiler since it is a version microcontroller aplicable...
    Anyway, I would try to change the line:
    sprintf(outString,"A2D = %d.%d volts",voltage);

    to something like:
    sprintf(outString,"A2D = %1.2f volts",voltage);

    (since you divide by 51.0 maximum integer part would be 5 that corresponds to 1 digit, and as for the decimal part... who cares about milivolts on a proximity sensor where there is always a significant reading error)
     
  11. engkhlaif

    engkhlaif New Member

    Joined:
    Nov 26, 2007
    Messages:
    67
    Likes:
    1
    dear,what is that mean when u type %1.2f and why u multilpy by this factor 1.2!!!!
    some of my friends here advice me to compile a posted one pls see page 3 from my post !!!!
     
  12. futz

    futz Active Member

    Joined:
    Sep 15, 2007
    Messages:
    2,043
    Likes:
    24
    Location:
    Vancouver, B.C.
    My code comes nowhere near the max size for 16F877. You're doing something weird.
     
  13. futz

    futz Active Member

    Joined:
    Sep 15, 2007
    Messages:
    2,043
    Likes:
    24
    Location:
    Vancouver, B.C.
    It's not possible to printf or sprintf floats with Hi-Tech PICC Lite demo/student edition anyway. The libraries aren't included because they would push code size over the max demo limit every time. That's why we went around it with Pommie's very clever method in my code.
     
    Last edited: Mar 24, 2008
  14. blueroomelectronics

    blueroomelectronics Well-Known Member

    Joined:
    Jan 21, 2007
    Messages:
    12,536
    Likes:
    170
    Location:
    Toronto, Canada
    Isn't the limit 2K on the 16F877A with Hi-Tech PICC lite?
     
  15. futz

    futz Active Member

    Joined:
    Sep 15, 2007
    Messages:
    2,043
    Likes:
    24
    Location:
    Vancouver, B.C.
    Yes, that's it. I'm at a different machine, but my foggy memory tells me that hex wasn't near the limit... Oh, OK! I'll get up and go look. :p

    The hex is 6.42K, or 6576 bytes. What it comes to on the chip I don't know, but I had no problems running it. Maybe I was real close to the limit and/or maybe engkhlaif added something, like more code or another library.

    EDIT: I just recompiled it to see. The compiler spits out this:
    Code (text):
    Memory Summary:
        Program space        used   48Ah (  1162) of   800h words   ( 56.7%)
        Data space           used    3Dh (    61) of    B0h bytes   ( 34.7%)
        EEPROM space         used     0h (     0) of   100h bytes   (  0.0%)
        Configuration bits   used     1h (     1) of     1h word    (100.0%)
        ID Location space    used     0h (     0) of     4h bytes   (  0.0%)

     
    So there's lots of room left. Engkhlaif has done something weird. :p
     
    Last edited: Mar 22, 2008
  16. blueroomelectronics

    blueroomelectronics Well-Known Member

    Joined:
    Jan 21, 2007
    Messages:
    12,536
    Likes:
    170
    Location:
    Toronto, Canada
    MPLAB supports the B Knudsen CC5X compiler, anyone ever use it?
     
  17. José Carlos Pinto Miranda

    José Carlos Pinto Miranda New Member

    Joined:
    Mar 22, 2008
    Messages:
    3
    Likes:
    0
    Sorry, I didn't see, before answering to your original post, that there where already that many answers been given...
    I did only reply because it was an easy question as C is concerned.
    The %1.2f means that when constructing the reply string which is to be placed in the outString, it has to use onde INTEGER digit and two DECIMAL ones separated by a simple decimal point. Since you are using a "float" variable it seems to be the correct way to display the wanted result. It is not a factor but a simple String format modifier.
    When you write "%d.%d" and then only put one variable, you're messing all string by trying to build two INTEGERS using only one variable... Usually this could crash a normal C program, or at least mess the display result. Note that %d aplies to integer variable and so to a space in memory using 2 bytes (usually or sometimes 4). By passing a float variable, using %d, it should always display strange results because float is memory mapped on a diferent way (an so uninterpretable as an integer).
    By browsing some of the previous pages it seams that someone has already proposed that you use the correct %f specifier instead of %d. The %1.2f is only to avoid unwanted precision (what use would it be to you to get a display information in the form of say 0.21378E+1, with a %f, instead of a clean 2.13 with a %1.2f)
    However if Futz says it is not suported by your compiler, then forget the %f. But bear in mind DON'T use %d.%d if on the list you only put ONE variable, that I'm sure is bound to fail.
     
    Last edited: Mar 22, 2008
  18. futz

    futz Active Member

    Joined:
    Sep 15, 2007
    Messages:
    2,043
    Likes:
    24
    Location:
    Vancouver, B.C.
    Ya, there's a lot to read to find out the context of this thread. My first thought was of course to just print it out as a float. And of course as soon as I tried it the compiler crapped out and the LCD printed "f" instead, because of the missing lib problem.
     
  19. engkhlaif

    engkhlaif New Member

    Joined:
    Nov 26, 2007
    Messages:
    67
    Likes:
    1
    Code (text):
    #include <pic.h>
    #include "lcd.h"
    #include "delay.h"
    #include<stdio.h>

    // Setup the configuration word for use with ICD2
    __CONFIG(DEBUGEN & WDTDIS & LVPDIS);


    void init_a2d(void)
    {
        ADCON0=0;   // select Fosc/2
        ADCON1=0;   // select left justify result. A/D port configuration 0
        ADON=1;     // turn on the A2D conversion module
    }
    unsigned char read_a2d(unsigned char channel)
    {
        channel&=0x07;  // truncate channel to 3 bits
        ADCON0&=0xC5;   // clear current channel select
        ADCON0|=(channel<<3);   // apply the new channel select
        ADGO=1; // initiate conversion on the selected channel
        while(ADGO)continue;
        return(ADRESH); // return 8 MSB of the result
    }

    char number[] = "                ";


    void main(void)
    {
      // unsigned char outString[20];
      int voltage;
       unsigned char a;
        long fract;
        char fract1,fract2;


    init_a2d(); // initialise the A2D module
        GIE=0;

    TRISE = 0;
    TRISD = 0;
    TRISB = 0;
    TRISA  = 0x01;
    ADCON1 = 0x0E;
    RE2 = 0;
    DelayMs(500);
    lcd_init();

    while(1)
        {

           lcd_clear() ;
          lcd_goto(0) ;
            a = read_a2d(0);
          voltage = a;
            voltage*=5;
            fract=((((long)voltage*100)/255)%100);
            fract1=(fract/10)%10;
            fract2=(fract)%10;
            voltage/=255;
            sprintf(number,"A2d = %d.%d%d volts",voltage,fract
    ,fract2);
          lcd_puts(number);
           DelayMs(3000);
        }



    }
     
    i have changed ur code Futz so that i can just take the function that convert the reading of ADC and then i compile it and test it
    i get the reading
    A2D =3.171 volts
    this reading appear just when i connect the power to the PIC and befor i connect the sensor to the PIC!!!
     
  20. futz

    futz Active Member

    Joined:
    Sep 15, 2007
    Messages:
    2,043
    Likes:
    24
    Location:
    Vancouver, B.C.
    Ya, that's probably pretty normal. With nothing connected to that pin at all, mine reads 70 (decimal), which would be around 1.37 volts. Yours has a circuit connected to it and may be different. What is important is this: does it work when you connect the sensor?
     
    Last edited: Mar 24, 2008
  21. engkhlaif

    engkhlaif New Member

    Joined:
    Nov 26, 2007
    Messages:
    67
    Likes:
    1
    dear Futz, i dont connect the sensor bcoz my time has finished in the Lab
    tomoro i will try to connect it
    also i want ask u?
    if i define PORTA as input do i have to ground all the pins(PORTA) pins except pin A0?
    and if i define PORTA=0x01 and i connect sensor output into pin A0 i think that i dont ground the rest, do i?
     

Share This Page