Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

programming a pic using pickit2/3

MrDEB

Well-Known Member
Been using the pickit 2 or 3 but recently I needed to see the results of my SF code.
Is there a way to examine whats going on (results) of the code.
Been using an LCD but the LCD quit working.
I recall using 2 pickits but ?
Te ardunio connects to the device and displays the results on the monitor but can his be done with an 18f series chip?
 
Been using the pickit 2 or 3 but recently I needed to see the results of my SF code.
Is there a way to examine whats going on (results) of the code.
Been using an LCD but the LCD quit working.
I recall using 2 pickits but ?
Te ardunio connects to the device and displays the results on the monitor but can his be done with an 18f series chip?

Simply use a serial port (either hardware or software) on the 18F to display whatever you want on your computer, using a serial comms program - this is all the Arduino system does. I use this fairly universally on my projects, mostly using a hardware serial port, but failing that using a software one on any available pin.
 
If you use the pickitminus software to program your k22, it has a uart tool built in that works with the same connections you use to program the chip.

You can even use it with other software to create a virtual com port.
 
Here's a super easy way to monitor serial with a PICkit 2/3 using the UART tool in the GUI program. In Swordfish Basic, you can set up a software UART and assign the transmit pin to whichever pin on the ICSP connector the PICkit uses to receive. The link explains exactly the procedure with Swordfish Basic.

If you follow those steps exactly, you can connect the PICkit to the ICSP connector, load code with the GUI, then turn on the UART tool to see results without changing the PICkit's connections.
 
Click TOOLS/UART TOOL.

Baud rate set in the Swordfish code must match baud rate in the UART tool.

Screenshot_20240922_125649_Edge.jpg
 
While searching through my collection of test boards I discovered one that the Picit3 discovered.
Onto my clock code problem
Attempting to add a pushbutton routine to adjust the time but can't get the time to change.
Been moving the sw1/led1 routine around but to no avail.
I need a method to adjust the clock to the proper time
Code:
Device = 18F4321
Clock = 8

// some LCD options...
#option LCD_DATA = PORTD.4
#option LCD_RS = PORTD.2
#option LCD_EN = PORTD.3

Include "lcd.bas"
Include "convert.bas"

{
   For One Second Update:

   8MHz Fosc = 2MHz internal clock = 0.5us per cycle (timer count)
   Use 8-bit Timer2, No Prescaler
   Set PR2 = 250; Timer2 resets on match every 250 counts = 125us
   Each Timer2 reset requires 1 cycle compensation... so set PR2 = 249
   8000 interrupts x 125us each = 1 second
}

Dim Int_Counter As Word               
Dim update As Boolean
Dim secs,mins,hrs As Byte
dim Hours as byte
 //testing for input
Dim sw1 As PORTC.4
Dim led1 As PORTC.0

Interrupt RTC()
   Dec(Int_Counter)
   If Int_Counter = 0 Then
      Int_Counter = 8000                  ' each interrupt = 125us x 8000 int's = 1 second
      update = true                       ' update LCD output
   End If
   PIR1.1 = 0                             ' clear interrupt flag 
End Interrupt

sub set-clock()
       If sw1 = 0 Then               // switch for setting time
   hrs = hours + 5
   led1 = 1
   DelayMS(2000)
   Else
   hrs = 0
   End If     
end sub


Sub Clock24()
 Dim clk As String
    Inc(secs)
    If secs = 60 Then                     ' check each tally for rollover
       secs = 0
       Inc(mins)
       If mins = 60 Then
          mins = 0
          Inc(hrs)
 
 
          If hrs = 24 Then
             hrs = 0
          End If
       End If
    End If
    clk = DecToStr(hrs,2)                ' output to LCD
    LCD.WriteAt(2,5,clk)
    clk = DecToStr(mins,2)
    LCD.WriteAt(2,8,clk)
    clk = DecToStr(secs,2)
    LCD.WriteAt(2,11,clk)
    update = false
End Sub

Sub Initialize()
   ADCON1 = 15
   secs = 0
   mins = 0
   hours = 0
  
   If sw1 = 0 Then               // switch for setting time
   hrs = hours + 5
   led1 = 1
   DelayMS(2000)
   //Else
   hrs = 0
   End If
 
   hrs = hours
   Int_Counter = 8000
   update = false
   LCD.Command(130)
   LCD.Write("24-HOUR CLOCK")
   LCD.Command(196)
   LCD.Write("00:00:00")     
   INTCON = 192                           ' enable GIE & PEIE
   T2CON = 0                              ' no prescaler or postscaler: timer2 OFF
   TMR2 = 0                               ' clear TMR2
   PR2 = 249                              ' set match value
   PIE1.1 = 1                             ' enable interrupt
   PIR1.1 = 0                             ' clear interrupt flag
   T2CON.2 = 1                            ' Timer2 ON
   Enable(RTC)
   Input (sw1)                           ' enable jump to RTC ISR
End Sub
   Input (sw1)
  
   Initialize

   While 1=1
       set-clock()
      If update = true Then
         Clock24()                          ' update 24H Clock output
      End If
   Wend
 
I tried inserting the sw1 at different locations, but the hrs never changed.
I inserted the LED to confirm a button press, but the LED never lit up.
Tried inserting routine before the Initialize() sub route.
I saved the the four different code examples from https://www.sfcompiler.co.uk/wiki/pmwiki.php?n=SwordfishUser.SoftRTC
The 2 things that came out good in locating a working test board is the board has the crystal on it and the LCD that I can plug in works.
ANY help in the direction will help!
 
issue 1 - the code in post 9 won't compile... you can't have a '-' char in a sub name (ie change 'set-clock')
issue 2 - you're setting the time variable 'hrs' from the variable 'hours', but nowhere in your code do you ever assign a value to 'hours'
issue 3 - don't screw around with the INTCON register unless you know what you're doing.
 
I think I discovered how to increment the hours
Code:
Sub Clock24()
 Dim clk As String
    Inc(secs)
    If secs = 60 Then                     ' check each tally for rollover
       secs = 0
       Inc(mins)
       If mins = 60 Then
          mins = 0
          Inc(hrs)
          If hrs = 24 Then
             hrs = 0
          End If
       End If
    End If
    if sw1 = 0 then
    led1 = 1
    hrs = hrs + 1
    end if
    clk = DecToStr(hrs,2)                ' output to LCD
    LCD.WriteAt(2,5,clk)
    clk = DecToStr(mins,2)
    LCD.WriteAt(2,8,clk)
    clk = DecToStr(secs,2)
    LCD.WriteAt(2,11,clk)
    update = false
End Sub
 
Here is where I am at so far. It appears to work and can adjust the hours and minutes
Made suggested suggestions
Code:
Device = 18F4321
Clock = 8

// some LCD options...
#option LCD_DATA = PORTD.4
#option LCD_RS = PORTD.2
#option LCD_EN = PORTD.3

Include "lcd.bas"
Include "convert.bas"

{
   For One Second Update:

   8MHz Fosc = 2MHz internal clock = 0.5us per cycle (timer count)
   Use 8-bit Timer2, No Prescaler
   Set PR2 = 250; Timer2 resets on match every 250 counts = 125us
   Each Timer2 reset requires 1 cycle compensation... so set PR2 = 249
   8000 interrupts x 125us each = 1 second
}

Dim Int_Counter As Word               
Dim update As Boolean
Dim secs,mins,hrs As Byte
Dim Hours As Byte
 //testing for input
Dim sw1 As PORTC.4
Dim sw2 As PORTC.6
Dim led1 As PORTC.0
Dim led2 As PORTC.1
Interrupt RTC()
   Dec(Int_Counter)
   If Int_Counter = 0 Then
      Int_Counter = 8000                  ' each interrupt = 125us x 8000 int's = 1 second
      update = true                       ' update LCD output
   End If
   PIR1.1 = 0                             ' clear interrupt flag 
End Interrupt


Sub Clock24()
 Dim clk As String
    Inc(secs)
    If secs = 60 Then                     ' check each tally for rollover
       secs = 0
       Inc(mins)
       If mins = 60 Then
          mins = 0
          Inc(hrs)
          If hrs = 24 Then
             hrs = 0
          End If
       End If
    End If
//routine for settime current time
///xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx   
    If sw1 = 0 Then          //advance hrs by 1
    hrs = hrs + 1
    End If
         If sw2 = 0 Then      //advance mins by 5
         mins = mins + 5
         End If
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx         
    clk = DecToStr(hrs,2)                ' output to LCD
    LCD.WriteAt(2,5,clk)
    clk = DecToStr(mins,2)
    LCD.WriteAt(2,8,clk)
    clk = DecToStr(secs,2)
    LCD.WriteAt(2,11,clk)
    update = false
End Sub

Sub Initialize()
   ADCON1 = 15
   secs = 0
   mins = 0
   Hours = 0
   hrs = 0
   Int_Counter = 8000
   update = false
   LCD.Command(130)
   LCD.Write("24-HOUR CLOCK")
   LCD.Command(196)
   LCD.Write("00:00:00")     
   INTCON = 192                           ' enable GIE & PEIE
   T2CON = 0                              ' no prescaler or postscaler: timer2 OFF
   TMR2 = 0                               ' clear TMR2
   PR2 = 249                              ' set match value
   PIE1.1 = 1                             ' enable interrupt
   PIR1.1 = 0                             ' clear interrupt flag
   T2CON.2 = 1                            ' Timer2 ON
   Enable(RTC)                            ' enable jump to RTC ISR
End Sub
   Input (sw1)
   Output (led1)                         
  ' Input (sw1)
  ' set-Clock()
   Initialize ()

   While 1=1
      
      If update = true Then
         Clock24()                          ' update 24H Clock output
      End If
   Wend
 
using the clock = 8 seems a little to fast
reduced to 6
That will not change anything. The 'clock='statement is purely so that the compiler can adjust any software delays to match your hardware settings, or if you're using the intosc. It has no effect on how fast the crystal is running.

remove this line:
Code:
   INTCON = 192                           ' enable GIE & PEIE
The interrupt enable bits are controlled by the 'enable(RTC)' statement.

Also, you should probably change your tests in Clock24() to '>=' instead of just '=' so that if you manage to adjust a time to an invalid setting it'll correct itself, something like
Code:
Sub Clock24()
  Dim clk As String
    Inc(secs)
    If secs >= 60 Then                     ' check each tally for rollover
       secs = 0
       Inc(mins)
       If mins >= 60 Then
          mins = 0
          Inc(hrs)
          If hrs >= 24 Then
             hrs = 0
          End If
       End If
    End If

Better yet would be to not let the time variables get set to invalid data in the first place when you increment them.
 
Last edited:
Code:
{
   For One Second Update:

   8MHz Fosc = 2MHz internal clock = 0.5us per cycle (timer count)
   Use 8-bit Timer2, No Prescaler
   Set PR2 = 250; Timer2 resets on match every 250 counts = 125us
   Each Timer2 reset requires 1 cycle compensation... so set PR2 = 249
   8000 interrupts x 125us each = 1 second
}

I suspect if this was written in Greek, it would be just as meaningful to MrDEB. I believe he's using a crystal (that's not 8MHz) and has not a clue what the above means.
 

Latest threads

New Articles From Microcontroller Tips

Back
Top