Introducing the 68HC12

Download Report

Transcript Introducing the 68HC12

Examples 1 - 4
Lecture L2.2
Example 1a
// Example 1a: Turn on every other segment on 7-seg display
#include <hidef.h>
/* common defines and macros */
#include <mc9s12dp256.h>
/* derivative information */
#include "main_asm.h"
/* interface to the assembly module */
#pragma LINK_INFO DERIVATIVE "mc9s12dp256b"
void main(void) {
PLL_init();
// set system clock frequency to 24 MHz
DDRH = 0xff;
// Port H is output
PTH
= 0x55;
// switch on every other segment
for(;;) {}
/* wait forever */
}
Example 1b
// Example 1b: 7-Segment Display
#include <hidef.h>
/* common defines and macros */
#include <mc9s12dp256.h>
/* derivative information */
#include "main_asm.h" /* interface to the assembly module */
#pragma LINK_INFO DERIVATIVE "mc9s12dp256b"
void main(void) {
PLL_init();
seg7_enable();
seg7_on(0x55);
// set system clock frequency to 24 MHz
// enable 7-segment display
// switch on every other segment
for(;;) {} /* wait forever */
}
Example 1
Example 1: C function calls for turning on the 7-segment display
C Function Call
Meaning
void seg7_enable();
Sets DDRH to outputs and clears all segments
void seg7_on(int);
Stores the lower 8 bits of the integer int in Port H
Example 2a
// Example 2a: Blinking 7-Segment Display
#include <hidef.h>
/* common defines and macros */
#include <mc9s12dp256.h>
/* derivative information */
#include "main_asm.h"
/* interface to the assembly module */
#pragma LINK_INFO DERIVATIVE "mc9s12dp256b"
void delay(void);
void main(void) {
seg7_enable();
while(1){
seg7_on(0xFF);
delay();
seg7_on(0x00);
delay();
}
}
// enable 7-segment display
// switch on all segments
// switch off all segments
void delay() {
int i,j;
for(i = 0; i < 500; i++) {
for(j = 0; j < 5999; j++) {
}
}
}
Example 2b
// Example 2b: Blinking 7-Segment Display - ms_delay(ms)
#include <hidef.h>
/* common defines and macros */
#include <mc9s12dp256.h>
/* derivative information */
#include "main_asm.h"
/* interface to the assembly module */
#pragma LINK_INFO DERIVATIVE "mc9s12dp256b"
void main(void) {
PLL_init();
// set system clock frequency to 24 MHz
seg7_enable();
// enable 7-segment display
while(1){
seg7_on(0xFF);
// switch on all segments
ms_delay(500);
// half-second delay
seg7_on(0x00);
// switch off all segments
ms_delay(500);
// half-second delay
}
}
Example 2
Example 2: C function call delaying n milliseconds
C Function Call
Meaning
void ms_delay(int n);
Delay n milliseconds
Example 3a
// Example 3a: 7-Segment Decoder - C version
#include <hidef.h>
/* common defines and macros */
#include <mc9s12dp256.h>
/* derivative information */
#include "main_asm.h"
/* interface to the assembly module */
#pragma LINK_INFO DERIVATIVE "mc9s12dp256b"
void delay(void);
void main(void) {
const char seg7tbl[] = {
0x3F,0x06,0x5B,0x4f,
0x66,0x6D,0x7D,0x07,
0x7F,0x6F,0x77,0x7C,
0x39,0x5E,0x79,0x71
};
int i;
Example 3a (cont.)
PLL_init();
// set system clock frequency to 24 MHz
seg7_enable();
// enable 7-segment display
while(1){
for(i = 0; i < 16; i++) {
PTH = seg7tbl[i];
delay();
}
}
}
void delay() {
int i,j;
for(i = 0; i < 500; i++) {
for(j = 0; j < 6248; j++) {
}
}
}
Example 3b
// Example 3b: 7-Segment Decoder - seg7dec(i)
#include <hidef.h>
/* common defines and macros */
#include <mc9s12dp256.h>
/* derivative information */
#include "main_asm.h" /* interface to the assembly module */
#pragma LINK_INFO DERIVATIVE "mc9s12dp256b"
void main(void) {
int i;
PLL_init();
// set system clock frequency to 24 MHz
seg7_enable();
// enable 7-segment display
while(1){
for(i = 0; i < 16; i++) {
seg7dec(i);
ms_delay(500);
}
}
}
Example 3
Example 3: C function call for displaying hex digits
C Function Call
Meaning
void seg7dec(int i);
Display the hex digit i on the 7-segment display
Example 4a
// Example 4a: Single segments in 7-Segment Display
#include <hidef.h>
/* common defines and macros */
#include <mc9s12dp256.h>
/* derivative information */
#include "main_asm.h" /* interface to the assembly module */
#pragma LINK_INFO DERIVATIVE "mc9s12dp256b"
void delay(void);
void main(void) {
PLL_init();
seg7_enable();
// set system clock frequency to 24 MHz
// enable 7-segment display
Example 4a (cont.)
while(1){
PTH |= 0x01;
delay();
PTH |= 0x40;
delay();
PTH |= 0x08;
delay();
PTH &= 0xFE;
delay();
PTH &= 0xBF;
delay();
PTH &= 0xF7;
delay();
}
}
// turn on segment a
// turn on segment g
// turn on segment d
// turn off segment a
// turn off segment g
// turn off segment d
void delay() {
int i,j;
for(i = 0; i < 500; i++) {
for(j = 0; j < 6248; j++) {
}
}
}
Example 4b
// Example 4b: Single segments in 7-Segment Display
#include <hidef.h>
/* common defines and macros */
#include <mc9s12dp256.h>
/* derivative information */
#include "main_asm.h" /* interface to the assembly module */
#pragma LINK_INFO DERIVATIVE "mc9s12dp256b"
void main(void) {
/* put your own code here */
seg7_enable();
// enable 7-segment display
Example 4b (cont.)
while(1){
PTH_HI(0);
ms_delay(500);
PTH_HI(6);
ms_delay(500);
PTH_HI(3);
ms_delay(500);
PTH_LO(0);
ms_delay(500);
PTH_LO(6);
ms_delay(500);
PTH_LO(3);
ms_delay(500);
}
}
//
//
//
//
//
//
//
//
//
//
//
//
turn on segment a
half-second delay
turn on segment g
half-second delay
turn on segment d
half-second delay
turn off segment a
half-second delay
turn off segment g
half-second delay
turn off segment d
half-second delay
Example 4
Example 4: C function calls for turning on or off a single bit of PTH
C Function Call
Meaning
void PTH_HI(int b);
Sets bit b of PTH high
void PTH_LO(int b);
Sets bit b of PTH low