C program to set up and use the HC12 A/D converter
/* Read temperature from PAD4. Turn on heater if temp too low,
* turn off heater if temp too high. Heater connected to Bit 0
* of Port A.
#include
#define TRUE 1
#define SET_POINT 72 /* Temp at which to turn heater on or off */
main()
{
ATDCTL2 |= 0x80; /* Power up A/D */
ATDCTL4 = 0x01; /* 9 us/conversion */
ATDCTL5 = 0x64; /* 0 1 1 0 0 1 0 0
| | | \____/
| | | |
| | | \___ Bit 4 of Port AD
| | \________ Mult = 0 => one channel only
| \__________ Scan = 1 => continuous conversion
\____________ S8CM => do eight conversions
*/
/**************************************************************************/
DDRA = 0xff; /* Make Port A output */
PORTA = 0x00; /* Turn off heater */
/*****************************************************************************/
while (TRUE)
{
if (ADR0H > SET_POINT)
PORTA &= ~0x01;
else
PORTA |= 0x01;
}
}