Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
58
Добавлен:
14.04.2015
Размер:
2.08 Mб
Скачать

Getting Started and Creating Applications

193

 

 

Watchdog Timer

The 8051Fx family of microcontrollers include a Programmable Counter Array

9

(PCA) with a watchdog timer. You can use the watchdog as a method of

recovery from hardware or software failures. The watchdog timer counts up. If the timer count matches a value stored in the PCA module 4 SFRs, the watchdog resets the MCU.

Your application must periodically update the PCA 4 value to avoid a watchdog reset. If your program does not reset the watchdog timer frequently enough or if your program crashes, the watchdog timer overflows and resets the CPU.

The following example code shows how to initialize the watchdog timer and how to reset it.

#include <reg51f.h>

/* This function adjusts the watchdog timer compare value to the current

*PCA timer value + 0xFF00. Note that you must write to CCAP4L first,

*then write to CCAP4H. */

void watchdog_reset (void) { unsigned char newval;

newval = CH + 0xFF; CCAP4L = 0;

CCAP4H = newval;

}

void main (void) { unsigned int i;

/* Configure PCA Module 4 as the watchdog and make sure it doesn't time-out immediately. */ watchdog_reset ();

CCAPM4 = 0x48;

/* Configure the PCA for watchdog timer. */ CMOD = (CMOD & 0x01) | 0x40;

/* Start the PCA Timer: From this point on, we must reset the watchdog timer every 0xFF00 clock cycles. If we don't, the watchdog timer will reset the MCU. */

CR = 1;

/* Do something for a while and make sure that we don't get reset by the watchdog. */

for (i = 0; i < 1000; i++) { watchdog_reset ();

}

/* Stop updating the watchdog and we should get reset. */ while (1);

}

194 Chapter 9. Using On-chip Peripherals

9

D/A Converter

A D/A converter takes a digital input and outputs an analog voltage on one of the

pins of the microcontroller. The Philips 87LPC769 is one of the many devices

that offers built-in D/A converters.

The Philips 87LPC769 provides a 2-channel, 8-bit D/A converter that is easy to program. The following example code shows how to initialize the D/A converter and output voltages using the D/A SFRs..

/*

*This program generates sawtooth waveforms on the DAC

*of the Philips 87LPC769.

*/

#include <REG769.H>

void main (void)

{

 

// Disable the A/D Converter (this is required for DAC0)

ADCI = 0;

// Clear A/D conversion complete flag

ADCS = 0;

// Clear A/D conversion start flag

ENADC = 0;

// Disable the A/D Converter

 

// Set P1.6 and P1.7 to Input Only (Hi Z).

P1M2 &= ~0xC0;

 

P1M1 |= 0xC0;

 

ENDAC0 = 1;

// Enable the D/A Converters

ENDAC1 = 1;

 

while (1) { unsigned int i;

//Create a sawtooth wave on DAC0 and the

//opposite sawtooth wave on DAC1.

for (i = 0; i < 0x100; i++) { DAC0 = i;

DAC1 = 0xFF - i;

}

}

}

Getting Started and Creating Applications

195

 

 

A/D Converter

The Analog Devices ADuC812 provides 8 channels of 12-bit analog to digital

9

conversion. Voltages presented to the analog input pins are converted to digital

values and may be read from the A/D data registers.

The on-chip A/D converter may be configured for a number of conversion modes. It can generate an interrupt when a conversion has completed. The following example code shows how to initialize the A/D converter to

cyclically convert each analog input channel and how to read and output the conversion results.

#include <ADUC812.H> #include <stdio.h>

void main

(void)

{

unsigned char chan_2_convert;

SCON

= 0x50;

// Configure the serial port.

TMOD |= 0x20;

 

TH1

= 0xA0;

 

TR1

= 1;

 

TI

= 1;

 

 

 

// Configure A/D to sequentially convert each input channel.

ADCCON1 = 0x7C;

// 0111 1100

while

(1) {

 

unsigned int conv_val; unsigned char channel;

//Start a conversion and wait for it to complete. chan_2_convert = (chan_2_convert + 1) % 8;

ADCCON2 = (ADCCON2 & 0xF0) | chan_2_convert; SCONV = 1;

while (ADCCON3 & 0x80);

//Read A/D data and print it out.

channel = ADCDATAH >> 4;

conv_val = ADCDATAL | ((ADCDATAH & 0x0F) << 8);

printf ("ADC Channel %bu = 0x%4.4X\r\n", channel, conv_val);

}

}

196 Chapter 9. Using On-chip Peripherals

 

 

Power Reduction Modes

9

 

The 8051Fx offers two different power saving modes you may invoke: Idle

 

Mode and Power Down Mode.

 

 

Idle Mode halts the CPU but lets interrupts, timer, and serial port functions

 

 

 

 

continue operating. When an interrupt condition occurs, Idle Mode is canceled.

 

 

Power consumption can be greatly decreased in idle mode.

 

 

Power Down Mode halts both the CPU and peripherals. It is canceled by a

 

 

hardware reset or an external interrupt condition.

 

 

To enter Idle Mode, your program must set the IDL bit in the PCON SFR. You

 

 

may do this directly in C as demonstrated by the following program.

 

 

 

 

 

 

sfr PCON = 0x87;

 

 

 

void main (void)

{

 

 

while (1) {

 

 

 

task_a ();

 

 

 

task_b ();

 

 

 

task_c ();

 

 

 

PCON |= 0x01;

/* Enter IDLE Mode - Wait for enabled interrupt */

 

 

}

 

 

 

}

 

 

 

To enter Power Down Mode, your application must set the PD bit in the PCON

 

 

SFR as demonstrated in the following program.

 

 

 

 

 

 

sfr PCON = 0x87;

 

 

 

void main (void)

{

 

 

while (1) {

 

 

 

task_a ();

 

 

 

task_b ();

 

 

 

task_c ();

 

 

 

PCON |= 0x02;

/* Enter Power Down Mode */

 

 

}

 

 

 

}

 

Соседние файлы в папке HLP