Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] LED flash with pic 12f1822

Status
Not open for further replies.

Amanda Lima

Junior Member level 1
Joined
Jul 24, 2021
Messages
16
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
121
Hello people, I really need your help, how to configure the timer0 of pic 12f1822 to flash a led at a frequency of one second using CCS Compiler? New%20Project.pdsprj gf.c
 

Solution
Is this a new problem?
Did you solve your previous one? If so what was the error?
Also you have not read (or not understood) my previous post (#7) as you don't declare 'cnt' to be volatile.
Is your question now 'how to set up a timer'? If so then I'd start with a 'flash a LED' level program and get the timer working with that. (I'm not saying more now as I have no real idea what you are asking.)
Susan
Hello Aussie Susan, This is not a new problem, it's just a continuation of the previous one, this one now depends on the previous one which is to use timer0 to time, so I wanted to make a program to flash an LED' and make the timer work with that, this last program worked in...

Amanda Lima

Junior Member level 1
Joined
Jul 24, 2021
Messages
16
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
121
Hello people, I really need your help, how to configure the timer0 of pic 12f1822 to flash a led at a frequency of one second using CCS Compiler? New%20Project.pdsprj gf.c
I'm sorry, I'll post

Code:
#include <gf.h>
int time = 0;//;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#INT_TIMER0
void  TIMER0_isr(void)

{
time ++;//;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
//}
//set_timer0(6);
set_timer0(0);
clear_interrupt(int_timer0);
//set_timer0( 15369 + get_timer0());   //;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
//Inicialização da contagem para o proximo ciclo de interrupcao com 15369
//get_timer_0() compensa o tempo de execucao das operacoes do timer ( delay_ms(50) )
}
void main()
{
   setup_adc_ports(sAN1|sAN2|sAN3);
   setup_adc(ADC_CLOCK_INTERNAL);
  
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256|RTCC_8_bit);      //65,5 ms overflow
   clear_interrupt(int_timer0);    //;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

   enable_interrupts(INT_TIMER0);
   enable_interrupts(GLOBAL);
   //set_timer0( 15369);                                      //;;;;;;;;;;;;;;;;;;;;;;;;;;;;
   //set_timer0(6);
   set_timer0(0);                                      //;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
   while(TRUE)
   {
       if (time < 7)
      {
       output_high(pin_a0);
       output_high(pin_a5);
     }
      if (time > 7)
    {
       output_low(pin_a5);
       output_low(pin_a0);
     }
      if (time > 14 )
     { time = 0;

     }
      }
}
 
Last edited by a moderator:

KlausST

Super Moderator
Staff member
Joined
Apr 17, 2014
Messages
20,352
Helped
4,441
Reputation
8,891
Reaction score
4,468
Trophy points
1,393
Activity points
134,557
Hi,

Basically "flashing" is the same as PWM ....
So you could do an internet search for
* software PWM, or
* hardware PWM, or
* interrupt driven PWM

I guess there are already million results. ... with code exampkes, with great explanations, even as videos

Klaus
 

c_mitra

Advanced Member level 5
Joined
Nov 13, 2012
Messages
3,739
Helped
921
Reputation
1,844
Reaction score
907
Trophy points
1,393
Activity points
29,394
Your header file is loaded? This code is for a particular board. You need to read the documentation for the calls. This is an isr driven routine and should be well documented.
 

Amanda Lima

Junior Member level 1
Joined
Jul 24, 2021
Messages
16
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
121
Hi,

Basically "flashing" is the same as PWM ....
So you could do an internet search for
* software PWM, or
* hardware PWM, or
* interrupt driven PWM

I guess there are already million results. ... with code exampkes, with great explanations, even as videos

Klaus
I searched the internet and didn't find it, that's why I'm here, in short, I need a timer for several times, first step, I'm wanting to make a led blink at a frequency of one second, next step put other times. Is this part of the interruption correct?

C:
#include <gf.h>
int time = 0;//;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#INT_TIMER0
void  TIMER0_isr(void)

{
time ++;//;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
//}
//set_timer0(6);
set_timer0(0);
clear_interrupt(int_timer0);
//set_timer0( 15369 + get_timer0());   //;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
//Inicialização da contagem para o proximo ciclo de interrupcao com 15369
//get_timer_0() compensa o tempo de execucao das operacoes do timer ( delay_ms(50) )
}
void main()
{
   setup_adc_ports(sAN1|sAN2|sAN3);
   setup_adc(ADC_CLOCK_INTERNAL);
 
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256|RTCC_8_bit);      //65,5 ms overflow
   clear_interrupt(int_timer0);    //;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

   enable_interrupts(INT_TIMER0);
   enable_interrupts(GLOBAL);
   //set_timer0( 15369);                                      //;;;;;;;;;;;;;;;;;;;;;;;;;;;;
   //set_timer0(6);
   set_timer0(0);                                      //;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
   while(TRUE)
   {
       if (time < 7)
      {
       output_high(pin_a0);
       output_high(pin_a5);
     }
      if (time > 7)
    {
       output_low(pin_a5);
       output_low(pin_a0);
     }
      if (time > 14 )
     { time = 0;

     }
     }
       }
 

Aussie Susan

Advanced Member level 4
Joined
Jan 5, 2015
Messages
1,314
Helped
386
Reputation
772
Reaction score
392
Trophy points
83
Activity points
14,182
I don't know what is defined and declared in 'gf.h' but any variable that is updated in an ISR and tested outside of that, must be declared 'volatile' to stop the compiler from assuming that it can put the value into a register and use it from there.
 

c_mitra

Advanced Member level 5
Joined
Nov 13, 2012
Messages
3,739
Helped
921
Reputation
1,844
Reaction score
907
Trophy points
1,393
Activity points
29,394
Please post the errors you are getting during the compile. I cannot comment because I do not know what is included in the gf.h header.
 

Amanda Lima

Junior Member level 1
Joined
Jul 24, 2021
Messages
16
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
121
Good afternoon, I want to assemble a photocell with pic 12f1822 for school work, and I can't program it, I'll send the diagram and the program, pin 2 (RA5) goes to the base of the transistor of the relay, pin 3 (RA4) comes from the splitter voltage a 100k ohm trimpot which is for timing with 8 hours, 1.2 3, 4.6, 8.10 and 12 hours, pin 4 (RA3 MCLR) is receiving a signal from the mains (clock signal type), but I want it to count without an external signal, pin 5 (RA2) comes from another 100k ohm trimpot with 6 lux levels, pin 6 (RA1) comes from the LDR, if you analyze the scheme during the day, it goes high logic level to this pin (RA1) and the night low level, pin 7 (RA0) goes to the base of the transistor, which when conducting shorts 1 diode, to save energy when the relay is off. The problem I found is to count on the pic and time it at those times using the timer, with delay I couldn't, if anyone can help me I appreciate it. Thanks.




C:
#include <main.h>
int16 analogico1 = 0;
int16 analogico2 = 0;
int16 analogico3 = 0;
int16 minuto = 0;

#fuses NOMCLR INTRC_IO PLL_SW
#device ADC = 10
#use delay(clock=32000000)
#use fast_io(A)
int32 cnt = 0;

#INT_RA                                // interrupção RA.
//------------------------------------------------------------------------
void RA_isr(void)                         // interromper a rotina de serviço
{
     clear_interrupt(INT_RA3);
  if(input(PIN_A3))                    // Se RA3 for HIGH
  {
   cnt++;
  }
}

void main()
{
     setup_adc_ports(sAN1|sAN2|sAN3);
     //setup_adc_ports(sAN1);
    // setup_adc_ports(sAN2);
    // setup_adc_ports(sAN3);
     setup_adc(ADC_CLOCK_INTERNAL);
     setup_oscillator(OSC_8MHZ | OSC_PLL_ON);   // Set internal oscillator to 32MHz (8MHz and PLL)
     set_tris_a  (0b00011110 );   //(0b00011110);                      // HEX 1E  RA Tris  76543210  0X1E
     enable_interrupts(GLOBAL);                  // Enable global interrupts
     enable_interrupts(INT_RA3);
     clear_interrupt(INT_RA3);                    // Clear RA IOC flag bit
     output_high(pin_a0);
     output_low(pin_a5);

while(TRUE)

    {
    if (cnt >= 3600 )     // 3599)                    // Passou 1 minuto
    {
      cnt = 0;
      minuto++;
    }
 
    set_adc_channel (1);
     delay_us (100);
     analogico1 = read_adc();
     delay_us (100);
     set_adc_channel (2);
    delay_us (100);
     analogico2 = read_adc();
     delay_us (100);
     set_adc_channel (3);
    delay_us(100);
     analogico3 = read_adc();
     delay_us (100);
   
      if( analogico1 > analogico2  )                  
    {
      output_low(pin_a5);               // Desliga rele    
      cnt = 0;                       // Zera contador
      minuto=0;
      output_high(pin_a0);
    }
    else
   {
   
   if ((analogico3<=127) && (analogico1 <= analogico2)  && (minuto < 59) )  //1 hora 216000
      {
       output_low(pin_a0);
       output_high(pin_a5);
       }
   
   if ((analogico3<=127) && (analogico1 <= analogico2)  && (minuto >59 ) )  //1 hora 216000
  {
      output_low(pin_a5);
      output_high(pin_a0);
    }
 
   if((analogico3>127) && (analogico3<=255) && (analogico1 <= analogico2)&& (minuto < 119) )    //   432000  2 horas
  {
     output_low(pin_a0);
     output_high(pin_a5);
    }
      if((analogico3>127) && (analogico3<=255) && (analogico1 <= analogico2)&& (minuto > 119) )  //   432000  2 horas
    {
     output_low(pin_a5);
     output_high(pin_a0);
      }
    if((analogico3>255) && (analogico3<=383) && (analogico1 <= analogico2 ) && (minuto < 179) )    // 648000 3horas
  {
     output_low(pin_a0);
     output_high(pin_a5);
   }
     if((analogico3>255) && (analogico3<=383) && (analogico1 <= analogico2 ) && (minuto > 179) )    // 648000 3horas
  {
      output_low(pin_a5);
      output_high(pin_a0);
      }
   if((analogico3>383) && (analogico3<=511) && (analogico1 <= analogico2) && (minuto < 239) )    // 864000 4 horas
  {
     output_low(pin_a0);
     output_high(pin_a5);
   }
     if((analogico3>383) && (analogico3<=511) && (analogico1 <= analogico2 ) && (minuto > 239) )    // 864000 4 horas
   {
      output_low(pin_a5);
      output_high(pin_a0);
      }
     if((analogico3>511) && (analogico3<=639) && (analogico1 <= analogico2 ) && (minuto < 359 ) )    // 1296000 6 horas
  {
     output_low(pin_a0);
     output_high(pin_a5);
    }
     if((analogico3>511) && (analogico3<=639) && (analogico1 <= analogico2) && (minuto > 359 ) )    // 1296000 6 horas
  {
      output_low(pin_a5);
      output_high(pin_a0);
      }
     if((analogico3>639) && (analogico3<=767) &&(analogico1 <= analogico2) && (minuto < 479) )    //1728000 8 horas
    {
     output_low(pin_a0);
     output_high(pin_a5);
     }
       if((analogico3>639) && (analogico3<=767) &&(analogico1 <= analogico2) && (minuto > 479) )    //1728000 8 horas
    {
      output_low(pin_a5);
      output_high(pin_a0);
      }
   
   if((analogico3>767) && (analogico3<=895) && (analogico1 <= analogico2) && (minuto < 599) )    // 2160000 10 horas
  {
    output_low(pin_a0);
     output_high(pin_a5);
   }
     if((analogico3>767) && (analogico3<=895) && (analogico1 <= analogico2) && (minuto > 599) )    // 2160000 10 horas
   {
      output_low(pin_a5);
      output_high(pin_a0);
      }
   
   if((analogico3>895) && (analogico3<=1023) && ( analogico1 <= analogico2) && (minuto < 719) )    // 2592000 12horas
  {
     output_low(pin_a0);
     output_high(pin_a5);
   }
    if((analogico3>895) && (analogico3<=1023) && ( analogico1 <= analogico2) && (minuto > 719) )    // 2592000 12horas  (!input(PIN_A1 ))
  {
      output_low(pin_a5);
      output_high(pin_a0);
      }
   
    if(minuto > 900)     {  //     900         15 horas preso nesse laço 3240000
        cnt = 0;
        minuto=0;  
    }
     }
      }
       }



moderator action: removed link to external file server. Please upload as PNG
 
Last edited by a moderator:

Aussie Susan

Advanced Member level 4
Joined
Jan 5, 2015
Messages
1,314
Helped
386
Reputation
772
Reaction score
392
Trophy points
83
Activity points
14,182
Is this a new problem?
Did you solve your previous one? If so what was the error?
Also you have not read (or not understood) my previous post (#7) as you don't declare 'cnt' to be volatile.
Is your question now 'how to set up a timer'? If so then I'd start with a 'flash a LED' level program and get the timer working with that. (I'm not saying more now as I have no real idea what you are asking.)
Susan
 

Amanda Lima

Junior Member level 1
Joined
Jul 24, 2021
Messages
16
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
121
Code:
 #include <main.h>
int16 analogico1 = 0;
int16 analogico2 = 0;
int16 analogico3 = 0;
int16 minuto = 0;

#fuses NOMCLR INTRC_IO PLL_SW
#device ADC = 10
#use delay(clock=32000000)
#use fast_io(A)
int32 cnt = 0;

#INT_RA                                // interrupção RA.
//------------------------------------------------------------------------
void RA_isr(void)                         // interromper a rotina de serviço
{
     clear_interrupt(INT_RA3);
  if(input(PIN_A3))                    // Se RA3 for HIGH
  {
   cnt++;
  }
}

void main()
{
     setup_adc_ports(sAN1|sAN2|sAN3);
     //setup_adc_ports(sAN1);
    // setup_adc_ports(sAN2);
    // setup_adc_ports(sAN3);
     setup_adc(ADC_CLOCK_INTERNAL);
     setup_oscillator(OSC_8MHZ | OSC_PLL_ON);   // Set internal oscillator to 32MHz (8MHz and PLL)
     set_tris_a  (0b00011110 );   //(0b00011110);                      // HEX 1E  RA Tris  76543210  0X1E
     enable_interrupts(GLOBAL);                  // Enable global interrupts
     enable_interrupts(INT_RA3);
     clear_interrupt(INT_RA3);                    // Clear RA IOC flag bit
     output_high(pin_a0);
     output_low(pin_a5);
 
while(TRUE)
 
    {
    if (cnt >= 3600 )     // 3599)                    // Passou 1 minuto
    {
      cnt = 0;
      minuto++;
    }
   
    set_adc_channel (1);
     delay_us (100);
     analogico1 = read_adc();
     delay_us (100);
     set_adc_channel (2);
    delay_us (100);
     analogico2 = read_adc();
     delay_us (100);
     set_adc_channel (3);
    delay_us(100);
     analogico3 = read_adc();
     delay_us (100);
   
      if( analogico1 > analogico2  )                  
    {
      output_low(pin_a5);               // Desliga rele      
      cnt = 0;                       // Zera contador
      minuto=0;
      output_high(pin_a0);
    }
    else
   {
     
   if ((analogico3<=127) && (analogico1 <= analogico2)  && (minuto < 59) )  //1 hora 216000
      {
       output_low(pin_a0);
       output_high(pin_a5);
       }
     
   if ((analogico3<=127) && (analogico1 <= analogico2)  && (minuto >59 ) )  //1 hora 216000
  {
      output_low(pin_a5);
      output_high(pin_a0);
    }
   
   if((analogico3>127) && (analogico3<=255) && (analogico1 <= analogico2)&& (minuto < 119) )    //   432000  2 horas
  {
     output_low(pin_a0);
     output_high(pin_a5);
    }
      if((analogico3>127) && (analogico3<=255) && (analogico1 <= analogico2)&& (minuto > 119) )  //   432000  2 horas
    {
     output_low(pin_a5);
     output_high(pin_a0);
      }
    if((analogico3>255) && (analogico3<=383) && (analogico1 <= analogico2 ) && (minuto < 179) )    // 648000 3horas
  {
     output_low(pin_a0);
     output_high(pin_a5);
   }
     if((analogico3>255) && (analogico3<=383) && (analogico1 <= analogico2 ) && (minuto > 179) )    // 648000 3horas
  {
      output_low(pin_a5);
      output_high(pin_a0);
      }
   if((analogico3>383) && (analogico3<=511) && (analogico1 <= analogico2) && (minuto < 239) )    // 864000 4 horas
  {
     output_low(pin_a0);
     output_high(pin_a5);
   }
     if((analogico3>383) && (analogico3<=511) && (analogico1 <= analogico2 ) && (minuto > 239) )    // 864000 4 horas
   {
      output_low(pin_a5);
      output_high(pin_a0);
      }
     if((analogico3>511) && (analogico3<=639) && (analogico1 <= analogico2 ) && (minuto < 359 ) )    // 1296000 6 horas
  {
     output_low(pin_a0);
     output_high(pin_a5);
    }
     if((analogico3>511) && (analogico3<=639) && (analogico1 <= analogico2) && (minuto > 359 ) )    // 1296000 6 horas
  {
      output_low(pin_a5);
      output_high(pin_a0);
      }
     if((analogico3>639) && (analogico3<=767) &&(analogico1 <= analogico2) && (minuto < 479) )    //1728000 8 horas
    {
     output_low(pin_a0);
     output_high(pin_a5);
     }
       if((analogico3>639) && (analogico3<=767) &&(analogico1 <= analogico2) && (minuto > 479) )    //1728000 8 horas
    {
      output_low(pin_a5);
      output_high(pin_a0);
      }
   
   if((analogico3>767) && (analogico3<=895) && (analogico1 <= analogico2) && (minuto < 599) )    // 2160000 10 horas
  {
    output_low(pin_a0);
     output_high(pin_a5);
   }
     if((analogico3>767) && (analogico3<=895) && (analogico1 <= analogico2) && (minuto > 599) )    // 2160000 10 horas
   {
      output_low(pin_a5);
      output_high(pin_a0);
      }
   
   if((analogico3>895) && (analogico3<=1023) && ( analogico1 <= analogico2) && (minuto < 719) )    // 2592000 12horas
  {
     output_low(pin_a0);
     output_high(pin_a5);
   }
    if((analogico3>895) && (analogico3<=1023) && ( analogico1 <= analogico2) && (minuto > 719) )    // 2592000 12horas  (!input(PIN_A1 ))
  {
      output_low(pin_a5);
      output_high(pin_a0);
      }
   
    if(minuto > 900)     {  //     900         15 horas preso nesse laço 3240000
        cnt = 0;
        minuto=0;    
    }
     }
      }
       }
Is this a new problem?
Did you solve your previous one? If so what was the error?
Also you have not read (or not understood) my previous post (#7) as you don't declare 'cnt' to be volatile.
Is your question now 'how to set up a timer'? If so then I'd start with a 'flash a LED' level program and get the timer working with that. (I'm not saying more now as I have no real idea what you are asking.)
Susan
Hello Aussie Susan, This is not a new problem, it's just a continuation of the previous one, this one now depends on the previous one which is to use timer0 to time, so I wanted to make a program to flash an LED' and make the timer work with this, I'm from Brazil and on the sit here I was not successful.
 

Amanda Lima

Junior Member level 1
Joined
Jul 24, 2021
Messages
16
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
121
Is this a new problem?
Did you solve your previous one? If so what was the error?
Also you have not read (or not understood) my previous post (#7) as you don't declare 'cnt' to be volatile.
Is your question now 'how to set up a timer'? If so then I'd start with a 'flash a LED' level program and get the timer working with that. (I'm not saying more now as I have no real idea what you are asking.)
Susan
Hello Aussie Susan, This is not a new problem, it's just a continuation of the previous one, this one now depends on the previous one which is to use timer0 to time, so I wanted to make a program to flash an LED' and make the timer work with that, this last program worked in simulation and in practice, but I don't know if the program is in error, it counts external pulse, I didn't want to use external pulse, that's why my insistence on timer0. I'm from Brazil and on the sit here I wasn't successful.
 

Solution

KlausST

Super Moderator
Staff member
Joined
Apr 17, 2014
Messages
20,352
Helped
4,441
Reputation
8,891
Reaction score
4,468
Trophy points
1,393
Activity points
134,557
Hi,

You did not answer a question.
You don't give an error description.
Instead you post a complete new code ... again with no error description.

It's hard to help this way.

Please focus on the problem. Remove all code that is not related to the problem.
Give detailed error description.
Tell us what you want to achieve. Tell us how you test this. Tell us what's happening instead.

*******

Please explain:
1) You say code of post#11 a continuaton of the initial problem:
* where is "timer0"?
* where is "LED"?
* where is "frequency of 1 second"?

2) how can "analogico3<=1023" be FALSE? What's the idea behind it?

Klaus
 

Amanda Lima

Junior Member level 1
Joined
Jul 24, 2021
Messages
16
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
121
Hi,

You did not answer a question.
You don't give an error description.
Instead you post a complete new code ... again with no error description.

It's hard to help this way.

Please focus on the problem. Remove all code that is not related to the problem.
Give detailed error description.
Tell us what you want to achieve. Tell us how you test this. Tell us what's happening instead.

*******
Hi,

You did not answer a question.
You don't give an error description.
Instead you post a complete new code ... again with no error description.

It's hard to help this way.

Please focus on the problem. Remove all code that is not related to the problem.
Give detailed error description.
Tell us what you want to achieve. Tell us how you test this. Tell us what's happening instead.

*******

Please explain:
1) You say code of post#11 a continuaton of the initial problem:
* where is "timer0"?
* where is "LED"?
* where is "frequency of 1 second"?

2) how can "analogico3<=1023" be FALSE? What's the idea behind it?

Klaus
Maybe I don't know how to explain it right, the complete code was tested in the Proteus simulator and also in the assembly I made with the pic on the printed circuit board and it worked, but the fact that it worked doesn't mean it could have an error, so I asked for it on forum a help to analyze the code and see if everything is ok, but as said, it uses an external pulse to count the time, I wanted it to count without needing an external pulse, so I thought of using the timer0, in short, I need a timer that turns on an LED when I press a button and turns it off 1, or 2 hours later, using delay I can't get long times.
Where is "timer0"?
In this complete code there isn't, I'm wanting to put it with your help.
* where is "LED"?
In this complete code there is no because I took a step forward by using a relay
* where is "1 second frequency"?
I'm trying to figure this out to use on the timer.
Reminder: Here in Brazil the network frequency is 60 hertz, the pic counts 60 pulses for one second or 3600 pulses for one minute, but I don't want to use an external pulse.


Please explain:
1) You say code of post#11 a continuaton of the initial problem:
* where is "timer0"?
* where is "LED"?
* where is "frequency of 1 second"?

2) how can "analogico3<=1023" be FALSE? What's the idea behind it?

Klaus
 

KlausST

Super Moderator
Staff member
Joined
Apr 17, 2014
Messages
20,352
Helped
4,441
Reputation
8,891
Reaction score
4,468
Trophy points
1,393
Activity points
134,557
Hi,

Still we need to guess a lot.

So as far as i understand you want a 1s tick / 1Hz generated from 32MHz clock using timer0.
Why not the more suitable timer1?

I do it with timer0 now...
So basically timer0 and software counters need to generate 1 tick every 1s, hich means every 32M clock ticks.
I do it the mathematical way .. (no trial and error)
32,000,000 as prime factors is 2^11 * 5^6
The max prescaler is 256 which is 2^8
Using it .. the remaining prime factor value is 2^3 * 5^6
Since timer0 is an 8 bit one you could use 2^1 * 5^3 = 250 as timeout
Now the remaining value for the sofware counter is 2^2 * 5^3 = 500

Check:
2^8 * (prescaler = 256)
2^1 * 5^3 * (timer0 timeout = 250)
2^2 * 5^3 = (software counter = 500)
2^11 * 5^6 ... correct (total 32,000,000)

Klaus
 

Amanda Lima

Junior Member level 1
Joined
Jul 24, 2021
Messages
16
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
121
Hi,

Still we need to guess a lot.

So as far as i understand you want a 1s tick / 1Hz generated from 32MHz clock using timer0.
Why not the more suitable timer1?

I do it with timer0 now...
So basically timer0 and software counters need to generate 1 tick every 1s, hich means every 32M clock ticks.
I do it the mathematical way .. (no trial and error)
32,000,000 as prime factors is 2^11 * 5^6
The max prescaler is 256 which is 2^8
Using it .. the remaining prime factor value is 2^3 * 5^6
Since timer0 is an 8 bit one you could use 2^1 * 5^3 = 250 as timeout
Now the remaining value for the sofware counter is 2^2 * 5^3 = 500

Check:
2^8 * (prescaler = 256)
2^1 * 5^3 * (timer0 timeout = 250)
2^2 * 5^3 = (software counter = 500)
2^11 * 5^6 ... correct (total 32,000,000)

Klaus
I said timer0, but it could be timer1, or some other way, I just need to have a timer code that I can turn off an LED or a relay after a while, I could do that using delay_ms() but with that I can't I get long times, (minutes or hours), I don't know if you understand me, I'll summarize, when I press a button the LED or relay turns on and off after a while (minutes or hours) if you're going to use timer0, timer1 or another way to me makes no difference, sorry for inconveniencing.
 

KlausST

Super Moderator
Staff member
Joined
Apr 17, 2014
Messages
20,352
Helped
4,441
Reputation
8,891
Reaction score
4,468
Trophy points
1,393
Activity points
134,557
Hi,

It's hard to understand ... at least for me.
I need requirements. Like "1 second", "PIC12 F1822", "LED", "timer0", "CCS compiler".

If then - in a following post - one ore more of the reqirements are not valid anymore ... I get confused, because I'm not sure whether the other requirements are still valid. If one can change from timer0 to timer1 ... can we change from PIC12F1822 to any other PIC?

Some members say "timer0", because timer1 is already used, thus not available...

******
I also don't understand why you say you want to use timer (timer0) "because with delay_ms you can't get long times".
It's true, that the time for delay_ms is limited ... but the same is true for timer_x.
Timer0 is limited to 256 x 256 clock cycles.
In both cases you need a software counter to extend for long delays.
The main difference for me is:
* delay_ms takes all of the processing power. The processor is not able to do something during a delay_ms.
* in opposite the timer needs no processing power, just the ISR...if you use it. The ISR maybe just needs 1% of the processing power.
* and the timer is absolutely accurate in terms of clock cycles. You can build a precise clock with a timer, but not with a delay_ms.

But honestly ... you already used 60Hz and a software counter to get 1s.
Is it really difficult to understand that the same can be used with a timer?
It's no rocket science to understand that 10ms x 100 gives 1s ... or 2 ms x 500 gives 1s ... or 6.25ms x 160.
And there really are millions of good examples for a timer to generate a timeout in the range of milliseconds.

So tell us your "real worries"...

Klaus
 

Aussie Susan

Advanced Member level 4
Joined
Jan 5, 2015
Messages
1,314
Helped
386
Reputation
772
Reaction score
392
Trophy points
83
Activity points
14,182
Lets look at the timers that are in that MCU:
- Timer0 is an 8-bit timer with a pre-scalar that has a maximum division by 256. You have a 32MHz clock so that means the 'slowest' that this timer can trigger an interrupt is every 8.19mSec
-Timer1 is a 16-bit timer with a pre-scalar that has a maximum of division by 8. Given the same clock and using the Fosc/4 input and again allowing the timer counter to run its maximum range, the slowest interrupt will be every 65mSec
-Timer2 is an 8 bit timer and using the prescalar and post-scalar together you can get a division by 1024. Therefore the slowest interrupts will be every 32.8mSec.
Of course none of those are 'nice' periods if you want to get an exact '1 second' 'tick' (by the method I'll get to shortly). Timer0 and Timer1 count up from whatever initial value you put into the TMRx register so when you get the timer interrupt you could reset the TMRx register with the appropriate value and let it count from there but that will mean a shorter interrupt period - probably not an issue for you but it does mean you need to reset the TMRx value on each interrupt. Timer2 has a PR2 register that you can set once and it will count up to that value before resetting to 0 and triggering the interrupt.
Which timer you select is really up to you - it is part of the 'design choices' that everyone faces.
Now, we have seen that we will get an interrupt some where in the 10's of mSec range. You could continue to do what you do now and constantly check in your main loop for a specific value and take action on that. (By the way, I think that you should NOT try to hammer the ADC in every loop - put the ADC reading code inside the 'then' part of the if statement that checks for the '1 minute' tick.)
What I would suggest is that you do the counting within the ISR and, when the count shows that a minute has passed, set a flag that is tested in the main loop. When the main loop sees the flag set, it can then increment the 'minute', 'hour' and any other counters that you want (as well as resetting the flag of course).
Personally I would get rid of all of the code that doesn't take part in the above algorithm (for now) and make sure that you can get the timing working correctly first. Once that is working, you can add back in whatever you need to based on the various counters. you need.
Susan
 

Amanda Lima

Junior Member level 1
Joined
Jul 24, 2021
Messages
16
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
121
Hi,

It's hard to understand ... at least for me.
I need requirements. Like "1 second", "PIC12 F1822", "LED", "timer0", "CCS compiler".

If then - in a following post - one ore more of the reqirements are not valid anymore ... I get confused, because I'm not sure whether the other requirements are still valid. If one can change from timer0 to timer1 ... can we change from PIC12F1822 to any other PIC?

Some members say "timer0", because timer1 is already used, thus not available...

******
I also don't understand why you say you want to use timer (timer0) "because with delay_ms you can't get long times".
It's true, that the time for delay_ms is limited ... but the same is true for timer_x.
Timer0 is limited to 256 x 256 clock cycles.
In both cases you need a software counter to extend for long delays.
The main difference for me is:
* delay_ms takes all of the processing power. The processor is not able to do something during a delay_ms.
* in opposite the timer needs no processing power, just the ISR...if you use it. The ISR maybe just needs 1% of the processing power.
* and the timer is absolutely accurate in terms of clock cycles. You can build a precise clock with a timer, but not with a delay_ms.

But honestly ... you already used 60Hz and a software counter to get 1s.
Is it really difficult to understand that the same can be used with a timer?
It's no rocket science to understand that 10ms x 100 gives 1s ... or 2 ms x 500 gives 1s ... or 6.25ms x 160.
And there really are millions of good examples for a timer to generate a timeout in the range of milliseconds.

So tell us your "real worries"...

Klaus
I used 60Hz and a software counter to get 1s, but I wanted to use timer0 or timer1 and a software counter to get 1s, if I can I'll put it in my code and I won't use 60Hz, I want to modify my code that uses the 60 Hz for a new code that uses timer0 or timer1 and I can't use another pic because I don't have another one just this pic12f1822. Thanks.
 

KlausST

Super Moderator
Staff member
Joined
Apr 17, 2014
Messages
20,352
Helped
4,441
Reputation
8,891
Reaction score
4,468
Trophy points
1,393
Activity points
134,557
Hi,

We already have given all informations you need.

We don't know what else you expect from us.

Klaus
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top