z8 ansi c programming
Post on 26-Dec-2015
64 Views
Preview:
DESCRIPTION
TRANSCRIPT
Ansi C programming
Write Code Compile Download
to device
Looking at our first code#include <ez8.h>int main(void){ //puts 0x05 to the LED’s
unsigned char leds = 0x05;PAADDR = 0x02;PACTL = 0x00;PAADDR = 0x01;PACTL = 0xF8;PAADDR = 0x03;PACTL = 0x00;PAADDR = 0x00;PAOUT = leds;return 0;
}
• This code sets up port A as on output, and controls the LED’s on the Z8 development board
#include <ez8.h>
• # – indicates that this instruction is done first when compiling
the project (preprocessor).
• #include – adds the contents of a file to your code.
– The angle brackets < > mean use the default path
• #include <ez8.h>– Includes the file ez8.h. This file defines all the hardware
names and locations used on the Z8.
int main(void)
• int
– Means this function returns an integer value when it is done.
• main
– The main function is defined in Ansi C as the start of your program.
• ()
– Holds the data being passed to your function
• (void)
– Means you pass nothing to the function
Curly braces, { }
• { }
– Curly braces denote a code block
– All instructions inside the block are treated as one unit.
– Used with functions main() { }
– Used with conditional code for() { }
{// insert your code here
}
//puts 0x05 to the LED’s
• //
– Double slashes start an inline comment
– Comment lasts until the end of the line
– This text is ignored by the compiler
• example
// this is comment // just a regular comment
#include <ez8.h> // adds ez8.h to the source code
//#include <ez8.h> // removes this line of code
//************** // often used as a separator
unsigned char leds = 0x05;
Reserves an 8 bit memory location
unsigned (all bits used for data)
char stands for the character data type
leds is the variable name given to the reserved space
= assigns the value 0x05 to the variable
; ends the instruction
The z8 ports are 8 bits wide, just like a character.
PAADDR = 0x02;
• Read it as the data in Port A ADDRess register
• PAADDR is a mnemonic
– Defined in ez8.h, and it is hardware specific
– Corresponds to a specific memory location in hardware.
– The mnemonic are actually text strings defined as pointers to data at specific address.
• PAADDR, PACTL, PAIN, PAOUT all do similar functions
PAADDR = 0x02;PACTL = 0x00;
• PAADDR = 0x02;– PxADDR selects which
port control register is being used
• PACTL = 0x00;– PxCTL is the data for the
port control register0000 00001 = alternate function0 = general purpose
• Together these commands turn the alternate function off for each bit.
Port Control RegistersPxADDR Register Bit Function
0x00 None
0x01 Data direction 1 = input0 = output
0x02 Alternate function 1 = alternate function0 = General purpose I/O
0x03 Output control 1 = Open drain (only low)0 = Push pull (high and low)
0x04 High drive enable 1 = high current0 = low current
0x05 Stop mode recovery 1 = restart program on change0 = ignored
PAADDR = 0x01;PACTL = 0xF8;
• PxADDR selects the data direction register
• PACTL = 0xF8;
– 1111 1000
– 1 = Input
– 0 = Output
• Together these commands set bits 2-0 as outputs.
PAADDR = 0x03;PACTL = 0x00;
• PxADDR selects the output control register
• PACTL = 0x00;
– 0 = push pull1 = open drain
Push pull vs open drain
Push pull– Can drive output high or low
Open drain
• The top transistor is disabled– Can only drive the output low
– Requires pull up resistors to work
PAADDR = 0x00;
• Sets PAADDR to a dummy control register
– Prevents it from being accidentally written to
PAOUT = leds;
• Move the data in leds to the Port A OUTput register
– Basically write data to the port
return 0;
• Ends the function
• Tells the CPU to return the value 0 to the calling function
Things to watch for
Errors
• Spelling errors
– Inspect your code for typos and swapped letter
• Syntax errors
– Look for missing semicolons ;
– Look for missing brackets and braces ( ) { }
– Verify the data being passed to functions
• Check your compiler settings
– Are your ports enabled in the compiler
Single step through you program
• What is the state of the LED’s as each line of your program.
– When do the LED’s come on?
– When do the LED’s turn off?
– When do the LED’s get set?
Program counter and stack pointer
Insert breakpoint
A breakpoint is a place where execution stops so that the programmer can check what is going on
Put the cursor at the desired line, and insert a breakpoint
Program control
• Download program to the Z8
• Restart the program
• Run the program
• Toggle breakpoint
• Enable/Disable a breakpoint
• Remove all breakpoints
Moving through code
• From left to right
– Run code
– Run to cursor
– Break (halt execution)
– Step into a function
– Step over a function
– Step out of a function
– Set next instruction at the cursor
Supporting Information
Standard ANSI C Headers
• <assert.h> Diagnostics• <ctype.h> Character-handling functions• <errno.h> Error numbers• <float.h> Floating-point limits• <limits.h> Integer limits• <math.h> Math functions• <setjmp.h> Nonlocal jump functions• <stdarg.h> Variable arguments functions• <stddef.h> Standard defines• <stdio.h> Standard input/output functions• <stdlib.h> General utilities functions• <string.h> String-handling functions
Type Sizes
• The type sizes for the basic data types on the Z8 Encore! C-Compiler are as follows:
• Int 16 bits• short int 16 bits• Char 8 bits• Long 32 bits• Float 32 bits• Double 32 bits
• The type sizes for the pointer data types on the Z8 Encore! C-Compiler are as follows:
• near pointer 8 bits• far pointer 16 bits• rom pointer 16 bits• All data are aligned on a
byte boundary.
Z8 Encore! Specific Header Files
• <eZ8.H>
– C library macros and functions
– Mnemonics (PAADDR,PACTL,PAIN,PAOUT…)
• <sio.h>
– input/output macros and functions.
– Used serial for communication
unsigned char leds = 0x05;
Data types used in C are
• Character
– 8 bit data, typically one letter of text
• Integer
– 16 bits of data, 0 – 65535 or -32768 to +32767
• Floating point number
• Double length floating point number, NOT SUPPORTED
• Each of these data types can be either signed or unsigned
USEFUL Documents
– PS0199
– UM130
top related