[SOLVED] ADC module becomes inactive during time delay.

Status
Not open for further replies.

pnjbtr

Full Member level 5
Joined
Feb 11, 2008
Messages
255
Helped
99
Reputation
198
Reaction score
89
Trophy points
1,318
Activity points
2,815
This program is for PIC16F676 in micro c compiler.
Code:
        sbit LED1 at RC0_bit;     // 1
        sbit LED2 at RA2_bit;     // 2
        sbit LED3 at RC2_bit;  // TIMER DISPLAY
        sbit SWITCH at RA3_bit;  //TIMER SWITCH
        
        int Adc; // Save analog Data
         void Read_Adc()
 {

 ADCON0.GO=1; // Start Convert
 while(ADCON0.GO); // Wait Until Convert Complete
          ADC = (ADRESH << 8)  + ADRESL;      //10 bit res

            
          }


     void main()
 {

 TRISA=0b11011001; //SET PORT A AS INPUT
 TRISC = 0b11000000; //SET PORT C AS OUTPUT

 PORTA=0X00;
      // portc=0b11111110;
        if (porta.f3==0){  // TIMER SWITCH PRESSED
         portc.f2=0;       //off during delay
          delay_ms(5000);
            portc.f2=1;     //on at delay end
          }
            else (portc.f2=1);  // if switch, not pressed then instant on.

  while(1)
 {




              CMCON=7; //DISABLE COMPARATOR
             ANSEL=0b00000001;
            ADCON1=0b01100000;   //AD CONVERSION CLOCK SPEED=/64
       ADCON0=0b10000011;//SELSECT AN0, RIGHT JUSTIFIED,VREF=VDD and bit1, ADON,fosc/32(bit6,7).bit1_godone=off

      //10 bit=5/1023=0.00488
    Read_Adc(); // Read Analog AN0
       if (Adc > 614) {   //3v
 //==========================================
      Portc.f0 = 1; //1st
      porta.f2 = 1;        //2nd

    }
 //============================================
    if (Adc<512) //   2.498v
 {
    portc.f0 = 0;
    delay_ms(5);
 }
   if  (Adc>532)   //2.596v
 {
    portc.f0 = 1;
 }
 //==================================================
        if (Adc < 426) {    // 2v

     porta.f2 = 0;          //2nd led off

   }
      if (Adc > 441 ) {  //     2.15v


     porta.f2= 1;            //2nd led on
  }
 }   //end of while


 }   // end of main
I am facing problem,when switch is pressed for time delay, adc module become inactive
and stop working.
And when time delay ends adc module start working.
 

Attachments

  • adc+timer.rar
    15.8 KB · Views: 95

hello,


it is normal, because you only wait 5000mS , without reading ADC !
I thing Delay is manage by a loop , not by a timer interrupt....

and the while loop must include the switch reading.
Init ADC must be done only one time (it is enough!)

Code:
  sbit LED1 at RC0_bit;     // 1
  sbit LED2 at RA2_bit;     // 2
  sbit LED3 at RC2_bit;  // TIMER DISPLAY
   sbit SWITCH at RA3_bit;  //TIMER SWITCH
        
  int Adc; // Save analog Data
 void Read_Adc()
 {

 ADCON0.GO=1; // Start Convert
 while(ADCON0.GO); // Wait Until Convert Complete
  ADC = (ADRESH << 8)  + ADRESL;      //10 bit res
   }

     void main()
 {

 TRISA=0b11011001; //SET PORT A AS INPUT
 TRISC = 0b11000000; //SET PORT C AS OUTPUT

 PORTA=0X00;
      // portc=0b11111110;

   CMCON=7; //DISABLE COMPARATOR
   ANSEL=0b00000001;
   ADCON1=0b01100000;   //AD CONVERSION CLOCK SPEED=/64
    ADCON0=0b10000011;//SELSECT AN0, RIGHT JUSTIFIED,VREF=VDD and bit1, ADON,fosc/32(bit6,7).bit1_godone=off      
      
      
  while(1)
 {

      if (porta.f3==0){  // TIMER SWITCH PRESSED
         portc.f2=0;       //off during delay
          delay_ms(5000);
            portc.f2=1;     //on at delay end
          }
            else (portc.f2=1);  // if switch, not pressed then instant on.

      //10 bit=5/1023=0.00488
    Read_Adc(); // Read Analog AN0
       if (Adc > 614) {   //3v
 //==========================================
      Portc.f0 = 1; //1st
      porta.f2 = 1;        //2nd

    }
 //============================================
    if (Adc<512) //   2.498v
 {
    portc.f0 = 0;
    delay_ms(5);
 }
   if  (Adc>532)   //2.596v
 {
    portc.f0 = 1;
 }
 //==================================================
        if (Adc < 426) {    // 2v

     porta.f2 = 0;          //2nd led off

   }
      if (Adc > 441 ) {  //     2.15v


     porta.f2= 1;            //2nd led on
  }
 }   //end of while


 }   // end of main
so use a timer interrupt to create this delay of 5000mS
and inside timer interrupt do ReadAC()



Explain more what do you want to do .
 
Reactions: pnjbtr

    pnjbtr

    Points: 2
    Helpful Answer Positive Rating
Hi, thanks for reply.
it is normal, because you only wait 5000mS
5000ms delay is only for testing in proteus for simulation.
Actual delay time is 5 minutes.
so use a timer interrupt to create this delay of 5000mS
and inside timer interrupt do ReadAC()
Do you have any example?
 

Try this code.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
float noOfTimer1Interrupts = 0.0, desiredDelayInSeconds = 5.0, timeElapsed = 0.0;
unsigned char timerFlag = 0;
 
//Timer1
//Prescaler 1:8; TMR1 Preload = 3035; Actual Interrupt Time : 500 ms
 
//Place/Copy this part in declaration section
void InitTimer1(){
      T1CON = 0x31;
      TMR1IF_bit = 0;
      TMR1H = 0x0B;
      TMR1L = 0xDB;
      TMR1IE_bit = 1;
      INTCON = 0xC0;
}
 
void Interrupt(){
      if (TMR1IF_bit){
            //Enter your code here
            timeElapsed = (++noOfTimer1Interrupts) / 2.0;
            if(timeElapsed == desiredDelayInSeconds) {
                           timerFlag = 1;
                           TMR1IE_bit = 0;
            }
            TMR1H = 0x0B;
            TMR1L = 0xDB;
            TMR1IF_bit = 0;
      }
}
 
 
//Call InitTimer1(); once when timer1 has to be started
 
//Test if timerFlag is set in while(1) loop and if set then do what you want to do. timerFlag set means required delay is reached.

 
Reactions: pnjbtr

    pnjbtr

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…