Metku.net

MetkuMods
It is currently 19.08.2018 00:35

All times are UTC + 2 hours [ DST ]




Post new topic Reply to topic  [ 18 posts ]  Go to page Previous  1, 2
Author Message
PostPosted: 01.09.2009 02:53 
Offline
Junior Modder
Junior Modder

Joined: 28.01.2009 02:09
Posts: 0
dedicated as I am to problems, when I feel for ;) here are the result of debugging and testing and trying different things ;)
the schematic should work as before, but I made a new one for how I connected it all..

Hopefully someone could verify that this works for them as well, on a ATmega32, so that I dont post something that
I think work, but doesnt work :)

But it should work as it works perfectly for me now on the ATmega32 :D


Posting the code here directly as well, added my name to the comment in the top, if that's ok? ;)

/**
* MetkuMods - http://metku.net/
* How to get started with AVR microcontrollers, part 2
* Example 2.3.1 - Reading the value of a potentiometer
*
* Author: Aki Korhonen
* Date: 2009-04-29
*
* Edited by: Chris Fredriksson, this code is for ATmega32
* Edited date: 2009-09-01
*/

// 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>

int main(void)
{
// PD2 in, others out
DDRD = 0x20;
PORTD = 0x00;

// Value for the register, placeholder
uint8_t value = 0;

// -------------------------
// Initialize PWM

// 8-bit PWM, Phase correct
// 8 bits give us 256 levels of brightness
TCCR1A |= (1<<WGM10);

// Clear OC1A/OC1B on Compare Match
// Set OC1A/OC1B at BOTTOM (non-inverting mode)
TCCR1A |= (1<<COM1A1);

// Set prescaler to 8
// 1 MHz / 8*256 = ~490 Hz PWM frequency
TCCR1B |= (1<<CS11);

// -------------------------
// Initialize ADC

// Enable ADC
ADCSRA = (1<<ADEN);

// Select ADC0
ADMUX |= (0<<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 calculates a delay between the steps.
while(1)
{
// Write the value to the Output Compare Register A
OCR1A = value;

// what happens here??
value = (uint8_t)(ADCW/4);

// Start ADC conversion
ADCSRA |= (1<<ADSC);

// Wait until the conversion is ready
loop_until_bit_is_set(ADCSRA, ADSC);
}

return 0;
}


Attachments:
File comment: Schematic and C code for ATmega32, Fade a LED with information from a potentiometer / variable resistor
metku.zip [64.53 KiB]
Downloaded 245 times
Top
 Profile  
 
PostPosted: 02.11.2009 23:19 
Offline
Junior Modder
Junior Modder

Joined: 02.11.2009 23:15
Posts: 0
I would suggest testing the 232/233 serial part first. Build the serial level translator (maxim chip) and connect the wires going to the microcontroller TxD/RxD together and not to the mcu. You'll have a loopback circuit. Power it up and connect to that with a terminal program. Anything you type in should come back to you. Once that works, power down and wire the TxD/RxD pins to the mcu. In case something goes wrong, you'll at least know it isn't the level converter.


Top
 Profile  
 
PostPosted: 03.11.2009 01:20 
Offline
Junior Modder
Junior Modder

Joined: 28.01.2009 02:09
Posts: 0
uhm, what are you replying to?


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 18 posts ]  Go to page Previous  1, 2

All times are UTC + 2 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group