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?
 
Yes, with pre and post scalers Timer2 can easily produce mS ticks, much easier than 125uS ticks. However, the 125uS ticks should still have worked.

Mike.
 
Note, PR2 needs to be set to 249 as it's zero based.

Is your clock gaining ~1 second every 4 minutes? I.E. 1 in 250 roughly.

You should really go back to your idea of using a battery backed I2C module.

Mike.
 
Looking at the crystal, the top has a marking that says "10,000N"
Will recheck the results and get back to you for comparison to cell phone timer and computer timer.
 
Looking at the crystal, the top has a marking that says "10,000N"
There's a good chance that's a 10MHz xtal, so try this:

Code:
device = 18F43K22
clock = 10

config FOSC = HSMP              // HS oscillator (medium power 4-16 MHz)
config PLLCFG = OFF             // Oscillator used directly
config PRICLKEN = ON            // Primary clock is always enabled
config FCMEN = OFF              // Fail-Safe Clock Monitor disabled
config IESO = OFF               // Oscillator Switchover mode disabled
config PWRTEN = ON              // Power up timer enabled
config BOREN = ON               // Brown-out Reset enabled and controlled by software (SBOREN is enabled)
config BORV = 285               // VBOR set to 2.85 V nominal
config WDTEN = OFF              // Watch dog timer is always disabled. SWDTEN has no effect
config WDTPS = 128              // 1:128
config CCP2MX = PORTC1          // CCP2 input/output is multiplexed with RC1
config PBADEN = OFF             // PORTB<5:0> pins are configured as digital I/O on Reset
config CCP3MX = PORTB5          // P3A/CCP3 input/output is multiplexed with RB5
config HFOFST = ON              // HFINTOSC output and ready status are not delayed by the oscillator stable status
config T3CMX = PORTC0           // T3CKI is on RC0
config P2BMX = PORTD2           // P2B is on RD2
config MCLRE = EXTMCLR          // MCLR pin enabled, RE3 input pin disabled
config STVREN = ON              // Stack full/underflow will cause Reset
config LVP = ON                 // Single-Supply ICSP enabled if MCLRE is also 1
config XINST = OFF              // Instruction set extension and Indexed Addressing mode disabled (Legacy mode)
config DEBUG = OFF              // Disabled
config CP0 = OFF                // Block 0 (000200-000FFFh) not code-protected
config CP1 = OFF                // Block 1 (001000-001FFFh) not code-protected
config CPB = OFF                // Boot block (000000-0001FFh) not code-protected
config CPD = OFF                // Data EEPROM not code-protected
config WRT0 = OFF               // Block 0 (000200-000FFFh) not write-protected
config WRT1 = OFF               // Block 1 (001000-001FFFh) not write-protected
config WRTC = OFF               // Configuration registers (300000-3000FFh) not write-protected
config WRTB = OFF               // Boot Block (000000-0001FFh) not write-protected
config WRTD = OFF               // Data EEPROM not write-protected
config EBTR0 = OFF              // Block 0 (000200-000FFFh) not protected from table reads executed in other blocks
config EBTR1 = OFF              // Block 1 (001000-001FFFh) not protected from table reads executed in other blocks
config EBTRB = OFF              // Boot Block (000000-0001FFh) not protected from table reads executed in other blocks

// for setalldigital
Include "setdigitalio.bas"

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

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

 //testing for input
Dim sw1 As PORTC.4
Dim sw2 As PORTC.6
Dim led1 As PORTC.0
Dim led2 As PORTC.1

Dim Int_Counter As Word               
Dim update As Boolean
Dim secs,mins,hrs As Byte

// TMR2 setup for 1ms ticks
const TICKS_PER_SEC = 1000
dim TMR2_IF as PIR1.1
dim TMR2_IE as PIE1.1

Interrupt RTC()
   Dec(Int_Counter)
   If Int_Counter = 0 Then
      Int_Counter = TICKS_PER_SEC         ' each interrupt = 1ms x 1000 int's = 1 second
      update = true                       ' update LCD output
   End If
   TMR2_IF = 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()
   secs = 0
   mins = 0
   hrs = 0
   Int_Counter = TICKS_PER_SEC
   update = false

   LCD.Command(130)
   LCD.Write("24-HOUR CLOCK")
   LCD.Command(196)
   LCD.Write("00:00:00")     

' Timer2 Registers:
   TMR2 = 0
   ' Prescaler=1:1; TMR2 PostScaler=1:10; PR2=249 - Freq = 1.00kHz - Period = 1,000,000 ns (1ms)
   PR2 = 249            ' PR2 (Timer2 Match value)(1 cycle adjusted)
   'T2CON |       = 72 ' bits 6-3 Post scaler 1:1 thru 1:16
   'T2CON.TOUTPS3 = 1 
   'T2CON.TOUTPS2 = 0 
   'T2CON.TOUTPS1 = 0 
   'T2CON.TOUTPS0 = 1 
   'T2CON.TMR2ON  = 1 ' Timer2 on bit: 1=Timer2 is on;
   'T2CON.T2CKPS1 = 0 ' bits 1-0  Prescaler Rate Select bits
   'T2CON.T2CKPS0 = 0 
   T2CON = %01001100

   TMR2_IF = 0                             ' clear TMR2 interrupt flag
   TMR2_IE = 1                             ' enable TMR2 interrupt

   Enable(RTC)                            ' enable jump to RTC ISR
End Sub

// main code
   setalldigital()
   
   Input (sw1)
   Output (led1)                         
   Initialize()

   While true
      If update Then
         Clock24()                          ' update 24H Clock output
      End If
   End while
 
I have 5 boards on order, awaiting delivery. Might as well mount the correct crystal.
I ran the code in post#25 but the LCD was all jumbled.
Going to try and recompile etc.
 
I located a 20mhz crystal, swaped out the 10mhz crystal and now the LCD only displays a bunch of black boxes.
Need to recheckcircuit etc.
 
About a week ago I unplugged My LCD same code worked before I unplugged it.
In my case I got nice white boxes. I picked up my screwdriver and changed the pot settings.
Bang the LCD displayed my ADC readings.

If your code worked one time Id be looking at your wiring

My problem was the power supply it was 5.22 volts
I used a different one that was less 4.89 volts so a change in the contrasted voltage was need .
 

Not that I suggest MrDEB attempts it, but one of our commercial products uses a similar LCD module - and my first design used a pot to set the contrast - this meant every unit had to be adjusted during manufacture to make it work. So I changed the design to use PWM contrast adjustment - and replaced the pot with a 1K resistor and a10uF capacitor fed from a PWM output pin. I added contrast adjustment to the units settings, with the value stored in data EEPROM memory after finalising adjustment.

The optimum value turned out to be about 12, so that is the default setting stored in the EEPROM memory during programming, so the units work instantly after assembly.

This change made assembly that little bit easier, and allowed the contrast to be customer adjustable if required, as for temperature changes etc. However, the displays we use seem remarkably tolerant of the contrast setting, and anywhere from 0 to 40/50 seems to be quite acceptable - if you set it too high though, the display blanks off.
 
Well after checking and making corrections as well as a new 18f43k22 chip, it appears to keep time.
Code:
Device = 18F43K22
Clock = 8
// int osc and IO pin libraries
Include "intosc.bas"
#option DIGITALIO_INIT = true       // automatically call setalldigital
Include "setdigitalio.bas"


// 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 SW1 As PORTC.0
Dim SW2 As PORTC.2
Dim Int_Counter As Word               
Dim update As Boolean
Dim secs,mins,hrs As Byte

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
    DELAYUS(5000)        //DEBOUNCE   
    hrs = hrs + 1
    End If
         If SW2 = 0 Then    //advance mins by 5
         DELAYUS(500)     
         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
   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)
   Input (SW2)
   Initialize()

   While 1=1
      If update = true Then
         Clock24()                          ' update 24H Clock output
      End If
   Wend
 
Is there a limit to the number od CASE SELECT ?
Code:
Sub Clock24()
 Dim clk As String
 mints = mins
    Inc(secs)
    If secs >= 60 Then                     ' check each tally for rollover
       secs = 0
       Inc(mins)
 //USE SELECT CASE TO INCREMENT THE MINUTE LEDS (60 - 3mm BLUE leds)
         // case 1 - 9 = minute 1 to 9   
  Select mins       //select which blue led is on
   Case 1  COLUM_2 = 1  Row_1 = 1               //#16 led = minute 1
   Case 2  COLUM_2 = 1  Row_2 = 1 Row_1 = 0              //#15LED = 1
   Case 3  COLUM_2 = 1  Row_3 = 1 Row_2 = 0              //#14lED
   Case 4  COLUM_2 = 1  Row_4 = 1 Row_3 = 0              //#13LED
   Case 5  COLUM_2 = 1  Row_5 = 1 Row_4 = 0              //#12LED
   Case 6  COLUM_2 = 1  Row_6 = 1 Row_5 = 0              //#11LED
   Case 7  COLUM_2 = 1  Row_7 = 1 Row_6 = 0              //#10LED
   Case 8  COLUM_2 = 1  Row_8 = 1 Row_7 = 0              //#9LED
   case 9  colum_2 = 1  row_9 = 1 Row_8 = 0              //#8 led
  
    //minutes 9 - 19
           Case 10 COLUM_1 = 1  Row_3 = 1 colum_2 = 0            //#7led  turn off colum_2
           Case 11 COLUM_1 = 1  Row_4 = 1 Row_3 = 0              //#6LED = 1
           Case 12 COLUM_1 = 1  Row_5 = 1 Row_4 = 0              //#5LED
           Case 13 COLUM_1 = 1  Row_6 = 1 Row_5 = 0              //#4LED
           Case 14 COLUM_1 = 1  Row_7 = 1 Row_6 = 0              //#3LED
           Case 15 COLUM_1 = 1  Row_8 = 1 Row_7 = 0              //#2LED
           Case 16 COLUM_1 = 1  Row_9 = 1 Row_8 = 0              //#1LED
           Case 17 COLUM_1 = 1  Row_10 = 1 Row_9 = 0              //#60LED
           case 18 colum_1 = 1  row_1 = 1  Row_10= 0             //#59led
           case 19 colum_1 = 1  row_2 = 1  Row_1 = 0             //#58led
          
      //minutes 21 - 30     
                   Case 20 COLUM_5 = 1  Row_1 = 1  colum_1 = 0  //#57led   turn off colum_1
                   Case 21 COLUM_5 = 1  Row_2 = 1  Row_1 = 0             //#56LED = 1
                   Case 22 COLUM_5 = 1  Row_3 = 1  Row_2 = 0             //#55LED
                   Case 23 COLUM_5 = 1  Row_4 = 1  Row_3 = 0             //#54LED
                   Case 24 COLUM_5 = 1  Row_5 = 1  Row_4 = 0             //#53LED
                   Case 25 COLUM_5 = 1  Row_6 = 1  Row_5 = 0             //#52ED
                   Case 26 COLUM_5 = 1  Row_7 = 1  Row_6 = 0             //#51LED
                   Case 27 COLUM_5 = 1  Row_8 = 1  Row_7 = 0             //#50LED
                   case 28 colum_5 = 1  row_9 = 1  Row_8 = 0             //#49led
                   case 29 colum_5 = 1  row_10 = 1 Row_9 = 0             //#48led
          
        // minutes 31 - 40 
                           Case 30 COLUM_3 = 1  Row_1 = 1 colum_5 = 0           //#47led  turn off colu_5
                           Case 31 COLUM_3 = 1  Row_2 = 1 Row_1 = 0             //#46LED = 1
                           Case 32 COLUM_3 = 1  Row_3 = 1 Row_2 = 0             //#45LED
                           Case 33 COLUM_3 = 1  Row_4 = 1 Row_3 = 0             //#44LED
                           Case 34 COLUM_3 = 1  Row_5 = 1 Row_4 = 0             //#43LED
                           Case 35 COLUM_3 = 1  Row_6 = 1 Row_5 = 0             //#42LED
                           Case 36 COLUM_3 = 1  Row_7 = 1 Row_6 = 0             //#41LED
                           Case 37 COLUM_3 = 1  Row_8 = 1 Row_7 = 0             //#40LED
                           case 38 colum_3 = 1  row_9 = 1 Row_8 = 0             //#39led
                           case 39 colum_3 = 1  row_10 = 1 Row_9 = 0            //#38led
          
           //minutes 41 - 50
                               Case 40 COLUM_6 = 1  Row_1 = 1  colum_3 = 0           //#37led
                               Case 41 COLUM_6 = 1  Row_2 = 1  Row_1 = 0             //#36LED = 1
                               Case 42 COLUM_6 = 1  Row_3 = 1  Row_2 = 0             //#35LED
                               Case 43 COLUM_6 = 1  Row_4 = 1  Row_3 = 0             //#34LED
                               Case 44 COLUM_6 = 1  Row_5 = 1  Row_4 = 0             //#33LED
                               Case 45 COLUM_6 = 1  Row_6 = 1  Row_5 = 0             //#32LED
                               Case 46 COLUM_6 = 1  Row_7 = 1  Row_6 = 0             //#31LED
                               Case 47 COLUM_6 = 1  Row_8 = 1  Row_7 = 0             //#30LED
                               case 48 colum_6 = 1  row_9 = 1  Row_8 = 0             //#29led
                               case 49 colum_6 = 1  row_10 = 1 Row_9 = 0             //#28led
                              
              //minutes 51 - 60
                                       Case 50 COLUM_4 = 1  Row_1 = 1 colum_6 = 0   //#27led
                                       Case 51 COLUM_4 = 1  Row_2 = 1 Row_1 = 0              //#26LED = 1
                                       Case 52 COLUM_4 = 1  Row_3 = 1 Row_2 = 0              //#25LED
                                       Case 53 COLUM_4 = 1  Row_4 = 1 Row_3 = 0              //#24LED
                                       Case 54 COLUM_4 = 1  Row_5 = 1 Row_4 = 0              //#23LED
                                       Case 55 COLUM_4 = 1  Row_6 = 1 Row_5 = 0              //#22LED
                                       Case 56 COLUM_4 = 1  Row_7 = 1 Row_6 = 0              //#21LED
                                       Case 57 COLUM_4 = 1  Row_8 = 1 Row_7 = 0              //#20LED
                                       case 58 colum_4 = 1  row_9 = 1 Row_8 = 0              //#19led
                                       case 59 colum_4 = 1  row_10 = 1 Row_9 = 0             //#18led
                                       case 60 colum_2 = 1  row_10 = 1 colum_4 = 0           //#17 led
          
      EndSelect
 
I have 72 LEDs to enable in sequence as per mins
I now have added 12 additional LEDs for the hours.
QUESTION? where have you been hiding the past couple years?
Code:
If mins >= 60 Then
          mins = 0
          Inc(hrs)
          If hrs = 24 Then
             hrs = 0
          End If
       End If
      
  select hours
 
         case 1 green_1 = 1 g_4 = 1                //12 o clk led     
         case 2 green_1 = 1 g_3 = 1 g_4 = 0       //1 o clk
         case 3 green_1 = 1 g_2 = 1 g_3 = 0      //2 o clk
         case 4 green_1 = 1 g_1 = 1 g_2 = 0      //3 o clk
         case 5 green_2 = 1 g_2 = 1 green_1 = 0  //4 o clk
         case 6 green_2 = 1 g_1 = 1 g_2 = 0      //5 o clk
         case 7 green_2 = 1 g_3 = 1 g_1 = 0      //6 o clk
         case 7 green_2 = 1 g_4 = 1 g_3 = 0      //7 o clk
         case 8 green_2 = 1 g_5 = 1 g_4 = 0      //8 o clk
         case 9 green_2 = 1 g_6 = 1 g_5 = 0      //9 o clk
         case 10 green_1 = 1 g_6 = 1 g_4 = 0 green_2 = 0 //10 o clk
         case 11 green_1 = 1 g_5 = 1 g_6 = 0     //11 o clk
   end select
 
Case is just the same as if else you write a bunch of then till you run out of code space

Where have I been sorry to say but I been working most of my time I wire up some nice Leds light shows
and computer-controlled arms that wash your car. Plus, the house wiring
 
Last edited:
Cookies are required to use this site. You must accept them to continue using the site. Learn more…