Skip to content

Instantly share code, notes, and snippets.

@francisbrito
Created June 22, 2018 17:19
Show Gist options
  • Save francisbrito/d15340d9f6ecf48991486bd0d75f3877 to your computer and use it in GitHub Desktop.
Save francisbrito/d15340d9f6ecf48991486bd0d75f3877 to your computer and use it in GitHub Desktop.
frdm-kl25z voltimeter test
/* p7_1.c: A to D conversion of channel 0
* This program converts the analog input from channel 0 (PTE20)
* using software trigger continuously.
* Bits 10-8 are used to control the tri-color LEDs. LED code is
* copied from p2_7. Connect a potentiometer between 3.3V and
* ground. The wiper of the potentiometer is connected to PTE20.
* When the potentiometer is turned, the LEDs should change color.
*/
#include <MKL25Z4.h>
#include "fsl_debug_console.h"
void ADC0_init(void);
int main(void) {
uint16_t result;
float volt;
ADC0_init(); /* Configure ADC0 */
while (1) {
ADC0->SC1[0] = 0; /* start conversion on channel 0 */
while (!(ADC0->SC1[0] & 0x80)) {
} /* wait for conversion complete */
result = ADC0->R[0]; /* read conversion result and clear COCO flag */
volt = (result * 3.3) / sizeof(uint16_t);
PRINTF("ADC value: %i\tVolt: %.2f", result, volt);
}
}
void ADC0_init(void) {
SIM->SCGC5 |= 0x2000; /* clock to PORTE */
PORTE->PCR[20] = 0; /* PTE20 analog input */
SIM->SCGC6 |= 0x8000000; /* clock to ADC0 */
ADC0->SC2 &= ~0x40; /* software trigger */
// TODO: Figure this out. Not sure what this values mean. Consider checking out the ADC component.
/* clock div by 4, long sample time, single ended 12 bit, bus clock */
ADC0->CFG1 = 0x40 | 0x10 | 0x4c | 0x00;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment