PIC 18F47J53 DelayMS decimal places

Discuss PIC and electronic related things

Moderators: David Barker, Jerry Messina

Post Reply
charllie_1
Posts: 5
Joined: Sun Feb 14, 2016 12:18 pm

PIC 18F47J53 DelayMS decimal places

Post by charllie_1 » Sun Feb 14, 2016 1:13 pm

Hi everyone,

Can anyone help with this problem i am having.


I need a LED to blink at the Frequency of 7.9hz which is 63.29114 on and 63.29114 off at the moment i can not place the decimal point in the code below. How can this be achieved?

any help would be greatly appreciated

Charlie



{
*****************************************************************************
* Name : UNTITLED.BAS *
* Author : [select VIEW...EDITOR OPTIONS] *
* Notice : Copyright (c) 2016 [select VIEW...EDITOR OPTIONS] *
* : All Rights Reserved *
* Date : 12/02/2016 *
* Version : 1.0 *
* Notes : *
* : *
*****************************************************************************
}
{
*****************************************************************************
* Name : StrobeTest-04.BAS *
* Date : 08/01/2014 *
* *
*****************************************************************************
}

Device = 18F47J53

Clock = 48
Dim mS As Word,
TMR1IE As PIE1.0, 'TMR1 Interrupt Enable
TMR1IF As PIR1.0, 'TMR1 Interrupt Flag
TMR1ON As T1CON.0, 'TMR1 Count Enable
Timer1 As TMR1L.AsWord, 'A quick way of creating a Word Alias
Trpt As Word,
Trpt2 As Word,
LED0 As PORTB.0,
LED1 As PORTB.1,
LED2 As PORTB.2,
LED3 As PORTB.3,
LED4 As PORTB.4,
LED5 As PORTB.5,
LED6 As PORTD.0




Const
TMR1StartVal = 15536, 'User defined TMR1 starting value
TMR1ReloadVal = TMR1StartVal + 0

Interrupt TMR1_Interrupt()
Save(0) 'Back up system variables
If TMR1IF = 1 Then
TMR1ON = 0 'Disable TMR1
Timer1 = TMR1ReloadVal 'Reload a new start value (includes non-counted cycles while disabled)
TMR1ON = 1 'Enable TMR1
TMR1IF = 0 'Clear the TMR1 Interrupt
Inc(mS, 1) 'Increment mS by 1
EndIf
Restore 'Restore system variables
End Interrupt


'Start Of Main Program...
mS = 0 'Clear the mS counter
Trpt = 0
Trpt2 = 0

High(LED0)
High(LED1)
High(LED2)
High(LED3)
High(LED4)
High(LED5)
High(LED6)

DelayMS(150)

Low(LED0)
Low(LED1)
Low(LED2)
Low(LED3)
Low(LED4)
Low(LED5)
Low(LED6)

DelayMS(150)


While Trpt2 < 1

While Trpt < 34

PORTD = %111111
PORTB = %111111
DelayMS(63)
PORTD = %0000000
PORTB = %0000000
DelayMS(63)
Inc(Trpt)



Wend
Trpt = 0
DelayMS(50)
While Trpt < 3

PORTB = %111111
PORTD = %000001
DelayMS(9000)
PORTB = %000000
PORTD = %000000
DelayMS(9000)
Inc(Trpt)


Wend
Trpt = 1
DelayMS(50)
Inc(Trpt2)
Wend
Trpt2 = 1

Jerry Messina
Swordfish Developer
Posts: 1469
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: PIC 18F47J53 DelayMS decimal places

Post by Jerry Messina » Sun Feb 14, 2016 7:42 pm

How about using 'delayus()' instead of 'delayms()'...

Code: Select all

  'DelayMS(63)
  delayus(63291)
Is that close enough?

The max value you can use is a 16-bit word, so that's up to 65535us (or 65.535ms)

charllie_1
Posts: 5
Joined: Sun Feb 14, 2016 12:18 pm

Re: PIC 18F47J53 DelayMS decimal places

Post by charllie_1 » Mon Feb 15, 2016 8:57 am

Thanks for your quick reply.

I did think of doing this, however i was worried that calculating in microsec may lose accuracy over a long period.
another question is why do i have set the clock at 48 in order make the delay work correctly? is the time set correctly?

Best wishes

Charlie

Jerry Messina
Swordfish Developer
Posts: 1469
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: PIC 18F47J53 DelayMS decimal places

Post by Jerry Messina » Mon Feb 15, 2016 10:46 am

DelayMS and DelayUS are software-based delays, so they just count instruction cycles each time they're called.

The 'clock = 48' statement just tells the compiler what frequency you intend to work at so it can compute the delays properly.
It doesn't actually set anything as far as the hardware goes. You have to setup the CONFIG and various OSCCON registers
yourself to get the chip to work at the desired freq depending on your setup.

You've declared a TMR1 interrupt routine but there's nothing actually using that. It's not even running.

charllie_1
Posts: 5
Joined: Sun Feb 14, 2016 12:18 pm

Re: PIC 18F47J53 DelayMS decimal places

Post by charllie_1 » Mon Feb 15, 2016 4:53 pm

Thank you for taking the time to reply, I very new to this and have a very limited understanding of this subject.

Here is some modified code.

Not sure how to set up the timer/clock, is the below correct?

Kind Regards

Charlie

{
*****************************************************************************
* Name : StrobeTest-04.BAS *
* Date : 08/01/2014 *
* *
*****************************************************************************
}

Device = 18F47J53

Clock = 48
Dim mS As Word,
TMR1IE As PIE1.0, 'TMR1 Interrupt Enable
TMR1IF As PIR1.0, 'TMR1 Interrupt Flag
TMR1ON As T1CON.0, 'TMR1 Count Enable
Timer1 As TMR1L.AsWord, 'A quick way of creating a Word Alias
Trpt As Word,
Trpt2 As Word,
LED0 As PORTB.0,
LED1 As PORTB.1,
LED2 As PORTB.2,
LED3 As PORTB.3,
LED4 As PORTB.4,
LED5 As PORTB.5


Const
TMR1StartVal = 15536, 'User defined TMR1 starting value
TMR1ReloadVal = TMR1StartVal + 0

Interrupt TMR1_Interrupt()
Save(0) 'Back up system variables
If TMR1IF = 1 Then
TMR1ON = 0 'Disable TMR1
Timer1 = TMR1ReloadVal 'Reload a new start value (includes non-counted cycles while disabled)
TMR1ON = 1 'Enable TMR1
TMR1IF = 0 'Clear the TMR1 Interrupt
Inc(mS, 1) 'Increment mS by 1
EndIf
Restore 'Restore system variables
End Interrupt

Sub TMR1_Initialize()
TMR1ON = 0 'Disable TMR1
T1CON.1 = 0 '1 = External clock from pin RC0/T1OSO/T1CKI (on the rising edge)
'0 = Internal clock (FOSC/4)
T1CON.4 = 1 '11 = 1:8 prescale value
T1CON.5 = 1 '10 = 1:4 prescale value
'01 = 1:2 prescale value
'00 = 1:1 prescale value
Timer1 = TMR1StartVal 'Fill the Timer register with a starting value
TMR1IE = 1 'Enable TMR1 Interrupts
TMR1ON = 1 'Enable TMR1 to Increment
Enable(TMR1_Interrupt) 'Enable TMR1 Interrupt Handle
End Sub
'Start Of Main Program...
mS = 0 'Clear the mS counter
Trpt = 0
Trpt2 = 0

TMR1_Initialize 'Setup and enable TMR1


High(LED0)
High(LED1)
High(LED2)
High(LED3)
High(LED4)
High(LED5)

DelayMS(250)

Low(LED0)
Low(LED1)
Low(LED2)
Low(LED3)
Low(LED4)
Low(LED5)

DelayMS(2000)

While Trpt2 < 150

While Trpt < 1000
PORTB = %111111
DelayUS(63291)
PORTB = %000000
DelayUS(63291)
Inc(Trpt)

Wend


Trpt = 1
DelayMS(50)
Inc(Trpt2)
Wend
Trpt2 = 1

Jerry Messina
Swordfish Developer
Posts: 1469
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: PIC 18F47J53 DelayMS decimal places

Post by Jerry Messina » Mon Feb 15, 2016 6:38 pm

That looks workable (depending on your hardware), but a few suggestions:

When you're posting code here on the forum it works out better if you put it in between code tags (use the 'code' button in the forum editor)
That way it'll preserve any formatting you have and make it easier to read.

I don't think the TMR1 reload value will get you what you want, but I'm just guessing. There are some addt'l steps you need to do in order to get the chip setup to run properly. How is your hardware setup? Do you have an external crystal, a clock, or do you intend to use the internal osc?

Also, as I mentioned the DelayMS()/DelayUS() calls don't use any of the timers so you'll need some other way of seeing if you have them setup properly.

charllie_1
Posts: 5
Joined: Sun Feb 14, 2016 12:18 pm

Re: PIC 18F47J53 DelayMS decimal places

Post by charllie_1 » Mon Feb 15, 2016 8:30 pm

Hi

The hardware i am using is the PIC 18F47J53 with no addons or external clock (straight out the box).
Sorry i did not notice the code button:-

TMR1 reload value i copied for the net any suggestion would greatly appreciated. The problem i am having is i really don't understand this level of the PIC.

I am not sure if i understand you regarding the Delay call, does that mean i should not worry about setting up the timers? I have set the delay for 10 secs and then used a stopwatch this how i came up with the clock at 48 at 16 i found it was running at 3.3 secs and at 48 it seems to work as it should. I need it to obviously work in milliseconds or microseconds and be accurate and this is what i am not sure about. If you shed any light on this it would be helpful and also how many channels can i us in this way?

once again thank for your support and quick response

kind regards

charlie

Jerry Messina
Swordfish Developer
Posts: 1469
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: PIC 18F47J53 DelayMS decimal places

Post by Jerry Messina » Mon Feb 15, 2016 11:07 pm

does that mean i should not worry about setting up the timers?
Well, there's nothing in the DelayXX() calls that require/use any timer.
and also how many channels can i us in this way?
Channels of what?

I'm glad to help out in any way I can, but it looks like you have a lot of reading to do first.I'd suggest you start with the Swordfish manual, the datasheet for your chip, and look at the various sample programs included with the compiler and on the wiki page.

charllie_1
Posts: 5
Joined: Sun Feb 14, 2016 12:18 pm

Re: PIC 18F47J53 DelayMS decimal places

Post by charllie_1 » Thu Feb 18, 2016 2:44 pm

Thank you for your reply, you are absolutely right !! I have started reading the sf manual and playing around with it.

Once again thanks for your advise

Best Wishes

Charlie

Post Reply