Chapter 1: Hello Embedded World - Programming 16-bit
Download
Report
Transcript Chapter 1: Hello Embedded World - Programming 16-bit
Chapter 1
The First Flight
Creating the first project and saying
“Hello to the Embedded World”
Part 2
Debugging and the Watches window
Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition)
I/O ports
Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition)
Debugging PORTA
int main( void)
{
PORTA = 0xff;
TRISA = 0;
return 0;
}
// all PORTA pins output
Debugging
functions:
Build Project for debugging and enter Debug Mode:
Exit Debug Mode
Step Over
Run (Debug Mode)
Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition)
Configuring the PIC24
/*
** config.h
**
** "Flying PIC24" projects device configuration
*/
#include <xc.h>
_CONFIG1(
&
&
&
&
JTAGEN_OFF
GCP_OFF
GWRP_OFF
ICS_PGx2
FWDTEN_OFF)
//
//
//
//
//
disable JTAG interface
disable general code protection
disable flash write protection
ICSP interface (2=default)
disable watchdog timer
_CONFIG2(
&
&
&
IESO_OFF
FCKSM_CSDCMD
FNOSC_PRIPLL
POSCMOD_XT)
//
//
//
//
two speed start up disabled
disable clk-swithcing/monitor
primary oscillator: enable PLL
primary oscillator: XT mode
Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition)
Creating an Include folder
Create a folder called “include” at the top
level of your working directory (Flyingpic24)
Save the config.h file in it
Example:
FlyingPIC24/
include/
config.h
1-HelloWorld.X/
Hello1.c
nbproject/
...
Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition)
To calculate a path, start from
the nbproject folder:
../../include
Add the Include Path
Add the Include Path to the project properties
1-Select the xc16-gcc options
2-Select the Preprocessing and
Messages options
Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition)
3-Provide a path to the Include
folder
Hello2.c – Using config.h
/*
** Hello Embedded World
**
** Hello2.c controlling PORTA pin direction
*/
#include <config.h>
// set the configuration bits
int main( void)
{
PORTA =
0xff;
TRISA =
0;
return 0;
}
// configure all PORTA pins as outputs
Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition)
Hello3.c – Using Port B
/*
** Hello Embedded World
**
** Hello3.c Testing PORTB, another surprise!
*/
#include <config.h>
int main( void)
{
PORTB =
0xff;
TRISB =
0;
return 0;
}
// configure all PORTB pins as outputs
Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition)
AD1PCFG register
Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition)
Hello4.c – Port B under Control
/*
** Hello Embedded World
**
** Hello4.c learning to control the Analog Pins
*/
#include <config.h>
int main( void)
{
PORTB
= 0xff;
AD1PCFG = 0xffff;
TRISB
= 0;
return 0;
}
// all PORTB as digital
// all PORTB as output
Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition)
Notes for Assembly Experts
You can still take a look at the assembly code
generated by the compiler
Learn to trust it!
Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition)
Meet the Dashboard
All the information about your project at a
glance
Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition)
Tips and Tricks
Interfacing to 5V input and output signals is
possible with some caution:
Digital Input pins are 5V tolerant
Digital Output pins can be configured as Open Drain
Use the ODCx registers to configure an output pin for
Open Drain mode.
Watch Out! Pins that are multiplexed with analog
functions are NOT 5V tolerant!
Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition)
Suggested Excercises
To test the PORTB example (Hello4.c):
Connect a Voltmeter to pin RB0
Watch the needle move as you single step through the code.
Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition)
Recommended Readings
Kernighan, B. & Ritchie, D.
The C Programming Language
Prentice-Hall, Englewood Cliffs, NJ
When you read or hear a programmer talk
about the “K&R” … they mean this book!
Also known as “the white book”, the C
language has evolved quite a bit since the
first edition was published in 1978!
The second edition (1988) includes the
more recent ANSI C standard definitions
of the language
The MPLAB C32 compiler adheres to the
ISO/IEC 9899:1990 (also known as C90)
standard
Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition)
Online Resources
http://en.wikibooks.org/wiki/C_Programming
This is a Wiki-book on C programming and as such it
is a bit of a work in progress. It’s convenient if you
don’t mind doing all your reading online.
Hint: look for the chapter called “A taste of C” to find
the omnipresent “Hello World!” example.
http://publications.gbdirect.co.uk/c_book/
This is the online version of The C Book, second
edition by Mike Banahan, Declan Brady and Mark
Doran, originally published by Addison Wesley in
1991. This version is made freely available.
Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition)