+ Post New Thread
Results 1 to 2 of 2
  1. #1
    Junior Member level 3
    Points: 397, Level: 4

    Join Date
    Nov 2014
    Posts
    29
    Helped
    0 / 0
    Points
    397
    Level
    4

    p18f1330 not working with my code, help needed

    hi, currently i'm working on a new pic, p18f1330. bt previously i was using p18f4580 and my coding works fine, but wen i convert it to port in p18f1330, its not working, pls help..my project is using ultrasonic hc-sr04 sensor. whereby the distance measured will be display in hyperterminal and LED wil blink at diferent rates.. now i port it to p18f1330, wit jus led blinking and nt dispalying in hyperterminal, but now its not fuctioning, please help

    i'm using mplab ide, c18 compiler and pickit2

    p18f4580 code
    Code:
    #include 
    #include 
    #include 
    #include 
    #include 
    
    #pragma config OSC= HS
    #pragma config WDT=OFF
    #pragma config LVP=OFF
    
    #define Trig PORTBbits.RB3
    #define Echo PORTBbits.RB2
    
    void T0Delay10us(void);
    void uart(void);
    void Call_Init_PIC (void);
    void sendPulse(void);
    
    unsigned int distance ;
    unsigned int time;
    unsigned f,b;
    
    void main(void)
    {
    TRISDbits.TRISD0=0;
    Call_Init_PIC ();
    uart();
    while(1)
    {
    TMR1H= 0;
    TMR1L= 0;
    sendPulse();
    
    while(Echo == 0);
    
    T1CONbits.TMR1ON = 1;
    while(Echo == 1);
    T1CONbits.TMR1ON = 0;  
    
                      
    
    time= TMR1H;
    time= time<<8;
    time= time|TMR1L;
    //time = (TMR1L + (TMR1H<<8));
    
    distance = time/28.8;
    
    f=distance/10;
    b=distance%10;
    printf("\r\nDistance = %d.%d cm",f,b);  
    PIR1bits.TMR1IF=0;
    Delay10KTCYx(255); 
    
    /*
    if(distance>=0 && distance<=250)
    {
    LATDbits.LATD0=1;
    }
    
    else if(distance>=251 && distance<=500)
    {
    LATDbits.LATD0=1;
    Delay10KTCYx(25);
    LATDbits.LATD0=0;
    }
    
    else if (distance>=501 && distance<=1000)
    {
    LATDbits.LATD0=1;
    Delay10KTCYx(25);
    Delay10KTCYx(25);
    Delay10KTCYx(25);
    LATDbits.LATD0=0;
    }
    
    else
    {
    }
    */
    }
    }
    
    void Call_Init_PIC (void)
    {
    TRISBbits.TRISB3=0;
    TRISBbits.TRISB2=1;
    }
    
    
    void uart(void)
    {
    TXSTA=0B00100000;
    SPBRG=32;
    TRISCbits.RC6=0;
    RCSTAbits.SPEN=1;
    }
    
    
    void sendPulse(void)
    	{
    	Trig=0;
    	Delay10KTCYx(255);
    	Trig=1;
    	T0Delay10us();
    	Trig=0;
    
    	}
    
    
    void T0Delay10us()
    	{
    	T0CON=0x08;
    	TMR0H=0XFF;
    	TMR0L=0XCE;
    	T0CONbits.TMR0ON=1;
    	while(INTCONbits.TMR0IF==0);
    	T0CONbits.TMR0ON=0;
    	INTCONbits.TMR0IF=0;
    	}
    p18f1330 code
    Code:
    #include  
    #include 
    
    #pragma config OSC = INTIO2 
    #pragma config WDT = OFF
    
    
    #define Trig PORTBbits.RB1
    #define Echo PORTBbits.RB0
    
    void Call_Init_PIC (void);
    void sendPulse(void);
    void T0Delay10us(void);
    
    unsigned int distance ;
    unsigned int time;
    
    void main(void)
    {
    TRISAbits.TRISA0=0;
    Call_Init_PIC ();
    while(1)
    {
    TMR0H= 0;
    TMR0L= 0;
    sendPulse();
    
    while(Echo == 0);
    
    T1CONbits.TMR1ON = 1;
    while(Echo == 1);
    T1CONbits.TMR1ON = 0;
    //Delay10KTCYx(255);   
    time= TMR0H;
    time= time<<8;
    time= time|TMR0L;
    distance = time/28.8;
    
    
    if(distance>=0 && distance<=250)
    
    {
    
    PORTAbits.RA0=1;
    
    }
    else
    
    {
    
    
    
    }
    }
    }
    
    void Call_Init_PIC (void)
    {
    TRISBbits.TRISB1=0;
    TRISBbits.TRISB0=1;
    }
    
    
    void sendPulse(void)
    	{
    	Trig=1;
    	T0Delay10us();
    	Trig=0;
    	}
    
    void T0Delay10us()
    	{
    	T0CON=0x08;
    	TMR0H=0XFF;
    	TMR0L=0XCE;
    	T0CONbits.TMR0ON=1;
    	while(INTCONbits.TMR0IF==0);
    	T0CONbits.TMR0ON=0;
    	INTCONbits.TMR0IF=0;
    	}

    •   AltAdvertisment

        
       

  2. #2
    Full Member level 1
    Points: 1,838, Level: 9
    Achievements:
    7 years registered
    ernpao's Avatar
    Join Date
    Sep 2010
    Posts
    100
    Helped
    27 / 27
    Points
    1,838
    Level
    9

    Re: p18f1330 not working with my code, help needed

    I noticed in your code for p18f1330 that you use time=TMR0H and not TMR1. Maybe this is the problem.



--[[ ]]--