PWM Leds

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 6

#include <hidef.

h> /* for EnableInterrupts macro */


#include "derivative.h" /* include peripheral declarations */
/*
@author:
@description:
@functions:
@environment:
@date:
@comments:
@version:

Walter Gonzalez Domenzain


Initialize LCD
None
MC9S08QE128
20/09/2012
1.0 - Writing my name
2.0 - Read Buttons

*/
/* Definitions */
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define

nIns
nData
PortRS
PortEnable
PortLCD
Button1
Button2
PortLeds
PortLedsHigh
PortLedsLow

#define
#define
#define
#define

nNotButtonPressed
nButton1Pressed
nButton2Pressed
nBothButtonsPressed

#define
#define
#define
#define
#define

n1Sec
n0.5Sec
nt30ms
nt15mseg
nt40useg

/* messages to display */
const unsigned char InitializeLCD[5]
x38, 0x0C, 0x01};
const unsigned char WALTER_GONZALEZ[]
LEZ"};
const unsigned char ROCIO_AVILES[]
O AVILES"};
/* Functions */
void cfgPorts(void);
/*@description: Initial Port Cfg
*/
void cfgISRs(void);
/*@description: Initial ISR Cfg
*/
void cfgADC(void);
/*@description: Initial ADC Cfg
*/

0x00
0x01
PTGD_PTGD0
PTGD_PTGD1
PTFD
PTAD_PTAD2
PTAD_PTAD3
PTCD
PTED
PTCD
0
1
2
3
0x8FFF
0x4FFF
0x0400
0x02E8
0x0016
= {0x38, 0x38, 0
= {"WALTER GONZA
= {"ROCI

void cfgPWM(void);
/*@description: Initial PWM Cfg
*/
unsigned char readPortA(void);
/*@description: Read porta to find out if a button is presssed and it returns
which button is.
*/
void delay(unsigned int uitimeDelay);
/*@description: Initial Port Cfg
*/
void initLCD(void);
/*@description: Initialize LCD
*/
void sendCode(unsigned char ucCodeCtrl, const unsigned char ucCodeToSend);
/*@description: Initialize LCD
*/
void sendLCDMessage(unsigned char loc, unsigned int length,
const unsigned char* MessageToSend,
unsigned char loc2, unsigned int length2
,
const unsigned char* MessageToSend2);
/* Global variables */
void main(void)
{
//unsigned int MAIN_uiVariable1 = 0;
/*@description:
*/
EnableInterrupts;
//Call subroutine for configuring ports
cfgPorts();
//cfgISRs();
//PTED_PTED7=0;
cfgPWM();
//cfgADC();
//initLCD();
/* Display character */
//sendCode(nData, 'a');
/* Display text */
//sendLCDMessage(0x80, 15, WALTER_GONZALEZ, 0xC2, 12, ROCIO_AVILES);
for(;;)
{
/* include your code here */
__RESET_WATCHDOG();
/* feeds the dog */
} /* loop forever */
}

void cfgPorts(void)
{
//Configure portf as output (Data LCD)
PTFDD = 0xFF;
//Configure portg as outputs (RS and Enable)
PTGDD = 0xFF;
//Configure portc as outputs (Led on demoboard)
PTCDD = 0xFF;
//Configure porte as outputs (Led on demoboard)
PTEDD = 0xFF;
//Turn off port leds
PTCD = 0xFF;
//Turn off port leds
PTED = 0xFF;
//Configure porta as inputs (Buttons)
PTADD = 0x00;
//Enable pullup for porta
PTAPE = 0XFF;
//Initialize LCD signals
PortRS = 0;
PortEnable = 0;
}
void cfgPWM(void)
{
SCGC1 = 0xC0;
//Clear Timer overflow flag; TOF interrupts disabled;
//Edge-aligned PWM; Bus rate clock; TPM Clock Source Divided-by 1
//0000 1000
TPM3SC = 0x40;
TPM2SC = 0x40;
//Determine the period size
TPM3MODH = 0x00;
TPM3MODL = 0xFF;
TPM2MODH = 0x00;
TPM2MODL = 0xFF;
//No input capture; Channel 0 interrupt requests enabled;
//Edge-aligned; High-true pulses
//0110 1100
TPM3C0SC = 0x6C;
TPM3C1SC = 0x6C;
TPM3C2SC = 0x6C;
TPM3C3SC = 0x6C;
TPM3C4SC = 0x6C;
TPM3C5SC = 0x6C;
TPM2C0SC = 0x6C;

TPM2C1SC = 0x6C;
//Clear Timer overflow flag; TOF interrupts enabled;
//Edge-aligned PWM; Bus rate clock; TPM Clock Source Divided-by 1
//0100 1000
TPM3SC = 0x48;
TPM2SC = 0x48;
//Determine the duty cycle of 0% for channel 0
TPM3C0VH = 0x00;
TPM3C0VL = 0x05;
//Determine the duty cycle of 13% for channel 1
TPM3C1VH = 0x00;
TPM3C1VL = 0x0D;
//Determine the duty cycle of 26% for channel 2
TPM3C2VH = 0x00;
TPM3C2VL = 0x1A;
//Determine the duty cycle of 40% for channel 3
TPM3C3VH = 0x00;
TPM3C3VL = 0x28;
//Determine the duty cycle of 53% for channel 4
TPM3C4VH = 0x00;
TPM3C4VL = 0x35;
//Determine the duty cycle of 70% for channel 5
TPM3C5VH = 0x00;
TPM3C5VL = 0x46;
//Determine the duty cycle of 83% for channel 6
TPM2C1VH = 0x00;
TPM2C1VL = 0x00;
//Determine the duty cycle of 100% for channel 7
TPM2C0VH = 0x00;
TPM2C0VL = 0x53;
}
unsigned char readPortA(void)
{
if((Button1 == 0) && (Button2 == 0))
{//Button 1 and 2 has been pressed
delay(nt30ms);
if((Button1 == 0) && (Button2 == 0))
{
return nBothButtonsPressed;
}
}
if(Button1 == 0)
{//Button 1 has been pressed
delay(nt30ms);

if(Button1 == 0)
{
return nButton1Pressed;
}
}
if(Button2 == 0)
{//Button 1 has been pressed
delay(nt30ms);
if(Button2 == 0)
{
return nButton2Pressed;
}
}
return nNotButtonPressed;
}
interrupt 25 void _interruptPWM0(void)
{
unsigned char temp;
//Clear Timer overflow flag; TOF interrupts enabled;
//Edge-aligned PWM; Bus rate clock; TPM Clock Source Divided-by 1
//0100 1000
PortLedsHigh=~TPM3SC;
PortLedsLow =~TPM3SC;
TPM3SC_TOF = 0;
}
interrupt 26 void _interruptPWM1(void)
{
unsigned char temp;
//Clear Timer overflow flag; TOF interrupts enabled;
//Edge-aligned PWM; Bus rate clock; TPM Clock Source Divided-by 1
//0100 1000
PortLedsHigh=~TPM3SC;
PortLedsLow =~TPM3SC;
TPM3SC_TOF = 0;
}
interrupt 27 void _interruptPWM2(void)
{
unsigned char temp;
//Clear Timer overflow flag; TOF interrupts enabled;
//Edge-aligned PWM; Bus rate clock; TPM Clock Source Divided-by 1
//0100 1000
PortLedsHigh=~TPM3SC;
PortLedsLow =~TPM3SC;
TPM3SC_TOF = 0;
}
interrupt 28 void _interruptPWM3(void)
{
unsigned char temp;
//Clear Timer overflow flag; TOF interrupts enabled;
//Edge-aligned PWM; Bus rate clock; TPM Clock Source Divided-by 1
//0100 1000
PortLedsHigh=~TPM3SC;

PortLedsLow =~TPM3SC;
TPM3SC_TOF = 0;
}
interrupt 29 void _interruptPWM4(void)
{
unsigned char temp;
//Clear Timer overflow flag; TOF interrupts enabled;
//Edge-aligned PWM; Bus rate clock; TPM Clock Source
//0100 1000
PortLedsHigh=~TPM3SC;
PortLedsLow =~TPM3SC;
TPM3SC_TOF = 0;
}
interrupt 30 void _interruptPWM5(void)
{
unsigned char temp;
//Clear Timer overflow flag; TOF interrupts enabled;
//Edge-aligned PWM; Bus rate clock; TPM Clock Source
//0100 1000
PortLedsHigh=~TPM3SC;
PortLedsLow =~TPM3SC;
TPM3SC_TOF = 0;
}
/*
interrupt 8 void _interruptPWM6(void)
{
unsigned char temp;
//Clear Timer overflow flag; TOF interrupts enabled;
//Edge-aligned PWM; Bus rate clock; TPM Clock Source
//0100 1000
PortLedsHigh=~TPM2SC;
//PortLedsLow =~TPM2SC;
TPM2SC_TOF = 0;
}
*/
interrupt 9 void _interruptPWM7(void)
{
unsigned char temp;
//Clear Timer overflow flag; TOF interrupts enabled;
//Edge-aligned PWM; Bus rate clock; TPM Clock Source
//0100 1000
PortLedsHigh=~TPM2SC;
PortLedsLow =~TPM2SC;
TPM2SC_TOF = 0;
}

Divided-by 1

Divided-by 1

Divided-by 1

Divided-by 1

You might also like