Soldering Station, "C" problem - Page 1
Forum section: electronics / general

author: shebu18
author_rank: Frequent Contributor
timestamp: January 11, 2012, 07:26:18 am
content: Hello everybody, i stared a project by building a soldering station with a Pensol Iron N and a home made controller board( something like the one on dangerous prototypes).I use a atmega 8 for the code, a 2x8 LCD for visual output, MCP617 as opamp, 24V 3A transformer and a bunch of other components.I managed to do some code in flowcode4 until i reached the point where i needed to put some PWM on a pin to control the transistor that controls the heating.Then i tried mikroC for avr and got stuck at the same point.(here i tried only pwm output to a pin not all the code again)So i am asking for some help on PWM output to a pin of a avr micro, or even better to the atmega8.If i will not resolve this problem i will have to change the micro to a 168 and use arduino. I am not that happy with that because i want to learn C for future programing.What i find great in Flowcode is that you have a simulation with visual output(button, led, lcd, 7-segment, etc.) It is more visual but you do not use direct C, you use block diagrams.In mikroC you use only C, you do not have that much of a simulation possibility (only real simulation with the hardware) but you learn C. You also have plenty of library’s to work with.i have found this tutorialhttp://www.societyofrobots.com/member_tutorials/node/229that explains very well how to use C for PWM and how to bring it to the pin but it didn't work. I will try it once more today.I used this codeCode:int main(){ // set up 2 PWM channels on PB1 and PB2 using Timer1TCCR1A = 0; // disable all PWM on Timer1 whilst we set it upICR1 = 19999; // frequency is every 20ms// Configure timer 1 for Fast PWM mode via ICR1, with no prescalingTCCR1A = (1 << WGM11)TCCR1B = (1 << WGM13) | (1<<WGM12) | (1 << CS10);// Set PB1 and PB2 as outputsDDRB |= _BV(1) | _BV(2);TCCR1A |= 2 << 6; // enable PWM on port B1 in non-inverted compare mode 2TCCR1A |= 2 << 4; // enable PWM on port B2 in non-inverted compare mode 2OCR1A = ICR1 * 2 /20; // 2ms pulse to left motor on PB1OCR1B = ICR1 * 2 /20; // 2ms pulse to right motor on PB2while(1){// do nothing - the hardware is pumping out 2ms pulses every 20ms to the servos on PB1 and PB2// for a differential drive robot the motors are on each side of the robot so the robot should be spinning around its midpoint}return 0;}I know it is for driving a servo but i should dim or lighten up a led. OR?LE: can a mod move this topic to projects? now i saw where i started it.

author: Psi
author_rank: Super Contributor
timestamp: January 11, 2012, 08:40:00 am
content: I'll have a read of the code and see if i can find the bug, but really, you don't need to use one of the hardware PWM modes. There's nothing wrong with using it, its just a bit of overkill for heating a resistor element.Since your driving a soldering iron you don't need fancy fast PWM modes, even with a PWM frequency of 10Hz it would work fine.You could use one of the timers in normal mode and do your own PWM quite easily without needing to do complicated pwm timer modes.

author: shebu18
author_rank: Frequent Contributor
timestamp: January 11, 2012, 08:53:01 am
content: Thanks for your replay.Even if it is fancy i would like to use it because only this way i will learn this stuff. I would like to use the 16bit timer(fancy again) for more steps. I also want to implement a PID algorithm, display temp on the lcd, use multiple modes for heating the element and so on. A development for learning.

author: Psi
author_rank: Super Contributor
timestamp: January 11, 2012, 09:07:54 am
content: I've never used the 'Phase and Frequency Correct PWM Mode' before so i'll have to first figure out how it works myself.We really need a big visual diagram of all the PWM modes and how they work, the text explanation in the datasheet, although well written, doesn't give a good visual understanding of how it all works.

author: firewalker
author_rank: Super Contributor
timestamp: January 11, 2012, 09:11:34 am
content: Just a quick one (ATmega8).Alexander.Code:#include <avr/io.h>#include <util/delay.h>#include <stdlib.h>#ifndef F_CPU#warning "F_CPU not defined defaulting to 8000000UL"#define F_CPU 8000000UL#endifint main(void){uint8_t i=0;int8_t j=1;DDRB|=(1<<PB1);TCCR1A |= (1<<WGM10) | (1<<COM1A1)| (1<<COM1B1);TCCR1B |= (1<<CS10) ;while(1){i=i+j;if (i==255){j=-1;} else if (i==0){j=1;};OCR1A=i;_delay_ms(10);};return 0;}

author: shebu18
author_rank: Frequent Contributor
timestamp: January 11, 2012, 09:18:30 am
content: Thanks firewalker, i will try the code when i get home.Psi, you can find a explanation in the link i posted(societyofrobots). Here are the modes explained.

author: Psi
author_rank: Super Contributor
timestamp: January 11, 2012, 09:38:06 am
content: Quote from: shebu18 on January 11, 2012, 09:18:30 amYeah, that's a good explanation of simple general purpose pwm but the AVR hardware PWM is a bit more powerful and involved. (If you choose to use it)The modes are designed to do all of the steps in hardware so it can generate pwm at almost clock frequency. And even some AVRS have a separate 64mhz pwm clock so you can generate super fast pwm on your 8mhz micro.There are 4 or so different styles (15 modes) but basically they work by either changing the top/bottom value of a sawtooth count waveform or by compare matching a triangular up and down count waveform. (as i understand them anyway)etc.That's not so you cant do simple pwm with the AVR timers, you can. You just do the required steps in software to clear the counter and change the output compare register like in that url exampleEDIT: Found this, which looks to be a good explantion about the AVRs hardware pwm modeshttp://www.wrightflyer.co.uk/Using%20AVR%20Counter.pdf

author: shebu18
author_rank: Frequent Contributor
timestamp: January 11, 2012, 10:08:13 am
content: I would like to use the phase and frequency pwm mode with a top value of 1023(0 to 1023). I this way i write the 10bit adc also from 0 to 1023 and i then have 1023 steps for the heating.This is just an idea. i will look at the explanation and i will watch the video at home, youtube is locked at work.

author: shebu18
author_rank: Frequent Contributor
timestamp: January 11, 2012, 06:27:29 pm
content: Tried your code, nothing happens. Can you send me the hex file please? make it a zip or change the extension.I used the default fuses, changed only the clock to external xtal 3-8Mhz and slow startup.LE: Why must it be so complicated? I tried 2 different atmega8's with the code from firewalker, no effect. Took them out and installed a breadboard with 8mhz xtal, still nothing. Changed the uC to atmega328 (as the exaple code from mikroC is for 168) now there is no flickering, nothing. ADC works great, even lcd (in simulation) but pwm is a problem, why is it so complicated? The last resort is to use a atmega8 with arduino and try to stay with a small code(6k i think).

author: shebu18
author_rank: Frequent Contributor
timestamp: January 13, 2012, 08:28:23 am
content: I made the pwm work. I used mikroC (they have some predefined library's) to generate the outpu. I had atmega8 and the code was for atmega168 so i needed to change some pins.in the do-while loop i had a for loop that goes 256 times and increases the duty by 1. After it's over it restart but the duty was 255 and after restart it wnt to 256 so the led went out. Is this write?

author: baljemmett
author_rank: Unknown
timestamp: January 13, 2012, 12:34:31 pm
content: Quote from: shebu18 on January 13, 2012, 08:28:23 amI seem to recall from another discussion a while back that the libraries the mikro compilers used are limited to 8-bit PWM -- the duty parameter is an 8-bit wide integer -- so when you get to 256 it'll wrap to 0. I might be mis-remembering the details, butthisseems to be the post.

author: shebu18
author_rank: Frequent Contributor
timestamp: January 14, 2012, 07:05:10 pm
content: THis will not be a problem, only if you use a for loop and loop al over again. Still, i will put the pwm out on a darlington configuration so i will always set a specific PWM duty. I am curious to see if i can modify and use de 16bit pwm for a finer adjustment.

author: shebu18
author_rank: Frequent Contributor
timestamp: January 15, 2012, 05:20:34 pm
content: Can i comand a IRF5305 direct from a microcontroller pin via PWM?(resistor in between). I could use a sencond transistor to comand the base of the irf.Something like this:Q1 is IRF5305Q2 is BC817