designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers...

53
A practical guide to… designing ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU www.bay-net.org

Transcript of designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers...

Page 1: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards

A practical guide to…

designing ham radio projects with

PIC microcontrollers

George Zafiropoulos

KJ6VU

www.bay-net.org

Page 2: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards

Topics

• Ham radio applications

• Microcontroller basics

• Hardware design examples

• Implementing your design

• Software tools

• Software examples

Page 3: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards

Ideas for Projects Using PIC Microcontrollers

• TR sequencer

• Memory keyer

• CW keyboard

• Station power controller

• Rotor controller

• Tone generator

• Test equipment controller

• Speaker / audio router

• Repeater controller

• Fox hunt tx controller

• Radio control

• Morse code decoder

• APRS encoder

• Frequency counter

• Battery monitor

• Audio meter

Page 4: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards

Hardware Options

Design your own hardwareUse existing

prototype board

Use pre-assembled

hardware platform

Software Options

Write your own firmware Write your own PC software

Page 5: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards

Topics

• Ham radio applications

• Microcontroller basics

• Hardware design examples

• Implementing your design

• Software tools

• Software examples

Page 6: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards

Picking a Microcontroller…

Selection Criteria for Ham Radio Projects

• Cheap

• Easy to program (relatively)

• Serial interface (for control and configuration)

• Fast

• Non-volatile flash memory (program and data)

• 5V supply and I/O

• Wide variety of IO besides digital in/out…

– A/D converter, SPI, UART, I2C, PWM (tone), etc…

• DIP package

• Code examples

• Big eco-system

• RF immune

• Low noise generation

Page 7: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards

Microcontroller Suppliers

Page 8: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards

Devices for Hobby Projects

Basic StampArduino

Atmel AVR

Microchip

PIC

Chips Modules Boards

SRS TR-1 Board

Page 9: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards

Lets Pick the PIC… what’s a PIC?

• Product of Microchip Technology

• PIC1640 originally developed by General Instrument's

Microelectronics Division

• PIC = "Programmable Interface Controller“

• PICs are low cost & widely available

• Large user base

• Low cost or free development tools

• In 2008 Microchip shipped its

6,000,000,000th PIC processor

Page 10: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards

PIC

18Fxxxx

Family

Page 11: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards

What’s Inside a PIC 18Fxxxx CPU Chip?

Page 12: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards
Page 13: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards
Page 14: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards

Lets Use the 18F2620…

• CPU Up to 10 MIPS performance (40 MHz)

• C compiler optimized RISC architecture

• 8 x 8 Single Cycle Hardware Multiply

• System Internal oscillator support 31 kHz to 8 MHz

• Fail-Safe Clock Monitor – allows safe shutdown if clock fails

• Watchdog Timer with separate RC oscillator

• Wide operating Voltage range; 2.0V to 5.5V

• nanoWatt Power Managed Modes Run, Idle and Sleep modes

• Idle mode currents down to 5.8uA typical

• Sleep mode currents down to 0.1uA typical

• Analog Features 10-bit ADC, 10 channels, 100K samples/second

• Programmable Low Voltage Detection Module

• Programmable Brown-out Reset module

• Two Analog comparators with input multiplexing

• Peripherals Master Synchronous Serial Port supports SPI ™ and

I2C™ Master and Slave Mode

• EUSART module including LIN bus support

• Four Timer Modules

• Up to 2 PWM outputs

Page 15: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards

18F2620 – 18F4620 – 18F8722

All code compatible, just more pins…

28 Pins

40 Pins

80 Pins

Page 16: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards

Topics

• Ham radio applications

• Microcontroller basics

• Hardware design examples

• Implementing your design

• Software tools

• Software examples

Page 17: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards

Minimum Configuration – “Blinky”

PicKit2 Programmer

Power Input

Page 18: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards

19 User Configurable I/O Pins

PicKit2 Programmer

Power Input

6 Analog Input

Or

Digital I/O Pins

13 Digital I/O Pins

Page 19: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards

Many Special Purpose I/O Pins

PicKit2 Programmer

Power Input

6 Analog Input

Or

Digital I/O Pins

Timers

PWM

SPI / I2C

Clock SPI Data In

or I2C Data

SPI data out

UART

Interrupt

PWM

Page 20: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards

Howz That Work?

Page 21: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards

Minimum Configuration – “Blinky”

PicKit2 Programmer

Power Input

Page 22: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards

Let’s add some additional input / output devices

Analog

Inputs

Digital

Inputs

Digital

Outputs

Page 23: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards

Now lets add an RS-232 Interface

RS-232

Interface

Page 24: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards

Topics

• Ham radio applications

• Microcontroller basics

• Hardware design examples

• Implementing your design

• Software tools

• Software examples

Page 25: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards

Design Your Own Boards

Low cost prototype PCB services

Free design tools

Prototype quality – Drilled and plated

Production quality – Drilled, plated,

solder mask, silkscreen

Prototypes…

Minimum order 2 boards

~$120 and up for 2

Or get 3 boards 2.5” x 3.8” for $52

www.expresspcb.com

www.pcbexpress.com

www.pcb123.com

Page 26: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards

Free Schematic Editor

Page 27: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards

Free Layout Editor

Page 28: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards

Use an bare “off the shelf”

Project BoardExamples from ME Labs

Blank Boards

Simple Prototype Boards

Multi-purpose Prototype

Boards

Page 29: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards

Use an “off the shelf”

Project Board with Firmware

SRS TR-1 Board

Page 30: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards

TR1 – Project Board

SRS TR-1 Board

Page 31: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards

TR1 Board

Page 32: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards

TR1 I/O

Block Diagram

Page 33: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards

TR1 Serial Interface

RS-232

RS-485“SierraBus”

Page 34: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards

Topics

• Ham radio applications

• Microcontroller basics

• Hardware design examples

• Implementing your design

• Software tools

• Software examples

Page 35: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards

Software Development Flow

Edit source code

Compile

program

Download into

CPU

Run program

Use any ASCII text editortest.c

Compile or assemble into executable filetest.hex

Use PicKit2 or similar programmer to

transfer the .hex file into the program store

flash memory in the PIC

It’s alive !

Page 36: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards

Compilers – Microchip MPLAB IDE & Assembler

Text editor

Assembler

Debugger

Free !

Page 37: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards

Microchip C18 Compiler Full-featured C compiler

ANSI compliant

Compatibility with object

modules generated by the

MPASM assembler

Extensive library support

• PWM

• SPI

• I2C

• UART, USART

• string manipulation

• math libraries

Free version available

Page 38: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards

Proton Basic Editor CompilerPowerful and easy to

use basic language

• String handling

• Relational operations

• Boolean Logic Operators

• Floating Point Math

• In line assembly

• Graphical LCD support

• Compact Flash read and

write

• Servo motor control

• X10 control

• USB in/out

• I2C

$199

Free lite version available

for some chips

Page 39: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards

Plug in and Download the ProgramOpen the .hex file Hit the “Write” button

Page 40: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards

Topics

• Ham radio applications

• Microcontroller basics

• Hardware design examples

• Implementing your design

• Software tools

• Software examples

Page 41: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards

Software Examples

Option #1

Design your own hardware

Write your own firmware

LED “Blinky” example

Option #2

Use firmware on TR1 board

Write your own PC control

program to control a coax relay

Page 42: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards

All Programs Have a Common

Control Program Structure

Initialize Program

Read Inputs

Make Decisions

Write Outputs

Subroutine Library

Loop forever

Page 43: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards

“Blinky” Written in Proton Basic

' ============================================================================

' blinky.bas

' Blinking LED demo using Proton Basic

' ============================================================================

Device 18F2620 ' Select device type

XTAL = 40 ' Set clock frequency to 40 MHz

CONFIG_START ' Set hardware configuration parameters

OSC = HSPLL ' Set 4x PLL on (10 MHz crystal * 4x = 40 MHz)

CONFIG_END

TRISC.1 = 0 ' Set pin C1 connected to the status LED to be an output

main_loop:

Low PORTC.1 ' Set pin C1 low (0 volts) LED on

DelayMS 200 ' Delay 200 ms

High PORTC.1 ' Set pin C1 high (5 volts) LED off

DelayMS 200 ' Delay 200 ms

GoTo main_loop ' Go to the top of the loop

Page 44: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards

“Blinky” Written in Microchip C18

/* blinky.c - This is a simple program that blinks the status LED */

#include <p18cxxx.h> /* Use PIC18 processor family */

#pragma config WDT = OFF /* Turn off watchdog timer checking */

/************************************************************************/

void delay (void) /* Delay subroutine */

{

long i; /* Declare variable i as a long integer */

for (i = 0; i < 1000; i++); /* Increment the counter */

}

/************************************************************************/

/************************************************************************/

void main (void)

{

TRISC = 0; /* Set IO register C to output mode */

while (1) /* Loop forever */

LATCbits.LATC1 = 0; /* Turn on status LED */

delay (); /* Wait by calling the delay subroutine */

LATCbits.LATC1 = 1; /* Turn off status LED */

delay (); /* Wait by calling the delay subroutine */

}

}

/************************************************************************/

Page 45: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards

SC1 Open Source Projects – Antenna Relay Control

RS232 TR1 BoardLatching

Coax Relay

Sends “//pulse 1” Sends “//pulse 2”

“//pulse 1”

Control program on PC

Page 46: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards

Station Controller

Page 47: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards
Page 48: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards
Page 49: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards

Serial Commands Available in The TR1 Board

OUTxx ON Turn ourput xx on (01-05)

OUTxx OFF Turn output xx off (01-05)

PULSE x Pulse output x for 250 ms.

ENABLE x Enables output x (1-5)

DISABLE x Disables output x (1-5)

HELP Display this help information

STATUS Display the current configuration

LOAD Load configuration from flash memory

SAVE Save configuration to flash memory

RESET Reset operating parameters to standard

default values

REBOOT Reboot the system

STATETRIGGER xxx Updates PC program display if delay is

> xxx (default = 250 ms)

EYECANDY Test mode

ADCALn xxx Ads or subtracts the offset xxx (in milivolts) to

the A/D converter 'n' (1-4)

ADnMIN xxx Sets low trigger alarm voltage to xxx volts

ADnMAX xxx Sets high trigger alarm voltage to xxx volts

RAD Read analog input voltage now

WM ON Turn on basic watt meter monitoring. High

reflected power disables sequencer"

WM OFF Turn off basic watt meter monitoring

WM CUSTOM Turn on watt meter monitoring. Inhibit

sequence when reflected voltage hits

WMLIMIT xxx Minimum reflected voltage that will inhibit the

sequence

BOOT SILENT Turns off boot message

BOOT VERBOSE Turns on boot message

OPMODE xxx Set operating mode: 1 direct station

control (default) or 2 TR Sequencer

mode

EVENT1 x Set event 1...3 to mode 1 pulse, 2 on, 3

off, 4 send update, 5 toggle

KEYER ON Turn on keyer mode. Output 5 will

send CW using the CW command

KEYER OFF Turn off keyer mode. Output 5 will

operate in normal sequencer mode

CW xxxxx Send text xxxxx in CW using output 5 in

keyer mode

WPM xx Set CW speed in approximate words

per minute (1-99)

SETADDR xx Where xx is the board's network

address (between 1 and 15)

GETADDR Get the value of the board's network

address

SILENT ON Do not send messages to serial port

when state changes

SILENT OFF Send messages to serial port when

state changes

MSGDIRECT Set response to plain text format

MSGNET Set response to network packet format

OUT0n ON Manually turn ON output n (1-5)

OUT0n OFF Manually turn OFF output n (1-5)

Page 50: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards

Antenna Relay Control – PC Program (Setup)'===================================================================

' DEFINE VARIABLES AND PARAMETERS

'===================================================================

' Define error handlers

on error goto [errorhandler] 'Go to [errorhandler] if there is a problem

oncomerror [comerror] 'Go to [comerror] if the com port has problems

Com = 16384 'Set com buffer size to 16k

'===================================================================

' OPEN CONFIG FILE coax_relay.env

'===================================================================

'First check to see if the .env file exists in the current directory

CallDLL #ff, "IsFile", "coax_relay.env" As ptr, result As long

If result <> 0 Then

notice "Configuration file (coax_relay.env) not found."

goto [done]

end if

'File exists, open the file.

open ".\coax_relay.env" for input as #11

line input #11, indata$

cport$ = indata$

close #11

'-------------------------------------------------------------------

'Com port parameters

if cport$ = "" then cport$ = "1" 'Assign com port number

cspeed$ = "9600" 'Assign com port baud rate

comport$ = "com" + cport$ + ":" + cspeed$ + ",N,8,1,CS0,RS,DS0”

Page 51: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards

Antenna Relay Control – PC Program (Setup)'===================================================================

' CONFIGURE WINDOW

'===================================================================

win.main.width = 300 : global win.main.width 'Set window width

win.main.height = 100 : global win.main.height 'Set window height

win.main.ULX = 1 : global win.main.ULX 'Set window upper left X coord

win.main.ULY = 1 : global win.main.ULY 'Set window upper left Y coord

'-------------------------------------------------------------------

' SETUP MAIN WINDOW

[WindowSetup]

WindowWidth = win.main.width 'Set window parameters

WindowHeight = win.main.height

UpperLeftX = win.main.ULX

UpperLeftY = win.main.ULY

statictext #main.msg sw.version$, 10, 10, 290, 18 'Print text in window

button #main.radio1, "Radio 1", [radio1], UL, 10, 35, 65, 30 'Define button 1

button #main.radio2, "Radio 2", [radio2], UL, 90, 35, 65, 30 'Define button 2

button #main.exit, "Exit", [done], UL, 170, 35, 65, 30 'Define button 3

Page 52: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards

Antenna Relay Control – PC Program (Application)'===================================================================

' OPEN MAIN WINDOW

'===================================================================

Open "SRS Remote Coax Switch" for Window as #main 'Open the main window

#main "trapclose [Done]"

[mainloop] 'Main program loop

wait

'===================================================================

' SUBROUTINES

'===================================================================

[radio1] 'Go here when button 1 is pressed

print #comm, "//pulse 1" 'Send string to com port

goto [mainloop] 'Go to main loop

[radio2]

print #comm, "//pulse 2"

goto [mainloop]

[comerror]

notice "COM port open failed. Check the coax_relay.env file."

'===================================================================

' SHUTDOWN PROGRAM

'===================================================================

[errorhandler]

[done]

close #main 'Begin shutdown sequence, close windows

close #comm 'Close com port

close #ff 'Close .dll

end 'End program

Page 53: designing ham radio projects with PIC microcontrollers ham radio projects with PIC microcontrollers George Zafiropoulos KJ6VU . ... Arduino Atmel AVR Microchip PIC Chips Modules Boards

Summary

• Easier then you may think

• Many good tools and examples

• Pick a project and jump in

Express PCB www.expresspcb.com

Microchip www.microchip.com

ME Labs www.melabs.com

Proton Basic www.picbasic.org

Sierra Radio www.sierraradio.net