2 Attachment(s)
18f27k40 - timer0 changes from K22, K80 series 8bit/16bit mode
Hi guys ,
found few changes on how timer0 on this chip implements the 8bit mode and the values set in TMR0L and TMR0H from the K80, K22 series
apart from the changes in TMR0 registers and their use to setup on the 27k40 from the k22,k80 series , it appears that the 8bit mode implements different from K80,K22 series
this appears to effect the values that need to be placed into TMR0L, TMR0H in 8 bit mode
can you please check this
currently in 8 bit mode on the k40 the values set in TMR0L, TMR0H result in a 500us interupt
Where the same values 8bit mode on a k22 on TMR0 = 10ms interupt
I simply got around it by setting it to 16 bit mode , but like to know what values i should set in 8bit mode k40 to get a 10ms interupt
cheers
Sheldon
K22 SERIES
Attachment 8740
K40 SERIES
Attachment 8741
Code setup for k22 , with a 10ms interupt
Code:
' ---------- Timer 0 Register Setups ----------------
INTCON2.2 = 1 ' TMR0IP - TIMER 0 Overflow Interupt Priority - 1 = High Priorty , 0 = Low Priority
T0CON.7 = 1 ' Bit7 - TMR0 1= Enable 0 = Stop timer
T0CON.6 = 0 ' Bit6 - TMR0 8/16bit control 1= 8bit 0=16bit,
T0CON.5 = 0 ' Bit5 - TMR0 Clock Source 1=clk on T0CKI pin input 0=Internal clcck (Fosc/4)
T0CON.4 = 0 ' Bit4 - TMR0 Source Edge Select 1= H/L of TOCKI 0= L/H of TOCKI ,
T0CON.3 = 0 ' Bit3 - TMR0 PSA select 1=TMR0 Bypass Prescaler 0= CLK Input from Prescaler output
T0CON.2 = 0 ' Bit2-0 - Timer0 prescaler Rate Select bits 2-0(set to 1:256)
T0CON.1 = 0 ' 000 = 1:2 , 001= 1:4 , 010 = 1:8 , 011 = 1:16
T0CON.0 = 0 ' 100 = 1:32 , 101 = 1:64, 110 = 1:128 , 111 = 1:256
TMR0H = $63 ' preset Timer 0 to a 10ms timer
TMR0L = $C0
timer 0 setup for k40 results in 500us timer
Code:
' ---------- Timer 0 Register Setups ----------------
' TOCON0 - POR = 0
' T0CON1 - POR = 0
' Used for a 10ms timer
' Set for 1:2 prescaler, 1:1 postscaler , use system clock (fosc/4) , preloaded in ISR with 25536 for 10ms
' Note: Interupt Priority level set disable by default IPEN(INTCON.5= 0 )
' Timer0 Intertupt Priority = High by default - IPR0.5 = 1
' TMR0 Interupt enable bit - PIE0.5 = 0 disabled PIE0.5 = 1 Enabled - set/cleared using DT-INTS-18
T0CON0.7 = 1 ' Bit7 - TMR0 1= Enable 0 = Stop timer
T0CON0.6 = 0 ' Bit6 - TMR0 N/A
T0CON0.5 = 0 ' Bit5 - TMR0 Output bit ( read only)
T0CON0.4 = 0 ' Bit4 - TMR0 8/16bit control 0= 8bit 1=16bit
T0CON0.3 = 0 ' Bit3-0 - Timer0 output Postscaler divider Selector bits 3-0
T0CON0.2 = 0 ' 0=1:1, 1=1:2, 2=1:3 , 3=1:4, 4=1:5, 5=1:6, 6=1:7
T0CON0.1 = 0 ' 7=1:8, 8=1:9, 9=1:10, 10=1:11, 11=1:12, 12=1:13
T0CON0.0 = 0 ' 13=1:14, 14=1:15, 15=1:16
T0CON1.7 = 0 ' Bit7-5 TMR0 Clock Source Select bits - use Fosc/4
T0CON1.6 = 1 ' 000= Ext pin T0CKIPPS(Non Inv),001 = Ext pin T0CKIPPS(Inverted)
T0CON1.5 = 0 ' 010= Fosc/4 , 011= HFINTOSC, 100= LFINTOSC, 101=SOSC,110-111 = Reserved
T0CON1.4 = 0 ' Bit4 - TMR0 ASYNCH Enable Bit - 1= T0 Input NOT sync to system clock , 0 = T0 Input sync to Fosc/4
T0CON1.3 = 0 ' Bit3-0 - Timer0 output Prescaler divider Selector bits 3-0
T0CON1.2 = 0 ' 0=1:1, 1=1:2, 2=1:4 , 3=1:8, 4=1:16, 5=1:32, 6=1:64
T0CON1.1 = 0 ' 7=1:128, 8=1:256, 9=1:512, 10=1:1024, 11=1:2048, 12=1:4096
T0CON1.0 = 1 ' 13=1:8192, 14=1:16384, 15=1:32768
TMR0H = $63 ' preset Timer 0 to a 10ms timer
TMR0L = $C0
Re: 18f27k40 - timer0 changes from K22, K80 series 8bit/16bit mode
posting values to the TMR0H reg when the timer is in 8 bit mode , how is that going to work ?
normally TMR0H would be cleared
Code:
TMR0H = $63 ' preset Timer 0 to a 10ms timer
TMR0L = $C0
I cannot simulate any of your numbers in the mcc 4/8/16,64 MHz fosc produce different 8 bit reload values that match none of your figures
what is your clock source ?
Re: 18f27k40 - timer0 changes from K22, K80 series 8bit/16bit mode
In the K40, TMR0 functions different than it did in previous devices.
In 8-bit mode TMR0H becomes a programmable period register (similar to how TMR2 works).
It looks like you're running at 16MHz.
If you have the timer set for Fosc/4 mode T0CS<2:0> = 010, then at 16MHz TMR0 counts 250ns
You want to count a total of 10ms/250ns = 40000
The postscaler settings are 1:1 to 1:16, and the 8-bit count = 1 to 256 (since it reloads on the next count)
So the largest count x postscaler value = 16x256 = 4096
You have to use a prescaler that gets you < 4096
Pick prescaler 1:32 = 40000/32 = 1250, which is < 4096
Then set count=125 with a 1:10 postscaler -> 1250
So, to get a 10ms TMR0 8-bit setting try this:
- set Fosc/4 mode T0CS<2:0> = 010
- set 8-bit mode T016BIT = 0
- set prescaler to 1:32 T0CKPS<3:0> = 0101
- set postscaler to 1:10 T0OUTPS<3:0> = 1001
- set period count = 125 (load TMR0H with 125-1=124)
Re: 18f27k40 - timer0 changes from K22, K80 series 8bit/16bit mode
@ fosc=16mhz settings from mcc for a 10mS timer
pic18f25k22
Code:
// Set TMR0 to the options selected in the User Interface
// TMR0H 0;
TMR0H = 0x00;
// TMR0L 100;
TMR0L = 0x64;
// Load TMR0 value to the 8-bit reload variable
timer0ReloadVal = 100;
// Clearing IF flag
INTCONbits.TMR0IF = 0;
// T0PS 1:256; T08BIT 8-bit; T0SE Increment_hi_lo; T0CS FOSC/4; TMR0ON enabled; PSA assigned;
T0CON = 0xD7;
18f27k40
Code:
// Set TMR0 to the options selected in the User Interface
// T0CS FOSC/4; T0CKPS 1:64; T0ASYNC synchronised;
T0CON1 = 0x46;
// TMR0H 155;
TMR0H = 0x9B;
// TMR0L 0;
TMR0L = 0x00;
// Clearing IF flag
PIR0bits.TMR0IF = 0;
// T0OUTPS 1:1; T0EN enabled; T016BIT 8-bit;
T0CON0 = 0x80;
Re: 18f27k40 - timer0 changes from K22, K80 series 8bit/16bit mode
thanks guys
i am running at 32mhz , so a few changes , its nice have both the prescaller and the postscaller something that timer0 never had , and without it 10ms in 8bit mode functioning in same way as TMR2 would not be possible at 32mhz
i pointed this out cos i have ported timer0 code from one processor to another , and was not expecting a change in timer0 internal functions when in 8 bit mode
cheers
Sheldon
1 Attachment(s)
Re: 18f27k40 - timer0 changes from K22, K80 series 8bit/16bit mode
oh and when i first setup this original code, back some years back i refereed to this calculator, so dont use this calc for timer 0 for k40
Attachment 8742
Re: 18f27k40 - timer0 changes from K22, K80 series 8bit/16bit mode
Quote:
// T0CS FOSC/4; T0CKPS 1:64; T0ASYNC synchronised;
// TMR0H 155;
// TMR0L 0;
// T0OUTPS 1:1; T0EN enabled; T016BIT 8-bit;
For the K40 in 8-bit mode I don't see how that gets you 10ms @ 16MHz clock
Doesn't that get you 156 x 64 x 250ns = 2.496ms ?
Re: 18f27k40 - timer0 changes from K22, K80 series 8bit/16bit mode
Quote:
Doesn't that get you 156 x 64 x 250ns = 2.496ms ?
yes , my bad it needs to be T0CKPS 1:256;
accidently set fosc to 4mhz when trying to match sheldons settings trying to guess at his fosc and forgot to regenerate the code
Re: 18f27k40 - timer0 changes from K22, K80 series 8bit/16bit mode
Quote:
it needs to be T0CKPS 1:256
That'll get you a lot closer... 9.984ms
If you use the postscaler you can get 10.000ms:
- set prescaler to 1:32 T0CKPS<3:0> = 0101
- set postscaler to 1:10 T0OUTPS<3:0> = 1001
- set period count = 125 (load TMR0H with 125-1=124)
gets you 32 x 10 x 125 x 250ns = 10.000ms
For 32MHz, just change the prescaler to 1:64:
64 x 10 x 125 x 125ns = 10.000ms
Re: 18f27k40 - timer0 changes from K22, K80 series 8bit/16bit mode
i'm impressed on just how versatile these later generation chips are. there is heaps to learn
1 Attachment(s)
Re: 18f27k40 - timer0 changes from K22, K80 series 8bit/16bit mode
also be aware now of the errata on timer0 k40 series , T0ASYNC bit problem , when FOSC/4 clock source
Attachment 8750