Author Topic: Soldering Station, "C" problem  (Read 5792 times)

0 Members and 1 Guest are viewing this topic.

Offline shebu18Topic starter

  • Frequent Contributor
  • **
  • Posts: 309
  • Country: ro
Soldering Station, "C" problem
« on: January 11, 2012, 07:26:18 am »

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 tutorial http://www.societyofrobots.com/member_tutorials/node/229   that 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 code
Code: [Select]
int main(){       // set up 2 PWM channels on PB1 and PB2 using Timer1

   TCCR1A = 0;     // disable all PWM on Timer1 whilst we set it up
   ICR1 = 19999;   // frequency is every 20ms


   // Configure timer 1 for Fast PWM mode via ICR1, with no prescaling
   TCCR1A = (1 << WGM11)
   TCCR1B = (1 << WGM13) | (1<<WGM12) | (1 << CS10);

   // Set PB1 and PB2 as outputs
   DDRB |= _BV(1) |  _BV(2);
   TCCR1A |= 2 <<  6;  // enable PWM on port B1 in non-inverted compare mode 2
    TCCR1A |= 2 <<  4;  // enable PWM on port B2 in non-inverted compare mode 2

   OCR1A = ICR1 * 2 /20; // 2ms pulse to left motor on PB1
   OCR1B = ICR1 * 2 /20; // 2ms pulse to right motor on PB2


   while(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.
« Last Edit: January 11, 2012, 07:53:51 am by shebu18 »
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 10168
  • Country: nz
Re: Soldering Station, "C" problem
« Reply #1 on: January 11, 2012, 08:40:00 am »
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.
« Last Edit: January 11, 2012, 08:50:21 am by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline shebu18Topic starter

  • Frequent Contributor
  • **
  • Posts: 309
  • Country: ro
Re: Soldering Station, "C" problem
« Reply #2 on: January 11, 2012, 08:53:01 am »
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.
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 10168
  • Country: nz
Re: Soldering Station, "C" problem
« Reply #3 on: January 11, 2012, 09:07:54 am »
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.
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline firewalker

  • Super Contributor
  • ***
  • Posts: 2452
  • Country: gr
Re: Soldering Station, "C" problem
« Reply #4 on: January 11, 2012, 09:11:34 am »
Just a quick one (ATmega8).

Alexander.

Code: [Select]
#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
#endif



int 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;
}

« Last Edit: January 11, 2012, 09:27:30 am by firewalker »
Become a realist, stay a dreamer.

 

Offline shebu18Topic starter

  • Frequent Contributor
  • **
  • Posts: 309
  • Country: ro
Re: Soldering Station, "C" problem
« Reply #5 on: January 11, 2012, 09:18:30 am »
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.
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 10168
  • Country: nz
Re: Soldering Station, "C" problem
« Reply #6 on: January 11, 2012, 09:38:06 am »
Psi, you can find a explanation in the link i posted(societyofrobots). Here are the modes explained.

Yeah, 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 example :)


EDIT: Found this, which looks to be a good explantion about the AVRs hardware pwm modes

http://www.wrightflyer.co.uk/Using%20AVR%20Counter.pdf
« Last Edit: January 11, 2012, 09:56:29 am by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline shebu18Topic starter

  • Frequent Contributor
  • **
  • Posts: 309
  • Country: ro
Re: Soldering Station, "C" problem
« Reply #7 on: January 11, 2012, 10:08:13 am »
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.
 

Offline shebu18Topic starter

  • Frequent Contributor
  • **
  • Posts: 309
  • Country: ro
Re: Soldering Station, "C" problem
« Reply #8 on: January 11, 2012, 06:27:29 pm »
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).
« Last Edit: January 11, 2012, 07:26:03 pm by shebu18 »
 

Offline shebu18Topic starter

  • Frequent Contributor
  • **
  • Posts: 309
  • Country: ro
Re: Soldering Station, "C" problem
« Reply #9 on: January 13, 2012, 08:28:23 am »
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?
 

Offline baljemmett

  • Supporter
  • ****
  • Posts: 665
  • Country: gb
Re: Soldering Station, "C" problem
« Reply #10 on: January 13, 2012, 12:34:31 pm »
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?
I 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, but this seems to be the post.
 

Offline shebu18Topic starter

  • Frequent Contributor
  • **
  • Posts: 309
  • Country: ro
Re: Soldering Station, "C" problem
« Reply #11 on: January 14, 2012, 07:05:10 pm »
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.
 

Offline shebu18Topic starter

  • Frequent Contributor
  • **
  • Posts: 309
  • Country: ro
Re: Soldering Station, "C" problem
« Reply #12 on: January 15, 2012, 05:20:34 pm »
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 IRF5305
Q2 is BC817
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf