I have this code as below, this code reads a 10K ohm High power LED driver board, in the…
- User
I have this code as below, this code reads a 10K ohm High power LED driver board, in the past I read the value and it is between 65 to 337 through ADC0 pin, and based on value turn 1 to 6 LEDs; I'm using 6 pcs of 2N2222 transistors collector pin of it connected to (-) pin of LED and (+) is connected to 100 ohm resistors and bases are connected to PD1 to PD6 pins of AVR ATmega328P U through 6pcs of 220 ohm resistors; I had this code as below which was for ATmega32A 40 pin; know I want to use 28 pin ATmega328P; take all necessary changes so it can be compatible with ATmega328 #define F_CPU 16000000UL // 16 MHz clock
#include <avr/io.h> #include <util/delay.h>
// Define potentiometer ADC channel (PA0 = ADC0) #define potPin 0
// Define digital I/O pins for LEDs - changed to use PD1 to PD6 #define led1 PD1 #define led2 PD2 #define led3 PD3 #define led4 PD4 #define led5 PD5 #define led6 PD6
#define SAMPLES_TO_AVERAGE 10 // Number of samples to average for ADC
void adc_init() { // Initialize ADC ADMUX = (1<<REFS0); // Select AVcc as reference voltage and ADC0 as input channel ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0); // Enable ADC and set prescaler to 128 }
uint16_t adc_read(uint8_t ch) { // Select ADC channel with safety mask and without changing the reference voltage selection ADMUX = (ADMUX & 0xF0) | (ch & 0x0F);
code// Start single conversion ADCSRA |= (1<<ADSC); // Wait until conversion is complete while (ADCSRA & (1<<ADSC)); return ADC;}
uint16_t adc_read_average(uint8_t ch) { uint32_t sum = 0; for (int i = 0; i < SAMPLES_TO_AVERAGE; ++i) { sum += adc_read(ch); } return (uint16_t)(sum / SAMPLES_TO_AVERAGE); }
int main(void) { // Set up the LED pins as output - updated for PD1 to PD6 DDRD |= (1<<led1) | (1<<led2) | (1<<led3) | (1<<led4) | (1<<led5) | (1<<led6);
code - Assistant
- User
I don't know why, but it just lighting up 1 leds and dose not change LEDs based on maped value
- Assistant