PIC16f676 I attach potentiometer 10k but its not working any one please help me.

Thread Starter

mevilbhojani

Joined Jun 26, 2020
1
C:
#include <HTC.H>

__CONFIG(0x3E54);   //PWRTEN

#define _XTAL_FREQ 4000000


#define LED1      RC0    //PIN NO10
#define LED2      RC1    //PIN NO9
#define RELAY     RC2    //PIN NO8
#define ECHO      RC5    //PIN NO5
#define TRIG      RC4    //PIN NO6
//#define VIN         RA1    //PIN NO12

//=============

unsigned int object_hit_count=0;  //initialization of variable that will count number ultrasonic hits out of check count
//=============

unsigned int Time_Set = 0;
unsigned  int distance = 0;

unsigned char   kk;
const unsigned int SetDistl = 2;   // lower distance of detection
const unsigned int SetDisth = 6;  //higher distance of detection
const unsigned int RelDist = 7;

void DELAY(void)
{
    unsigned char I;
    LED2 = 1 ;
    RELAY = 0;
    for(I=0;I<20;I++)
    {}
}

void Delay200ms(void)
{
   unsigned long int l;
   for(l = 0x204b9; l > 0; l--);         // Use hex long value to change delay for high
}

void Delay10us(void)
{
   unsigned char i;
   for(i = 0x10; i > 0; i--);
   #asm
       NOP
       NOP
    #endasm
}

void Read_ADC(void)
{
   ADCON1   =   0xF0; //ADC configuration register1
   ADCON0   =    0x85; //ADC CONFIGURATION REGISTER0
   DELAY();
   GO_DONE   =   1;
   while(GO_DONE == 1);    //while(GODONE);
   Time_Set   =   ADRESH; //ADC MSB BITS
   Time_Set   =   (Time_Set<<8)|ADRESL; //ADC LSB BITS
}


//============FUNCTION FOR COLLECTING DISTANCE====================


void Read_Dist(void)
{
   int i;
   unsigned int time_taken=0;
   object_hit_count=0;

   for(i=0;i<10;i++){
   distance = 0;
   TMR1H    =    0; //TIMER1 MSB REGISTER
   TMR1L   =   0; //TIMER2 LSB REGISTER
   TRIG   =   1; //GIVING TRIGGER
   Delay10us();
   TRIG   =   0; //TURNING OF THE TRIG PIN
   while(ECHO == 0); //WAITING WHILE ECHO STARTS
   TMR1ON = 1; //STARTING THE TIMER
   while(ECHO == 1); //WAIT UNTIL ECHO STOPS
   TMR1ON = 0; //STOPS THE TIMER
   time_taken    = (unsigned)(TMR1L | (TMR1H<<8)); //COLLECTING THE TIME TAKEN TO VARIABLE "time_taken"
   distance = (unsigned)(time_taken*0.017);   //EQUATION FOR CALCULATING DISTANCE
   if((distance > SetDistl)&&(distance < SetDisth)){ //CHECKING WEATHER IF CALCULATED DIASTANCE GREATER THAN LOW THRESHOLD DIS AND LESSER THAN HIGH THRESHOLD DIS
       object_hit_count++; //INCRESING THE HIT COUNT
   }
   __delay_ms(10);    //10MS DELAY BEFORE STARTING THE CHECKING AAGAIN
   }

}
//=====================================MAIN===================================
void main(void)
{
   int k;
   int j;
   unsigned int cur_time = 0;
   TRISA      =      0x3F; //PORT A DIRECTION SELECT REGISTER
   TRISC      =      0x20; //PORT C DIRECTION SELECT REGISTER
   T1CON      =      0x10; //TIMER 1 CONFIGURATION REGISTER
   ANSEL      =       0x02; // REGISTER FOR ADC CONFIGURATION OF PINS
   TRIG      =      0;    //SETTING TRIG PIN AS LOW INITIALY
   RELAY      =       0; //SETTING RELAY PIN AS LOW INITIALY
   LED1      =      0;    //SETTING LED1  PIN AS LOW INITIALY
   LED2      =       1; //SETTING LED2 PIN AS HIGH INITIALY
   kk         =      0;
   Read_ADC();
   for(k=0;k<100;k++){
      __delay_ms(10); //WAITING FOR 1S
   }
   while(1)
   {
      Read_ADC();    //READING ADC
      Read_Dist(); //READING DISTANCE
      if(object_hit_count>=5) // CHECKING WEATHER NUMBER OF HITS GREATER THAN THRESHOLD
      {
          kk = 1;
         cur_time = 0; //CURRENT TIME
         RELAY   = 1; //TURNING THE RELAY ON
        // __delay_ms(95); // Water Dispense time
        // RELAY = 0; //TURNING THE RELAY OFF
         LED1   = 1; //g //TURNING ON LED1
         LED2   = 0; //TURNING OFF LED2
         if(Time_Set == 0)
              __delay_ms(65);         // On time set for motor
            else{     
             Z:
             __delay_us(147);
             //Delay10ms();
             if(cur_time < Time_Set)
             {
                cur_time++;
                goto Z;
             }
                // Delay after motor off
         }
            RELAY   = 0; //TURNING OFF RELAY
            LED1   = 0; //TURNING OFF LED 1
            LED2   = 1; //TURNING ON LED 2
v:
LED1    = 1;
LED2    = 0;
for(j=0;j<25;j++)
{
    __delay_ms(10); //WAITING FOR 2 SECONDS
}
LED1    = 0;
LED2    = 1;
{
    __delay_ms(10); //WAITING FOR 2 SECONDS
}
   
      }
    
   }
}
Mod edit: code tags - JohnInTX
 
Last edited by a moderator:

jpanhalt

Joined Jan 18, 2008
9,755
That looks like a program for some sort of line follower or obstacle avoidance robot. What is it? Can you provide a fuller description and a block diagram and/or schematic?
 

upand_at_them

Joined May 15, 2010
328
You've set all of PORTA as input. Do you have things attached, or pullups, on all of those pins? My normal procedure is to set everything as output, and drive low, and then set to input only those things that are actually inputs. Floating inputs cause problems.
 

Ian Rogers

Joined Dec 12, 2012
784
Just check you are not loading your sram... That chip only has 64 bytes.. You are using 26 bytes before any calls.. The stack may be right on its limits... That chip ISN'T C friendly..
 
Top