12 Eylül 2017 Salı

RS232 Communication

We will create a simple circuit to read temperature remotely using RS232 line. RA0 analog port is connected to a LM35 thermometer and RC6, RC7 ports TX/RX connected to a virtual RS232 terminal.




MCC: select peripheral ADC and select RA0 analog port. 

Select EUSART peripheral with default values, 9600, 8bit etc


XC8: reading thermometer value and conversion is as we did in thermometer example. Write the value to EUSART port, char by char. Delay one second. delete the line with backspace to refresh. When you change the temperature on LM35 you will see new values on virtual terminal. 

source code is:

   unsigned char v[12], h;
    uint16_t cV = 0;
    float  t;
    int i;
    
    while (1)
    {
        ADC_StartConversion(channel_AN0);
        while(!ADC_IsConversionDone());
        cV = ADC_GetConversionResult(); 
        t=cV*500.0/65536;
        sprintf(v, "%.1f", t); 
       
        EUSART_Write('T');
        EUSART_Write('e');
        EUSART_Write('m');
        EUSART_Write('p');
        EUSART_Write('=');
        
        EUSART_Write(v[0]);
        EUSART_Write(v[1]);
        EUSART_Write(v[2]);
        EUSART_Write(v[3]);
        EUSART_Write('C');

        __delay_ms(1000);
        
        for (i=0; i<10; i++){
        EUSART_Write(0x08); }//backspace
    
    }



Hiç yorum yok:

Yorum Gönder