I am new to the tinyavr 1 series microcontrollers. I've tried copying the basic example out of the Microchip TB3209 guide, but it doesn't seem to do anything.
I use the below code, where PA3 is an LED and PA6 is my ADC pin. All it's supposed to do is on boot, read the ADC pin, and if it is below the threshold, do nothing, otherwise turn the LED on. But not matter if I have the ADC grounded or pulled to 5V, it always gets a reading from the ADC that is higher than the threshold.
The below code is straight from Microchip.
/* ATMEL ATTINY212
Pin 6 is /RESET
+-\/-+
Vcc 1| |8 GND
DAC TXD PA6 2| |7 PA3 SCK
RXD PA7 3| |6 PA0 UPDI RESET
SDA MOSI PA1 4| |5 PA2 MISO SCL
+----+
PA3 is the LED output pin
PA6 is the ADC pin AIN6
*/
#define F_CPU 3333333UL // Default Setting micro to 3.33MHz
// #define F_CPU 20000000UL // Setting micro to 20MHz
#include <avr/io.h>
#include <avr/interrupt.h>
uint16_t adcVal;
void ADC0_init(void);
uint16_t ADC0_read(void);
void ADC0_init(void)
{
/* Disable digital input buffer */
PORTA.PIN6CTRL &= ~PORT_ISC_gm;
PORTA.PIN6CTRL |= PORT_ISC_INPUT_DISABLE_gc;
/* Disable pull-up resistor */
PORTA.PIN6CTRL &= ~PORT_PULLUPEN_bm;
ADC0.CTRLC = ADC_PRESC_DIV4_gc /* CLK_PER divided by 4 */
| ADC_REFSEL_INTREF_gc; /* Internal reference */
ADC0.CTRLA = ADC_ENABLE_bm /* ADC Enable: enabled */
| ADC_RESSEL_10BIT_gc; /* 10-bit mode */
/* Select ADC channel */
ADC0.MUXPOS = ADC_MUXPOS_AIN6_gc;
}
uint16_t ADC0_read(void)
{
/* Start ADC conversion */
ADC0.COMMAND = ADC_STCONV_bm;
/* Wait until ADC conversion done */
while ( !(ADC0.INTFLAGS & ADC_RESRDY_bm) )
{
;
}
/* Clear the interrupt flag by writing 1: */
ADC0.INTFLAGS = ADC_RESRDY_bm;
return ADC0.RES;
}
int main(void)
{
ADC0_init();
adcVal = ADC0_read();
// set PA3 to output mode
PORTA.DIR |= (1 << 3);
if(adcVal > 10) {
// PA3 LED on
PORTA.OUTSET = (1 << 3);
} else {
// PA3 LED off
PORTA.OUTCLR = (1 << 3);
}
while (1)
{
;
}
}
As of the board, it's working with a blink or digital input code properly. It's just a SOIC to DIP adapter on a bread board going to an LED (with CLR) and a potentiometer.