A simple RL78 ADC usage example

In this article I show you a simple example of how to use the 10-bit ADC of the RL78. The ADC found in the RL78 is a SAR (successive approximation register) one, with a resolution of 10 bits. The configuration and operation is very similar to the ADC found on other microcontrollers. The main features are:

  1. Auto scan mode, where the ADC converts four channels in sequence;
  2. Analog comparator: which can set minimum and maximum limits for each conversion, triggering an interrupt when the result is inside or outside that window;
  3. Operation while the CPU is in snooze mode: the converter can operate even while the CPU is in a low-power mode.

A following example demonstrates the basic operation of this module. It was taken from the “Microcontroladores RL78: Guia Básico” book and shows how to read the result of a continuous conversion of a single channel. This code was written for the R5F100LE found on the YRPBRL78G13 board. It monitors the voltage set by the R15 trimpot, turning the LED when the result is higher than 511 (the equivalent of 2.5V approximately).

#include "ior5f100le.h"
#include "ior5f100le_ext.h"
#include "intrinsics.h"
#include "myRL78.h"

// set watchdog = off
#pragma location = "OPTBYTE"
__root __far const char opbyte0 = WDT_OFF; 
// set low-voltage detector = off
#pragma location = "OPTBYTE"
__root __far const char opbyte1 = LVD_OFF; 
// oscillator @ 32MHz, flash high speed
#pragma location = "OPTBYTE"
__root __far const char opbyte2 = FLASH_HS | CLK_32MHZ;
// debug enabled, erase memory if the password is wrong
#pragma location = "OPTBYTE"
__root __far const char opbyte3 = DEBUG_ON_ERASE; 
/* set security ID */
#pragma location = "SECUID"
__root __far const char pass[10] = {0,0,0,0,0,0,0,0,0,0};

#define LED P7_bit.no7

#pragma vector = INTAD_vect
__interrupt void service_ADC(void)
  unsigned int result;
  result = ADCR >> 6;   // read the conversion result
  if (result>511) LED = 0; else LED = 1;

void main(void)
  PM7_bit.no7 = 0;  // pin P77 as output
  LED = 0;          // LED off
  ADPC = 4;         // pins P20, P21 and P22 in analog mode
  ADCEN = 1;        // ADC enabled
  // ADC setup (multiple conversions, single channel, software triggered)
  ADS = ADC_CH2;    // selects channel 2
  ADMK = 0;         // enable ADC interrupt
  __enable_interrupt(); // enable RL78 interrupts
  ADCS = 1;         // start a conversion
  while (1);

Leave a Reply