View previous topic :: View next topic |
Author |
Message |
artohautala
Joined: 17 Nov 2011 Posts: 187
|
how to change timer0 to 16 bits using pic18f452 [SOLVED] |
Posted: Wed Nov 11, 2015 10:09 am |
|
|
Hi,
I want to count pulses to pinA4 using timer0... but I have to count more
than 255pulses/s ... so how I can change timer0 to 16 bits ?
(I read somewhere that in 18F family timer0 can be 16 bits wide)
And another question:
Is it so that when A4 counts pulses it "doubles" them by incrementing
every high to low and low to high ?
Here's my code works in practise to 255...
Code: |
unsigned int16 wind_speed(void){
// here's my interrupt code :
/*
#int_timer1
void gate_time (void)
{
++g_ints; //this is global identifier
}
*/
/*
measure windspeed pulses from wind speed meter to A4 pin and use timer_0
and use resistors as level converter to 0...+12 V to 0...+5 V levels
*/
//TO8BIT 1;
int16 w_speed;
//T08BIT 1;
//I send pulses from function generator and I'm sure it's OK
//setup_timer_0(RTCC_EXT_L_TO_H | RTCC_16_BIT); //this don't compile!!
setup_timer_0(RTCC_EXT_L_TO_H | RTCC_8_BIT);
/* some math:
clock = 8 Mhz, timer_1 clock / 4 ... 2 Mhz ... SETUP_TIMER_1(T1_INTERNAL | T1_DIV_BY_8);
so one tick is 1 / (8 Mhz / 4 / 8) = 4 us ?
timer1 is overfloawing every 4 us * 65536 = 0.262144 seconds
so 1 second gate time needs about 4 overfloats ?
so why this must be that way: while(g_ints < 7); for right result ?
*/
set_timer0(0);
set_timer1(0);
SETUP_TIMER_1(T1_INTERNAL | T1_DIV_BY_8);
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
g_ints = 0;
set_timer0(0);
while(g_ints < 7); //wait for gate time 1s , res. 1 Hz
set_timer1(25000); //set exactly 1s gate time
g_ints = 0;
while(g_ints < 1);
disable_interrupts(INT_TIMER1);
disable_interrupts(GLOBAL);
w_speed = get_timer0();
return w_speed;
}
|
Best regards for you all !
-arto hautala-
Last edited by artohautala on Thu Nov 12, 2015 6:06 am; edited 1 time in total |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19553
|
|
Posted: Wed Nov 11, 2015 10:14 am |
|
|
It _defaults_ to 16bit.
RTCC_8_BIT
Switches it _down_ to 8bit. Just remove this (and the OR '|'). |
|
|
artohautala
Joined: 17 Nov 2011 Posts: 187
|
|
Posted: Wed Nov 11, 2015 10:55 am |
|
|
Ttelmah wrote: | It _defaults_ to 16bit.
RTCC_8_BIT
Switches it _down_ to 8bit. Just remove this (and the OR '|'). |
Thank for your answer ...
I supposed too that it defaults I have tried it ...
Maybe my compiler is too old I have version 4.018 ?
Is there any way to set this bit : //T08BIT 1;
maybe it's then 16 bits?
brdgs
-arto- |
|
|
wangine
Joined: 07 Jul 2009 Posts: 98 Location: Curtea de Arges, Romania
|
|
Posted: Wed Nov 11, 2015 4:30 pm |
|
|
Ttelmah wrote: | It _defaults_ to 16bit.
RTCC_8_BIT
Switches it _down_ to 8bit. Just remove this (and the OR '|'). |
Like Ttelmah say
Code: | setup_timer_0(RTCC_EXT_L_TO_H ); |
artohautala wrote: |
Is there any way to set this bit : //T08BIT 1;
|
Or like that :
Code: |
union {
struct {
unsigned int T0PS0:3;
unsigned int PSA:1;
unsigned int T0SE:1;
unsigned int T0CS:1;
unsigned int T08BIT:1;
unsigned int TMR0ON:1;
};
struct {
unsigned int T0PS:3;
};
} T0CON;
#byte T0CON = 0xFD5
#word TMR0 = 0xFD6 |
and setting bit
Code: | T0CON.T08BIT = false; |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Nov 11, 2015 4:53 pm |
|
|
Quote: | Thank for your answer ...
I supposed too that it defaults I have tried it ...
Maybe my compiler is too old I have version 4.018 ?
Is there any way to set this bit : //T08BIT 1;
maybe it's then 16 bits? |
Vs. 4.018 works fine. Run the code, look at the .LST file.
Test program:
Code: | #include <18F452.h>
#fuses XT, NOWDT, BROWNOUT, PUT, NOLVP
#use delay(clock=4M)
//=====================================
void main()
{
setup_timer_0(RTCC_EXT_L_TO_H);
setup_timer_0(RTCC_EXT_L_TO_H | RTCC_8_BIT);
while(TRUE);
}
|
Here is the .LST file. The first call to setup_timer_0() sets T0CON = 0xA0
and that is a default to a 16-bit timer. It works just fine.
Code: |
.................... void main()
.................... {
0004: CLRF TBLPTRU
0006: BCF RCON.IPEN
0008: CLRF FSR0H
000A: CLRF FSR0L
000C: BSF ADCON1.PCFG0
000E: BSF ADCON1.PCFG1
0010: BSF ADCON1.PCFG2
0012: BCF ADCON1.PCFG3
.................... setup_timer_0(RTCC_EXT_L_TO_H);
0014: MOVLW A0
0016: MOVWF T0CON
....................
.................... setup_timer_0(RTCC_EXT_L_TO_H | RTCC_8_BIT);
0018: MOVLW E0
001A: MOVWF T0CON
....................
.................... while(TRUE);
001C: BRA 001C
.................... } |
|
|
|
artohautala
Joined: 17 Nov 2011 Posts: 187
|
|
Posted: Thu Nov 12, 2015 5:01 am |
|
|
PCM programmer wrote: | Quote: | Thank for your answer ...
I supposed too that it defaults I have tried it ...
Maybe my compiler is too old I have version 4.018 ?
Is there any way to set this bit : //T08BIT 1;
maybe it's then 16 bits? |
Vs. 4.018 works fine. Run the code, look at the .LST file.
Test program:
Code: | #include <18F452.h>
#fuses XT, NOWDT, BROWNOUT, PUT, NOLVP
#use delay(clock=4M)
//=====================================
void main()
{
setup_timer_0(RTCC_EXT_L_TO_H);
setup_timer_0(RTCC_EXT_L_TO_H | RTCC_8_BIT);
while(TRUE);
}
|
Here is the .LST file. The first call to setup_timer_0() sets T0CON = 0xA0
and that is a default to a 16-bit timer. It works just fine.
Code: |
.................... void main()
.................... {
0004: CLRF TBLPTRU
0006: BCF RCON.IPEN
0008: CLRF FSR0H
000A: CLRF FSR0L
000C: BSF ADCON1.PCFG0
000E: BSF ADCON1.PCFG1
0010: BSF ADCON1.PCFG2
0012: BCF ADCON1.PCFG3
.................... setup_timer_0(RTCC_EXT_L_TO_H);
0014: MOVLW A0
0016: MOVWF T0CON
....................
.................... setup_timer_0(RTCC_EXT_L_TO_H | RTCC_8_BIT);
0018: MOVLW E0
001A: MOVWF T0CON
....................
.................... while(TRUE);
001C: BRA 001C
.................... } |
|
Thank's for your good advice... here's part of my lst-file:
....................
.................... setup_timer_0(RTCC_EXT_L_TO_H); //pulses from wind speed meter
*
0222: MOVLW A0
0224: MOVWF FD5
so it seems to me that timero is now 16 bits...
could it be so that it counts only 8 bits 0 ... 255 from A4 coming pulses
though timer0 is 16 bits wide?
or what's wrong ?
brdgs
-arto- |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19553
|
|
Posted: Thu Nov 12, 2015 5:08 am |
|
|
The odds are your real problem is somewhere else.
Somewhere in the code downstream, where you use the value being returned, your code is treating it as 8bits.
You need to look at your code where you use the value returned by the wind_speed function.... |
|
|
artohautala
Joined: 17 Nov 2011 Posts: 187
|
|
Posted: Thu Nov 12, 2015 5:14 am |
|
|
PCM programmer wrote: | Quote: | Thank for your answer ...
I supposed too that it defaults I have tried it ...
Maybe my compiler is too old I have version 4.018 ?
Is there any way to set this bit : //T08BIT 1;
maybe it's then 16 bits? |
Vs. 4.018 works fine. Run the code, look at the .LST file.
Test program:
Code: | #include <18F452.h>
#fuses XT, NOWDT, BROWNOUT, PUT, NOLVP
#use delay(clock=4M)
//=====================================
void main()
{
setup_timer_0(RTCC_EXT_L_TO_H);
setup_timer_0(RTCC_EXT_L_TO_H | RTCC_8_BIT);
while(TRUE);
}
|
Here is the .LST file. The first call to setup_timer_0() sets T0CON = 0xA0
and that is a default to a 16-bit timer. It works just fine.
Code: |
.................... void main()
.................... {
0004: CLRF TBLPTRU
0006: BCF RCON.IPEN
0008: CLRF FSR0H
000A: CLRF FSR0L
000C: BSF ADCON1.PCFG0
000E: BSF ADCON1.PCFG1
0010: BSF ADCON1.PCFG2
0012: BCF ADCON1.PCFG3
.................... setup_timer_0(RTCC_EXT_L_TO_H);
0014: MOVLW A0
0016: MOVWF T0CON
....................
.................... setup_timer_0(RTCC_EXT_L_TO_H | RTCC_8_BIT);
0018: MOVLW E0
001A: MOVWF T0CON
....................
.................... while(TRUE);
001C: BRA 001C
.................... } |
|
thank's for your good advice... here's part of my lst-file:
....................
.................... setup_timer_0(RTCC_EXT_L_TO_H); //pulses from wind speed meter
*
0222: MOVLW A0
0224: MOVWF FD5
so it seems to me that timer0 is now 16 bits...
could it be so that it counts only 8 bits 0 ... 255 from A4 coming pulses
though timer0 is 16 bits wide?
or what's wrong ?
brdgs
-arto- |
|
|
artohautala
Joined: 17 Nov 2011 Posts: 187
|
|
Posted: Thu Nov 12, 2015 5:34 am |
|
|
Ttelmah wrote: | The odds are your real problem is somewhere else.
Somewhere in the code downstream, where you use the value being returned, your code is treating it as 8bits.
You need to look at your code where you use the value returned by the wind_speed function.... |
Yes you are right...
Code: |
unsigned int16 windspeed;
loop:
windspeed = wind_speed(); //returns unsigned int16
clear_lcd();
show_calibration((unsigned int16)windspeed);
goto loop;
|
... my stupid error ... this unsigned int16 windspeed;
was unsigned int8 windspeed; !
Thank you very much !
How I mark it's [solved] do I have to type it "[solved]" ?
-arto- |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19553
|
|
Posted: Thu Nov 12, 2015 5:49 am |
|
|
Ideally yes.
You just edit the title, to include [Solved].
Thanks. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Thu Nov 12, 2015 9:33 am |
|
|
Quote: | Maybe my compiler is too old I have version 4.018 | I'm surprised nobody else remarked on this. The first releases of the v4.xxx series of compilers were very bad, only around v4.075 the compiler became stable again for simple programs.
I'm sure you will run into problems when you continue to use this bad version. Save everyone a lot of time by using a stable version like the older v3.249 or one of the newer releases v4.075+ |
|
|
|