Author Topic: pwm code help!!  (Read 5797 times)

0 Members and 1 Guest are viewing this topic.

Offline CALEBTopic starter

  • Jr. Member
  • **
  • Posts: 17
  • Helpful? 0
pwm code help!!
« on: January 19, 2009, 04:37:40 PM »
How come when i use the Example code that will PWM on H3 and H6:
My h3 does nothing and my H6 just holds where it is at? the code is below
Code: [Select]


//enter your control equation in here

void control(void)
{
//you may use 8,9, or 10 bit PWM/timer resolution
//use 8 only for 8 bit timers, etc.
//timer0 and timer2 are 8 bit, others are 16
rprintf("Initializing timer2 for PWM\r\n");
PWM_Init_timer2_H6(10);
PWM_Init_timer4_H3(10);

rprintf("Turning on both PWM channels\r\n");
PWM_timer2_On_H6();
PWM_timer4_On_H3();
delay_ms(1000);


rprintf("Setting PWM to 50%% duty cycle\r\n");
PWM_timer2_Set_H6(127);
PWM_timer4_Set_H3(127);
delay_ms(1000);



rprintf("Turning off PWM\r\n");
PWM_timer2_Off_H6();
PWM_timer4_Off_H3();
delay_ms(1000);

}



Can someone explain to me what is happening when I use the PWM_timer2_On_H6();
I think it is turring the pwm on h6 on but what does that really mean.I have read all material on pwm and understand i fairly well.I just need help programming.I am trying to use the timer640.c and .h files they are working fine ,but how do i use them to turn the srevos?As in what comands acculaty turn the servos?Thanks
« Last Edit: January 19, 2009, 04:38:51 PM by CALEB »

Offline CALEBTopic starter

  • Jr. Member
  • **
  • Posts: 17
  • Helpful? 0
Re: pwm code help!!
« Reply #1 on: January 19, 2009, 08:56:56 PM »
Can someone please help me? I have been working on this for hrs and cant get the servos to turn.

Offline airman00

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 3,650
  • Helpful? 21
  • narobo.com
    • Narobo.com - Mechatronics and related
Re: pwm code help!!
« Reply #2 on: January 19, 2009, 09:01:55 PM »
did you set those pins to be outputs in configure_outputs in SoR_utils.h ?

Btw , hes using the  Axon everyone
Check out the Roboduino, Arduino-compatible board!


Link: http://curiousinventor.com/kits/roboduino

www.Narobo.com

Offline CALEBTopic starter

  • Jr. Member
  • **
  • Posts: 17
  • Helpful? 0
Re: pwm code help!!
« Reply #3 on: January 19, 2009, 09:13:06 PM »
I did not know I had to .How would i go about doing that.I never saw anything that said that i had to.Thanks for the help.

Offline CALEBTopic starter

  • Jr. Member
  • **
  • Posts: 17
  • Helpful? 0
Re: pwm code help!!
« Reply #4 on: January 19, 2009, 09:31:58 PM »
I am not really sure what is going on with my pwm.When i try and use th example code to pluse the led all it does is stay on no pluse .Does anyone have any idea what I could be doing wrong? I am using the atmega640.

Offline CALEBTopic starter

  • Jr. Member
  • **
  • Posts: 17
  • Helpful? 0
duty cycle problem.
« Reply #5 on: January 20, 2009, 11:30:17 AM »
How come my servo moves all 180 deg when I set the duty cycle to 300. Timer 4 is set to 10 bits so shouldent it be 1023 that makes it go the full 180?
Code: [Select]

void control(void)

 {
//initialize PWM and timer resolution
//PWM_Init_timer1_LED(8);
PWM_Init_timer2_H6(8);
//PWM_Init_timer3_E3(8);
//PWM_Init_timer3_E4(8);
//PWM_Init_timer3_E5(8);
PWM_Init_timer4_H3(10);
PWM_Init_timer4_H4(10);
PWM_Init_timer4_H5(10);

//turn on PWM
//PWM_timer1_On_LED();
PWM_timer2_On_H6();
//PWM_timer3_On_E3();
//PWM_timer3_On_E4();
//PWM_timer3_On_E5();
PWM_timer4_On_H3();
PWM_timer4_On_H4();
PWM_timer4_On_H5();
delay_ms(1000);

//set PWM duty cycle %
//0-255 for 8bit PWM
//0-511 for 9bit PWM
//0-1023 for 10bit PWM
//PWM_timer1_Set_LED(1);
PWM_timer2_Set_H6(127);
//PWM_timer3_Set_E3(127);
//PWM_timer3_Set_E4(127);
//PWM_timer3_Set_E5(127);
PWM_timer4_Set_H3(300);
PWM_timer4_Set_H4(10);
PWM_timer4_Set_H5(10);


while(1);

Offline Dscrimager

  • Full Member
  • ***
  • Posts: 57
  • Helpful? 0
Re: pwm code help!!
« Reply #6 on: January 21, 2009, 07:45:39 AM »
CALEB

Please post your SoR_Utils.h file so we can see your pin declarations....

Doug

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,704
  • Helpful? 173
    • Society of Robots
Re: pwm code help!!
« Reply #7 on: January 21, 2009, 08:38:33 AM »
Quote
My h3 does nothing and my H6 just holds where it is at?
Do you have a way to check? ie a multimeter with frequency function, and LED, or oscilloscope?

Which version code are you using? PWM was a feature added in later versions. The latest version was supposed to fix/improve PWM, but it has a major bug in it that I can't figure out . . . still working on it. Try v1.02, some but not all the PWM pins work on that.

Offline yerbie

  • Full Member
  • ***
  • Posts: 62
  • Helpful? 3
Re: pwm code help!!
« Reply #8 on: January 21, 2009, 08:46:39 AM »
Quote
still working on it. Try v1.02, some but not all the PWM pins work on that.
Are you having issues with certain pins not working with PWM?
I just tried mine, E3-E5 & H3-H5, PWM and they all work as expected.

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,704
  • Helpful? 173
    • Society of Robots
Re: pwm code help!!
« Reply #9 on: January 21, 2009, 08:51:13 AM »
Quote
still working on it. Try v1.02, some but not all the PWM pins work on that.
Are you having issues with certain pins not working with PWM?
I just tried mine, E3-E5 & H3-H5, PWM and they all work as expected.
Nah the PWM works/worked, its just that other mysterious problem:
http://www.societyofrobots.com/robotforum/index.php?topic=6468.0

Offline CALEBTopic starter

  • Jr. Member
  • **
  • Posts: 17
  • Helpful? 0
Re: pwm code help!!
« Reply #10 on: January 21, 2009, 11:11:51 AM »
here is my sor_utils.
Code: [Select]
/****************************************************************************
*
*   Copyright (c) 2008 www.societyofrobots.com
*   (please link back if you use this code!)
*
*   This program is free software; you can redistribute it and/or modify
*   it under the terms of the GNU General Public License version 2 as
*   published by the Free Software Foundation.
*
*   Alternatively, this software may be distributed under the terms of BSD
*   license.
*
****************************************************************************/

//AVR includes
#include <avr/io.h>     // include I/O definitions (port names, pin names, etc)
#include <avr/interrupt.h> // include interrupt support
#include <stdio.h> // stuff
#include <stdlib.h> // stuff
#include <math.h> // stuff
//#include "libm.a" // required with math.h
#include <string.h> // allow strings to be used

//AVRlib includes
#include "pwm.h"
#include "global.h" // include global settings
#include "uart4.h" // include uart function library, includes buffer.h
#include "rprintf.h" // include printf function library
#include "timerx8.h" // include timer function library (timing, PWM, etc)
#include "timer640.h"
#include "a2d.h" // include A/D converter function library
#include "servo.h"
//define port functions; example: PORT_ON( PORTD, 6);
#define PORT_ON( port_letter, number ) port_letter |= (1<<number)
#define PORT_OFF( port_letter, number ) port_letter &= ~(1<<number)
//#define PORT_ALL_ON( port_letter, number ) port_letter |= (number)
//#define PORT_ALL_OFF( port_letter, number ) port_letter &= ~(number)
#define FLIP_PORT( port_letter, number ) port_letter ^= (1<<number)
#define PORT_IS_ON( port_letter, number ) ( port_letter & (1<<number) )
#define PORT_IS_OFF( port_letter, number ) !( port_letter & (1<<number) )

//define the servo function macro
#define servo(port,number,position)   (PORT_ON(port,number), delay_cycles(position), PORT_OFF(port,number))


//************CONFIGURE PORTS************
//configure ports for input or output - specific to ATmega2560
void configure_ports(void)
{
//examples: configure ports for input or output
  //DDRH = 0X7F;
      //DDRB = 0x7F;  //configure B ports 0->4 for output (google search '0b00011111 to hex')
//DDRC = 0x7F;  //configure all C ports for output, except 7 (0b01111111 to hex)
//DDRD = 0xFE;  //configure all D ports for output, except 0 and 1 (RX/TX) FC
    //DDRC = _BV (PC0); //PC0 is digital output
//0b76543210 <- port # order, 1 for output, 0 for input
//to convert from binary to hex, google search '0b00011111 to hex'
//DDRG &= ~_BV(PG5); //make port G5 an input pin
//cbi(DDRG, PG5);  //set G5 as input pin

//ANALOG PORTS
//useful for sensors, SharpIR, photoresistors, etc.
//analog ports can be configured as digital ports if desired
DDRF = 0b00000000;  //configure all F ports for input 0x00
PORTF = 0b00000000; //make sure pull-up resistors are turned off 0x00
DDRK = 0b00000000;  //configure all F ports for input 0x00
PORTK = 0b00000000; //make sure pull-up resistors are turned off 0x00

//DIGITAL PORTS
//useful for servos, PWM, LED's, UART, interrupts, timers
DDRA = 0b11111111;  //configure ports for output
//       ||||||||
//       |||||||\___0:
//       ||||||\____1:
//       |||||\_____2:
//       ||||\______3:
//       |||\_______4:
//       ||\________5:
//       |\_________6:
//       \__________7:
//PORTB reserved for programmer (use programmer pins if you know what you are doing)
DDRB = _BV (PB6); //PB6 is LED, hold low to turn it on
DDRC = 0b11111111;  //configure ports for output
//       ||||||||
//       |||||||\___0: Bulk Right
//       ||||||\____1: Rib5 Right
//       |||||\_____2: Rib4 Right
//       ||||\______3: Rib2 Right
//       |||\_______4: Rib1 Right
//       ||\________5:
//       |\_________6:
//       \__________7:
DDRD = 0b11111011;  //configure ports for output
//       ||||||||
//       |||||||\___0:
//       ||||||\____1:
//       |||||\_____2: serial RXD1, input  -> 0
//       ||||\______3: serial TXD1, output -> 1
//       |||\_______4:
//       ||\________5:
//       |\_________6:
//       \__________7: timer0
DDRE = 0b11111110;  //configure ports for output
//       ||||||||
//       |||||||\___0: serial RXD0, input  -> 0
//       ||||||\____1: serial TXD0, output -> 1
//       |||||\_____2:
//       ||||\______3:
//       |||\_______4:
//       ||\________5:
//       |\_________6:
//       \__________7:
//cbi(PORTG, PG5);  // disable pull-up resistor for v1e and earlier
sbi(PORTG, PG5);  // enable pull-up resistor for v1f and later
cbi(DDRG, PG5); //PG5 is for the button, make a digital input
//note that button actions are inverted between both versions!!!
DDRH = 0b11111110;  //configure ports for output
//       ||||||||
//       |||||||\___0: serial RXD2, input  -> 0
//       ||||||\____1: serial TXD2, output -> 1
//       |||||\_____2: Rib1 Left
//       ||||\______3: Rib2 Left
//       |||\_______4: Rib4 Left
//       ||\________5: Rib5 Left
//       |\_________6: Bulk Left
//       \__________7:
DDRJ = 0b11111110;  //configure ports for output
//       ||||||||
//       |||||||\___0: serial RXD3, input  -> 0
//       ||||||\____1: serial TXD3, output -> 1
//       |||||\_____2:
//       ||||\______3:
//       |||\_______4:
//       ||\________5:
//       |\_________6:
//       \__________7:
//PORTL has no headers
};
//***************************************


//************DELAY FUNCTIONS************
//wait for X amount of cycles (234 cycles is about 1.003 milliseconds)//incorrect
//to calculate: 234/1.003*(time in milliseconds) = number of cycles
void delay_cycles(unsigned long int cycles)
{
cycles=cycles;//doubled frequency but too lazy to change times
while(cycles > 0)
cycles--;
}
//***************************************


//***************STATUS LED**************
//tank test
void LED_off(void)
{PORT_ON(PORTB,6);}
void LED_on(void)
{PORT_OFF(PORTB,6);}
//***************************************


//**************RESET TIMER**************
void reset_timer(void)
{
//restart timer count
TCNT0=0x00;//clear timer
timer0ClearOverflowCount();//clear timer0's overflow counter.
}
//***************************************


//*****************BUTTON****************
int button_pressed(void)
{
return (bit_is_clear(PING, 5));
//return ((PING) & (1<<PG5));//old version, went high when button pushed
}
//***************************************


//trig tables not yet tested . . .
//***********TRIG LOOKUP TABLES**********
//returns a trig sin or cos calculation value multiplied by 100 (to avoid floating point math)
//returns a trig tan calculation value multiplied by 10 (to avoid floating point math)
//only allows for angles between 0 and 360 degrees

//multiplied by 100 so no floating point math
signed int angtable[73]={100,100,98,97,94,91,87,82,77,71,64,57,50,42,34,26,17,9,0,-9,-17,-26,-34,-42,-50,-57,-64,-71,-77,-82,-87,-91,-94,-97,-98,-100,
-100,-100,-98,-97,-94,-91,-87,-82,-77,-71,-64,-57,-50,-42,-34,-26,-17,-9,0,9,17,26,34,42,50,57,64,71,77,82,87,91,94,97,98,100,100};

signed int cos_SoR(long signed int degrees)//returns cos*100
{
if (degrees >= 0)//positive angles
return angtable[degrees/5];
else
return -angtable[72-(-degrees)/5];
}

signed int sin_SoR(long signed int degrees)//returns sin*100
{
degrees=degrees - 90;//phase shift 90 degrees

if (degrees >= 0)//positive angles
return angtable[degrees/5];
else
return -angtable[72-(-degrees)/5];
}

signed int tan_SoR(long signed int degrees)//returns tan * 10
{
//tan(x) = sin(x)/cos(x)
if (degrees == 90 || degrees == -90 || degrees == 270 || degrees == -270)//blows up
return 0;//what else should I return?!?!?
return sin_SoR(degrees)/cos_SoR(degrees)*10;
}

//***************************************

/*
notes to self:
int *p_variable;//initiate pointer

p_variable = &variable;//store address of variable in p_variable

printf("variable address = %d, variable = %d", p_variable, *p_variable);

----
char *p_variable stores variable of char type, int *p_variable stores variable of int type
*/

to me it looks like all of the h pins are set to outputs .thanks for all the help.

Offline CALEBTopic starter

  • Jr. Member
  • **
  • Posts: 17
  • Helpful? 0
Re: pwm code help!!
« Reply #11 on: January 21, 2009, 11:19:02 AM »
Also yes i could only get H345 to work none of the E pins would work so I switched to mbatemans code .
http://www.societyofrobots.com/robotforum/index.php?topic=5590.0
Now the H and E pins work but he does not have H6 working .I would like to use the timer640.h and .c  though.If i could jsust get the E pins and h6 to work with that all would be well.Another thing i noticed for the h345 pins that did work using the 640 timer is that the duty cycle was not right .I thought it was suppose to be between 1000 and 2000. But it was between 0 and 300 ?

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,704
  • Helpful? 173
    • Society of Robots
Re: pwm code help!!
« Reply #12 on: January 22, 2009, 08:27:23 PM »
Yea only H345 worked on the older version.

See my latest post on the other thread:
http://www.societyofrobots.com/robotforum/index.php?topic=6468.msg50027#msg50027

 

SMF spam blocked by CleanTalk