Society of Robots - Robot Forum
Software => Software => Topic started by: karlis 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 http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=37871 (http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=37871) 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
//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--)
_delay_ms(1);
}
int main(void)
{
int a;
sbi(DDRD,PD6); //PD4 output
sbi(DDRD,PD5); //PD5 output
sbi(DDRD,PD7);
cbi(PORTC,2); //inputu for button
cbi(DDRC,2);
cbi(PORTD,6);//jumper PD6
cbi(DDRC,6);
cbi(PORTD,5);//jumper PD5
cbi(DDRC,5);
while (1) {
if(PINC & 0x04){ // if button pusht then use jumpers value
while (PINC & 0x04) {
cbi(PORTD,PD7); //on
delay_ms(a);
sbi(PORTD,PD7); // off
delay_ms(5000);
}}
//if jumper 1 on
if (PIND & (0x01 << 5)){
a="5000";
}
else
//if jumper 2 on
if (PIND & (0x01 << 6));{
a="10000";
}
}}
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
karlis
-
Maybe you have add the following at the top of your file before including delay.h:
#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.