|
|
View previous topic :: View next topic |
Author |
Message |
pacman91
Joined: 17 Jun 2011 Posts: 28 Location: Malaysia
|
How to set the PWM duty with the accelerometer value? |
Posted: Sat Jul 23, 2011 12:08 am |
|
|
Code: | #include<18F4520.h>
#DEVICE ADC=8
#fuses HS,NOWDT,NOPROTECT,NOLVP, BROWNOUT, PUT
#use delay(clock=4000000)
//#Define timer PIN_C2
#include <flex_lcd.c>
#define data_width 3
int8 AccData[data_width];
long valuex,valuey,
void main(void)
{
int i;
output_high(LCD_POWER); // Turn on power to LCD
output_low(LCD_RW);
lcd_init();
setup_adc(ADC_CLOCK_INTERNAL);//enables the a/d module
//and sets the clock to internal adc clock
setup_adc_ports(AN0_TO_AN2); //set the adc pins to analog
output_low(PIN_C1); // Set CCP2 output low
output_low(PIN_C2); // Set CCP1 output low
setup_ccp1(CCP_PWM); // Configure CCP1 as a PWM
setup_ccp2(CCP_PWM); // Configure CCP2 as a PWM
enable_interrupts(int_timer2);
enable_interrupts(GLOBAL);
setup_timer_2(T2_DIV_BY_16, 250, 5); // 4 ms
while(1)
{
disable_interrupts(int_timer2);
disable_interrupts(GLOBAL);
for(i = 0; i < data_width; i++)
{
set_adc_channel(i);
delay_us(10); //a small delay is required after setting the channel
AccData[i] = read_adc(); //starts the conversion
}
lcd_gotoxy(1,1);
printf(lcd_putc, "x = %u y = %u ", AccData[0], AccData[1]);
lcd_gotoxy(1,2);
printf(lcd_putc, "z = %u ", AccData[2]);
delay_ms(10);
valuex = AccData[0];
valuey = AccData[1];
enable_interrupts(int_timer2);
enable_interrupts(GLOBAL);
}
#int_timer2 // setup_timer_2(T2_DIV_BY_16, 250, 5); // 4 ms * 5(postscale) =20ms
void ISR_Timer2()
{
disable_interrupts(int_timer2); // which disable is needed in this program? teach me.
//disable_interrupts(GLOBAL); //
set_pwm1_duty(valuex); // duty cycle on pin C2
set_pwm2_duty(valuey); // duty cycle on pin C1
enable_interrupts(int_timer2);
//enable_interrupts(GLOBAL);
} |
Quote: | I just simply type this code to let you all know what i wanted to do, the program is not simulated, PWM period is 4ms, please teach me how to set to 20ms, the variable in program, valuex and valuey in the set_pwm_duty(), I just simply put, because i dun know how to set the value with the reading of the accelerometer with the short range from 126_to_142(x-axis) and also the Y-axis |
Quote: | I though that the program can run as I imagine, after20ms, execute interrupt, set PWM duty cycle, 0.5ms_to_2.35ms( the time of SERVO MOTOR 0_to_180 degree), but the output of RC2 and RC1 result is 4ms, doesn't have any change with the postscale. |
the value read from the MMA7269Q
AXIS = Angle of SERVO MOTOR ( 0 90 180)
X = 126 134 142 ( the range of the reading from accelerometer)
0 90 180 degree
Y = 69 81 96
Z = I don't want use it.
those value is the reading from accelerometer, I wanted to set the MIN, MEDIUM, and MAX value as the 0,90,180 degree of the SERVO MOTOR. I want to control 2 servo motor( model C36R) for the X-axis and Y-axis turning with the accelerometer.
Last edited by pacman91 on Sat Jul 23, 2011 3:22 am; edited 1 time in total |
|
|
pacman91
Joined: 17 Jun 2011 Posts: 28 Location: Malaysia
|
|
Posted: Sat Jul 23, 2011 12:30 am |
|
|
Code: | #include<18F4520.h>
#DEVICE ADC=8
#fuses HS,NOWDT,NOPROTECT,NOLVP, BROWNOUT, PUT
#use delay(clock=4000000)
#include <flex_lcd.c>
#define data_width 3
int8 AccData[data_width];
long timerx,timery,valuex,valuey;
void main(void)
{
int i;
output_high(LCD_POWER); // Turn on power to LCD
output_low(LCD_RW);
lcd_init();
setup_adc(ADC_CLOCK_INTERNAL);//enables the a/d module
//and sets the clock to internal adc clock
setup_adc_ports(AN0_TO_AN2); //set the adc pins to analog
enable_interrupts(int_timer2);
enable_interrupts(GLOBAL);
setup_timer_2(T2_DIV_BY_16, 250, 5);
while(1)
{
disable_interrupts(int_timer2);
disable_interrupts(GLOBAL);
output_low(PIN_B0);
output_low(PIN_B1);
for(i = 0; i < data_width; i++)
{
set_adc_channel(i);
delay_us(10); //a small delay is required after setting the channel
AccData[i] = read_adc(); //starts the conversion
}
lcd_gotoxy(1,1);
printf(lcd_putc, "x = %u y = %u ", AccData[0], AccData[1]);
lcd_gotoxy(1,2);
printf(lcd_putc, "z = %u ", AccData[2]);
delay_ms(10);
valuey = AccData[1];
timery= (valuey^2)/376;
valuex = AccData[0];
timerx = (valuex^10)/794000000000000000;
enable_interrupts(int_timer2);
enable_interrupts(GLOBAL);
}
#int_timer2
void ISR_Timer2()
{
disable_interrupts(int_timer2);
//disable_interrupts(GLOBAL);
output_high(PIN_B0);
delay_us(timery);
output_high(PIN_B1);
delay_us(timerx);
enable_interrupts(int_timer2);
// enable_interrupts(GLOBAL);
} |
Quote: | this code i tried before the code i shown at top, this code is pull up the PIN_B0 with the delay with calculation, but the simulation result output is totally different with my calculation, why?) |
because i failed to use the delay, so i change to the SET_DUTY_PWM.
but i wanted to know what problem in this, so i shown this code to you all, so i can learned it |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|