help me, something wrong with my output (pwm arduino)

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

help me, something wrong with my output (pwm arduino)

Postby haneys » Thu May 31, 2012 12:11 pm

Hi.. I need help.. i have the coding as shown below, but its not working as what i expected.. base on what i know, the coding below should provide me a 2MHz frecuency at output pin 9 and 1kHz at ouput pin 8.. but when i test it with oscilloscope i only obtain 2kHz at output pin 9.. what should i do to obtain 2MHz at output pin 9?? how can i change/modified the code?? many2 thankz..

Code: Select all
#include <util/delay.h>
int rog = 8;
void setup() {
  pinMode(9, OUTPUT);
  pinMode(rog, 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(){
delayrog();

}

void delayrog(){
   while (1)
   {
      
  digitalWrite(rog, HIGH);
  _delay_ms(0.52);
  digitalWrite(rog, LOW);
  _delay_ms(0.52);
 
   }
}
haneys
 
Posts: 3
Joined: Thu May 31, 2012 12:08 pm

Re: help me, something wrong with my output (pwm arduino)

Postby adafruit_support_rick » Thu May 31, 2012 1:59 pm

Looks right to me. I'm getting 2MHZ from your code on a Uno

What processor are you running? Are you sure your CPU is running at 16MHz?

Are you sure you're reading your scope correctly?
User avatar
adafruit_support_rick
 
Posts: 2882
Joined: Tue Mar 15, 2011 10:42 am
Location: Buffalo, NY

Re: help me, something wrong with my output (pwm arduino)

Postby haneys » Thu May 31, 2012 10:23 pm

really? then how can i make sure i run my cpu at 16MHz?
i can get 2Mz at output pin 9 when i only use the first code below, but when i add up another output at pin 8 i wont get 2Mz at output pin 9.. i found this weird.. help me..

without output at pin 8:
Code: Select all
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(){

}


having output at pin 8:
Code: Select all
#include <util/delay.h>
int rog = 8;
void setup() {
  pinMode(9, OUTPUT);
  pinMode(rog, 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(){
delayrog();

}

void delayrog(){
   while (1)
   {
     
  digitalWrite(rog, HIGH);
  _delay_ms(0.52);
  digitalWrite(rog, LOW);
  _delay_ms(0.52);
 
   }
}
haneys
 
Posts: 3
Joined: Thu May 31, 2012 12:08 pm

Re: help me, something wrong with my output (pwm arduino)

Postby pburgess » Thu May 31, 2012 10:35 pm

Perhaps DigitalWrite() is playing dirty pool with the PORT/PIN registers. Try using direct PORT manipulation and see if that helps: tutorial on Arduino.cc site.
User avatar
pburgess
 
Posts: 1331
Joined: Sun Oct 26, 2008 1:29 am

Re: help me, something wrong with my output (pwm arduino)

Postby haneys » Thu May 31, 2012 11:56 pm

Did u mean like this? still wont work.. :(

Code: Select all
#include <avr/io.h>
#define F_CPU 16000000UL //set 16mhz crystal
#include <util/delay.h>
int rog = 8;
float rogdelay = 0.6;
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;
//  OCR2B = 3;
}

void loop(){
delayrog();

}
DDRB |=0b00001010; //set pin mana jadi output
   PORTB = 0b00000000;
               PORTB = 0b00000001 ;
      _delay_ms(0.52);
      PORTB = 0b00000000 ;
      _delay_ms(0.52);
   }
}
haneys
 
Posts: 3
Joined: Thu May 31, 2012 12:08 pm

Re: help me, something wrong with my output (pwm arduino)

Postby adafruit_support_rick » Fri Jun 01, 2012 9:08 am

I think it's more likely that _delay_ms is doing something with the timer. None of the standard arduino library implementations seem to use any timers, but maybe you've got some other toolchain?

As I said, I ran the exact code you posted on a Uno with Arduino 1.0.1, and I'm getting 2MHz on Pin 9. I'm getting about 947Hz on pin 8.

BTW - you don't need to set _BV(ICNC1) in TCCRB1, since you're not using input capture. Anyway, input capture is disabled when you set pin 9 to output.
User avatar
adafruit_support_rick
 
Posts: 2882
Joined: Tue Mar 15, 2011 10:42 am
Location: Buffalo, NY


Return to Arduino

Who is online

Users browsing this forum: No registered users and 7 guests

Stuff to buy from the Adafruit store and links to product documentation!


New Products [100]

Raspberry Pi[80]
 
FLORA[23]
 
Bunnie Studios[9]
 
FPGA[1]
 
mbed[11]
Arduino[60]
 
NETduino[14]
 
BeagleBone[24]
 
Android[6]
 
XBee[10]
More Dev Boards[30]


 
BoArduino[8]
 
SpokePOV[4]
 
TV-B-Gone[4]
 
MiniPOV[3]
 
SIM reader[3]
 
Microtouch[5]
 
Clocks & Watches[18]
 
Drawdio[4]
 
Brain Machine[1]
 
Game of Life[2]
 
MintyBoost[2]
More DIY Kits[16]


 
MaKey MaKey[3]
 
Tweet-a-Watt[5]
 
Young Engineers[33]
 
Discover Electronics[2]
 
Snap Circuits[4]
 
littleBits[3]
 
Project packs[8]


 
Breakout Boards[33]
LCDs & Displays[48]
Components & Parts[69]
Batteries & Power[49]
EL Wire/Tape/Panel[52]
LEDs[108]
 
Wireless[14]
Cables[60]
 
Lasers[6]
Sensors/Parts[145]
 
Enclosures/Cases[11]
 
Solar[11]
 
RFID / NFC[13]
Prototyping[69]
 
iDevices[13]
Tools[71]
 
Wearables[39]
 
CNC[37]
 
Robotics[29]
 
3D printing[1]
 
Materials[24]


 
Stickers[41]
 
Skill badges[55]
 
Books[25]
 
Circuit Playground[7]
 
Gift Certificates[4]