Practicas Con MC68HC908QY4

Post on 05-Apr-2015

191 views 1 download

transcript

Practicas con MC68HC908QY4

Practica #1

Lectura de puerto A (interruptores) escritura en puerto B

Código del programa y esquemático

VSS16

PTB72

PTA0/AD0/TCH0/KBI013

PTA2/IRQ/KBI29

PTA3/RST/KBI38PTA4/OSC2/AD2/KBI4

5PTA5/OSC1/AD3/KBI5

4

VDD1

PTB310

PTB2 11

PTB114

PTB015

PTA1/AD1/TCH1/KBI112

PTB5 6

PTB47

PTB63

U1

MC68HC908QY4CP

VCC

GND

S?

SW-PB

S?

SW-PB

GND

GND

D?

LED0

D?

LED0D?

LED0

D?

LED0

D?

LED0

D?

LED0

D?

LED0

D?

LED0

GND

#include <hidef.h> /* for EnableInterrupts macro */

#include <MC68HC908QY4.h> /* include peripheral declarations */

#include <startup.h>

void inicializacion( void );

void main(void) {

inicializacion();

for(;;){ //ciclo infinito

if ( PTA_PTA0 == 0 ) { //DECLARAMOS CONDICION PARA EL PUERTO A

PTB= 0xFF;

}else{

PTB = 0x00;

}

}

}

void inicializacion(){

CONFIG1 = 0x09; // Deshabilita el perro guardián

OSCTRIM = 0x80; // Registro de ajuste de oscilador interno

PTAPUE = 0x3F; // Resistencias de pull-up

DDRA = 0x00; // Puerto A, entradas.

DDRB = 0xFF; // Puerto B, salidas.

}

/* please make sure that you never leave this function */

Practica 2

Contador de pulsos y salida en binario

El mismo esquematico

Programa

#include <hidef.h> /* for EnableInterrupts macro */

#include "derivative.h" /* include peripheral declarations */

#include <startup.h>

unsigned char val=0;

void inicializacion( void );

void retardo( unsigned int );

void main(void) {

inicializacion();

for(;;){

if (PTA_PTA0 == 0){

retardo(20000);

val=val+1;

}

PTB=val;

}

/* loop forever */

/* please make sure that you never leave this function */

}

void inicializacion() {

CONFIG1 = 0x09; // Deshabilita el perro guardián

CONFIG2 = 0x00; //Configuramos no reset y cristal interno

OSCTRIM = 0x80; // Registro de ajuste de oscilador interno

PTAPUE = 0x3F; // Resistencias de pull-up

DDRA = 0x10; // Puerto A, entradas.

DDRB = 0xFF; // Puerto B, salidas.

}

void retardo( unsigned int ciclos ) {

unsigned int cuenta;

for ( cuenta = 0; cuenta < ciclos; cuenta++ );

}

Practica 3

Desplazamiento de leds a la izquierda y a la derecha

Mismo esquematico

Programa

#include <hidef.h> /* for EnableInterrupts macro */

#include "derivative.h" /* include peripheral declarations */

#include <startup.h>

/////variables////////

const unsigned char leds[] = {

0x00, 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01

};

///////funciones//////

void inicializacion(void);

void retardo( unsigned int );

void secIzquierda( void );

void secDerecha( void );

////////programa principal////////////

void main(void) {

inicializacion();

for(;;){

if ( PTA_PTA0 == 0 )

secDerecha();

else if ( PTA_PTA1 == 0 )

secIzquierda();

else

PTB = leds[ 0 ]; // PTB = 0x00;

}

/* please make sure that you never leave this function */

}

void inicializacion() {

CONFIG1 = 0x09; // Deshabilita el perro guardián

CONFIG2 = 0x00;

OSCTRIM = 0x80; // Registro de ajuste de oscilador interno

PTAPUE = 0x3F; // Resistencias de pull-up

DDRA = 0x00; // Puerto A, entradas.

DDRB = 0xFF; // Puerto B, salidas.

}

void retardo( unsigned int ciclos ) {

unsigned int cuenta;

for ( cuenta = 0; cuenta < ciclos; cuenta++ );

}

void secDerecha() {

int cuenta;

for ( cuenta = 0; cuenta < 9; cuenta++ ) {

PTB = leds[ cuenta ];

retardo( 20000 );

}

}

void secIzquierda() {

int cuenta;

for ( cuenta = 8; cuenta >= 0; cuenta-- ) {

PTB = leds[ cuenta ];

retardo( 20000 );

}

}

Practica 4

Tarjeta de visualizador de display 7 segmentos

VSS16

PTB72

PTA0/AD0/TCH0/KBI013

PTA2/IRQ/KBI29

PTA3/RST/KBI38

PTA4/OSC2/AD2/KBI45 PTA5/OSC1/AD3/KBI54

VDD1

PTB310

PTB211

PTB114

PTB015

PTA1/AD1/TCH1/KBI112

PTB56

PTB47

PTB63

U1

MC68HC908QY4CP

VCC

GND

S?

SW-PB

S?

SW-PB

GND

GND

D?

LED0

D?

LED0D?

LED0

D?

LED0

D?

LED0

D?

LED0

D?

LED0

D?

LED0

GND

K1

f2

g3

e4

d5

K6

c8

DP7

b9

a10

DS?

Dpy Amber-CC

GND

Programa

#include <hidef.h> /* for EnableInterrupts macro */

#include "derivative.h" /* include peripheral declarations */

#include <startup.h>

unsigned char val=0;

void inicializacion(void);

void retardo( unsigned int );

char segmentos( unsigned char );

void main(void) {

inicializacion();

/* include your code here */

for(;;){

if(PTA_PTA0 == 0){

retardo(24000);

val=val+1;

}

if ( PTA_PTA1 == 0 ){

retardo(24000);

val=val-1;

}

PTB=segmentos(val);

}

/* loop forever */

/* please make sure that you never leave this function */

}

void inicializacion(){

CONFIG1=0x09;

CONFIG2=0x00;

OSCTRIM=0x80;

PTAPUE=0x3F;

DDRA = 0x00; // Puerto A ENTRADAS

DDRB = 0xFF; // Puerto B salidas

}

void retardo( unsigned int ciclos ) {

unsigned int cuenta;

for ( cuenta = 0; cuenta < ciclos; cuenta++ );

}

char segmentos(unsigned char val){

unsigned char valseg;

switch (val){

case 0:

valseg=0x3F;

break;

case 1:

valseg=0x06;

break;

case 2:

valseg=0x5B;

break;

case 3:

valseg=0x4F;

break;

case 4:

valseg=0x66;

break;

case 5:

valseg=0x6D;

break;

case 6:

valseg=0x7D;

break;

case 7:

valseg=0x07;

break;

case 8:

valseg=0x7F;

break;

case 9:

valseg=0x6F;

break;

}

return valseg;

}

Practica 5 y 6

Control de relevador de CA para energizar bomba y para control de triac

Esquemático para triac

Q?Triac

1K

R?

Res2

U?

Opto TRIAC

DS?

Lamp

1K

R?

Res2

GND

CA

K?

Relay-DPST

K10

IN6 6

OUT712

IN77

OUT613OUT514

IN33

OUT415

IN1 1

OUT316 IN2

2

OUT811

GND 9

IN44

IN88

IN5 5

OUT118

OUT217

U2

ULN2803AGND

D?D Zener

GND

VCC

VCC

Esquema para relevador

VSS16

PTB7 2

PTA0/AD0/TCH0/KBI013

PTA2/IRQ/KBI29 PTA3/RST/KBI38 PTA4/OSC2/AD2/KBI45 PTA5/OSC1/AD3/KBI54

VDD 1

PTB3 10

PTB2 11

PTB1 14

PTB0 15

PTA1/AD1/TCH1/KBI112

PTB5 6

PTB4 7

PTB6 3

U1

MC68HC908QY4CP

VCC

GND

S?

SW-PB

S?

SW-PB

ULN2803AD?D Zener

GND1K

R?RPot

GND

VCC

Programa, aquí se utilizo el ADC del micro para activar una salida a partir de que rebasa los 2.5 volts en el AN3, pin 4

#include <hidef.h> /* for EnableInterrupts macro */

#include "derivative.h" /* include peripheral declarations */

#include <startup.h>

byte result;

void inicializacion(void);

void main(void) {

inicializacion();

for(;;){

ADSCR = 0x23;

result = ADR;

PTB=result;

if(result>0x7F){

PTA_PTA0=1;

}else{

PTA_PTA0=0;

}

}

/* loop forever */

/* please make sure that you never leave this function */

}

void inicializacion(){

CONFIG1=0x09;

OSCTRIM=0x80;

DDRA = 0x1B;

DDRB = 0xFF;

ADICLK = 0x40;

ADSCR = 0x23;

}

Practica 7

Manejo de un teclado numérico y mostrar la tecla en un display 7 segmentos

Esquematico

VSS 16

PTB72

PTA0/AD0/TCH0/KBI0 13

PTA2/IRQ/KBI29PTA3/RST/KBI38PTA4/OSC2/AD2/KBI45

PTA5/OSC1/AD3/KBI5 4

VDD1

PTB310

PTB211

PTB114

PTB015

PTA1/AD1/TCH1/KBI112

PTB56

PTB47

PTB63

U?

MC68HC908QY4CP

S?

SW-PB

S?

SW-PB

S?

SW-PB

S?

SW-PB

S?

SW-PB

S?

SW-PB

S?

SW-PB

S?

SW-PB

S?

SW-PB

f9g10

e1

d2

K3

c 4

DP 5

b 6

a 7K8

DS?

Dpy Blue-CC

Código de programa

#include <hidef.h> /* for EnableInterrupts macro */

#include "derivative.h" /* include peripheral declarations */

#include <startup.h>

void inicializacion(void);

void retardo( unsigned int );

void main(void) {

inicializacion();

EnableInterrupts; /* enable interrupts */

/* include your code here */

PTB=0x00;

for(;;) {

PTA_PTA0=1;

if((PTA_PTA3==1)&&(PTA_PTA0==1)){

PTB=0x06;

retardo(24000);

}

if((PTA_PTA4==1)&&(PTA_PTA0==1)){

PTB=0x5B;

retardo(24000);

}

if((PTA_PTA5==1)&&(PTA_PTA0==1)){

PTB=0x4F;

retardo(24000);

}

PTA_PTA0=0;

PTA_PTA1=1;

if((PTA_PTA3==1)&&(PTA_PTA1==1)){

PTB=0x66;

retardo(24000);

}

if((PTA_PTA4==1)&&(PTA_PTA1==1)){

PTB=0x6D;

retardo(24000);

}

if((PTA_PTA5==1)&&(PTA_PTA1==1)){

PTB=0x7D;

retardo(24000);

}

PTA_PTA1=0;

PTA_PTA2=1;

if((PTA_PTA3==1)&&(PTA_PTA2==1)){

PTB=0x07;

retardo(24000);

}

if((PTA_PTA4==1)&&(PTA_PTA2==1)){

PTB=0x7F;

retardo(24000);

}

if((PTA_PTA5==1)&&(PTA_PTA2==1)){

PTB=0x67;

retardo(24000);

}

PTA_PTA2=0;

__RESET_WATCHDOG(); /* feeds the dog */

} /* loop forever */

/* please make sure that you never leave main */

}

void inicializacion(){

CONFIG1=0x09;

OSCTRIM=0x80;

DDRA = 0x07;

DDRB = 0xFF;

}

void retardo( unsigned int ciclos ) {

unsigned int cuenta;

for ( cuenta = 0; cuenta < ciclos; cuenta++ );

}

Practica 8

Control de Motor PP velocidad y direccion

Esquematico, el que vemos anteriormente, con el uln2803 y las salidas al motor PP , la dirección la controlas por los dipswitch de a0 y a1, y la velocidad mediante el potenciómetro, dependiendo del valor analógico mayor es la velocidad

VSS16

PTB72

PTA0/AD0/TCH0/KBI013

PTA2/IRQ/KBI29

PTA3/RST/KBI38

PTA4/OSC2/AD2/KBI45

PTA5/OSC1/AD3/KBI54

VDD1

PTB3 10

PTB211

PTB114

PTB015

PTA1/AD1/TCH1/KBI112

PTB56

PTB47

PTB63

U?

MC68HC908QY4CP

IN11

IN22

IN33

IN44

IN55

IN66

IN77

IN88

OUT118

OUT316

OUT415

OUT514

OUT6 13

OUT712

OUT811

OUT217

COM D10

GND9

U?

ULN2803AGND

GND

VCC

D?D Zener

VCC

12345

P?

Header 5

VCC

1K

R?RPot

GND

VCC

S?

SW-PBS?

SW-PB

GND

Programa

#include <hidef.h> /* for EnableInterrupts macro */

#include "derivative.h" /* include peripheral declarations */

#include <startup.h>

const unsigned char leds[] = {

0x01, 0x02, 0x04, 0x08

};

unsigned int valor;

void inicializacion(void);

void retardo (unsigned int);

void secIzquierda( void );

void secDerecha( void );

void main(void) {

inicializacion();

for(;;) {

ADSCR = 0x23;

valor = ADR;

valor = valor*200;

if(PTA_PTA0 == 0)

secDerecha();

else if (PTA_PTA1 == 0)

secIzquierda();

else

PTB = 0x00; // PTB = 0x00;

__RESET_WATCHDOG(); /* feeds the dog */

} /* loop forever */

/* please make sure that you never leave main */

}

void inicializacion(){

CONFIG1= 0x09;

OSCTRIM = 0x80;

PTAPUE = 0x1F; // Resistencias de pull-up

DDRA = 0x00; // Puerto A, entradas.

DDRB = 0xFF; // Puerto B, salidas.

ADICLK = 0x40; // Divisor de velocidad del bus entre 4 para

// que el ADC trabaje a 3.2 MHz / 4 < 1 MHz.

// Conversiones continuas, interrupciones

}

void retardo( unsigned int ciclos ) {

unsigned int cuenta;

for ( cuenta = 0; cuenta < ciclos; cuenta++ );

}

void secDerecha() {

int cuenta;

for ( cuenta = 0; cuenta < 4; cuenta++ ) {

PTB = leds[ cuenta ];

retardo( valor );

}

}

void secIzquierda() {

int cuenta;

for ( cuenta = 3; cuenta >= 0; cuenta-- ) {

PTB = leds[ cuenta ];

retardo( valor );

}

}

Practica 9

Medición de velocidad de un motor de CD para formar grafica

VSS16

PTB7 2

PTA0/AD0/TCH0/KBI013

PTA2/IRQ/KBI29

PTA3/RST/KBI38PTA4/OSC2/AD2/KBI4

5PTA5/OSC1/AD3/KBI54

VDD1

PTB310

PTB211

PTB114

PTB0 15

PTA1/AD1/TCH1/KBI112

PTB5 6

PTB47

PTB63

U?

MC68HC908QY4CP

C1+1

VDD2

C1-3

C2+4

C2-5

VEE 6

T2OUT7

R2IN 8R2OUT9

T2IN10 T1IN11

R1OUT12

R1IN13

T1OUT14

GND15

VCC16

U?

MAX232ACPE

1

2

3

4

5

6

7

8

9

11

10

J?

D Connector 9

VCC

VCC

GND

GND

S?

SW-PB

GND

GND

10uF

C?

Cap Pol1

10uF

C?

Cap Pol1

GND

VCC

12

Y?XTAL1M

Programa, lo que va a hacer, es mediante la interrupción externa, nosotros vamos a poder contar los pulsos que nos va a indicar un optointerruptor, y cada segundo, se va a desplegar la velocidad en la computadora mediante la hyperterminal

Código

#include <hidef.h> /* for EnableInterrupts macro */

#include "derivative.h" /* include peripheral declarations */

#include <startup.h>

unsigned char flag=0;

interrupt 2 void cargar_dato_serie(void);

interrupt 6 void TIMER();

void delay_baudio(void);

unsigned char tiempo_bit;

void inicializacion(void);

void Configurar_Timer(void);

void Transmitir_Caracter(void);

unsigned char Caracter =0;

unsigned char bit;

void main(void) {

inicializacion();

Configurar_Timer();

EnableInterrupts;

PTB=0x00;

for(;;) {

__RESET_WATCHDOG(); /* feeds the dog */

} /* loop forever */

/* please make sure that you never leave main */

}

void inicializacion() {

CONFIG1 = 0x09; // Deshabilita el perro guardián

// Registro de ajuste de oscilador interno

DDRA = 0x38; // Pines 5,4 y 3 salidas, 0 y 2 entradas.

DDRB = 0xff; // Puerto B salidas

CONFIG2 = 0x58; // Resistencia de pull-up habilitada para el pin A2 que

// funciona como IRQ.

INTSCR = 0x00; // Habilita interrupción por IRQ.

}

//////////CONFIGURACION TIMER REGISTROS////////////

void Configurar_Timer(void){

TSC = 0X70; /*Stop & reset,interrupcion activa, prescaler= 0*/

TSC0 = 0; /*Resto de funciones desactivadas*/

TSC1 = 0;

TMODH = 0xFF; /* Se carga el módulo con 04E4 (1252 decimal)*/

TMODL = 0xFF;

TSC ^= 0x10; /*Un-reset TIMer*/

}

///////////interrupcion externa////////////////

interrupt 2 void cargar_dato_serie( void ) {

flag++;

// Borra la bandera del IRQF1

INTSCR_ACK = 1;

}

////////////INTERRUPCION TIMER///////////////

interrupt 6 void TIMER(){

Caracter=flag;

Transmitir_Caracter();

flag=0;

TSC ^= 0x80; /* Se borra la bandera de la interrupción */

}

/*******************************************************/

/* Subrutina que maneja el Tranmisor */

/*******************************************************/

void Transmitir_Caracter(void){

//DisableInterrupts; /*Interrupciones Desactivadas*/

__RESET_WATCHDOG();

bit =0;

PTB_PTB0 = 0; //Pongo el bit de inicio

delay_baudio();

do{

if(Caracter & 0x01 == 0x01){

PTB_PTB0 = 1; //Pongo TX en 1

delay_baudio();

}

else

{

PTB_PTB0 = 0; //Pongo TX en 0

delay_baudio();

}

Caracter = Caracter >>1; //Roto para pasar los 8 bits

bit++;

}while(bit <=7);

PTB_PTB0 = 1; //Pongo el bit de Stop

delay_baudio();

__RESET_WATCHDOG();

}

void delay_baudio(void){

for(tiempo_bit=0;tiempo_bit <=10;tiempo_bit++){

}

}