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...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
I'm sorry, I'll postHello 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? **broken link removed**
#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;
}
}
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?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
#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;
}
}
}
#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;
}
}
}
}
#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;
}
}
}
}
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.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.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
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.
*******
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.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
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
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.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 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.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
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?