Getting Started with PIC24F/PIC24H Programming and Interfacing in C



Similar documents
An Introduction to MPLAB Integrated Development Environment

Fondamenti su strumenti di sviluppo per microcontrollori PIC

MX PIC24F Educational Module User Manual

PICNet 1. PICNet 1 PIC18 Network & SD/MMC Development Board. Features. Applications. Description

Microcontroller Code Example Explanation and Words of Wisdom For Senior Design

How To Program A Microcontroller Board (Eb064) With A Psp Microcontroller (B064-74) With An Ios 2.5V (Power) And A Ppt (Power Control) (Power Supply) (

Chapter 13. PIC Family Microcontroller

FlowKit in-circuit debug system

SPI. Overview and Use of the PICmicro Serial Peripheral Interface. Getting Started: SPI

ET-BASE AVR ATmega64/128

C Programming Structure of a C18 Program

Evo Laser Firmware Developer s Manual

Processor Extension Pak and Header Specification

PICkit 3 Programmer/Debugger User s Guide

Tutorial for MPLAB Starter Kit for PIC18F

SKP16C62P Tutorial 1 Software Development Process using HEW. Renesas Technology America Inc.

Introduction to PIC Programming

AN-812 APPLICATION NOTE

Palaparthi.Jagadeesh Chand. Associate Professor in ECE Department, Nimra Institute of Science & Technology, Vijayawada, A.P.

PIC-MAXI-WEB development board Users Manual

Section 19. Voltage Reference

Programming PIC Microcontrollers in PicBasic Pro Lesson 1 Cornerstone Electronics Technology and Robotics II

PICmicro tm Development Board

Section 27. Device Configuration Bits

M68EVB908QL4 Development Board for Motorola MC68HC908QL4

Tutorial - Creating Your Own Applications

PC Base Adapter Daughter Card UART GPIO. Figure 1. ToolStick Development Platform Block Diagram

Pololu P-Star 25K50 Micro User s

EasyPIC4 User s Manual

RN-131-PICTAIL & RN-171-PICTAIL Evaluation Boards

Hello, and welcome to this presentation of the STM32L4 reset and clock controller.

Embedded Patient Monitoring System

ETEC Digital Controls PIC Lab 10 Pulse Width Modulation

Lab Experiment 1: The LPC 2148 Education Board

MPLAB ICD 3 In-Circuit Debugger User s Guide For MPLAB X IDE

DKWF121 WF121-A B/G/N MODULE EVALUATION BOARD

RN-131-PICTAIL & RN-171-PICTAIL Web-Server Demo Application

USBSPYDER08 Discovery Kit for Freescale MC9RS08KA, MC9S08QD and MC9S08QG Microcontrollers User s Manual

Peak Power Tracker Circuit Description (from

Opgui and OpenProg user s guide. v.1.2

Using Altera MAX Series as Microcontroller I/O Expanders

DsPIC HOW-TO GUIDE Creating & Debugging a Project in MPLAB

16-bit Microcontrollers. High-Performance PIC24 Microcontroller Family.

Programming the PIC18/ XC8 Using C- Coding Dr. Farahmand

BE635 User Manual. Rev. V Bolymin, Inc. All Rights Reserved.

Section 7. Oscillator

LED board datasheet EB

Lab 1 Course Guideline and Review

Welcome to the tutorial for the MPLAB Starter Kit for dspic DSCs

AN1229. Class B Safety Software Library for PIC MCUs and dspic DSCs OVERVIEW OF THE IEC STANDARD INTRODUCTION

K8048 PIC PROGRAMMER BOARD

Focus Product Selector Guide

Voltage Measurement with A PIC Microcontroller

Wireless Temperature

Using Xbee in Serial Communication

QL-PROG Microchip MCU Programmer User s Manual Qianlongsheng Electronic Technology Co., Ltd.

MPLAB C18 C Compiler

Data Sheet. Adaptive Design ltd. Arduino Dual L6470 Stepper Motor Shield V th November L6470 Stepper Motor Shield

ENGI E1112 Departmental Project Report: Computer Science/Computer Engineering

Lecture N -1- PHYS Microcontrollers

Measuring Resistance Using Digital I/O

FLYPORT Wi-Fi G

Introduction. Getting familiar with chipkit Pi

How To Use Microchip.Com

Software Real Time Clock Implementation on MC9S08LG32

POCKET SCOPE 2. The idea 2. Design criteria 3

Develop a Dallas 1-Wire Master Using the Z8F1680 Series of MCUs

Switch board datasheet EB

Analog Devices Welcomes Hittite Microwave Corporation NO CONTENT ON THE ATTACHED DOCUMENT HAS CHANGED

Block 3 Size 0 KB 0 KB 16KB 32KB. Start Address N/A N/A F4000H F0000H. Start Address FA000H F8000H F8000H F8000H. Block 2 Size 8KB 16KB 16KB 16KB

Embedded Web Server for Infra Red Remote Control

c0003 A Simple PIC Application CHAPTER 3

USB OTG and Embedded Host Microchip Technology Incorporated. All Rights Reserved. Slide 1

Section 29. Real-Time Clock and Calendar (RTCC)

Analog to Digital Conversion of Sound with the MSP430F2013

MPLAB Harmony System Service Libraries Help

DRV8312-C2-KIT How to Run Guide

Hello, welcome to this presentation of the low power timer, or LPTMR, module for Kinetis MCUs. In this session you ll learn about the LPTMR, it s

Accurate Measurement of the Mains Electricity Frequency

RC2200DK Demonstration Kit User Manual

PICkit 2 Microcontroller Programmer USER S GUIDE

Microcontroller Based Low Cost Portable PC Mouse and Keyboard Tester

Controlling a Dot Matrix LED Display with a Microcontroller

Bluetooth to serial HC-06 wireless module

USER GUIDE EDBG. Description

AVR Butterfly Training. Atmel Norway, AVR Applications Group

Real-Time Clock. * Real-Time Computing, edited by Duncan A. Mellichamp, Van Nostrand Reinhold

Web Streamed SDR s in Service at GB2RHQ Hack Green Bunker

Arduino Due Back. Warning: Unlike other Arduino boards, the Arduino Due board runs at 3.3V. The maximum. Overview

2.0 Command and Data Handling Subsystem

Computer Organization and Components

AN3252 Application note

Z8 Encore! XP F082A Series Development Kit

dspic30f dspic30f Flash Programming Specification 1.0 OVERVIEW AND SCOPE 2.0 PROGRAMMING OVERVIEW OF THE dspic30f 2.1 Hardware Requirements

Focus Product Selector Guide

Murdoch University. Engineering Internship Report ENG450. Condition Monitoring of Points Machine motors using a PIC microcontroller.

C Programming. for Embedded Microcontrollers. Warwick A. Smith. Postbus 11. Elektor International Media BV. 6114ZG Susteren The Netherlands

PHYS 2P32 Project: MIDI for Arduino/ 8 Note Keyboard

ONLINE HEALTH MONITORING SYSTEM USING ZIGBEE

A 5 Degree Feedback Control Robotic Arm (Haptic Arm)

Transcription:

Getting Started with PIC24F/PIC24H Programming and Interfacing in C This series of short articles covers the basics of programming a PIC24FJ32GA002/PIC24H 16-bit microcontroller, using Microchip s free C30 compiler. These articles assume a very basic understanding of Microchip microcontrollers and development tools, especially MPLAB IDE. A number of projects will be presented, which are based upon using a low cost PIC24F/H board as the main platform, Microchip s free C30 C compiler, and a low cost PICKIT2 (or PICKIT3) used for programming and debugging function. S Gupta www.mcjournal.com Page 1

Project 1 Hello World Turning On a LED Introduction This article covers the traditional Hello World program that turns on a LED connected to one of the I/O pins of a PIC24FJ32GA002 microcontroller. The article provides the interface diagram, as well as project files, containing C30 code. The LED Interface The LED is connected to RA0 pin (pin#2 on the PIC24F board). A 330 Ω resistor is used as a series dropping resistor to limit the current. Power to the circuit board is drawn from the USB interface, which supplies 3.3 V to PIC24F. The current drawn through the LED is given by the following expression LED_Current x R + LED_Vdrop = 3.3 V Assuming a LED drop of 1.5 V, LED_Current = = 5.5 ma OK If a higher brightness is desired, a smaller resistor value can be used. S Gupta www.mcjournal.com Page 2

Figure 1 Schematic Diagram - Hello World Operation (See the code below). The code starts by first configuring the chip. There are two configuration registers CW1 and CW2, which are present in Flash program memory. These registers control the physical characteristics of the microcontroller unit (MCU).. Once programmed, these register values are retained till the next power-on-rest (POR) operation. These registers are outside the user program memory space and can only be accessed through Table read and write instructions. C30 along with MPLAB provides convenient _Config statements that allow the configuration to be set after a POR. CW1 This register controls the JTAG interface, Watch Dog Timer (WDT), WDT prescaler (if WDT is enabled), and ICD interface. FWDTEN: This bit enables (or disables) the watch dog timer and can only be programmed can only be modified after the next POR. The bit is set by default, which enables the WDT. For this development mode the WDT has been disabled through FWDTEN_OFF statement in _CONFIG1. DEBUG: This bit enables (or disables) Debug mode. If set, the device enters the debug mode after a POR and if cleared, the device enters operational mode without getting into Debug. The bit is also set by default, which enables the debug mode. For this development mode the WDT has been disabled through BKBUG_ON statement in _CONFIG1. S Gupta www.mcjournal.com Page 3

ICS1:ICS0: These two bits control the pins, which are used for ICD interface (EMUC: EMUD and PGC:PGD). PIC24FJ32GA002 has 3 possibilities for ICD interface (see pin diagram). These are: ICS1:ICS0 = 1:z Use PGC1: PGD1 ICD interface available on RB1 and RB0 ICS1:ICS0 = 1:0 Use PGC2: PGD2 ICD interface on RB10 and RB11 ICS1:ICS0 = 0:1 Use PGC3: PGD3 ICD interface on RB6 and RB5 The PIC24F board is set to use PGC1:PGD1 interface is used. It is configured through ICS_PGx1 statement in _CONFIG1. The Code The code is shown below. The configuration bits are used to set the operating mode of the microcontroller. // ----------------------------------- Start of Code -------------------------------------------------- // SG // April 17, 2011 // PIC24FJ32GA002 // PIC24FJ32_LED_1.c #include <p24fj32ga002.h> // ************************************************************************ // Configuration Bits // ************************************************************************/ _CONFIG1 (JTAGEN_OFF & BKBUG_ON & ICS_PGx1 & FWDTEN_OFF) // JTAG disabled // Background debug on // Communication Channel: PGC1/EMUC1 and PGD1/EMUD1 // Watchdog Timer disabled _CONFIG2 (FNOSC_FRC) // Select FRC Oscillator // ============================================================= // int main (void) // ============================================================= int main (void) { OSCTUN = 0; // Tune FRC oscillator, if FRC is used RCONbits.SWDTEN = 0; // Disable Watch Dog Timer // Make AN0 (RA0) as digital others as analog AD1PCFG = 0b00000000000001; S Gupta www.mcjournal.com Page 4

TRISAbits.TRISA0 = 0; LATAbits.LATA0 = 1; // Set RA0 as an output for LED // Turn on the LED while(1); // Stay here for now return (0); } // ----------------------------------- End of Code -------------------------------------------------- Code Dissection The various sections of the codes are now briefly explained. // ----------------------------------- Start of Code -------------------------------------------------- // SG // April 17, 2011 // PIC24FJ32GA002 // PIC24FJ32_LED_1.c The above are the comments used to provide basic information. // style commenting is used here that indicates that all text that follows is to be ignored by compiler. #include <p24fj32ga002.h> This (pre-processor) statement indicates to the compiler that register names and bits that are used in the program have been declared in the indicated header file (essentially a text file that can be opened and viewed, but should not be modified). _CONFIG1 (JTAGEN_OFF & BKBUG_ON & ICS_PGx1 & FWDTEN_OFF) // JTAG disabled // Background debug on // Communication Channel: PGC1/EMUC1 and PGD1/EMUD1 // Watchdog Timer disabled _CONFIG2 (FNOSC_FRC) // Select FRC Oscillator These statements are required to configure the operation of microcontroller. The configuration bits are located in a reserved area of Flash memory. Some of the critical aspects of the operation that are configured here are: Select internal FRC oscillator Turn off the JTAG interface available on PIC24 Enable Debugger interface Set the debugger interface on pins corresponding to PGC1 and PGD1 Disable the Watchdog Timer Refer to PIC24FJ32GA002 data sheet at www.microchip.com for additional details. S Gupta www.mcjournal.com Page 5

int main (void) The main function declared as return an integer (common format). OSCTUN = 0; // Tune FRC oscillator, if FRC is used Use factory calibrated FRC oscillator setting RCONbits.SWDTEN = 0; // Disable Watch Dog Timer The Watchdog timer can be enabled in software as well. This statement ensures that software Watchdog timer is not enabled AD1PCFG = 0b00000000000001; By default, the microcontroller pins that can be used for analog input are configured as analog pins and digital Input/output is disabled. This statement makes RA0 to be used for digital Input/output and the remaining pins are left for analog inputs (default). TRISAbits.TRISA0 = 0; // Set RA0 as an output for LED Once the RA0 pin is set for digital Input/output, its direction for actual usage needs to be set. In this case the pin is used for controlling the voltage to the LED, so it is configured as an output. LATAbits.LATA0 = 1; // Turn on the LED This statement simply set the voltage level on RA0 pinto high, enabling the LED to be turned on. while(1); As the program runs in a linear fashion (from top to bottom), eventually it will reach the terminating statement in main (), which will terminate the user program (main() function). C compiler places an indefinite loop in the program so that on termination the main is called again, essentially causing the program to reset. This can be troublesome and it a while (1); statement prevents the termination of the program and prevent unnecessary program reset.. return (0); The terminating statement in main () function indicates that a 0 is being returned (common in computer C programs) and program terminated normally. Reference: PIC24FJ64GA004 Family Data Sheet www.microchip.com S Gupta www.mcjournal.com Page 6