Pages: [1]   Go Down
Author Topic: pwm arduino  (Read 1165 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hi. im a very beginner to arduino. i would like to ask about generating pwm using arduino, i have this code for ATmel as below that generate 8.2us of period or 121kHz frequency.. i've tried to use the codes n test it using oscilloscope in arduino, and it wont do..how can i modified the codes for me to be able to obtain same signal frequency using arduino 0023.. please help me. thank u so much..

#include <avr/io.h>
#define F_CPU 16000000UL //set 16mhz crystal

int main (void)
{
   DDRD |=0xFF;
   
   //initialization for timer1
   TCCR1A |= 1<<WGM11 | 1<<COM1A1 | 0<<COM1A0; // COM1A1=1,COM1A0=0,FOR NON INVERTED MODE...1:1 FOR INVERTED MODE
   TCCR1B |= 1<<WGM12 | 1<<WGM13 | 1<<CS10 | 1<<ICNC1; //sebelum ni CS10 =1;ICNC1 =1 noise canceller
   ICR1 = 7; //counter value utk 16MHZ

   OCR1A = 3;    
}

Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 162
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Need more info.  That's why no answers yet.

Quote
DDRD |=0xFF; 
Since this is in the main loop, it does nothing, repeatedly  ORing in all 1's on top of 1's.
Perhaps you want XOR, which toggles.

Also, why is init code in the main loop, rather than init routine.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 300
Posts: 26210
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Since this is in the main loop, it does nothing, repeatedly
Read it again  smiley-wink
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 480
Posts: 18722
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

i have this code for ATmel as below that generate 8.2us of period or 121kHz frequency

How do you calculate that? You are counting up to 8 with a duty cycle of 4. So you have a 50% duty cycle with a period of 500 nS, giving a frequency of 2 MHz. It outputs on pin 9 so you need:

Code:
   DDRB |= 0xFF;

Personally I prefer:

Code:
pinMode (9, OUTPUT);
Logged


Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 300
Posts: 26210
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@OP, posting the same question in different parts of the forum is not a good way to make a good first impression; it simply wastes everyones time.
I've deleted the duplicate.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 480
Posts: 18722
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Oh? I didn't notice that. Just as well, eh?

Anyway, proof of what happens on pin 9:




Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@awol, sorry my bad, its my first time joining forum.. smiley-grin

i c.. then how can i write it using arduino, i tried as below, its not working n no signal shows up. correct me please.. and actually what should i put in void loop()..  many2 thankz!!  smiley-small

void setup() {
 pinMode(9, OUTPUT);

 TCCR1A |= _BV(WGM11) | _BV(COM1A1);
 TCCR1B |= _BV(WGM12) | _BV(WGM13) | _BV(CS10) | _BV(ICNC1);

 ICR1= 7;
 
   OCR1A = 3;}
 
 void loop(){
 }
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 480
Posts: 18722
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Try:

Code:
void setup() {
  pinMode(9, OUTPUT);

  TCCR1A = 0;
  TCCR1B = 0;
  
  TCCR1A |= _BV(WGM11) | _BV(COM1A1);
  TCCR1B |= _BV(WGM12) | _BV(WGM13) | _BV(CS10) | _BV(ICNC1);

  ICR1= 7;

  OCR1A = 3;
}

void loop(){
}

The Arduino IDE initializes Timer 1 to do stuff. You need to stop all that before you start "oring" in new values.

Or try this:

Code:
void setup()
  {
  pinMode(9, OUTPUT);

  TCCR1A = _BV(WGM11) | _BV(COM1A1);
  TCCR1B = _BV(WGM12) | _BV(WGM13) | _BV(CS10) | _BV(ICNC1);
  ICR1= 7;
  OCR1A = 3;
  }

void loop() {}

If you are going to "|=" things you need to be pretty certain about what the existing values are.
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

if comparing these two codes, do they obtain similar signal? because as the first code there is 0<<COM1A0, since COM1A0 = 0, i just exclude it in the second code..i have to go to lab to use oscilloscope,but lab close after 5.  smiley-sad  if u dont mind, may i know is there any software u used to get the previous image result one..

1st code:

Code:
#include <avr/io.h>
#define F_CPU 16000000UL //set 16mhz crystal

int main (void)
{
   DDRD |=0xFF;
   
   //initialization for timer1
   TCCR1A |= 1<<WGM11 | 1<<COM1A1 | 0<<COM1A0; // COM1A1=1,COM1A0=0,FOR NON INVERTED MODE...1:1 FOR INVERTED MODE
   TCCR1B |= 1<<WGM12 | 1<<WGM13 | 1<<CS10 | 1<<ICNC1; //sebelum ni CS10 =1;ICNC1 =1 noise canceller
   ICR1 = 7; //counter value utk 16MHZ

   OCR1A = 3;   
}


2nd code:

Code:
void setup() {
  pinMode(9, OUTPUT);

  TCCR1A = 0;
  TCCR1B = 0;
 
  TCCR1A |= _BV(WGM11) | _BV(COM1A1);
  TCCR1B |= _BV(WGM12) | _BV(WGM13) | _BV(CS10) | _BV(ICNC1);

  ICR1= 7;

  OCR1A = 3;
}

void loop(){
}

btw, tq tq tq..
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 480
Posts: 18722
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

the first code there is 0<<COM1A0, since COM1A0 = 0, i just exclude it in the second code..

That's right. No difference.
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok. thankz a lot..  smiley
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 162
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Also equivalent and more concise is:
Code:
void setup() {
  pinMode(9, OUTPUT);

  TCCR1A = _BV(WGM11) | _BV(COM1A1);
  TCCR1B = _BV(WGM12) | _BV(WGM13) | _BV(CS10) | _BV(ICNC1);

  ICR1= 7;
  OCR1A = 3;
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

tq for helping everyone.. smiley
Logged

Pages: [1]   Go Up
Jump to: