help to correct code speed meter , RPM and distance -pic 16f877 ( UART)

jayanthd

Joined Jul 4, 2015
945
Don't use different arrays[] for getting the string results. I see that when your code is compiled we get

"IRP bit must be set manually for indirect access to 'show_distance' variable frecuenciametr0.c"

RAM is very less in PIC16F877A and so just use one char array[] like char msg[17] to get the string values for printing.

What is not working in your code ?
 

jayanthd

Joined Jul 4, 2015
945
Try this code. Tell me what happens.

Is this the display you are using ?

https://robu.in/product/nextion-nx3...0-jcdiK_Cy3Hxk2vlzBz36VvD7iNOkXUaAkylEALw_wcB

Code:
unsigned long counter = 0;
unsigned long period = 0;
unsigned int currentCaptureTime = 0;
unsigned int previousCaptureTime = 0;
unsigned long rpm = 0;
unsigned int rps = 0;
double distance = 0.0;
double speedKmsPerHour = 0.0;
double speedMilesPerHour = 0.0;
char msg[17];

void interrupt() {
     if((TMR1IE_bit) && (TMR1IF_bit)) {
         TMR1IF_bit = 0;
         ++counter;
     }

     if((CCP1IE_bit) && (CCP1IF_bit)) {
         CCP1IF_bit = 0;
         currentCaptureTime = CCPR1;
         period = counter * (unsigned long)0xFFFF - (unsigned long)previousCaptureTime + (unsigned long)currentCaptureTime;
         rpm = (unsigned long)(60.0e6 / (double)period);
         rps = rpm / 60;
         distance = distance + (double)rps * 34.55751915e-5;
         speedKmsPerHour = (double)rps * 34.55751915e-5 * 2.77778e-4;
         speedMilesPerHour = (double)rps * 34.55751915e-5 * 6.21371e-6 * 2.77778e-4;

         previousCaptureTime = currentCaptureTime;
         counter = 0;
     }
}

// copy const to ram string
char *CopyConst2Ram(char *dest, const char *src) {
    char *d;

    d = dest;

    for(;*dest++ = *src++;);

    return d;
}

void Delay_XSec(unsigned char sec) {
     while(sec) {
        asm clrwdt
        Delay_ms(200);
        asm clrwdt
        Delay_ms(200);
        asm clrwdt
        Delay_ms(200);
        asm clrwdt
        Delay_ms(200);
        asm clrwdt
        Delay_ms(200);
        sec--;
     }
}

void nextionWrite255() {
    asm clrwdt
    UART1_Write(0xFF);
    UART1_Write(0xFF);
    UART1_Write(0xFF);
    asm clrwdt
}

void main() {
    asm clrwdt
    OPTION_REG = 0x8F;

    TRISC = 0x84;
    PORTC = 0x00;

    UART1_Init(115200);
    Delay_ms(100);

    T1CON = 0x01;
    TMR1H = 0x00;
    TMR1L = 0x00;
    TMR1IE_bit = 1;

    CCP1CON = 0x04;
    CCP1IE_bit = 1;

    PEIE_bit = 1;
    GIE_bit = 1;

    while(1) {
        asm clrwdt
        sprinti(msg,"n0.val=%d",rpm/1000);
        UART1_Write_Text(msg);
        nextionWrite255();

        sprinti(msg,"n1.val=%d",distance * 1e4);
        UART1_Write_Text(msg);
        nextionWrite255();

        sprinti(msg,"n2.val=%d",speedKmsPerHour * 1e6);
        UART1_Write_Text(msg);
        nextionWrite255();
     
        sprinti(msg,"n3.val=%d",speedMilesPerHour * 1e6);
        UART1_Write_Text(msg);
        nextionWrite255();

        Delay_XSec(1);
    }
}
 
Last edited:

jayanthd

Joined Jul 4, 2015
945
Did you check the Lcd I mentioned ? Is your Lcd part name same as th eone I showed ?
NX3224T024-Generic 2.4″ TFT ?


Sorry, test this new code. The sprinti() cannot format floating point numbers. PIC16F877A doesn't support sprintf() in mikroC.

Code:
unsigned long counter = 0;
unsigned long period = 0;
unsigned int currentCaptureTime = 0;
unsigned int previousCaptureTime = 0;
unsigned long rpm = 0;
unsigned int rps = 0;
double distance = 0.0;
double speedKmsPerHour = 0.0;
double speedMilesPerHour = 0.0;
char msg[32];
char txt[32];

void interrupt() {
     if((TMR1IE_bit) && (TMR1IF_bit)) {
         TMR1IF_bit = 0;
         ++counter;
     }

     if((CCP1IE_bit) && (CCP1IF_bit)) {
         CCP1IF_bit = 0;
         currentCaptureTime = CCPR1;
         period = counter * (unsigned long)0xFFFF - (unsigned long)previousCaptureTime + (unsigned long)currentCaptureTime;
         rpm = (unsigned long)(60.0e6 / (double)period);
         rps = rpm / 60;
         distance = distance + (double)rps * 34.55751915e-5;
         speedKmsPerHour = (double)rps * 34.55751915e-5 * 2.77778e-4;
         speedMilesPerHour = (double)rps * 34.55751915e-5 * 6.21371e-6 * 2.77778e-4;

         previousCaptureTime = currentCaptureTime;
         counter = 0;
     }
}

// copy const to ram string
char *CopyConst2Ram(char *dest, const char *src) {
    char *d;

    d = dest;

    for(;*dest++ = *src++;);

    return d;
}

void Delay_XSec(unsigned char sec) {
     while(sec) {
        asm clrwdt
        Delay_ms(200);
        asm clrwdt
        Delay_ms(200);
        asm clrwdt
        Delay_ms(200);
        asm clrwdt
        Delay_ms(200);
        asm clrwdt
        Delay_ms(200);
        sec--;
     }
}

void nextionWrite255() {
    asm clrwdt
    UART1_Write(0xFF);
    UART1_Write(0xFF);
    UART1_Write(0xFF);
    asm clrwdt
}

void main() {
    asm clrwdt
    OPTION_REG = 0x8F;

    TRISC = 0x84;
    PORTC = 0x00;

    UART1_Init(115200);
    Delay_ms(100);

    T1CON = 0x01;
    TMR1H = 0x00;
    TMR1L = 0x00;
    TMR1IF_bit = 0;
    TMR1IE_bit = 1;

    CCP1CON = 0x04;
    CCP1IF_bit = 0;
    CCP1IE_bit = 1;

    PEIE_bit = 1;
    GIE_bit = 1;

    while(1) {
        asm clrwdt
        sprinti(msg,"n0.val=%d",rpm/1000);
        UART1_Write_Text(msg);
        nextionWrite255();

        strcpy(msg,"n1.val=");
        FloatToStr(distance,txt);
        strcat(msg,txt);
        UART1_Write_Text(msg);
        nextionWrite255();

        strcpy(msg,"n2.val=");
        FloatToStr(speedKmsPerHour,txt);
        strcat(msg,txt);
        UART1_Write_Text(msg);
        nextionWrite255();
       
        strcpy(msg,"n3.val=");
        FloatToStr(speedMilesPerHour,txt);
        strcat(msg,txt);
        UART1_Write_Text(msg);
        nextionWrite255();
       
        Delay_XSec(1);
    }
}

Try this code also.

Code:
unsigned long counter = 0;
unsigned long period = 0;
unsigned int currentCaptureTime = 0;
unsigned int previousCaptureTime = 0;
unsigned long rpm = 0;
unsigned int rps = 0;
double distance = 0.0;
double speedKmsPerHour = 0.0;
double speedMilesPerHour = 0.0;
char msg[32];
char txt[32];

void interrupt() {
     if((TMR1IE_bit) && (TMR1IF_bit)) {
         TMR1IF_bit = 0;
         ++counter;
     }

     if((CCP1IE_bit) && (CCP1IF_bit)) {
         CCP1IF_bit = 0;
         currentCaptureTime = CCPR1;
         period = counter * (unsigned long)0xFFFF - (unsigned long)previousCaptureTime + (unsigned long)currentCaptureTime;
         rpm = (unsigned long)(60.0e6 / (double)period);
         rps = rpm / 60;
         distance = distance + (double)rpm * 34.55751915e-5;
         speedKmsPerHour = (double)rps * 34.55751915e-5 * 2.77778e-4;
         speedMilesPerHour = (double)rps * 34.55751915e-5 * 6.21371e-6 * 2.77778e-4;

         previousCaptureTime = currentCaptureTime;
         counter = 0;
     }
}

// copy const to ram string
char *CopyConst2Ram(char *dest, const char *src) {
    char *d;

    d = dest;

    for(;*dest++ = *src++;);

    return d;
}

void Delay_XSec(unsigned char sec) {
     while(sec) {
        asm clrwdt
        Delay_ms(200);
        asm clrwdt
        Delay_ms(200);
        asm clrwdt
        Delay_ms(200);
        asm clrwdt
        Delay_ms(200);
        asm clrwdt
        Delay_ms(200);
        sec--;
     }
}

void nextionWrite255() {
    asm clrwdt
    UART1_Write(0xFF);
    UART1_Write(0xFF);
    UART1_Write(0xFF);
    asm clrwdt
}

void main() {
    asm clrwdt
    OPTION_REG = 0x8F;

    TRISC = 0x84;
    PORTC = 0x00;

    UART1_Init(115200);
    Delay_ms(100);

    T1CON = 0x01;
    TMR1H = 0x00;
    TMR1L = 0x00;
    TMR1IF_bit = 0;
    TMR1IE_bit = 1;

    CCP1CON = 0x04;
    CCP1IF_bit = 0;
    CCP1IE_bit = 1;

    PEIE_bit = 1;
    GIE_bit = 1;

    while(1) {
        asm clrwdt
        sprinti(msg,"n0.val=%d",rpm/1000);
        UART1_Write_Text(msg);
        nextionWrite255();

        strcpy(msg,"n1.val=");
        FloatToStr(distance,txt);
        asm clrwdt
        strcat(msg,txt);
        strcat(msg," kms");
        asm clrwdt
        UART1_Write_Text(msg);
        nextionWrite255();

        strcpy(msg,"n2.val=");
        FloatToStr(speedKmsPerHour,txt);
        asm clrwdt
        strcat(msg,txt);
        strcat(msg," kms/hr");
        asm clrwdt
        UART1_Write_Text(msg);
        nextionWrite255();
       
        strcpy(msg,"n3.val=");
        FloatToStr(speedMilesPerHour,txt);
        asm clrwdt
        strcat(msg,txt);
        strcat(msg," miles/hr");
        asm clrwdt
        UART1_Write_Text(msg);
        nextionWrite255();
       
        Delay_XSec(1);
    }
}
 
Last edited:

jayanthd

Joined Jul 4, 2015
945
Do you want distance in cm or kms ?

Here is distance test code. It should display distance in cms. Test and reply.

C:
unsigned long counter = 0;
unsigned long period = 0;
unsigned int currentCaptureTime = 0;
unsigned int previousCaptureTime = 0;
unsigned long rpm = 0;
unsigned int rps = 0;
double distance = 0.0;
double speedKmsPerHour = 0.0;
double speedMilesPerHour = 0.0;
char msg[32];
char txt[32];

void interrupt() {
     if((TMR1IE_bit) && (TMR1IF_bit)) {
         TMR1IF_bit = 0;
         ++counter;
     }

     if((CCP1IE_bit) && (CCP1IF_bit)) {
         CCP1IF_bit = 0;
         currentCaptureTime = CCPR1;
         period = counter * (unsigned long)0xFFFF - (unsigned long)previousCaptureTime + (unsigned long)currentCaptureTime;
         rpm = (unsigned long)(60.0e6 / (double)period);
         rps = rpm / 60;
         distance = distance + (double)rpm * 0.9599;
         /*
         //speedKmsPerHour = (double)rps * 34.55751915e-5 * 2.77778e-4;
         //speedMilesPerHour = (double)rps * 34.55751915e-5 * 6.21371e-6 * 2.77778e-4;
         speedKmsPerHour = (double)rps * 99.9532e-9;
         speedMilesPerHour = (double)rps * 596.474e-15;
         */

         previousCaptureTime = currentCaptureTime;
         counter = 0;
     }
}

// copy const to ram string
char *CopyConst2Ram(char *dest, const char *src) {
    char *d;

    d = dest;

    for(;*dest++ = *src++;);

    return d;
}

void Delay_XSec(unsigned char sec) {
     while(sec) {
        asm clrwdt
        Delay_ms(200);
        asm clrwdt
        Delay_ms(200);
        asm clrwdt
        Delay_ms(200);
        asm clrwdt
        Delay_ms(200);
        asm clrwdt
        Delay_ms(200);
        sec--;
     }
}

void nextionWrite255() {
    asm clrwdt
    UART1_Write(0xFF);
    UART1_Write(0xFF);
    UART1_Write(0xFF);
    asm clrwdt
}

void main() {
    asm clrwdt
    OPTION_REG = 0x8F;

    TRISC = 0x84;
    PORTC = 0x00;

    UART1_Init(115200);
    Delay_ms(100);

    T1CON = 0x01;
    TMR1H = 0x00;
    TMR1L = 0x00;
    TMR1IF_bit = 0;
    TMR1IE_bit = 1;

    CCP1CON = 0x04;
    CCP1IF_bit = 0;
    CCP1IE_bit = 1;

    PEIE_bit = 1;
    GIE_bit = 1;

    while(1) {
        asm clrwdt
        sprinti(msg,"n0.val=%d",rpm/1000);
        UART1_Write_Text(msg);
        nextionWrite255();

        strcpy(msg,"n1.val=");
        FloatToStr(distance,txt);
        asm clrwdt
        strcat(msg,txt);
        strcat(msg," cms");
        asm clrwdt
        UART1_Write_Text(msg);
        nextionWrite255();

        /*
        strcpy(msg,"n2.val=");
        FloatToStr(speedKmsPerHour,txt);
        asm clrwdt
        strcat(msg,txt);
        strcat(msg," kms/hr");
        asm clrwdt
        UART1_Write_Text(msg);
        nextionWrite255();
       
        strcpy(msg,"n3.val=");
        FloatToStr(speedMilesPerHour,txt);
        asm clrwdt
        strcat(msg,txt);
        strcat(msg," miles/hr");
        asm clrwdt
        UART1_Write_Text(msg);
        nextionWrite255();
        */
       
        Delay_XSec(1);
    }
}
 
Last edited:

jayanthd

Joined Jul 4, 2015
945
Test this new code. I am sure the issues related to rpm and distance are fixed in this.

You calculation is wrong.

Your Fosc is 20 MHz and you had used 1:1 prescalar. I have changed prescalar to 1:8 and so,

Fosc / 4 = 20 MHz / 4 = 5000000

1/5000000 = 0.0000002

Prescalar = 1:8 = 8

0.0000002 * 8 = 0.0000016 = 1.6us

60/1.6us = 37.5e6

/*

Calculation shown in book page no. 136 is

10 MHz / 4 = 2500000

1/2500000 = 0.0000004

Prescalar = 1:2 = 2

0.0000004 * 2 = 0.8us

60/0.8us = 75e6

*/

rpm = 37.5e6 * period / 36 is correct

In book code they use 1 pulse per revolution and you have 36 pulse per revolution.

So, 37.5/36 = 1.042

So, code should be

Code:
unsigned long counter = 0;
unsigned long period = 0;
unsigned int currentCaptureTime = 0;
unsigned int previousCaptureTime = 0;
unsigned long rpm = 0;
unsigned int rps = 0;
double distance = 0.0;
double speedKmsPerHour = 0.0;
double speedMilesPerHour = 0.0;
char msg[32];
char txt[32];

void interrupt() {
     if((TMR1IE_bit) && (TMR1IF_bit)) {
         TMR1IF_bit = 0;
         ++counter;
     }

     if((CCP1IE_bit) && (CCP1IF_bit)) {
         CCP1IF_bit = 0;
         currentCaptureTime = CCPR1;
         period = counter * (unsigned long)0xFFFF - (unsigned long)previousCaptureTime + (unsigned long)currentCaptureTime;
         rpm = (unsigned long)(37.5e6 / (double)period / 36.0);
         rps = rpm / 60;
         distance = distance + (double)rps * 34.55752e-5;  //rps * 2 * pi *r = rps * 2 * pi * d / 2 = rps * pi * d cms
                                                                 //1cm = 1e-5 km
         /*
         //speedKmsPerHour = (double)rps * 34.55751915e-5 * 2.77778e-4;
         //speedMilesPerHour = (double)rps * 34.55751915e-5 * 6.21371e-6 * 2.77778e-4;
         speedKmsPerHour = (double)rps * 99.9532e-9;
         speedMilesPerHour = (double)rps * 596.474e-15;
         */
       
         previousCaptureTime = currentCaptureTime;
         counter = 0;
     }
}

// copy const to ram string
char *CopyConst2Ram(char *dest, const char *src) {
    char *d;

    d = dest;

    for(;*dest++ = *src++;);

    return d;
}

void Delay_XSec(unsigned char sec) {
     while(sec) {
        asm clrwdt
        Delay_ms(200);
        asm clrwdt
        Delay_ms(200);
        asm clrwdt
        Delay_ms(200);
        asm clrwdt
        Delay_ms(200);
        asm clrwdt
        Delay_ms(200);
        sec--;
     }
}

void nextionWrite255() {
    asm clrwdt
    UART1_Write(0xFF);
    UART1_Write(0xFF);
    UART1_Write(0xFF);
    //UART1_Write_Text("\r\n");
    asm clrwdt
}

void main() {
    asm clrwdt
    OPTION_REG = 0x8F;

    TRISC = 0x84;
    PORTC = 0x00;

    UART1_Init(115200);
    Delay_ms(100);

    T1CON = 0x31;
    TMR1H = 0x00;
    TMR1L = 0x00;
    TMR1IF_bit = 0;
    TMR1IE_bit = 1;

    CCP1CON = 0x04;
    CCP1IF_bit = 0;
    CCP1IE_bit = 1;

    PEIE_bit = 1;
    GIE_bit = 1;

    while(1) {
        asm clrwdt
        /*
        //strcpy(msg,"rps = ");
        strcpy(msg,"n0.val=");
        LongToStr(rps,txt);
        asm clrwdt
        strcat(msg,txt);
        asm clrwdt
        UART1_Write_Text(msg);
        nextionWrite255();
        */
     
        //strcpy(msg,"rpm = ");
        strcpy(msg,"n0.val=");
        LongToStr(rpm,txt);
        asm clrwdt
        strcat(msg,txt);
        asm clrwdt
        UART1_Write_Text(msg);
        nextionWrite255();

        //strcpy(msg,"distance = ");
        strcpy(msg,"n1.val=");
        FloatToStr(distance,txt);
        asm clrwdt
        strcat(msg,txt);
        strcat(msg," kms");
        asm clrwdt
        UART1_Write_Text(msg);
        nextionWrite255();

        /*
        strcpy(msg,"n2.val=");
        FloatToStr(speedKmsPerHour,txt);
        asm clrwdt
        strcat(msg,txt);
        strcat(msg," kms/hr");
        asm clrwdt
        UART1_Write_Text(msg);
        nextionWrite255();
     
        strcpy(msg,"n3.val=");
        FloatToStr(speedMilesPerHour,txt);
        asm clrwdt
        strcat(msg,txt);
        strcat(msg," miles/hr");
        asm clrwdt
        UART1_Write_Text(msg);
        nextionWrite255();
        */
     
        Delay_XSec(1);
    }
}
360 pulses/sec = 10 revolutions (36 pulses = 1 revolution)

10 revolutions/sec (Proteus Simulation)

rps = 10 for input 360 Hz

rpm = rps * 60 = 10 * 60 = 600

SIM2.png
 

Thread Starter

MCU_1548107851

Joined Jan 21, 2019
30
Do you want distance in cm or kms ?

yes i need in km
-----------------------------------------------

and last code you send to me contain many comment //
which one i have to keep it and which delete
i think you try code with protues similation

but with nextion lcd will not work
because you use string converter

see your last code in attachment

also which mhz cristal you recommend me to use 4 mhz or 20 mhz
to give exact time
 

Attachments

jayanthd

Joined Jul 4, 2015
945
Try this for nextion.

It displays rpm and distance as of now.

I am getting IRP bit must be set manually to access txt[]. This happens with mikroC and PIC16Fs that have very low RAM.

Solution = use PIC18F and problem will be solved.

PIC18F mikroc library supports sprinti() and also sprintf() for floating point values.

I am using 20 MHz crystal and 1:8 Prescalar for Timer1.

This is the new nextion code. LongtoStr() and IntToStr() does the same thing that sprinti() does. Don't change anything. Just test this code.

Code:
#define PULSE_PER_REVOLUTION       36.0
#define FOSC                 20000000.0
#define PRESCALAR                   8.0
#define PI                          3.14159265
#define DIA                        11.0
#define cm_to_km                    1.0e-5
#define sec_to_hr                   2.77778e-4
#define km_to_miles                 6.21371e-6

unsigned long counter = 0;
unsigned long period = 0;
unsigned int currentCaptureTime = 0;
unsigned int previousCaptureTime = 0;
unsigned long rpm = 0;
unsigned int rps = 0;
double distance = 0.0;
double speedKmsPerHour = 0.0;
double speedMilesPerHour = 0.0;
char msg[32];
char txt[32];

void interrupt() {
     if((TMR1IE_bit) && (TMR1IF_bit)) {
         TMR1IF_bit = 0;
         ++counter;
     }

     if((CCP1IE_bit) && (CCP1IF_bit)) {
         CCP1IF_bit = 0;
         currentCaptureTime = CCPR1;
         period = counter * (unsigned long)0xFFFF - (unsigned long)previousCaptureTime + (unsigned long)currentCaptureTime;
         rpm = (unsigned long)((60.0 / ((1.0 / (FOSC / 4.0) * PRESCALAR))) / (double)period / PULSE_PER_REVOLUTION);
         rps = rpm / 60;
         distance = distance + (double)rps * PI * DIA * cm_to_km;  //rps * 2 * pi *r = rps * 2 * pi * d / 2 = rps * pi * d cms
                                                                   //1cm = 1e-5 km
         speedKmsPerHour = (double)rps * PI * DIA * cm_to_km * sec_to_hr;
         speedMilesPerHour = (double)rps * PI * DIA * cm_to_km * km_to_miles * sec_to_hr;

         previousCaptureTime = currentCaptureTime;
         counter = 0;
     }
}

// copy const to ram string
char *CopyConst2Ram(char *dest, const char *src) {
    char *d;

    d = dest;

    for(;*dest++ = *src++;);

    return d;
}

void Delay_XSec(unsigned char sec) {
     while(sec) {
        asm clrwdt
        Delay_ms(200);
        asm clrwdt
        Delay_ms(200);
        asm clrwdt
        Delay_ms(200);
        asm clrwdt
        Delay_ms(200);
        asm clrwdt
        Delay_ms(200);
        sec--;
     }
}

void nextionWrite255() {
    asm clrwdt
    UART1_Write(0xFF);
    UART1_Write(0xFF);
    UART1_Write(0xFF);
    //UART1_Write_Text("\r\n");
    asm clrwdt
}

void main() {
    asm clrwdt
    OPTION_REG = 0x8F;

    TRISC = 0x84;
    PORTC = 0x00;

    UART1_Init(115200);
    Delay_ms(100);

    T1CON = 0x31;
    TMR1H = 0x00;
    TMR1L = 0x00;
    TMR1IF_bit = 0;
    TMR1IE_bit = 1;

    CCP1CON = 0x04;
    CCP1IF_bit = 0;
    CCP1IE_bit = 1;

    PEIE_bit = 1;
    GIE_bit = 1;

    while(1) {
        asm clrwdt
        /*
        //strcpy(msg,"rps = ");
        strcpy(msg,"n0.val=");
        LongToStr(rps,txt);
        asm clrwdt
        strcat(msg,txt);
        asm clrwdt
        UART1_Write_Text(msg);
        nextionWrite255();
        */
      
        //strcpy(msg,"rpm = ");
        strcpy(msg,"n0.val=");
        LongToStr(rpm,txt);
        asm clrwdt
        strcat(msg,txt);
        asm clrwdt
        UART1_Write_Text(msg);
        nextionWrite255();

        //strcpy(msg,"distance = ");
        strcpy(msg,"n1.val=");
        FloatToStr(distance,txt);
        asm clrwdt
        strcat(msg,txt);
        strcat(msg," kms");
        asm clrwdt
        UART1_Write_Text(msg);
        nextionWrite255();

        /*
        strcpy(msg,"n2.val=");
        FloatToStr(speedKmsPerHour,txt);
        asm clrwdt
        strcat(msg,txt);
        strcat(msg," kms/hr");
        asm clrwdt
        UART1_Write_Text(msg);
        nextionWrite255();
      
        strcpy(msg,"n3.val=");
        FloatToStr(speedMilesPerHour,txt);
        asm clrwdt
        strcat(msg,txt);
        strcat(msg," miles/hr");
        asm clrwdt
        UART1_Write_Text(msg);
        nextionWrite255();
        */

        Delay_XSec(1);
    }
}


Code:
#define PULSE_PER_REVOLUTION  36.0
#define FOSC                  20.0e6
#define PRESCALAR              8.0
#define PI                     3.14159265
#define DIA                   11.0
#define cm_to_km               1.0e-5
#define sec_to_hr              2.77778e-4
#define km_to_miles            6.21371e-6
 

jayanthd

Joined Jul 4, 2015
945
Just use this code. See if nextion display works.

Code:
#define PULSE_PER_REVOLUTION  36.0
#define FOSC                  20.0e6
#define PRESCALAR              8.0
#define PI                     3.14159265
#define DIA                   11.0
#define cm_to_km               1.0e-5
#define sec_to_hr              2.77778e-4
#define km_to_miles            6.21371e-6

unsigned long counter = 0;
unsigned long period = 0;
unsigned int currentCaptureTime = 0;
unsigned int previousCaptureTime = 0;
unsigned long rpm = 0;
unsigned int rps = 0;
double distance = 0.0;
double speedKmsPerHour = 0.0;
double speedMilesPerHour = 0.0;
char msg[32];
char txt[32];

void interrupt() {
     if((TMR1IE_bit) && (TMR1IF_bit)) {
         TMR1IF_bit = 0;
         ++counter;
     }

     if((CCP1IE_bit) && (CCP1IF_bit)) {
         CCP1IF_bit = 0;
         currentCaptureTime = CCPR1;
         period = counter * (unsigned long)0xFFFF - (unsigned long)previousCaptureTime + (unsigned long)currentCaptureTime;
         rpm = (unsigned long)((60.0 / ((1.0 / (FOSC / 4.0) * PRESCALAR))) / (double)period / PULSE_PER_REVOLUTION);
         rps = rpm / 60;
         distance = distance + (double)rps * PI * DIA * cm_to_km;  //rps * 2 * pi *r = rps * 2 * pi * d / 2 = rps * pi * d cms
                                                                   //1cm = 1e-5 km
         speedKmsPerHour = (double)rps * PI * DIA * cm_to_km * sec_to_hr;
         speedMilesPerHour = (double)rps * PI * DIA * cm_to_km * km_to_miles * sec_to_hr;

         previousCaptureTime = currentCaptureTime;
         counter = 0;
     }
}

// copy const to ram string
char *CopyConst2Ram(char *dest, const char *src) {
    char *d;

    d = dest;

    for(;*dest++ = *src++;);

    return d;
}

void nextionWrite255() {
    UART1_Write(0xFF);
    UART1_Write(0xFF);
    UART1_Write(0xFF);
}

void main() {
    TRISC = 0x84;
    PORTC = 0x00;

    UART1_Init(115200);
    Delay_ms(100);

    T1CON = 0x31;
    TMR1H = 0x00;
    TMR1L = 0x00;
    TMR1IF_bit = 0;
    TMR1IE_bit = 1;

    CCP1CON = 0x04;
    CCP1IF_bit = 0;
    CCP1IE_bit = 1;

    PEIE_bit = 1;
    GIE_bit = 1;

    while(1) {
        strcpy(msg,"n0.val=");
        LongToStr(rpm,txt);
        strcat(msg,txt);
        UART1_Write_Text(msg);
        nextionWrite255();

        strcpy(msg,"n1.val=");
        FloatToStr(distance,txt);
        strcat(msg,txt);
        //strcat(msg," kms");
        UART1_Write_Text(msg);
        nextionWrite255();

        strcpy(msg,"n2.val=");
        FloatToStr(speedKmsPerHour,txt);
        strcat(msg,txt);
        //strcat(msg," kms/hr");
        UART1_Write_Text(msg);
        nextionWrite255();
       
        strcpy(msg,"n3.val=");
        FloatToStr(speedMilesPerHour,txt);
        strcat(msg,txt);
        //strcat(msg," miles/hr");
        UART1_Write_Text(msg);
        nextionWrite255();

        Delay_ms(1000);
    }
}
 

jayanthd

Joined Jul 4, 2015
945
New code, test it.

cm to miles conversion was wrong. I have fixed it.

Code:
#define PULSE_PER_REVOLUTION  36.0
#define FOSC                  20.0e6
#define PRESCALAR              8.0
#define PI                     3.14159265
#define DIA                   11.0
#define cm_to_km               1.0e-5
#define sec_to_hr              2.77778e-4
#define cm_to_miles            6.21371e-6

unsigned long counter = 0;
unsigned long period = 0;
unsigned int currentCaptureTime = 0;
unsigned int previousCaptureTime = 0;
unsigned long rpm = 0;
unsigned int rps = 0;
double distance = 0.0;
double speedKmsPerHour = 0.0;
double speedMilesPerHour = 0.0;
char msg[32];
char txt[32];

void interrupt() {
     if((TMR1IE_bit) && (TMR1IF_bit)) {
         TMR1IF_bit = 0;
         ++counter;
     }

     if((CCP1IE_bit) && (CCP1IF_bit)) {
         CCP1IF_bit = 0;
         currentCaptureTime = CCPR1;
         period = counter * (unsigned long)0xFFFF - (unsigned long)previousCaptureTime + (unsigned long)currentCaptureTime;
         rpm = (unsigned long)((60.0 / ((1.0 / (FOSC / 4.0) * PRESCALAR))) / (double)period / PULSE_PER_REVOLUTION);
         rps = rpm / 60;
         distance = distance + (double)rps * PI * DIA * cm_to_km;  //rps * 2 * pi *r = rps * 2 * pi * d / 2 = rps * pi * d cms
                                                                   //1cm = 1e-5 km
         speedKmsPerHour = (double)rps * PI * DIA * cm_to_km * sec_to_hr;
         speedMilesPerHour = (double)rps * PI * DIA * cm_to_miles * sec_to_hr;

         previousCaptureTime = currentCaptureTime;
         counter = 0;
     }
}

// copy const to ram string
char *CopyConst2Ram(char *dest, const char *src) {
    char *d;

    d = dest;

    for(;*dest++ = *src++;);

    return d;
}

void nextionWrite255() {
    UART1_Write(0xFF);
    UART1_Write(0xFF);
    UART1_Write(0xFF);
}

void main() {
    TRISC = 0x84;
    PORTC = 0x00;

    UART1_Init(115200);
    Delay_ms(100);

    T1CON = 0x31;
    TMR1H = 0x00;
    TMR1L = 0x00;
    TMR1IF_bit = 0;
    TMR1IE_bit = 1;

    CCP1CON = 0x04;
    CCP1IF_bit = 0;
    CCP1IE_bit = 1;

    PEIE_bit = 1;
    GIE_bit = 1;

    while(1) {
        strcpy(msg,"n0.val=");
        LongToStr(rpm,txt);
        strcat(msg,txt);
        UART1_Write_Text(msg);
        nextionWrite255();

        strcpy(msg,"n1.val=");
        FloatToStr(distance,txt);
        strcat(msg,txt);
        //strcat(msg," kms");
        UART1_Write_Text(msg);
        nextionWrite255();

        strcpy(msg,"n2.val=");
        FloatToStr(speedKmsPerHour,txt);
        strcat(msg,txt);
        //strcat(msg," kms/hr");
        UART1_Write_Text(msg);
        nextionWrite255();
       
        strcpy(msg,"n3.val=");
        FloatToStr(speedMilesPerHour,txt);
        strcat(msg,txt);
        //strcat(msg," miles/hr");
        UART1_Write_Text(msg);
        nextionWrite255();

        Delay_ms(1000);
    }
}
 

jayanthd

Joined Jul 4, 2015
945
See if this gives all the values on nextion display.

Code:
#define PULSE_PER_REVOLUTION  36.0
#define FOSC                  20.0e6
#define PRESCALAR              8.0
#define PI                     3.14159265
#define DIA                   11.0
#define cm_to_km               1.0e-5
#define cm_to_miles            6.21371e-6
#define sec_to_hr              2.77778e-4

unsigned long counter = 0;
unsigned long period = 0;
unsigned int currentCaptureTime = 0;
unsigned int previousCaptureTime = 0;
unsigned long rpm = 0;
unsigned int rps = 0;
unsigned long rph = 0;
double distance = 0.0;
double speedKmsPerHour = 0.0;
double speedMilesPerHour = 0.0;
char msg[32];
char txt[32];

void interrupt() {
     if((TMR1IE_bit) && (TMR1IF_bit)) {
         TMR1IF_bit = 0;
         ++counter;
     }

     if((CCP1IE_bit) && (CCP1IF_bit)) {
         CCP1IF_bit = 0;
         currentCaptureTime = CCPR1;
         period = counter * (unsigned long)0xFFFF - (unsigned long)previousCaptureTime + (unsigned long)currentCaptureTime;
         rpm = (unsigned long)((60.0 / ((1.0 / (FOSC / 4.0) * PRESCALAR))) / (double)period / PULSE_PER_REVOLUTION);
         rps = rpm / 60;
         rph = rpm * (double)60;
         distance = distance + (double)rps * PI * DIA * cm_to_km;  //rps * 2 * pi *r = rps * 2 * pi * d / 2 = rps * pi * d cms
                                                                   //1cm = 1e-5 km
         speedKmsPerHour = (double)rph * PI * DIA * cm_to_km;
         speedMilesPerHour = (double)rph * PI * DIA * cm_to_miles;

         //speedKmsPerHour = (double)rps * PI * DIA * cm_to_km * sec_to_hr;
         //speedMilesPerHour = (double)rps * PI * DIA * cm_to_miles * sec_to_hr;
         
         previousCaptureTime = currentCaptureTime;
         counter = 0;
     }
}

// copy const to ram string
char *CopyConst2Ram(char *dest, const char *src) {
    char *d;

    d = dest;

    for(;*dest++ = *src++;);

    return d;
}

void nextionWrite255() {
    UART1_Write(0xFF);
    UART1_Write(0xFF);
    UART1_Write(0xFF);
}

void main() {
    TRISC = 0x84;
    PORTC = 0x00;

    UART1_Init(115200);
    Delay_ms(100);

    T1CON = 0x31;
    TMR1H = 0x00;
    TMR1L = 0x00;
    TMR1IF_bit = 0;
    TMR1IE_bit = 1;

    CCP1CON = 0x04;
    CCP1IF_bit = 0;
    CCP1IE_bit = 1;

    PEIE_bit = 1;
    GIE_bit = 1;

    while(1) {
        strcpy(msg,"n0.val=");
        LongToStr(rpm,txt);
        strcat(msg,txt);
        UART1_Write_Text(msg);
        nextionWrite255();

        strcpy(msg,"n1.val=");
        FloatToStr(distance,txt);
        strcat(msg,txt);
        //strcat(msg," kms");
        UART1_Write_Text(msg);
        nextionWrite255();

        strcpy(msg,"n2.val=");
        FloatToStr(speedKmsPerHour,txt);
        strcat(msg,txt);
        //strcat(msg," kms/hr");
        UART1_Write_Text(msg);
        nextionWrite255();
       
        strcpy(msg,"n3.val=");
        FloatToStr(speedMilesPerHour,txt);
        strcat(msg,txt);
        //strcat(msg," miles/hr");
        UART1_Write_Text(msg);
        nextionWrite255();

        Delay_ms(1000);
    }
}
 

Thread Starter

MCU_1548107851

Joined Jan 21, 2019
30
i try all code it not working

you told me test without change any thing but i know problem from here
FloatToStr(speedMilesPerHour,txt);

!!!!!!!!!!!!!!!!!!!!
 

jayanthd

Joined Jul 4, 2015
945
SIM3.png
i try all code it not working

you told me test without change any thing but i know problem from here
FloatToStr(speedMilesPerHour,txt);

!!!!!!!!!!!!!!!!!!!!
Why problem ? Explain why ?

sprinti() does the same this as IntToStr() and LongToStr(). You just need to convert rpm, distance, etc,... values to string before sending to display.

Try this code. Don't change anything. I am getting in Proteus all the values.

Verify if these are correct.

360 pulses input per sec (Proteus test)
36 pulses per revolution
rps = 10
rpm = 600
rph = 600 * 60 = 36000
speed kms/hr = 36000 * PI * DIA * cm_to_km = 12.4407 kms/hr (verify if this is correct)
speed miles/hr = 36000 * PI * DIA * cm_to_miles = 7.7303 miles/hr (verify if this is correct)

Code:
//#define PROTEUS

#ifndef PROTEUS
  #define NEXTION
#endif

#define PULSE_PER_REVOLUTION  36.0
#define FOSC                  20.0e6
#define PRESCALAR              8.0
#define PI                     3.14159265
#define DIA                   11.0
#define cm_to_km               1.0e-5
#define cm_to_miles            6.21371e-6
#define sec_to_hr              2.77778e-4

unsigned long counter = 0;
unsigned long period = 0;
unsigned int currentCaptureTime = 0;
unsigned int previousCaptureTime = 0;
unsigned long rpm = 0;
unsigned int rps = 0;
unsigned long rph = 0;
double distance = 0.0;
double speedKmsPerHour = 0.0;
double speedMilesPerHour = 0.0;
char msg[32];
char txt[32];

void interrupt() {
     if((TMR1IE_bit) && (TMR1IF_bit)) {
         TMR1IF_bit = 0;
         ++counter;
     }

     if((CCP1IE_bit) && (CCP1IF_bit)) {
         CCP1IF_bit = 0;
         currentCaptureTime = CCPR1;
         period = counter * (unsigned long)0xFFFF - (unsigned long)previousCaptureTime + (unsigned long)currentCaptureTime;
         rpm = (unsigned long)((60.0 / ((1.0 / (FOSC / 4.0) * PRESCALAR))) / (double)period / PULSE_PER_REVOLUTION);
         rps = rpm / 60;
         rph = rpm * (double)60;
         distance = distance + (double)rps * PI * DIA * cm_to_km;  //rps * 2 * pi *r = rps * 2 * pi * d / 2 = rps * pi * d cms
                                                                   //1cm = 1e-5 km
         speedKmsPerHour = (double)rph * PI * DIA * cm_to_km;
         speedMilesPerHour = (double)rph * PI * DIA * cm_to_miles;

         //speedKmsPerHour = (double)rps * PI * DIA * cm_to_km * sec_to_hr;
         //speedMilesPerHour = (double)rps * PI * DIA * cm_to_miles * sec_to_hr;
    
         previousCaptureTime = currentCaptureTime;
         counter = 0;
     }
}

// copy const to ram string
char *CopyConst2Ram(char *dest, const char *src) {
    char *d;

    d = dest;

    for(;*dest++ = *src++;);

    return d;
}

void nextionWrite255() {
    UART1_Write(0xFF);
    UART1_Write(0xFF);
    UART1_Write(0xFF);
}

void main() {
    TRISC = 0x84;
    PORTC = 0x00;

    UART1_Init(115200);
    Delay_ms(100);

    T1CON = 0x31;
    TMR1H = 0x00;
    TMR1L = 0x00;
    TMR1IF_bit = 0;
    TMR1IE_bit = 1;

    CCP1CON = 0x04;
    CCP1IF_bit = 0;
    CCP1IE_bit = 1;

    PEIE_bit = 1;
    GIE_bit = 1;

    while(1) {
        #ifdef PROTEUS
          strcpy(msg,"rps = ");
          LongToStr(rps,txt);
          strcat(msg,txt);
          strcat(msg,"\r\n");
          UART1_Write_Text(msg);
        #endif

        #ifdef PROTEUS
          strcpy(msg,"rpm = ");
        #else
          strcpy(msg,"n0.val=");
        #endif
        LongToStr(rpm,txt);
        Ltrim(txt);
        strcat(msg,txt);
        UART1_Write_Text(msg);
        #ifdef NEXTION
          nextionWrite255();
        #endif


        #ifdef PROTEUS
          strcpy(msg,"distance = ");
        #else
          strcpy(msg,"n1.val=");
        #endif
        FloatToStr(distance,txt);
        Ltrim(txt);
        strcat(msg,txt);
        #ifdef PROTEUS
          strcat(msg," kms\r\n");
        #endif
        UART1_Write_Text(msg);
        #ifdef NEXTION
          nextionWrite255();
        #endif

        #ifdef PROTEUS
          strcpy(msg,"speed = ");
        #else
          strcpy(msg,"n2.val=");
        #endif
        FloatToStr(speedKmsPerHour,txt);
        Ltrim(txt);
        strcat(msg,txt);
        #ifdef PROTEUS
          strcat(msg," kms/hr\r\n");
        #endif
        UART1_Write_Text(msg);
        #ifdef NEXTION
          nextionWrite255();
        #endif
  
  
        #ifdef PROTEUS
          strcpy(msg,"speed = ");
        #else
          strcpy(msg,"n3.val=");
        #endif
        FloatToStr(speedMilesPerHour,txt);
        Ltrim(txt);
        strcat(msg,txt);
        #ifdef PROTEUS
          strcat(msg," miles/hr\r\n");
        #endif
        UART1_Write_Text(msg);
        #ifdef NEXTION
          nextionWrite255();
        #endif

        Delay_ms(1000);
    }
}
 
Last edited:

Thread Starter

MCU_1548107851

Joined Jan 21, 2019
30
no its working !!!!!!

----------------------------------

it should be like that
// LongToStr(rpm,msg); (((((((((((((( no need this line ))))))))))))))))
Ltrim(msg);
asm clrwdt
strcat(msg,"\r\n");
sprinti(envia,"n0.val=%d",rpm/1000);
UART1_Write_Text(envia);
UART1_Write(0XFF);
UART1_Write(0XFF);
UART1_Write(0XFF);
 

jayanthd

Joined Jul 4, 2015
945
no its working !!!!!!

----------------------------------

it should be like that
// LongToStr(rpm,msg); (((((((((((((( no need this line ))))))))))))))))
Ltrim(msg);
asm clrwdt
strcat(msg,"\r\n");
sprinti(envia,"n0.val=%d",rpm/1000);
UART1_Write_Text(envia);
UART1_Write(0XFF);
UART1_Write(0XFF);
UART1_Write(0XFF);

Why rpm/1000 while printing ? What is your min and max rpm's ?

Show video of working display for my final code with display drive code changed to yours. Don't vary rpm in video (don't vary variac voltage). Set rpm to 600 and send video.

See if these calculations are correct. According to my previous post.

360 pulses input per sec (Proteus test)
36 pulses per revolution
rps = 10
rpm = 600
rph = 600 * 60 = 36000
speed kms/hr = 36000 * PI * DIA * cm_to_km = 12.4407 kms/hr (verify if this is correct)
speed miles/hr = 36000 * PI * DIA * cm_to_miles = 7.7303 miles/hr (verify if this is correct)

rps = 10
distance (cm) in 1 sec = rps * PI * DIA = 345.57 cm
distance (kms) in 1 sec = 345.57 * 1e-5 = 0.003455751915 kms

speed kms per sec = 0.003455751915 kms/sec
speed kms / hr = 0.003455751915 * 60 * 60 = 12.4407 kms/hr

distance (cm) in 1 sec = rps * PI * DIA = 345.57 cm
speed miles per sec = 345.57 * cm_to_miles miles/sec
= 345.57 * 6.21371e-6 miles/sec
= 0.0021472717647 miles/sec

speed miles / hr = miles/sec * 60 * 60 = x miles/hr
speed miles / hr = 0.0021472717647 * 60 * 60 = 7.7302 miles/hr

SIM4.png
 
Last edited:

spinnaker

Joined Oct 29, 2009
7,830
I don't ssee how this code can work:

period = counter * (unsigned long)0xFFFF - (unsigned long)previousCaptureTime + (unsigned long)currentCaptureTime;


What happens when the timer does not overflow before the CCP interrupt? counter will always be zero and the period zero.
 
Top