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

#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);
}
}
#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
#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);
}
}
#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);
}
}
#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);
}
}
Why problem ? Explain why ?i try all code it not working
you told me test without change any thing but i know problem from here
FloatToStr(speedMilesPerHour,txt);
!!!!!!!!!!!!!!!!!!!!
//#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);
}
}
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);
