ATtiny1624
Testing getting ADC to work on 2-Series ATtiny1624 (or any 2-Series). Below is what I finally got working. Its set to only 8 bit though but easily changed.
/* * ATtiny1624-ADC test.c * * Created: 6/7/2022 10:59:41 * Author : sherm */ #define F_CPU 3333333UL#define ADC_TIMEBASE_VALUE ((uint8_t)ceil(F_CPU * 0.000001) << ADC_TIMEBASE0_bp)#include <avr/io.h>#include <util/delay.h>
uint16_t adcVal;uint8_t i=0;
void adcInit(void){ ADC0.CTRLA |= ADC_ENABLE_bm;// ADC0.CTRLB = 7; ADC0.CTRLC = ADC_REFSEL_VDD_gc ;//0; //vcc ref// ADC0.CTRLC = ADC_REFSEL_1024MV_gc; /* Reference selection 1.024V */ /* Single-Ended Mode Configuration */ ADC0.CTRLE = 0x1b; /* Sample Duration: 0x1b */// ADC0.COMMAND &= ~ADC_DIFF_bm; ADC0.COMMAND = 0x0; //single 8 bit /* 8-bit */ ADC0.COMMAND = ADC_MODE_SINGLE_8BIT_gc; /* 12-bit */// ADC0.COMMAND = ADC_MODE_SINGLE_12BIT_gc;// ADC0.MUXPOS = ADC_MUXPOS_1_bm; ADC0.MUXPOS = ADC_MUXPOS_AIN1_gc; //pin PA1 for adc input// ADC0.MUXPOS = channel; ADC0.COMMAND |= ADC_START_IMMEDIATE_gc; while(!(ADC0.INTFLAGS & ADC_RESRDY_bm)){;}// while(ADC0.STATUS & (1<<0)){;} adcVal = ADC0.RESULT; }
int main(void){ PORTB.DIR |= 0b00001111; PORTA.DIR |= 0b11110000; while (1) { adcInit(); i=adcVal; PORTA.OUT =i; PORTB.OUT = i; _delay_ms(300) ; PORTB.OUTCLR = i; PORTA.OUTCLR = i; }}
uint16_t adcVal;uint8_t i=0;
void adcInit(void){ ADC0.CTRLA |= ADC_ENABLE_bm;// ADC0.CTRLB = 7; ADC0.CTRLC = ADC_REFSEL_VDD_gc ;//0; //vcc ref// ADC0.CTRLC = ADC_REFSEL_1024MV_gc; /* Reference selection 1.024V */ /* Single-Ended Mode Configuration */ ADC0.CTRLE = 0x1b; /* Sample Duration: 0x1b */// ADC0.COMMAND &= ~ADC_DIFF_bm; ADC0.COMMAND = 0x0; //single 8 bit /* 8-bit */ ADC0.COMMAND = ADC_MODE_SINGLE_8BIT_gc; /* 12-bit */// ADC0.COMMAND = ADC_MODE_SINGLE_12BIT_gc;// ADC0.MUXPOS = ADC_MUXPOS_1_bm; ADC0.MUXPOS = ADC_MUXPOS_AIN1_gc; //pin PA1 for adc input// ADC0.MUXPOS = channel; ADC0.COMMAND |= ADC_START_IMMEDIATE_gc; while(!(ADC0.INTFLAGS & ADC_RESRDY_bm)){;}// while(ADC0.STATUS & (1<<0)){;} adcVal = ADC0.RESULT; }
int main(void){ PORTB.DIR |= 0b00001111; PORTA.DIR |= 0b11110000; while (1) { adcInit(); i=adcVal; PORTA.OUT =i; PORTB.OUT = i; _delay_ms(300) ; PORTB.OUTCLR = i; PORTA.OUTCLR = i; }}