Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

atmega32L programming help!!!!!

Status
Not open for further replies.

adiishamz

Newbie level 5
Joined
Sep 26, 2010
Messages
10
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,283
Activity points
1,368
i have to make a C programme for atmega32L which enables me to communicate with sim300DZ and respond accordingly to the load circuit which will be attached to output of atmega32L. but i am not very good at C programming and i have designed a program which switches ON and OFF 2 LEDs. can you anyone please help me how to make this program according to my requirement?
1. #include <avr/io.h>
2. #include <util/delay.h>
3.
4. // This program will turn the LEDs on for 100ms,
5. // then off for 200ms, endlessly.
6.
7. int main(void)
8. {
9. // Set Port B pins for 3 and 4 as outputs
10. // PORTB bit 3 = physical pin #2 on the ATTINY45
11. // PORTB bit 4 = physical pin #3 on the ATTINY45
12.
13. DDRB = 0x18; // In binary this is 0001 1000 (note that is bit 3 and 4)
14. // AVR-GCC also would accept 0b00011000, by the way.
15.
16. // Set up a forever loop using your favorite C-style 'for' loop
17. for ( ; 1==1 ; ) // loop while 1 equals 1
18. {
19. // Set Port B pins for 3 and 4 as HIGH (i.e. turn the LEDs on)
20. PORTB = 0x18; // If we wanted only PB4 on, it'd be PORTB=0x10
21.
22. // Use a function (defined in delay.h) to pause 100 milliseconds
23. _delay_ms(100);
24.
25. // Set PORTB to be all LOWs (i.e. turn the LEDs off)
26. PORTB = 0x00;
27.
28. // Delay for a 200ms
29. _delay_ms(200);
30. }
31.
32. return 1;
33. }
 

for ( ; 1==1 ; )
Its the first time i see this, i usually do while(1) {}
Delay works fine but note that if you have any kind of interrupt then the delay stops, the interrupt code executes and then the delay resumes.
And return 1 is not needed, actually you can safely use void main(void)

Everything else seems to be fine.

There are some ways that make your work easier, i like these defines

Code:
#define SETBIT(ADDRESS,BIT) (ADDRESS |= (1<<BIT)) 
#define CLEARBIT(ADDRESS,BIT) (ADDRESS &= ~(1<<BIT)) 
#define FLIPBIT(ADDRESS,BIT) (ADDRESS ^= (1<<BIT)) 
#define CHECKBIT(ADDRESS,BIT) (ADDRESS & (1<<BIT)) 
#define WRITEBIT(RADDRESS,RBIT,WADDRESS,WBIT) (CHECKBIT(RADDRESS,RBIT) ? SETBIT(WADDRESS,WBIT) : CLEARBIT(WADDRESS,WBIT))

After these defines you can:

SETBIT(PORTA,2) turns on PORTA bit2
CLEARBIT(PORTB,0) turns off PORTB bit0
FLIPBIT(PORTB,0) inverts PORTB bit0 (if it was 1 it becomes 0 and the opposite)
CHECKBIT(PORTB,0) returns 1 if the specified bit has a value of 1
WRITEBIT(PINA,0,PORTB,1) read the value of PINA bit0 and writes it to PORTB bit1

I forgot to mention that the above work with any of PORTX, DDRX, PINX

Also when you want to change only some of the bits and not the entire port you can do

PORTA = PORTA | 0b10001001 this will turn on bits 0,3,7 without changing the other bits (they keep the previous value)

PORTA = PORTA & 0b01110110 this will turn off bits 0,3,7 without changing the other bits (they keep the previous value)

It is not difficult to remember these since any bit ORed with 1 has the result of 1 and any bit ANDed with 0 has a result of 0.

Hope these help
Alex
 
Last edited:
thanks for this useful information "alexan_e"
but my main problem is the usage of AT commands for the GPRS module (Sim300dz). which are as follows....

can you please show me 1 example of how to use At command in C.
my main idea is to develop home automation system using GSM.
if i send a message through my cell phone to GSM Module(sim300dz) it recieve text and then send it to microcontroller which will Turn the load ON or off according to the information provided.. Plz plz help!! i am stuck with this big time
 

Attachments

  • SIM300D_ATC_V2.00.pdf
    1.7 MB · Views: 62

I'm sorry but i have no idea how to use AT commands (or what they are), maybe someone else has an idea.

Alex

---------- Post added at 02:40 ---------- Previous post was at 01:33 ----------

You can also try to ask AVR Freaks
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top