Author Topic: please help newbie to learn program atmega8  (Read 3506 times)

0 Members and 1 Guest are viewing this topic.

Offline karlisTopic starter

  • Full Member
  • ***
  • Posts: 79
  • Helpful? 0
please help newbie to learn program atmega8
« on: April 21, 2009, 04:15:49 PM »
Hi folks, recently i have solderet atmega8, got it working and its programmable

i want to control my led light up time, if i can say so via jumper when button is presst

button on pin PC2
Led on pin PD7
jumper1 on PD5
jumper2 on PD6

when jumper is connectet its grounded, and if no jumper is attached then pins are soldered to ground via 10k resistor

allso i dont think that my bits are right for jumpers, i have read this tutorial but still have problems to understand how i can find out what ports have which bits, i know that its in hexadecimal form, for easyer life, i can convert them from hex to binary and other (learned binarry math at school) but its not "guilng" to gather with bits :(

thats little bit about my hardware and here is the code
Code: [Select]
//Nos kontrole v0.2
#include <avr/io.h>
#include <util/delay.h>

#define sbi(a, b)  (a |= (1<< b))
#define cbi(a, b)  (a &= ~(1 << b))
#define set_bits(a, b)  (a |= (1 << b))
#define clear_bits(a, b)  (a &= ~(1 << b))

#define S1 (PINC & (1<<PC2)) // poga uz pc2 portu

void delay_ms(unsigned int time_ms)
  while (time_ms--)

int main(void)
int  a;
     sbi(DDRD,PD6); //PD4 output
     sbi(DDRD,PD5); //PD5 output
cbi(PORTC,2); //inputu for button

cbi(PORTD,6);//jumper PD6
cbi(PORTD,5);//jumper PD5

     while (1) {
if(PINC & 0x04){ // if button pusht then use jumpers value
while (PINC & 0x04) {

  cbi(PORTD,PD7); //on  
          sbi(PORTD,PD7); // off

//if jumper 1 on
if (PIND & (0x01 << 5)){
//if jumper 2 on
if (PIND & (0x01 << 6));{


for me it looks ok, but when i connect jumper to what ever position led is flashin constantly not for 5 or 10 secs

acctualy i think my method how to control time sucks ( i`m sorry for my language) i have read tutorial about timers but i have no clue how to implant timer in my code, or how to code all from scratch

my goal is to cut off power to led after 5/10/15sec (by default without any jumper)
can any one help me out ? its hard to understand without matching example, so i could experiment

ps i`m not an programmer so i`m begging to explane for me what i`m doing wrongly
pps forgive me for my language


Offline Poly_Patje

  • Beginner
  • *
  • Posts: 1
  • Helpful? 0
Re: please help newbie to learn program atmega8
« Reply #1 on: April 23, 2009, 01:33:48 AM »
Maybe you have add the following at the top of your file before including delay.h:

Code: [Select]
#define F_CPU 8000000 // 8mhz

You may change the value corresponding the actual osc. speed that you have selected (you have to write the full hz). This will make up the correct reference for the _delay_ms() function.


Get Your Ad Here