Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Purpose is to measure the frequency of a waveform and to display the value on a

ID: 3572210 • Letter: P

Question

Purpose is to measure the frequency of a waveform and to display the value on a LCD. Using a PIC18F26k22. I have everything hooked up and running. just need to check my code cause its not working correctly. Its values are jumping to high and to low.

Code section:


#include <stdio.h>
#include <stdlib.h>
#include <xc.h>
#include "pic18f26k22.h"

#pragma config WDTEN = OFF     //Watch dog timer is off
#pragma config PWRTEN = ON     //Power-up timer is on
#pragma config LVP = OFF     //Low voltage programming is disabled
#pragma config FOSC = INTIO67 //Internal oscillator selected

#define _XTAL_FREQ 16000000
#define E PORTAbits.RA2
#define RS PORTAbits.RA0
#define RW PORTAbits.RA1
void delay(int sec);
void initLCD(void);
void clockE(void);

/*
*
*/
int main(int argc, char** argv) {
    int results=0;
    float freq=0;
    unsigned char f[5],i;
    OSCCON =0x72;
    ANSELA =0;
    ANSELB =0;
    ANSELC =0;
    TRISA =0;
    TRISC =0;
    initLCD();
    PORTC=0xFF;
    while(1){
        delay(2);
        TRISBbits.RB0=1;
        T1CON=0X42;
        CCPTMRS1=0x00;
        CCP4CON=0x05;
        PIR4bits.CCP4IF=0;
        TMR1L=0;
        TMR1H=0;
        while(!PIR4bits.CCP4IF);
        T1CONbits.TMR1ON=1;
        CCP4CON=0x04;
        CCPR4H=0;
        CCPR4L=0;
      
        PIR4bits.CCP4IF=0;
        while(!PIR4bits.CCP4IF);
        results=CCPR4H;
        results=results<<8;
        results=results|CCPR4L;
        freq=1.0/((float)results*(0.0000000625));
      
        sprintf(f,"%f",freq);
        RS=0;
        PORTC=0x80;
        clockE();
        RS=1;
        PORTC=0x66;
        clockE();
        PORTC=0x20;
        clockE();
        PORTC=0x3D;
        clockE();
        PORTC=0x20;
        clockE();
//        for(i=0;i<1;i++){
//            PORTC=f[i];
//            clockE();}
      
        for(i=1;i<5;i++){
            PORTC=f[i];
            clockE();}
      
        PORTC=0x20;
        clockE();
        PORTC=0x48;
        clockE();
        PORTC=0x7A;
        clockE();
        }      
  
    
    return (EXIT_SUCCESS);
}
void delay(int sec){
    int i,j;
    for(i=0;i<sec;i++)
        for(j=0;j<100;j++)
            __delay_ms(10);
}

void clockE(void)
{
    E =1; //Assume E defined by a macro
    __delay_ms(1);
    E =0;
    __delay_ms(1);
    E =1;
  
}
void initLCD(void)
{
    __delay_ms(20);
    RS=0;
    RW=0;
    PORTC=0X30;
    clockE();
  
     __delay_ms(20);
    RS=0;
    RW=0;
    PORTC=0X30;
    clockE();
  
     __delay_ms(20);
    RS=0;
    RW=0;
    PORTC=0X30;
    clockE();
  
     __delay_ms(20);
    RS=0;
    RW=0;
    PORTC=0X38;
    clockE();
  
     __delay_ms(20);
    RS=0;
    RW=0;
    PORTC=0X08;
    clockE();
  
     __delay_ms(20);
    RS=0;
    RW=0;
    PORTC=0X0C;
    clockE();
  
     __delay_ms(20);
    RS=0;
    RW=0;
    PORTC=0X06;
    clockE();
  
     __delay_ms(20);
    RS=0;
    RW=0;
    PORTC=0X01;
    clockE();
  
  
}

Explanation / Answer

I think issue is with your delay(2) seconds in yuor very first line in infinite while loop. Making 2 seconds delay cause remianing lines execute in synchronous way. So better to remove it. otherwise code seemed to be good.

while(1){
// delay(2);
        TRISBbits.RB0=1;
        T1CON=0X42;
        CCPTMRS1=0x00;
        CCP4CON=0x05;
        PIR4bits.CCP4IF=0;
        TMR1L=0;
        TMR1H=0;
        while(!PIR4bits.CCP4IF);
        T1CONbits.TMR1ON=1;
        CCP4CON=0x04;
        CCPR4H=0;
        CCPR4L=0;
  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote