PIC18F26K22 – Blink

Here’s a fancier version of the usual blink routine that also demonstrates the various options for setting the internal oscillator.

* A more complicated Blink program for the PIC18F26K22
* Set the clock:
* 111 = HFINTOSC ? (16 MHz)
* 110 = HFINTOSC/2 ? (8 MHz)
* 101 = HFINTOSC/4 ? (4 MHz)
* 100 = HFINTOSC/8 ? (2 MHz)
* 011 = HFINTOSC/16 ? (1 MHz)(3)
* Use the PLL to quadruple the frequency.
* OSCTUNEbits.PLLEN = 1;
* connect LEDs to RA0 - RA5
* The program clock speed can be measured on pin RA6
#include /* MCU header file ***********/

#pragma config WDTEN = OFF
#pragma config FOSC=INTIO7 // ;Internal oscillator block
#pragma config PLLCFG = ON
#pragma config PRICLKEN = ON
#pragma config IESO = OFF
#pragma config PWRTEN = OFF // ;Power up timer disabled

int counter;
void main (void)
OSCCON = 0x62; // Fosc = 8MHz
OSCCONbits.SCS0 = 0;
OSCCONbits.SCS1 = 0;
OSCTUNEbits.PLLEN = 1; // Use the PLL to up the clock to 32Mhz

counter = 1;
TRISA = 0; /* configure PORTB for output */
while (counter <= 255)
PORTA = counter; /* display value of ‘counter’ on the LEDs */


PIC18F26K22 – Serial Hello World example

After having compiler issues using the new PIC18F25K50 chip, I switched to the K22 series.  Specifically the 28 pin PIC18F26K22.  The dual USART, dual SPI/I2C interfaces will be handy and allow for more flexible board layouts.

But, I had some difficulty getting the serial output to work.  I read through a variety of examples, and questions/problems posted by others, most without responses.  I did find several examples, but none of these worked.  With some additional messing about, I managed to come up with this minimal Hello World example.

Hopefully the following will save someone else some time.

/* Serial Hello World
* This works!
* Clock set to 4Mhz, no PLL, transmits at 2400 Baud
* PIC18F26K22
* Complier: C18 v 3.45


#pragma config FOSC = INTIO7 // internal clock, clock output on RA6

IRCF: Internal RC Oscillator Frequency Select bits(2)
111 = HFINTOSC (16 MHz)
110 = HFINTOSC/2 (8 MHz)
101 = HFINTOSC/4 (4 MHz)
100 = HFINTOSC/8 (2 MHz)
011 = HFINTOSC/16 (1 MHz)

void main (void)
OSCCONbits.IRCF = 0b101; //change Fosc to 4Mhz
// wait until IOFS = 1 (osc. stable)
while (!OSCCONbits.IOFS)
* Open the USART configured as
* 8N1, 2400 baud, in polled mode

while (1)
putrs1USART(“Hello World!\n\r”);

HiTech-C and the PIC18F26K22

I have been using HiTech-C with great success on the PIC16F series chips.  So, when I moved to the 18F series, I tried to stick with it. But, I hit a few problems…

The new PIC18F25K50 chips aren’t supported at all.

So, I tried the supported PIC18F26K22…  Some things do work, but the string libraries don’t seem to work… so, might as well just ditch this, as the PIC18 version of the compiler hasn’t been updated for a long time, I expect that Microchip may be abandoning this in favor of their new C18 compiler.


Microchip PIC18F25K50 compiler problems

After using the 16F series and some of the simpler 18F devices, I picked up some of the PIC18F25K50 microcontrollers. 

I found that the HiTech-C compiler doesn’t support the K50 series at all, so I switched over to the C18 compiler.  I can get the code to compile in C18, but it won’t link.  It turns out that Microchip messedup the toolchain and didn’t include the necessary libs in version 3.45.

The only thing to do is pack up those chips and wait another 6–18 months for Microchip to pull their thumb out.



Bark Controller

Device to discourage dogs from barking


One or more remote controlled devices(annunciators) which will emit a high frequency audio pulse.  One or more remote transmitters that can trigger all remote annunciators simultaneously.

Principle of operation:

  1. Transmitters will use the 315MHz ISM band.  The transmitters will all use the same address/code.  Matching ISM receivers, all set to the same address/code.
  2. The audio frequency is not critical, and will sweep across a range of 18 – 22 KHz.  
  3. The audio will be generated by a PWM from a microcontroller.  
  4. The audio burst should be 3-5 seconds in duration.
  5. The PWM signal from the microcontroller will be sent through a 2N2222A to a piezo speaker with a parallel inductor to boost the voltage.
  6. The device should be able to run on 5 VDC.
  7. The device does not need to be battery powered, so standby power consumption is not critical.


  • RF remote transmitter:  keyfob remote from Adafruit.com – based on the PT2262
  • RF receiver:  Simple RF T4 momentary from adafruit.com – based on the PT2272
  • Picaxe-08M2  – why?  had one laying around
  • Speaker:  piezo tweeter
  • Power Supply – 5V USB plug-in



Screenshot of output, note the 50Vpp output:

Bark_scope1Here’s the code:

; Bark Controller
;  initializes LED and Piezo to OFF
;  waits for a signal
;   when a signal is received:
;   turn on LED
;   step through a set of 3 tones, 10 times
;   turn off the LED
;   turn off the sound
;   wait a couple of seconds
; Tones to play:
;   frequency 18Khz = pwmout C.2, 55, 111
;   frequency 20Khz = pwmout C.2, 49, 100
;   frequency 22Khz = pwmout C.2, 44, 91

low C.4 ;LED is C.3 = PIN 2
pwmout C.2, off  ;Piezo is C.2 = PIN 5
do  ;input is C.3 = PIN 4

if pin3 = 1 then
 high C.4 ;LED on
  For b0 = 1 to 10
pwmout C.2, 55, 111 ; 18Khz
pause 100  ; play for 0.1 seconds
pwmout C.2, 49, 100 ; 20Khz
pause 100
pwmout C.2, 44, 91 ; 22Khz
pause 100
 low C.4
 pwmout C.2, off  ; turn off the piezo
 pause 2000



« Older Entries Recent Entries »