I want to use my atmega8 uC as a h-bridge.
Can anybody give me the source code using C, so that the microcontroller acts as an H-Bridge.
Robotics Stack Exchange is a question and answer site for professional robotic engineers, hobbyists, researchers and students. It only takes a minute to sign up.Sign up to join this community
It is not possible to use the micro-controller directly as an H-Bridge. A micro-controller is a micro-controller and an H-bridge is an H-bridge. You need both of them to control a motor (or something equivalent).
I will suppose that you are using motors and not servo. Let's say you want to use your micro-controller as an H-bridge. You have two cables from the motor. One for the ground and the other one for the current. When both of them are plugged, how are you going to wired it to the micro-controller to control the motor ? Or are you going to wire directly the current supply wire to the micro-controller ?
The thing is you will not be able to input enough current to move the motor with just the pin of a micro-controller. You will need another source of current and thus, something to control it, if you want to control the direction and speed of your motor. That's where the H-bridge is used.
You should read some documentation about H-Bridge, it's not that complicated to understand.
And if you really want not to use H-Bridge, you can create one with transistors on the bread-board. That tutorial is pretty good for that.
If in the end you want to use H-Bridge, the tutorial that EDDY74 gave you is perfect for that. Really simple and easy to understand. To, then, control the motor via a PWM is fairly easy since you just have to modulate the enable pin or the motor logic pins (I guess it depend on the H-bridge you are using). There a discussion about speed control on the Arduino forum.
I'm not really familiar with Atmel microcontrollers, but I'm assuming you're using some Arduino variant. Here's a pretty thorough tutorial about using an Arduino with a SN754410 h-bridge (found it via the SN754410 Sparkfun entry). It seems the only thing the tutorial lacks is controlling the motors via PWM, which of course means you will only be able to control the motors direction, and whether the motor is on or off. Just google Arduino PWM, I'm sure there's loads of code examples. [Update] See also Adafruit Arduino tutorials.
One thing to keep in mind is to never enable both directional signals at the same time (which obviously causes a straight short across your motor rail). One way of ensuring this never happens is by using only one micro-controller output (eg
MOTOR_DIR_FORWARD), and then feeding that into a not-gate, and then into your other directional signal (in other words, instead of having a motor enable ouput, and two directional outputs, you'll only have a motor enable output, and a single directional output).
Also remember that if your motors and micro are running off the same supply that insufficient decoupling may cause your micro to reset.
Just for completeness -- although you will typically need an external H bridge to drive motors, you can use just the AVR pins directly if you are certain that the current will be low enough (for driving a really tiny motor, or maybe a LED pair).
The code would look something like this:
DDRB |= _BV(PB0) | _BV(PB1); // This sets PB0 and PB1 as output. // After setting it as output, both the pins are driven low. PORTB |= _BV(PB1); // This sets PB1 high and start spinning the motor in one direction PORTB ^= _BV(PB0) | _BV(PB1); // This toggles state of both PB0 and PB1 -- change direction of the rotation PORTB &= ~(_BV(PB0) | _BV(PB1)); // This sets both pins to low -- stop the motor