z8 Ansi C Programming

31
Ansi C programming

description

z8 Ansi C Programming

Transcript of z8 Ansi C Programming

Page 1: z8 Ansi C Programming

Ansi C programming

Page 2: z8 Ansi C Programming

Write Code Compile Download

to device

Page 3: z8 Ansi C Programming

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

Page 4: z8 Ansi C Programming

#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.

Page 5: z8 Ansi C Programming

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

Page 6: z8 Ansi C Programming

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

}

Page 7: z8 Ansi C Programming

//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

Page 8: z8 Ansi C Programming

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.

Page 9: z8 Ansi C Programming

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

Page 10: z8 Ansi C Programming

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.

Page 11: z8 Ansi C Programming

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

Page 12: z8 Ansi C Programming

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.

Page 13: z8 Ansi C Programming

PAADDR = 0x03;PACTL = 0x00;

• PxADDR selects the output control register

• PACTL = 0x00;

– 0 = push pull1 = open drain

Page 14: z8 Ansi C Programming

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

Page 15: z8 Ansi C Programming

PAADDR = 0x00;

• Sets PAADDR to a dummy control register

– Prevents it from being accidentally written to

Page 16: z8 Ansi C Programming

PAOUT = leds;

• Move the data in leds to the Port A OUTput register

– Basically write data to the port

Page 17: z8 Ansi C Programming

return 0;

• Ends the function

• Tells the CPU to return the value 0 to the calling function

Page 18: z8 Ansi C Programming

Things to watch for

Page 19: z8 Ansi C Programming

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

Page 20: z8 Ansi C Programming

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?

Page 21: z8 Ansi C Programming

Program counter and stack pointer

Page 22: z8 Ansi C Programming

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

Page 23: z8 Ansi C Programming

Program control

• Download program to the Z8

• Restart the program

• Run the program

• Toggle breakpoint

• Enable/Disable a breakpoint

• Remove all breakpoints

Page 24: z8 Ansi C Programming

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

Page 25: z8 Ansi C Programming

Supporting Information

Page 26: z8 Ansi C Programming

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

Page 27: z8 Ansi C Programming

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.

Page 28: z8 Ansi C Programming

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

Page 29: z8 Ansi C Programming

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

Page 30: z8 Ansi C Programming
Page 31: z8 Ansi C Programming

USEFUL Documents

– PS0199

– UM130