Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Why isn't my code working?

  1. Dec 15, 2016 #1
    Hey guys. I got a project in which I'm working on right now and I gotta use a microcontroller in it. The microcontroller is a PIC 18F2550 and I have to receive data whenever I send the 'a' character via the serial port(RS232). The data I'm suposed to receive is generated with the ADC from the chip. The ADC is working fine and my code is capable of sending data quickly but the point of the code is to send data only when I tell it to but that part in the code does not seem to be working at all.
    My code is this:

    Code (Text):
    #Include <18F2550.h>    //Libreria del PIC
    #Device adc = 10    //Deficicion del ADC
    #Fuses HS,NOWDT,NOPROTECT,CPUDIV1
    #Use delay(clock = 20000000)    //Frecuencia del Oscilador
    #Use  rs232(baud=9600,xmit=pin_c6,rcv=pin_c7,parity=N,bits=8)    //Directiva para comunicacion serial
    #include <stdio.h>

    #fuses NOLVP, PLL1, CPUDIV1, NOPBADEN, NOVREGEN, NOPUT, NOBROWNOUT

    #use fast_io(a) //Configura los pines como entrada o salida, se necesita pines de configuracion.
    #use fast_io(b) //Configura los pines como entrada o salida, se necesita pines de configuracion.
    #use fast_io(c)
    //!#use fast_io(d) //Configura los pines como entrada o salida, se necesita pines de configuracion.
    //!#use fast_io(e) //COnfigura los pines como entrada o salida, se necesita pines de configuracion.
    //!int8 z, u, c, j;



                      //***Variables globales***\\\
    float ADC;    //Variable del convertidor analogico-digital
    float ADC1;    //Variable OffSet
    float Tension;    //Devuelve un valor como tension
    float Desplazamiento;    //Valor de desplazamiento del LVDT
    char z;
     
       void habilitar()
    {
       enable_interrupts(GLOBAL);//Interrupción global
       enable_interrupts(INT_RDA);//Interrupción de puerto serie
     
    }



    void Inicializar(void)    //Funcion para Inicializar todas las variables
    {
       output_a (0x00);
       output_b (0x00);
       output_c (0x00);
    //!    output_d (0x00);
       output_e (0x00);
     
       set_tris_a(0b00000001);//Declaramos los pines menos significativos de puerto A como entradas
       set_tris_b(0b00000000);//Todo el puerto B como salida
       set_tris_c(0b10000000);//Una sola entrada en puerto C
    //!    set_tris_d(0b00000000);//todo el puerto D como salida
       set_tris_e(0b00000000);//Todo el puerto E como salida
       //setup_adc(ADC_ON);


       setup_adc_ports(AN0_TO_AN3 | VSS_VDD);
       setup_adc(adc_clock_internal);
       setup_adc(ADC_CLOCK_DIV_2);
       //lcd_init();    //Inicializar LCD
       set_adc_channel(0);    //Pin analogico cero
       delay_us(50);
       ADC1 = read_adc();   //Coloca un OffSet de inicio
       }

    #INT_RDA

    void INTER_RDA() //Interrupción en el buffer de datos. Permite que se ejecute una tarea una vez que se detecta un dato en el buffer.
    {
    //!    kbhit();
        z=getc();
        Putc(z);
    }

     
    void main()
       {
       Inicializar();
       habilitar();

    //printf("Desplazamiento:");
       while (true)
          {
             if(z=='a')
             {
             set_adc_channel(0);    //Pin analogico cero
               delay_us(50);    //Retardo entre cada lectura
               ADC = read_adc()-ADC1;    //Los valores se guardan en la variable ADC, ADC1 genera un cero de partida
               Tension = (ADC*5)/1023;    //Conversion de Digital-analogico
               Desplazamiento = -(.055*(Tension*Tension))+(6.8*Tension)-.011;    //Variable de salida

                 printf("%F",Desplazamiento);
                z='b';
             }
                }
                }
    As you can see, this is not assembler languaje for I'm using picc compiler. Both the simulations I've done in proteus and the tests I've made in real life haven't showed me any numbers coming from the chip, all I've got in the hyperterminal is a blank screen.
    I would really appreciate any guide on this. I'm sorry if this is not the correct section of the forum to post this but it involves electronics and not only programming so I thought it would be better suited for this section.
     
    Last edited by a moderator: Dec 15, 2016
  2. jcsd
  3. Dec 15, 2016 #2

    mfb

    User Avatar
    2016 Award

    Staff: Mentor

    Did you test smaller parts of it? Having the input and output in the main function to check that part? Instead of sending a char back, activating some LED to see if the interrupt works at all?
     
  4. Dec 16, 2016 #3

    Svein

    User Avatar
    Science Advisor

    You code in C - and C is case sensitive. So #Include and #include are essentially different commands to the preprocessor.
     
  5. Dec 16, 2016 #4
    Hi mfb thank you for your aswer. Yesterdy a few hours after I posted my code I found out it is actually working fine, I am getting the values immediately after I send the character. However, I was testing in a simulating software and the physical circuit was still not working. I guess it's a hardware problem and the friend of mine who soldered the whole thing took it home. I'll have to wait till monday to see what's going on.
    Thank you for your answer svein. Should it start with a lowercase letter?
     
  6. Dec 17, 2016 #5

    Svein

    User Avatar
    Science Advisor

  7. Dec 17, 2016 #6

    jim mcnamara

    User Avatar

    Staff: Mentor

    There are also what appear to be pragmas ex:
    Code (Text):
    #use fast_io(a)
    pragmas are not going to be in the list @Svein gave you.

    And the same warning about capital letters: there are lines that start #Use
     
  8. Dec 17, 2016 #7
    Thank you Svein and Jim, I'll check those out in a while :)
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted