Rate the eM8 Part 2
By Fred Eady
You can’t build robot smarts if you can’t speak the language. Or, in this case,
speak the languages. We’re about to continue our port of the eM8 assembler
application to CCS C. Although this is a tutorial of sorts, we’re also forming a
picture of the eM8 hardware as we translate the assembler source code. When
we ran out of paper last time, we had just finished porting the PIC16F882
definitions and SRAM allocations. At this point, we’re ready to port some eM8
executable code.
Start
Assembler-based embedded programs don’t have to
house their code inside the braces of the C main function.
However, from a porting standpoint, the assembler code
behind the START label will be rounded up and placed
within a pair of CCS C braces. We don’t have to port the
RESET code, but I thought it might be a good idea to show
you how the assembler application kicks off following a
microcontroller reset:
;——————————————————————————————————————
; Start Of Program after Reset
;——————————————————————————————————————
RESET ORG 0x000
GOTO START
; processor reset
; vector
; go to beginning of
; program
The interrupt handler code lies between the RESET
label and the START label in the original eM8 source. We’ll
deal with the interrupt handler port in due time. For now,
let’s dig into the oscillator setup:
START
;******* Set Oscillator Factoty Correction
BANKSEL OSCTUNE ; select bank 1
; using mpasm macro
MOVLW 0x00 ; set oscillator to
; factory calibrated freq
OSCTUNE
OSCCON
OSCCON,IRCF0
; set osc to 8M operation
BANKSEL STATUS ; select bank 0 using mpasm
; macro
MOVWF
BANKSEL
BSF
clrf
clrf
UF
CF
We already know that the PIC16F882 is clocked from
its internal oscillator. So, according to the PIC16F882
datasheet, the OSCTUNE register will reset with a value of
38 SERVO 12.2010
0x00 which does not apply any frequency correction to the
default clock frequency of 4 MHz. At this point, there is no
reason to adjust the instruction clock frequency. However,
we do need to set up the internal oscillator to run at 8
MHz. As for setting the internal oscillator base frequency,
CCS C provides a couple of ways for us to specify the
instruction execution rate. The first method is found in the
em8.h include file that is created by the CCS C PIC Wizard:
#use delay(clock=8000000)
The CCS C #use delay directive sets the necessary bits
in the associated SFRs (Special Function Registers) to select
an internal oscillator speed of 8 MHz. As you can see in the
eM8 assembler source, bit IRCF0 in SFR OSCCON is set to
complete the bit pattern necessary to initiate 8 MHz
internal oscillator operation. Here’s method number two
which happens to be a CCS C built-in function that resides
inside of the C main function:
setup_oscillator(OSC_8MHZ);
It doesn’t hurt to specify the internal oscillator
frequency more than once. However, the most recently
encountered frequency statement sets the operating clock
speed.
The eM8 application also clears the flags in this
assembler code snippet and so will we. The original eM8
assembler source allocated an eight-bit SRAM location for
each set of flags. We did the same but in a CCS C kind of
way. The results of clearing the UF and CF flag structures
are shown in Screenshot 1. The CCS C code that cleared
the bits in Screenshot 1 looks like this:
UF = 0;
CF = 0;
The eM8 assembler source makes provisions for
Versions 1 and 2: