Why is my PIC 18F2550 not sending data when I tell it to?

AI Thread Summary
The user is experiencing issues with their PIC 18F2550 microcontroller not sending data via the serial port when the 'a' character is received. Initial tests in simulation showed no output, but later, it was confirmed that the code works correctly, as data is sent after the character is received; however, the physical circuit may have issues. Suggestions from other users included checking case sensitivity in code, as C is case-sensitive, and verifying smaller parts of the code for functionality. The user suspects a hardware problem since the circuit was not functioning as expected in real life. They plan to investigate further once they have access to the physical circuit again.
Guidestone
Messages
93
Reaction score
5
Hey guys. I got a project in which I'm working on right now and I got to 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:
#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:
Engineering news on Phys.org
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?
 
  • Like
Likes cnh1995
You code in C - and C is case sensitive. So #Include and #include are essentially different commands to the preprocessor.
 
mfb said:
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?
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.
Svein said:
You code in C - and C is case sensitive. So #Include and #include are essentially different commands to the preprocessor.
Thank you for your answer svein. Should it start with a lowercase letter?
 
There are also what appear to be pragmas ex:
Code:
#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
 
Thank you Svein and Jim, I'll check those out in a while :)
 
Back
Top