Metku.net

MetkuMods
It is currently 27.08.2014 17:53

All times are UTC + 2 hours [ DST ]




Post new topic Reply to topic  [ 18 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: 06.05.2009 00:44 
Offline
Site Admin
Site Admin
User avatar

Joined: 24.01.2002 10:09
Posts: 4683
Location: Finland
Image
Image In English
Image Suomeksi

Questions, comments?

Kysymyksiä, kommentteja?

_________________
Metku | AlltheMods | LedCalc | Teknolelu | AndroidCoding
Main Computer: Intel i5, MSI H55M-ED55, 12 Gigabytes of DDR3, 3 Terabytes of HDD, GTX295 and Benq 27"


Top
 Profile  
 
PostPosted: 06.05.2009 12:35 
Offline
Regular Modder
Regular Modder

Joined: 02.11.2006 19:30
Posts: 249
Great examples with schematics, photos of circuit on breadboard and well commented source codes... Isn't that all we could wish for?

Hyviä esimerkkejä kytkentäkaavioilla, kuvilla koekytkentäalustalle kasatuista piireistä ja hyvin kommentoiduilla lähdekoodeilla... Eikös siinä ole melkein kaikki mitä voidaan toivoa?


Top
 Profile  
 
PostPosted: 09.05.2009 00:36 
Offline
Ultra Modder
Ultra Modder
User avatar

Joined: 07.12.2004 19:45
Posts: 2537
Location: Finland CPU: i5-750 RAM: 4Gt, HDD: 2000 Gt, GFX: RADEON HD 5770, D: 22"+44"
Loistava opastus mikrokontrollien maailmaan :D

_________________
Image


Top
 Profile  
 
PostPosted: 13.05.2009 17:50 
Offline
Regular Modder
Regular Modder

Joined: 14.05.2005 19:47
Posts: 198
Vihdoin selvästi tehty ja helposti ymmärrettävä ohje ledin ohjaamiseen pwm:llä!
ATtiny2313:ssa ei taida olla tuota PWM:ää sisäänrakennettuna? Täytyykin kokeilla, jos saisin tuosta esimerkki koodista väsäiltyä RGB-ledeille ohjaimen jossa väriä säädetään napeilla.


Top
 Profile  
 
PostPosted: 13.05.2009 22:17 
Offline
Regular Modder
Regular Modder

Joined: 02.11.2006 19:30
Posts: 249
ATtiny2313:ssa on nelikanavainen PWM...

Lähde: www.avrfreaks.com

Freakseissa kun klikkaa etusivulla "DEVICES" tabia ja sitten kirjoittaa hakuun piirin mallin, saa paljon tärkeää tietoa piiristä. Tosi kätevä vertailla eri piirejä ja päättää, minkä lähikaupan valikoimissa olevan piirin ostaisi.


Top
 Profile  
 
PostPosted: 06.08.2009 20:37 
Offline
Junior Modder
Junior Modder

Joined: 06.08.2009 20:25
Posts: 0
En ole nyt täysin varma mutta näyttäisi siltä, että esimerkin 2.5 kuvissa on pientä ristiriitaisuutta. Kytkentäkaaviossa on MAX233 piirin pinni 7 vedetty +5V:iin mutta koekytkentälevyllä +5V on kytkettykin pinniin 8.


Top
 Profile  
 
PostPosted: 06.08.2009 20:52 
Offline
Regular Modder
Regular Modder

Joined: 02.11.2006 19:30
Posts: 249
Ainakin seiskan pitäisi olla tosiaan +5v, kuten kytkentäkaaviossa onkin. Kaheksaista ei tarvitse kytkeä mihinkään.


Top
 Profile  
 
PostPosted: 29.08.2009 20:46 
Offline
Junior Modder
Junior Modder

Joined: 28.01.2009 02:09
Posts: 0
Hey all, I've just this past week finally got some components to try out some projects I've been wanting to do for some time now..

But I'm not able to compile the code straight on cause of some errors that you can see below.
Can you help me out with what may be wrong?

Thanks in advance =)

edit.. I know you can see it in the log text, but I'm using a mega32 instead of the one you used..
but I guess it should work anyway, without altering the code cause this is some code error right?


Build started 29.8.2009 at 19:36:41
avr-gcc -mmcu=atmega32 -Wall -gdwarf-2 -Os -std=gnu99 -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -MD -MP -MT metku.o -MF dep/metku.o.d -c ../metku.c
../metku.c: In function 'main':
../metku.c:42: error: 'TCCR0A' undeclared (first use in this function)
../metku.c:42: error: (Each undeclared identifier is reported only once
../metku.c:42: error: for each function it appears in.)
../metku.c:46: error: 'COM0A1' undeclared (first use in this function)
../metku.c:50: error: 'TCCR0B' undeclared (first use in this function)
../metku.c:86: error: 'OCR0A' undeclared (first use in this function)
make: *** [metku.o] Error 1
Build failed with 6 errors and 0 warnings...


Top
 Profile  
 
PostPosted: 29.08.2009 20:56 
Offline
Junior Modder
Junior Modder

Joined: 28.01.2009 02:09
Posts: 0
I just noticed that it was because I used another uC.. so I'll have to find out the new names of the variables to see if that works..

cause when I changed to ATtiny45 and tried to compile it worked just fine :D
so it clearly is a uC problem in the code, so I'll try and port it.. if I get it to work I can post it here if there are any interest :)


Top
 Profile  
 
PostPosted: 29.08.2009 21:11 
Offline
Site Admin
Site Admin
User avatar

Joined: 24.01.2002 10:09
Posts: 4683
Location: Finland
Please do share. I'm sure there are people that would like to start directly with more powerful chips.


Top
 Profile  
 
PostPosted: 29.08.2009 21:29 
Offline
Junior Modder
Junior Modder

Joined: 28.01.2009 02:09
Posts: 0
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;
}


Top
 Profile  
 
PostPosted: 30.08.2009 07:36 
Offline
Veteran Modder
Veteran Modder

Joined: 15.08.2004 17:57
Posts: 582
Location: Salo, Finland
Hi,

It looks as you define LED in the beginning of the program, but don't use that definition anywhere? Is this because you want to remember where the led in question is, or for a i/o purpose?

Also you do not do anything for the led other than set it off by defining PORTB = 0x00; And the DDRB register might be better off as 0xFF

You should check out what the program should do when OCR1x registers match/mismatch.

This is getting a bit long for a quick response ;)

// Potentiometer slide at PB2, (ADC1)
#define POT 2

This is not really true, but i trust that you have just copied from Aki's code. AtMEGA32 has all 8 pins for ADC, but they are PORTA pins 0-7.

Also, nothing happens to this define or nothing is done with it.

I suggest you check the example code again, and then if need be your wirings, but i believe this just to be an error with the adc port.

bleh, it got all messy, it's 7 AM i'll try to be clearer in the PM hours

-gN


Top
 Profile  
 
PostPosted: 30.08.2009 19:02 
Offline
Junior Modder
Junior Modder

Joined: 28.01.2009 02:09
Posts: 0
yeah I've noticed that as well, but as I'm not that great with C programming yet I figured that
some sample code should be correct and should work ;)

yes I've tried with setting the PORT to A as well from B as those are the ADC pins, that I knew :)
But it did not do any difference..

Anyway, the only thing I changed before I posted the code here on the forums was the
TCCR0A to TCCR1A and COM0A1 to COM1A1, because then it could compile for the ATmega32..
However, I don't know if that was correct to do and if more should be done to get it to work,
but perhaps the defines need to be handled as well otherwise the uC dont know where the
LED and POT are.

I'm still looking at other examples, while hard to find therefore I tried Aki's code :)
but I havent found anything out yet.. ;)

When I finally get some code that works I'll make sure to post it here if someone else
haven't posted it by then..

But if someone can see the problem in this code, please feel free to help out! :)


Top
 Profile  
 
PostPosted: 30.08.2009 20:11 
Offline
Veteran Modder
Veteran Modder

Joined: 15.08.2004 17:57
Posts: 582
Location: Salo, Finland
I was trying to direct you in the right direction but it seems we weren't on the same page.

The problem with the output pin PB2 is that you have set the whole port as 0 with the command PORTB = 0x00;

This puts PORTB as input and you can't get the VCC value out of it.

Also, the ADC is reading the potentiometer as it should, it just isn't indicating the results anywhere.

Here is how you should go about fixing it.

You have nothing else stuck on PORTB besides the led. (I am assuming here) You can go ahead and set DDRB as 0xFF (1111 1111) then set PORTB as 0x00 (0000 0000). Now you have DDRB (PORTB I/O) register set as output and the led turned off.

As i see it the defines for LED and POT are totally useless?

Maybe Aki would like to comment on those.

I checked the datasheet for ATMEGA32 (i assume you have too) the OC0 pin is at PB3 not PB2 so try as you might the led won't light up at PB2.

I wish you happy times with µcontrollers and a word of advice. Always check the wiring from the datasheet.

If you have any questions or my advice is just totally wrong (has been known to happen on more than one occasion) let us know. And keep us posted on your progress

-gN


Top
 Profile  
 
PostPosted: 30.08.2009 23:25 
Offline
Veteran Modder
Veteran Modder
User avatar

Joined: 15.04.2002 18:52
Posts: 671
Location: Tampere, Finland
The defines for POT and LED don't actually do anything, I kinda forgot them there as reminders. You can remove those lines if you want to.

It should be safe to delete these lines too:
// PB2 in, others out
DDRB = 0b11011;
PORTB = 0x00;

Tiny and mega -chips have slightly different names for registers and if you are porting a code meant for different chip than you are using, you just need to crawl through the datasheets of both of them and look for matching registers and change the code accordingly.

Here are some replacements I looked up, you seemed to have some of them right, but I'll include them here. Put these in the code and test them out, I don't have my ATmega32 development board at hand right now so I can't test those yet myself.

TCCR0A -> TCCR1A
WGM00 -> WGM10
COM0A1 -> COM1A1
TCCR0B -> TCCR1B
CS01 -> CS11

"The COM1A1:0 and COM1B1:0 control the Output Compare pins (OC1A and OC1B respectively) behavior."

So if we look at mega32's datasheet, we see that OC1A is at PD5 (port D / bit 5, look for the physical pin from the datasheet).

Then what comes to the ADC, you'll need to supply 5 volts to the AREF pin on your ATmega32 because it can't use VCC as reference like the ATtiny45 does.

To avoid too much code changes, let's still use ADC1 and it's located in PA1 (port A / bit 1) on ATmega32.

So in short:
1) Replace the registers
2) Supply 5 volts to AREF
3) PD5 for OC1A (connect led here)
4) PA1 for ADC1 (connect potentiometer here)

In theory the example should work with these instructions, but I can't confirm it right now because my devboard is somewhere.


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

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