well here we go then.. here are the updated code for use with mega32..
however, I'm not able to get any voltage over the led..
I've both tried with port B and A and there are no differences.. I dont get any voltage either before
or after the resistor for the led.. the pot works and gives
correct readings and that far it goes well but then
coming out of the uC seems to be the problem..
any ideas on how to solve it?
I'm 100% sure about the connections as I've gone through the schematic a few times and
checked on different leads to see if it's correct and so..
/**
* MetkuMods -
http://metku.net/* How to get started with AVR microcontrollers, part 2
* Example 2.3.2 - Reading the value of a potentiometer
*
* Author: Aki Korhonen
* Date: 2009-04-29
*/
// If clock speed isn't set in the project settings then it is set here
#ifndef F_CPU
#define F_CPU 1000000UL // 1 MHz
#endif
#include <avr/io.h>
#include <inttypes.h>
#include <util/delay.h>
// Led at PB0, (OCR0A)
#define LED 0
// Potentiometer slide at PB2, (ADC1)
#define POT 2
int main(void)
{
// PB2 in, others out
DDRB = 0b11011;
PORTB = 0x00;
// Value for the register
// "value" equals brightness of the led
// 0 = off | 255 = full brightness
uint8_t value = 0;
// -------------------------
// Initialize PWM
// 8-bit PWM, Phase correct
// 8 bits give us 256 levels of brightness
TCCR1A |= (1<<WGM00);
// Clear OC0A/OC0B on Compare Match
// Set OC0A/OC0B at BOTTOM (non-inverting mode)
TCCR1A |= (1<<COM1A1);
// Set prescaler to 8
// 1 MHz / 8*256 = ~490 Hz PWM frequency
TCCR1B |= (1<<CS01);
// -------------------------
// Initialize ADC
// Enable ADC
ADCSRA = (1<<ADEN);
// Select ADC1
ADMUX |= (1<<MUX0);
// Use Vcc as voltage reference
ADMUX &= ~(1<<REFS1) | ~(1<<REFS0);
// Select divider factor 8, so we get 1 MHz/8 = 125 kHz ADC clock
ADCSRA |= (1<<ADPS1) | (1<<ADPS0);
// -------------------------
// The program reads the ADC value from the potentiometer slide
// all the time and updates the PWM register with that value so
// the led brightness is controlled with the potentiometer.
while(1)
{
// Start ADC conversion
ADCSRA |= (1<<ADSC);
// Wait until the conversion is ready
loop_until_bit_is_set(ADCSRA, ADSC);
// Read the ADC value and scale it to 8-bit value
// because the ADC is 10-bit (1024 values) and the
// PWM register is only 8-bit (256 values), 1024/256 = 4
value = (uint8_t)(ADCW/4);
// Write the value to the Output Compare Register A
OCR1A = value;
// Wait for 2 ms
_delay_ms(2);
}
return 0;
}