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

In summary, the code is trying to send data only when it is told to, but the part in the code that is supposed to check if the data is valid is not working.
  • #1
Guidestone
93
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
  • #2
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
  • #3
You code in C - and C is case sensitive. So #Include and #include are essentially different commands to the preprocessor.
 
  • #4
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?
 
  • #6
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
 
  • #7
Thank you Svein and Jim, I'll check those out in a while :)
 

1. Why am I getting an error message when I run my code?

There could be several reasons for this. It could be due to a syntax error, missing or incorrect variable assignment, or a logical error in your code. Carefully review your code and check for any typos or incorrect syntax. Make sure all variables are properly declared and assigned. If the error message is indicating a specific line or section of code, focus on that area and try to identify the issue.

2. Why is my code not producing the expected output?

This could be due to a logical error in your code. Check the logic of your code and make sure it is functioning as intended. It could also be caused by incorrect input or missing data. Make sure all necessary input is provided and that it is in the correct format.

3. Why is my code running slowly?

This could be due to inefficient code or large amounts of data being processed. Review your code and look for any areas that could be optimized. If you are working with large datasets, consider using more efficient data structures or algorithms.

4. Why is my code not working on a different computer or platform?

This could be due to differences in the environment or dependencies on the other computer or platform. Make sure all necessary libraries and dependencies are installed and that the code is compatible with the other computer or platform.

5. Why is my code not working after I made changes to it?

This could be due to introducing new errors or breaking existing functionality while making changes. Carefully review the changes you made and try to identify any potential issues. It could also be helpful to use version control to track changes and revert back to a previous working version if needed.

Similar threads

Replies
14
Views
24K
Back
Top