View previous topic :: View next topic |
Author |
Message |
SZTRAD
Joined: 30 Dec 2019 Posts: 165
|
Posted: Fri Feb 12, 2021 8:57 am Post subject: PIT xttiny |
|
|
Hi
Another interesting peripheral is PIT. As the name suggests, it is used to periodically call interrupts.Thanks to the fact that PIT shares the same clock as RTC, it can also run in sleep modes, so you can also use it to wake up regularly. PIT can generate regular events, so other peripherals can use its services.
It's connected to the internal RTC, so we have to accept the clock on which the RTC is on.
We have 3 timing options:
1-outer crystal 32768kHz
2-inner 32k ULP
3-general outer brought via TOSC1
The first and third choices are not needed to demonstrate functions. Just please remember that at room temperature its error is 3% and at 70°C to 10%. Count this when designing a solution or you'd better connect an external crystal.
To configure the function and control PIT we need registers:
RTC_CKSEL -signal source settings
RTC_PITCTRLA-setting in which period to call the interrupt You can choose one of 14 values. So with ULP you have a range of 1/(32768/4)=122us to 1/(32768/32768)=1s or you can stretch the time by choosing 0x01 in RTC_CKSEL when the timing signal is about 1kHz and in this case you can get to times of 4ms-32s.
The PITEN bit of course starts the Timer run.
RTC_PITSTATUS-Attention! The PIT is running asynchronously so it is necessary to wait a few periods after writing to RTC_PITCTLA before the change is actually made.The manufacturer remembered this and in the register RTC_PITSTATUS bitem CTRLBUSY lets you know about it.If the set write is in progress.
RTC_PITINTCTRL- interrupt permission
RTC_PITINFLAGS-flag from overflow
Nobody will probably need this registry in BASCOM, but what if RTC_PITDBGCTRL-permit running during debug
There is nothing more to be said about basic familiarization with PIT functionality. So let's try to write something and test the functionality of what the manufacturer invented. We'll set the run from the UPL oscillator, and we'll flash with the LED in the 1Hz interval.
Code: |
$regfile = "atXtiny1606.dat"
$crystal = 20000000
$hwstack = 16
$swstack = 16
$framesize = 24
'set the system clock and prescaler
Config Sysclock = 16_20mhz , Prescale = 1
'configure port
config pina.3=output 'led connection
'initPIT
RTC_CLKSEL= &H00 'internal signal source 32768kHz
RTC_PITINTCTRL=&H01 'enable interrupt PIT
RTC_PITCTRLA= &H71 ' prescaler 32768 time 1s,start timer
while RTC_PITSTATUS.0 =1 ' waiting for write to registry
wend
enable interrupts
enable PIT_ovf 'enable interrupt from PIT overflow
on pit_ovf led nosave 'interrupt handler
do
loop
'there is nothing to do
end
led:
rtc_PITINTFLAGS.0=1 'PIT flag setting,'1' deleting flag
toggle portA.3 'pin 'LED' status change
return
|
Later, I'll add programs to connect to xttiny peripherals.
RS[/code] |
|
Back to top |
|
|
SZTRAD
Joined: 30 Dec 2019 Posts: 165
|
Posted: Sat Feb 13, 2021 12:20 pm Post subject: |
|
|
Hi All
The first simple example.
Simple multitasking when using PIT. A milis variable is created in which the current time is stored in milliseconds from the start of the program run. It is not a pure millisecond (see program and previous description). He uses interrupt.
Code: |
' multitasking
'--------------------------------------------------------------------------------
$regfile = "atXtiny1606.dat"
$crystal = 20000000
$hwstack = 32
$swstack = 32
$framesize = 48
'set the system clock and prescaler
Config Sysclock = 16_20mhz , Prescale = 1
'configure port
config pina.3=output 'led1 connection
config pina.2=output 'led2 connection
'promenné
dim milis as word
dim cas_zmeny1 as word
dim cas_zmeny2 as word
'set constant
const time_led1= 300 'call process 1,300us]
const time_led2=400 'call process 2,400us)
declare sub led1(byval milis as word,byval time_led1 as word)
declare sub led2(byval milis as word,byval time_led2 as word)
'initPIT
RTC_CLKSEL= &H00 'internal signal source 32768kHz
waitms 200
RTC_PITINTCTRL=&H01 'enable interrupt PIT
RTC_PITCTRLA= &H21 ' prescaler 32 time 0.001s(0,000977s),start timer
while RTC_PITSTATUS.0 =1 ' waiting for write to registry
wend
enable interrupts
enable PIT_ovf 'enable interrupt from PIT overflow
on pit_ovf inter save 'interrupt handler
milis=0
reset porta.2
reset porta.3
do
call led1(milis,time_led1)
call led2(milis,time_led2)
loop
end
inter:
rtc_PITINTFLAGS.0=1 'PIT flag setting,'1' deleting flag
milis = milis+1
return
sub led1(byval milis as word,byval time_led1 as word)
local a1 as word 'auxiliary variable to compare time
a1=milis-cas_zmeny1 'calculating the difference between the current time and the last change time
if a1>= time_led1 then 'if the difference is equal to or greater than the desired
cas_zmeny1=milis ' save new change time
toggle porta.3 ' performs the requested task(in this case port status changes)
end if
end sub
sub led2(byval milis as word,byval time_led2 as word)
local a2 as word
a2=milis-cas_zmeny2
if a2 >= time_led2 then
cas_zmeny2=milis
toggle porta.2
end if
end sub |
RS |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum
|
|