AES Lab(HCS12)
SWITC
HING
LED(s)
2016-17
AES Lab(HCS12)
Learning Objectives:
This module will help in understanding how to Blink LED’s of MC9S12HY64
microcontroller board along with writing a delay program
Success Criteria:
When you complete this module, you will be able to write a code to blink LED’s of
MC9S12HY64 microcontroller board
Requirements:
Code warrior installed basic knowledge of C and MC9S12HY64 board.
LED 1 PTR_PTR0
LED 2 PTR_PTR1
LED 3 PTR_PTR2
LED 4 PTR_PTR3
2016-17
AES Lab(HCS12)
Experiment 1
Aim: Switching the LED ON and OFF One Time
Program:
#include <hidef.h>
#include "derivative.h"
void main(void)
{
int x,y;
DDRR=0x0F; // Data Direction Register for Port R as Input
PTR=0X00; // Port R is ON
for(x=0;x<1000;x++)
for(y=0;y<1000;y++); // Delay
PTR=0X0F; // Port R is OFF
for(x=0;x<1000;x++)
for(y=0;y<1000;y++); // Delay
PTR=0X00; // Port R is ON
}
2016-17
AES Lab(HCS12)
Experiment 2
Aim: Switching the LED ON and OFF Infinitely
Program:
#include <hidef.h>
#include "derivative.h"
void main(void)
int x,y;
DDRR=0x0F; // Data Direction Register for Port R as Input
PTR=0X00; // Port R is ON
while(1)
for(x=0;x<1000;x++)
for(y=0;y<1000;y++); // Delay
PTR=0X0F; // Port R is OFF
for(x=0;x<1000;x++)
for(y=0;y<1000;y++); // Delay
PTR=0X00; // Port R is ON
Experiment 3
2016-17
AES Lab(HCS12)
Aim: Switching the LED ON and OFF Ten Times
Program:
#include <hidef.h>
#include "derivative.h"
void main(void)
int i,x,y;
DDRR=0x0F; // Data Direction Register for Port R as Input
PTR=0XFF; // Port R is OFF
for(i=0;i<10;i++) // Loop for 10 times execution
for(x=0;x<1000;x++)
for(y=0;y<1000;y++); // Delay
PTR=0X0F; // Port R is OFF
for(x=0;x<1000;x++)
for(y=0;y<1000;y++); // Delay
PTR=0X00; // Port R is ON
2016-17
AES Lab(HCS12)
SWITCH
INTERFACING
Overview:
2016-17
AES Lab(HCS12)
Turning ON the digital LED’s using analog switches
Learning Objectives:
This module will help in understanding how to turn ON the digital LED’s using analog
switches by configuring ports for switches and LED’s along with enabling the pull-up
registers and enabling Analog to Digital Input register
Success Criteria:
When you complete this module, you will be able to write a code to control LED’s of
MC9S12HY64 microcontroller board with the switches present on it
Requirements:
Code warrior installed basic knowledge of C and MC9S12HY64 board.
SWITCH 1 PT1AD_PT1AD4
SWITCH 2 PT1AD_PT1AD5
SWITCH 3 PT1AD_PT1AD6
SWITCH 4 PT1AD_PT1AD7
Experiment 1
2016-17
AES Lab(HCS12)
Aim: Switching the LED using input from Switches
Program:
#include <hidef.h> /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
void main(void)
{
DDRR=0xFF; // Data Direction Register Port R as Output
DDR1AD=0x00; // Data Direction Register Port AD as Input
ATDDIEN=0xF0; // Analog to Digital Input Enable Register is Enabled
PER1AD=0XFF; // Port AD PULL UP enable register is Enabled
PTR=0XFF; // Port R is OFF
while(1)
{
if(PT1AD_PT1AD4==0)
{
PTR=0x0E;
}
if(PT1AD_PT1AD5==0)
{
PTR=0x0D;
}
if(PT1AD_PT1AD6==0)
{
PTR=0x0B;
}
if(PT1AD_PT1AD7==0)
{
PTR=0x07;
}
2016-17
AES Lab(HCS12)
}
}
Experiment 2
Aim: Switching the LED using input from Switches Using Macros
Program:
#include <hidef.h> /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
#define sw1 PT1AD_PT1AD4
#define sw2 PT1AD_PT1AD5
#define sw3 PT1AD_PT1AD6
#define sw4 PT1AD_PT1AD7
#define LED PTR
void main(void)
{
DDRR=0xFF; // Data Direction Register Port R as Output
DDR1AD=0x00; // Data Direction Register Port AD as Input
ATDDIEN=0xF0; // Analog to Digital Input Enable Register is Enabled
PER1AD=0XFF; // Port AD PULL UP enable register is Enabled
PTR=0XFF; // Port R is OFF
while(1)
{
if(sw1==0)
{
LED=0x0E;
}
if(sw2==0)
{
LED=0x0D;
}
2016-17
AES Lab(HCS12)
if(sw3==0)
{
LED=0x0B;
}
if(sw4==0)
{
LED=0x07;
}
}
}
Experiment 3
2016-17
AES Lab(HCS12)
Aim: Switching the LED Depending on the Number of times a Switch is
pressed
Program:
#include <hidef.h> /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
#define sw1 PT1AD_PT1AD4
#define sw2 PT1AD_PT1AD5
#define sw3 PT1AD_PT1AD6
#define sw4 PT1AD_PT1AD7
#define LED PTR
void main(void)
{
int count=0;
DDRR=0xFF; // Data Direction Register Port R as Output
DDR1AD=0x00; // Data Direction Register Port AD as Input
ATDDIEN=0xF0; // Analog to Digital Input Enable Register is Enabled
PER1AD=0XFF; // Port AD PULL UP enable register is Enabled
PTR=0XFF; // Port R is OFF
while(1)
{
while(sw1==0)
{
count++;
}
if(count%2==0)
LED=0x00;
else
LED=0xFF;
while(count==1);
}
2016-17
AES Lab(HCS12)
Experiment 4
Aim: Switching the LED to make a Pattern using SWITCH
Program:
#include <hidef.h> /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
#define sw1 PT1AD_PT1AD4
#define sw2 PT1AD_PT1AD5
#define sw3 PT1AD_PT1AD6
#define sw4 PT1AD_PT1AD7
#define LED PTR
void main(void)
{
int x,y;
DDRR=0xFF; // Data Direction Register Port R as Output
DDR1AD=0x00; // Data Direction Register Port AD as Input
ATDDIEN=0xF0; // Analog to Digital Input Enable Register is Enabled
PER1AD=0XFF; // Port AD PULL UP enable register is Enabled
PTR=0XFF; // Port R is OFF
while(1)
{
if(sw1==0)
{
LED=0x0E;
for(x=0;x<1000;x++)
for(y=0;y<1000;y++);
LED=0x0D;
for(x=0;x<1000;x++)
2016-17
AES Lab(HCS12)
for(y=0;y<1000;y++);
LED=0x0B;
for(x=0;x<1000;x++)
for(y=0;y<1000;y++);
LED=0x07;
for(x=0;x<1000;x++)
for(y=0;y<1000;y++);
}
else
LED=0xFF;
}
}
2016-17
AES Lab(HCS12)
ANALOG TO
DIGITAL
CONVERSION
Overview:
2016-17
AES Lab(HCS12)
Analog to Digital Conversion using POT and LEDs
Learning Objectives:
This module will help in understanding how to implement Analog to Digital Conversion by
taking POT as analog input and converting it to digital data using ADC module of HCS12
and seeing the digital output on LEDs by initializing the registers of ADC module
Success Criteria:
When you complete this module, you will be able to write a code to convert analog data to
digital using MC9S12HY64 microcontroller board
Requirements:
Code warrior installed basic knowledge of C and MC9S12HY64board.
Experiment 1
2016-17
AES Lab(HCS12)
Aim: Switching the LED Depending on the Analog Input from POT
Program:
#include <hidef.h> /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
void main(void)
{
DDRR=0XFF; // Data Direction Register Port R as Output
PTR=0X0F; // Port R is OFF
ATDCTL0=0X00; // Control Register 0
ATDCTL1=0X00; // Control Register 1
ATDCTL2=0XE0; // Control Register 2
ATDCTL3=0X90; // Control Register 3
ATDCTL4=0X01; // Control Register 4
ATDCTL5=0X20; // Control Register 5
while(1)
{
while(!ATDSTAT0_SCF);
PTR=ATDDR0; // Data from POT on LEDs
}
}
Experiment 2
Aim: Switching the LED Depending on the Range of Analog Input
2016-17
AES Lab(HCS12)
Program:
#include <hidef.h> /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
#define LED PTR
void main(void)
{
int x,y;
DDRR=0XFF; // Data Direction Register Port R as Output
PTR=0X0F; // Port R is OFF
ATDCTL0=0X00; // Control Register 0
ATDCTL1=0X00; // Control Register 1
ATDCTL2=0XE0; // Control Register 2
ATDCTL3=0X90; // Control Register 3
ATDCTL4=0X01; // Control Register 4
ATDCTL5=0X20; // Control Register 5
while(1)
{
while(!ATDSTAT0_SCF);
if(ATDDR0<=0x7F)
{
LED=0x0E;
for(x=0;x<1000;x++)
for(y=0;y<1000;y++);
LED=0x0D;
for(x=0;x<1000;x++)
for(y=0;y<1000;y++);
}// First two LED’s ON for this range
else
{
2016-17
AES Lab(HCS12)
LED=0x0B;
for(x=0;x<1000;x++)
for(y=0;y<1000;y++);
LED=0x07;
for(x=0;x<1000;x++)
for(y=0;y<1000;y++); // Next two LED’s ON for this range
}
}
}
2016-17
AES Lab(HCS12)
SERIAL
COMMUNICATION
INTERFACE
Overview
The need to exchange data between the MCU and peripheral devices can be satisfied by using
parallel data transfer (multiple bits in one transfer operation). However, there are a few
drawbacks. The serial communication interface (SCI) was designed to transfer data in an
asynchronous mode
2016-17
AES Lab(HCS12)
Learning Objectives
This module will help in understanding how to communicate between two or more
microcontrollers or between a microcontroller and desktop using SCI protocol
Success Criteria
When you complete this module, you will be able to use the SCI port of HCS12 to connect to
another microcontroller or to a desktop PC with a COM port.
Requirements
Code warrior installed basic knowledge of C and MC9S12HY64 board.
Theory of operation:
Experiment 1
Aim: Sending a String from HCS 12 to PC and Display in the Terminal
Program:
2016-17
AES Lab(HCS12)
#include <hidef.h> /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
void send(char);
void main(void)
{
int i;
char a[ ]="CHAITANYA";
PER1AD=0xFF;
SCICR1=0X00;
SCICR2=0X0C;
SCIBDH=0X00;
SCIBDL=0X34;
for(i=0;a[i]!=0;i++)
{
send(a[i]);
}
}
void send(char c)
{
while(SCISR1_TDRE == 0);
SCIDRL = c;
}
Experiment 2
Aim: Sending a Letter from HCS 12 to PC and Display in the Terminal
2016-17
AES Lab(HCS12)
Program:
#include <hidef.h> /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
void send(char);
void main(void)
{
PER1AD=0xFF;
SCICR1=0X00;
SCICR2=0X0C;
SCIBDH=0X00;
SCIBDL=0X34;
send('a');
}
void send(char c)
{
while(SCISR1_TDRE == 0);
SCIDRL = c;
}
Experiment 3
Aim: Sending ‘HI’ from HCS 12 to PC and Display ‘BYE’ in the Terminal
Program:
2016-17
AES Lab(HCS12)
#include <hidef.h> /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
char rec();
void send(char);
void init();
void main(void)
{
int i;
unsigned char a[]="BYE" ;
init();
while(1)
{
if(rec()=='H')
{
if(rec()=='I')
{
for(i=0;a[i]!=0;i++)
{
send(a[i]);
}
}
}
}
}
void init()
{
DDRS=0XFF;
DDR1AD=0xFF;
PER1AD=0xFF;
SCICR1=0X04;
2016-17
AES Lab(HCS12)
SCICR2=0X0C;
SCIBDH=0X00;
SCIBDL=0X34;
DDRR= 0x0F;
PTR = 0x0F;
}
char rec( )
{
char a;
while(SCISR1_RDRF == 0);
a= SCIDRL;
return (a);
}
void send(char c)
{
while(SCISR1_TDRE == 0);
SCIDRL = c;
}
Experiment 4
Aim: Sending a Letter from HCS 12 to PC and Blink the Corresponding
LED
Program:
2016-17
AES Lab(HCS12)
#include <hidef.h> /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
char rec();
void send(char);
void init();
void main(void)
{
unsigned char a ;
init();
while(1)
{
a=rec();
send(a);
if(a == 'A' )
{
PTR=0x0E;
}
if(a == 'B' )
{
PTR=0x0D;
}
if(a == 'C' )
{
PTR=0x0B;
}
if(a == 'D' )
{
PTR=0x07;
}
}
}
2016-17
AES Lab(HCS12)
void init()
{
DDRS=0XFF;
DDR1AD=0xFF;
PER1AD=0xFF;
SCICR1=0X04;
SCICR2=0X0C;
SCIBDH=0X00;
SCIBDL=0X34;
DDRR= 0x0F;
PTR = 0x0F;
}
char rec( )
{
char a;
while(SCISR1_RDRF == 0);
a= SCIDRL;
return (a);
}
void send(char c)
{
while(SCISR1_TDRE == 0);
SCIDRL = c;
}
2016-17
AES Lab(HCS12)
TIMERS
Overview
At the heart of the HCS12 timer system is the 16-bit timer counter (TCNT). This counter can
be started or stopped, as you like. One of the timer functions is called input-capture. The
input-capture function copies the contents of the 16-bit timer to a latch when the specified
event arrives. Another timer function is called output-compare. The output-compare function
is often used to generate a time delay, trigger an action at some future time, and generate a
digital waveform. The third timer function is the pulse accumulator. This circuit is often used
to count the events arriving in a certain interval or measure the frequency of an unknown
signal.
2016-17
AES Lab(HCS12)
Learning Objectives
This module will help in understanding how to implement a timer by initializing the registers
of Timer Module
Success Criteria
When you complete this module, you will be able to initialize registers of timer module to
implement it
Requirements
Code warrior installed basic knowledge of C and MC9S12HY64 board.
Timer Module:
2016-17
AES Lab(HCS12)
Experiment 1
Aim: Blink LED using TIMERs
Program:
2016-17
AES Lab(HCS12)
#include <hidef.h> /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
#define ms_Delay_Cnt 0x1F40 // count used in Delay_1ms routine
#define TFLG1_T1F_MASK 0x80
void Init_Timer1(void);
void Delay_1ms(void);
void Delay(word count);
void main(void)
{
DDRR=0x0F;
PTR=0x0F;
Init_Timer1();
while(1)
{
PTR=0xFF;
Delay(1000);
PTR=0x00;
Delay(1000);
}
}
void Init_Timer1(void)
{
TIM1_TSCR1_TEN = 0;
TIM1_TIOS = 0x80; // setup T1C7 as output compare
TIM1_TCTL1 &= 0x3C; // pin is GPIO
TIM1_TSCR2_PR = 0x00; // timer runs at bus clk rate
TIM1_TSCR1_TEN = 1; // enable timer1 as free-running
}
void Delay_1ms(void)
{
dword t_count;
2016-17
AES Lab(HCS12)
t_count = TIM1_TCNT; // get TIM1 count
t_count += ms_Delay_Cnt; // add offset count
TIM1_TC7 = (word)t_count; // load new mod value
while(!(TIM1_TFLG1_C7F)); // wait for timer flag to set
TIM1_TFLG1_C7F = 1; // clear overflow flag
}
void Delay(word count)
{
word m;
for(m=0; m<count; m++)
{
Delay_1ms();
}
}
2016-17
AES Lab(HCS12)
PWM and
CONTROLLING the
SPEED of MOTOR
Overview:
There are many applications that require the generation of digital waveforms. The output-
compare function has been used to generate digital waveforms with any duty cycle. However,
the generation of waveforms using the output-compare function requires frequent attention
from the MCU. Most microcontrollers designed in the last few years have incorporated the
Pulse-Width Modulation (PWM) function to simplify the task of waveform generation
2016-17
AES Lab(HCS12)
Learning Objectives:
This module will help in understanding basic concepts of Period and Duty-Cycle so as to
generate PWM signals
Success Criteria:
When you complete this module, you will be able to write a code to generate PWM signals of
required period and duty-cycle from MC9S12HY64 microcontroller board
Requirements:
Code warrior installed basic knowledge of C and MC9S12HY64 board.
Block Diagram:
2016-17
AES Lab(HCS12)
Experiment 1
Aim: Control the Intensity of the LED/ Speed of MOTOR using FOR Loop
Delay
Program:
2016-17
AES Lab(HCS12)
#include <hidef.h>
#include <mc9s12hy64.h>
#include "derivative.h"
void delay(int);
void main(void)
{
int i;
PTPRRL=0x0F;
PWMCAE=0X01;
PWMPOL=0X00;
PWMCLK=0x01;
PWMSCLA=0x01;
PWMPRCLK=0x00;
PWMPER0=0xFA;
PWME=0x0F;
while(1)
{
for(i=0;i<=250;i++)
{
PWMDTY0=i;
delay(50);
}
for(i=250;i>=0;i--)
{
PWMDTY0=i;
delay(50);
}
}
}
void delay(int a)
2016-17
AES Lab(HCS12)
{
int i,j;
for(i=0;i<a;i++)
{
for(j=0;j<1000;j++);
}
}
Experiment 2
Aim: Control the Intensity of LED/ Speed of MOTOR using PWM
Registers
Program:
2016-17
AES Lab(HCS12)
#include <hidef.h>
#include <mc9s12hy64.h>
#include "derivative.h"
#define SW1 PT1AD_PT1AD4
#define SW2 PT1AD_PT1AD5
void delay(int);
void main(void)
{
int i;
PTPRRL=0x0F;
PWMCAE=0X01;
PWMPOL=0X00;
PWMCLK=0x01;
PWMSCLA=0x01;
PWMPRCLK=0x00;
PWMPER0=0xFA;
DDRR=0xFF;
DDR1AD=0x00;
ATDDIENL=0xF0;
PER1AD=0xF0;
PTR=0xFF;
PWME=0x0F;
while(1)
{
if(SW1==0 )
{
PTR_PTR1=1;
PWMDTY0=100;
delay(50);
}
2016-17
AES Lab(HCS12)
else if(SW2==0 )
{
PTR_PTR3=1;
PWMDTY0=150;
delay(50);
}
else
{
PTR_PTR2=0;
PWMDTY0=50;
delay(50);
}
}
}
void delay(int a)
{
int i,j;
for(i=0;i<a;i++)
{
for(j=0;j<1000;j++);
}
}
2016-17
AES Lab(HCS12)
SERIAL PERIPHERAL
INTERFACE
Overview
The serial peripheral interface (SPI) allows the HCS12 to communicate synchronously with
peripheral devices and other microcontrollers. The SPI system in the HCS12 can operate as a
master or as a slave. When the SPI module is configured as a master, it is responsible for
generating the clock signal (SCK) during an SPI data transfer. The SPI subsystem is mainly
used in interfacing with peripherals such as TTL shift registers, LED/LCD display drivers,
phase locked-loop (PLL) chips, memory components with serial interface, or A/D and D/A
converter chips that do not need a very high data rate
Learning Objectives
2016-17
AES Lab(HCS12)
This module will help in understanding how to send and receive characters using SPI
protocol
Success Criteria
When you complete this module, you will be able to write a code to communicate using SPI
protocol
Requirements
Code warrior installed basic knowledge of C and MC9S12HY64 board.
Block Diagram:
2016-17
AES Lab(HCS12)
Experiment 1
Aim: Send a single character on Boot and Display it on the Terminal.
Program:
#include <hidef.h>
#include "derivative.h"
2016-17
AES Lab(HCS12)
void spi_transmitter (char);
void main(void)
{
PER1AD=0xFF;
SPICR1= 0x56;
SPICR2= 0x10;
SPIBR= 0x77;
while(1)
{
spi_transmitter(‘A’);
}
}
void spi_transmitter (char data)
{
while(SPISR_SPTEF==0);
SPIDRL=data;
}
Experiment 2
Aim: Receive a single character from Terminal and Display on LED(s).
Program:
#include <hidef.h>
#include "derivative.h"
2016-17
AES Lab(HCS12)
char spi_receiver ();
void main(void)
{
PER1AD=0xFF;
SPICR1= 0x46;
SPICR2= 0x10;
SPIBR= 0x77;
DDRR=0xFF;
while(1)
{
if (spi_receiver( )= ='A')
{
PTR=0x00;
}
else
PTR=0x0f;
}
}
char spi_receiver (char data)
{
SPIDRL=data;
while(SPISR_SPTEF==0);
return(SPIDRL);
}
2016-17