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

Thread Starter

MCU_1548107851

Joined Jan 21, 2019
30
now am confusing
why you change all time way to uart display
before its working
just need caculation to be change
but keep display to nextion as before ( nextion accept it before )
only change cacullation



why you copy string and display like that ????????
  1. strcpy(msg,"n0.val=");
  2. LongToStr(rpm,txt);
  3. strcat(msg,txt);
  4. UART1_Write_Text(msg);
  5. nextionWrite255();

  6. strcpy(msg,"n1.val=");
  7. FloatToStr(distance,txt);
  8. strcat(msg,txt);
  9. //strcat(msg," kms");
  10. UART1_Write_Text(msg);
  11. nextionWrite255();

  12. strcpy(msg,"n2.val=");
  13. FloatToStr(speedKmsPerHour,txt);
  14. strcat(msg,txt);
  15. //strcat(msg," kms/hr");
  16. UART1_Write_Text(msg);
  17. nextionWrite255();
  18. strcpy(msg,"n3.val=");
  19. FloatToStr(speedMilesPerHour,txt);
  20. strcat(msg,txt);
  21. //strcat(msg," miles/hr");
  22. UART1_Write_Text(msg);
  23. nextionWrite255();

  24. Delay_ms(1000);
  25. }
  26. }
 

Thread Starter

MCU_1548107851

Joined Jan 21, 2019
30
can you make code only for speed km/h
like rpm work before

to see from where problem

am confusing hhhhhhh

before i with you one by one now :) you make code so diffcult and more converting and string copy
before working without all this

i think this way much better
sprinti(envia,"n0.val=%d",rpm);
UART1_Write_Text(envia);
 

jayanthd

Joined Jul 4, 2015
945
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.
Timer tick period (20 MHz 1:8 prescalar) = 1.6us

For 65536 it is 65536 * 1.6us = 104.8 ms

OP has not mentioned min and max rpm.
 

jayanthd

Joined Jul 4, 2015
945
Okay, try this.

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;
     }
}

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) {
        sprinti(msg,"n0.val=%d",rpm);
        UART1_Write_Text(msg);
       
        //These will not display correct values
        sprintl(msg,"n1.val=%d",distance);
        UART1_Write_Text(msg);
       
        sprinti(msg,"n2.val=%d",(unsigned int)speedKmsPerHour);
        UART1_Write_Text(msg);
       
        sprinti(msg,"n3.val=%d",(unsigned int)speedMilesPerHour);
        UART1_Write_Text(msg);
       
        /*
        #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);
    }
}
 

jayanthd

Joined Jul 4, 2015
945
Try this new code. It should at least print the whole number part of the speed values with rpm and distance.

In your old code why were you sending 3x 0xFF for nextion like;

Code:
UART1_Write(0xFF);
UART1_Write(0xFF);
UART1_Write(0xFF);
???
Are those required as delimiters ?

20 MHz

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;
     }
}

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) {
        sprinti(msg,"n0.val=%d",rpm);
        UART1_Write_Text(msg);
     
        //These will not display correct values
        sprintl(msg,"n1.val=%u",(unsigned long)distance);
        UART1_Write_Text(msg);
     
        sprintl(msg,"n2.val=%u",(unsigned long)speedKmsPerHour);
        UART1_Write_Text(msg);
     
        sprintl(msg,"n3.val=%u",(unsigned long)speedMilesPerHour);
        UART1_Write_Text(msg);

        /*
        #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);
        Rtrim(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);
        Rtrim(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);
        Rtrim(txt);
        strcat(msg,txt);
        #ifdef PROTEUS
          strcat(msg," miles/hr\r\n");
        #endif
        UART1_Write_Text(msg);
        #ifdef NEXTION
          //nextionWrite255();
        #endif
        */

        Delay_ms(500);
    }
}
 

jayanthd

Joined Jul 4, 2015
945
Okay, here is the final code. Test it and it will work fine. Discard all previous codes. Only consider below code.

I checked on net how to send data to nextion TFTs.

The 3x 0xFF end bytes have to be sent to display. It works as delimiter.

Post the final video here. Upload video to mega.nz and provide link here. I need the video for reference. In video set variac voltage = rpm 600.

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 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;

         previousCaptureTime = currentCaptureTime;
         counter = 0;
     }
}

void nextionWriteEndBytes() {
    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) {
        sprinti(txt,"n0.val=%d",rpm);
        UART1_Write_Text(txt);
        nextionWriteEndBytes();
     
        UART1_Write_Text("n1.val=");
        FloatToStr(distance,txt);
        UART1_Write_Text(Rtrim(Ltrim(txt)));
        nextionWriteEndBytes();
     
        UART1_Write_Text("n2.val=");
        FloatToStr(speedKmsPerHour,txt);
        UART1_Write_Text(Rtrim(Ltrim(txt)));
        nextionWriteEndBytes();
     
        UART1_Write_Text("n3.val=");
        FloatToStr(speedMilesPerHour,txt);
        UART1_Write_Text(Rtrim(Ltrim(txt)));
        nextionWriteEndBytes();

        Delay_ms(500);
    }
}
 

jayanthd

Joined Jul 4, 2015
945
Cleaned up final code. mikroC PRO PIC project attached with Proteus simulation file. Proteus 8.8 SP1 is needed to open the file.

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 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;

         previousCaptureTime = currentCaptureTime;
         counter = 0;
     }
}

void writeEndBytesToNextion() {
    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) {
        UART1_Write_Text("n1.val=");
        LongToStr(rpm,txt);
        UART1_Write_Text(Rtrim(Ltrim(txt)));
        writeEndBytesToNextion();
   
        UART1_Write_Text("n1.val=");
        FloatToStr(distance,txt);
        UART1_Write_Text(Rtrim(Ltrim(txt)));
        writeEndBytesToNextion();
   
        UART1_Write_Text("n2.val=");
        FloatToStr(speedKmsPerHour,txt);
        UART1_Write_Text(Rtrim(Ltrim(txt)));
        writeEndBytesToNextion();
   
        UART1_Write_Text("n3.val=");
        FloatToStr(speedMilesPerHour,txt);
        UART1_Write_Text(Rtrim(Ltrim(txt)));
        writeEndBytesToNextion();

        Delay_ms(500);
    }
}
SIM6.png

CONVERSION.png
 

Attachments

Last edited:

Thread Starter

MCU_1548107851

Joined Jan 21, 2019
30
nothing work with last 5 code
only last code rpm work and other no and not correct ...
you ask me what max and min rpm i need same first code in book page 136 its ok





why not working ?!?!? other

can you make only speed meter and let me test it
one by one much better
 

Thread Starter

MCU_1548107851

Joined Jan 21, 2019
30
  1. UART1_Write_Text("n1.val="); ////////////////////////////////////////////////////// i correct it n0 not n1 but only rpm work but result not like page 136 before top good

  2. LongToStr(rpm,txt);
  3. UART1_Write_Text(Rtrim(Ltrim(txt)));
  4. writeEndBytesToNextion();
  5. UART1_Write_Text("n1.val=");
  6. FloatToStr(distance,txt);
  7. UART1_Write_Text(Rtrim(Ltrim(txt)));
  8. writeEndBytesToNextion();
 

jayanthd

Joined Jul 4, 2015
945
Test this. See if you get values on all the 4 lines of TFT.

This is a test code. I am just printing rpm value on all the lines of TFT.

Code:
[*]        UART1_Write_Text("n0.val=");
[*]        LongToStr(rpm,txt);
[*]        UART1_Write_Text(Rtrim(Ltrim(txt)));
[*]        writeEndBytesToNextion();
[*] 
[*]        UART1_Write_Text("n1.val=");
[*]        LongToStr(rpm,txt);
[*]        UART1_Write_Text(Rtrim(Ltrim(txt)));
[*]        writeEndBytesToNextion();
[*] 
[*]        UART1_Write_Text("n2.val=");
[*]        LongToStr(rpm,txt);
[*]        UART1_Write_Text(Rtrim(Ltrim(txt)));
[*]        writeEndBytesToNextion();
[*] 
[*]        UART1_Write_Text("n3.val=");
[*]        LongToStr(rpm,txt);
[*]        UART1_Write_Text(Rtrim(Ltrim(txt)));
[*]        writeEndBytesToNextion();
[*]
If that doesn't work then try this

Code:
[*]        UART1_Write_Text("n0.val=");
[*]        LongToStr(rpm,txt);
[*]        UART1_Write_Text(Rtrim(Ltrim(txt)));
[*]        
[*]        UART1_Write_Text("n1.val=");
[*]        UART1_Write_Text(Rtrim(Ltrim(txt)));
[*]         
[*]        UART1_Write_Text("n2.val=");
[*]        UART1_Write_Text(Rtrim(Ltrim(txt)));
[*]         
[*]        UART1_Write_Text("n3.val=");
[*]        UART1_Write_Text(Rtrim(Ltrim(txt)));

[*]        writeEndBytesToNextion();
[*]
 

Thread Starter

MCU_1548107851

Joined Jan 21, 2019
30
both not working

i do some thing just to try

its working but caculation all wrong

sprinti(envia,"n0.val=%d",rpm);
UART1_Write_Text(envia);
writeEndBytesToNextion();

sprinti(envia,"n1.val=%d",distance);
UART1_Write_Text(envia);
writeEndBytesToNextion();

sprinti(envia,"n2.val=%d",speedKmsPerHour);
UART1_Write_Text(envia);
writeEndBytesToNextion();

sprinti(envia,"n3.val=%d",speedMilesPerHour);
UART1_Write_Text(envia);
writeEndBytesToNextion();
 

Thread Starter

MCU_1548107851

Joined Jan 21, 2019
30
see attachment
this RPM code work fine

when you add speed and other
rpm cacullation become wrongand other not working


can you see attachment and make only speed meter km/h to see its working or not
 

Attachments

jayanthd

Joined Jul 4, 2015
945
see attachment
this RPM code work fine

when you add speed and other
rpm cacullation become wrongand other not working

can you see attachment and make only speed meter km/h to see its working or not
You are not reading my posts. I don't have nextion display to test and I have never used one. I checked some online nextion codes and all that I understood is that you have to send the formatted string and then 3x 0xFF for each transmission.

I have also mentioned that sprinti() is used to format integers into strings. It cannot be used to format floating point numbers into strings and that is causing the problem with printing values. RPM is a integer value and so sprinti() works fine with it.

Don't use sprinti() for distance, speedKmsPerHour and speedMilesPerHour. It will not work atleast in mikroC.

mikroC PRO PIC doesn't support sprintf() for PIC16F877A as this device has very less RAM.

Maybe you have not configured display properly to write floating point values to distance, speed kms/hr and speed miles/hr TFT fields.

Here is the code to display just speed kms/hr. Try it and reply.

In the below code uncomment only one of these 4 lines to test. After uncommenting one of these 4 lines, compile the code. I have also attached the full project as .rar file.

The posted code and attached project will display speed kms/hr value only.

Code:
//#define RPM
//#define DISTANCE
#define   SPEED_KMS_PER_HOUR
//#define SPEED_MILES_PER_HOUR
Full code

Code:
//#define RPM
//#define DISTANCE
#define   SPEED_KMS_PER_HOUR
//#define SPEED_MILES_PER_HOUR


#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 txt[23];

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;

         previousCaptureTime = currentCaptureTime;
         counter = 0;
     }
}

void writeEndBytesToNextion() {
    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 RPM
        LongToStr(rpm,txt);
        UART1_Write_Text("n0.val=");
        UART1_Write_Text(Rtrim(Ltrim(txt)));
        writeEndBytesToNextion();
        #endif
      
        #ifdef DISTANCE
        FloatToStr(distance,txt);
        UART1_Write_Text("n1.val=");
        UART1_Write_Text(Rtrim(Ltrim(txt)));
        writeEndBytesToNextion();
        #endif
      
        #ifdef SPEED_KMS_PER_HOUR
        FloatToStr(speedKmsPerHour,txt);
        UART1_Write_Text("n2.val=");
        UART1_Write_Text(Rtrim(Ltrim(txt)));
        writeEndBytesToNextion();
        #endif
      
        #ifdef SPEED_MILES_PER_HOUR
        FloatToStr(speedMilesPerHour,txt);
        UART1_Write_Text("n3.val=");
        UART1_Write_Text(Rtrim(Ltrim(txt)));
        writeEndBytesToNextion();
        #endif
      
        Delay_ms(500);
    }
}
 

Attachments

Thread Starter

MCU_1548107851

Joined Jan 21, 2019
30
also same not working there is some small thing missing in your code

i have some idea please
see attachment i have tow code in attachment

vary nice code for nextion
one for pic 16f877A
and other 18f2550


code for 16f877 more advance

see attachment and try to have some idea and if you please correct code
code example for volt meter and temperature also code send and receive from nextion for pwm controll
 

Attachments

jayanthd

Joined Jul 4, 2015
945
Try this code.

Code:
[*]//#define RPM
[*]//#define DISTANCE
[*]#define   SPEED_KMS_PER_HOUR
[*]//#define SPEED_MILES_PER_HOUR
[*]

[*]

[*]#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 txt[23];
[*]

[*]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;
[*]

[*]         previousCaptureTime = currentCaptureTime;
[*]         counter = 0;
[*]     }
[*]}
[*]

[*]void writeEndBytesToNextion() {
[*]    UART1_Write_Text("\xFF\xFF\xFF");   
[*]}
[*]

[*]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 RPM
[*]        LongToStr(rpm,txt);
[*]        UART1_Write_Text("n0.val=");
[*]        UART1_Write_Text(Rtrim(Ltrim(txt)));
[*]        writeEndBytesToNextion();
[*]        #endif
[*]    
[*]        #ifdef DISTANCE
[*]        FloatToStr(distance,txt);
[*]        UART1_Write_Text("n1.val=");
[*]        UART1_Write_Text(Rtrim(Ltrim(txt)));
[*]        writeEndBytesToNextion();
[*]        #endif
[*]    
[*]        #ifdef SPEED_KMS_PER_HOUR
[*]        FloatToStr(speedKmsPerHour,txt);
[*]        UART1_Write_Text("n2.val=");
[*]        UART1_Write_Text(Rtrim(Ltrim(txt)));
[*]        writeEndBytesToNextion();
[*]        #endif
[*]    
[*]        #ifdef SPEED_MILES_PER_HOUR
[*]        FloatToStr(speedMilesPerHour,txt);
[*]        UART1_Write_Text("n3.val=");
[*]        UART1_Write_Text(Rtrim(Ltrim(txt)));
[*]        writeEndBytesToNextion();
[*]        #endif
[*]    
[*]        Delay_ms(500);
[*]    }
[*]}
 

Thread Starter

MCU_1548107851

Joined Jan 21, 2019
30
speedKmsPerHour = (double)rph * PI * DIA * cm_to_km;

question plz .... how to caculate speed without divide distance
-----------------------------------------------------------------------------------

also i have code rpm work fine
but another code you change cacullation for RPM
------------------------------------------------------------------------------------------------------------------------
rpm = (unsigned long)(60.0e6 / (double)period);////////////////////// first code

-------------------------------------------------------------------------------------------------------------------------
rpm = (unsigned long)((60.0 / ((1.0 / (FOSC / 4.0) * PRESCALAR))) / (double)period / PULSE_PER_REVOLUTION);////////////////////////////// other code not working


which one is correct
 

Thread Starter

MCU_1548107851

Joined Jan 21, 2019
30
60.0e6

what the meaning of
e6

--------------------------------------------------------
also
eriod = counter * (unsigned long)0xFFFF - (unsigned long)previousCaptureTime +

what the meaning of 0xFFFF -- ?? is it 0xff
can you explane this line and caculation
 

jayanthd

Joined Jul 4, 2015
945
speedKmsPerHour = (double)rph * PI * DIA * cm_to_km;

question plz .... how to caculate speed without divide distance
-----------------------------------------------------------------------------------

also i have code rpm work fine
but another code you change cacullation for RPM
------------------------------------------------------------------------------------------------------------------------
rpm = (unsigned long)(60.0e6 / (double)period);////////////////////// first code

-------------------------------------------------------------------------------------------------------------------------
rpm = (unsigned long)((60.0 / ((1.0 / (FOSC / 4.0) * PRESCALAR))) / (double)period / PULSE_PER_REVOLUTION);////////////////////////////// other code not working


which one is correct
60.0e6

what the meaning of
e6

--------------------------------------------------------
also
eriod = counter * (unsigned long)0xFFFF - (unsigned long)previousCaptureTime +

what the meaning of 0xFFFF -- ?? is it 0xff
can you explane this line and caculation


Calculations are already explained in post 29, 37 and 39. Read them.
Also simulation results for 360 pulses per second have been posted in those posts.

0xFFFF = 65535 because timer1 is 16 bits. Its timer1 resolution.


Code:
rpm = (unsigned long)((60.0 / ((1.0 / (FOSC / 4.0) * PRESCALAR))) / (double)period / PULSE_PER_REVOLUTION);
The above code is written by me after looking at page 136 of the book.

In page no. 136 it is mentioned; "The rpm equation is derived as follows"

rpm = (60/counts)/0.8 = 75e6/counts

e6 = 1 X 10^6. It is 10 to the power of 6.

In below equation

rpm = (60/counts)/0.8 = 75e6/counts

0.8 is obtained like this.

They have used 1:2 Prescalar for timer1.

Fosc = 10 MHz

Fosc/4 = 10MHz/4 = 2500000

1/2500000 = 0.0000004 = 0.4us

0.4us * (Prescalar 1:2) = 0.4us * 2 = 0.8us

In equation

rpm = (60/counts)/0.8 = 75e6/counts

assume counts = 1

60/1/0.8 = 75000000 = 76e6 (75e6 = 6 zeroes after 75)

In book's calculation (page no. 135) section "Engine rpm Measurement Using Capture/Compare Module 1" they mention pulse/revolution = 1.

If count = 65535 then rpm = (60/counts)/0.8 = 60/2/0.8 = 37.5
-------------------------------------------------------------------------------------------------------------------------------------------
In my Calculation the equation is

Code:
rpm = (unsigned long)((60.0 / ((1.0 / (FOSC / 4.0) * PRESCALAR))) / (double)period / PULSE_PER_REVOLUTION);
There was a mistake in the above equation (my equation). it should be

Code:
rpm = (unsigned long)((60.0 / (1.0 / (FOSC / 4.0 / PRESCALAR))) / (double)period / PULSE_PER_REVOLUTION);
If my equation if used with books' values

60 / (1 / (10MHz/4/2))

(10MHz/4/2) = 1250000

1/1250000 = 0.0000008

60/0.0000008 = 75000000 = 75e6 (same as mentioned in the book).

PULSE_PER_REVOLUTION = 1

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

In your code

Fosc = 20e6 = 20MHz and PULSE_PER_REVOLUTION = 36 and PRESCALAR = 8 (1:8)

360 pulses per second input
36 pulses = 1 revolution (taken)
so, 10 revolutions per second (360/36 = 10)
rps = 10
rpm = rps * 60 = 10 * 60 = 600 (as shown in image)

Here is the fixed code. Test it and reply.

Code:
#define RPM
#define DISTANCE
#define   SPEED_KMS_PER_HOUR
#define SPEED_MILES_PER_HOUR


#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 txt[23];

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;

         previousCaptureTime = currentCaptureTime;
         counter = 0;
     }
}

void writeEndBytesToNextion() {
    UART1_Write_Text("\xFF\xFF\xFF");
}

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 RPM
        LongToStr(rpm,txt);
        UART1_Write_Text("n0.val=");
        UART1_Write_Text(Rtrim(Ltrim(txt)));
        writeEndBytesToNextion();
        #endif
     
        #ifdef DISTANCE
        FloatToStr(distance,txt);
        UART1_Write_Text("n1.val=");
        UART1_Write_Text(Rtrim(Ltrim(txt)));
        writeEndBytesToNextion();
        #endif
     
        #ifdef SPEED_KMS_PER_HOUR
        FloatToStr(speedKmsPerHour,txt);
        UART1_Write_Text("n2.val=");
        UART1_Write_Text(Rtrim(Ltrim(txt)));
        writeEndBytesToNextion();
        #endif
     
        #ifdef SPEED_MILES_PER_HOUR
        FloatToStr(speedMilesPerHour,txt);
        UART1_Write_Text("n3.val=");
        UART1_Write_Text(Rtrim(Ltrim(txt)));
        writeEndBytesToNextion();
        #endif
     
        Delay_ms(500);
    }
}
SS2.png
 

Attachments

Last edited:

Thread Starter

MCU_1548107851

Joined Jan 21, 2019
30
thanks for give me alot of your time
but unfortunately its not working
can you arange nextion lcd and test ??

another thing
see this code can you please in this code
remove rpm caculation and put only speed km/r
delete usless code not put in comment

i need to test some thing

code
unsigned long counter = 0;
unsigned long period = 0;
unsigned int currentCaptureTime = 0;
unsigned int previousCaptureTime = 0;
unsigned long rpm = 0;
char msg[17];
char envia[30];

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);
previousCaptureTime = currentCaptureTime;
counter = 0;
}
}

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 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
// LongToStr(rpm,msg);
Ltrim(msg);
asm clrwdt
strcat(msg,"\r\n");
sprinti(envia,"n0.val=%d",rpm/100);
UART1_Write_Text(envia);
UART1_Write(0XFF);
UART1_Write(0XFF);
UART1_Write(0XFF);

asm clrwdt
// Delay_XSec(1);
delay_ms(100);
}
}
 
Top