View previous topic :: View next topic |
Author |
Message |
noyz
Joined: 31 Jan 2009 Posts: 59
|
PIC 12F629 RC-electronic speed controller |
Posted: Thu Aug 06, 2009 9:16 am |
|
|
I have a ppm signal from a RC receiver.
1 ms up full bw.
1.5 ms up neutral.
2 ms up full fw.
20 ms down.
My code is:
Code: |
#include "C:\Documents and Settings\Noyz2k\My Documents\rc car\acc.h"
int i,b,c;
unsigned int t;
#define preload 65458
#define red pin_A2
//*********************************************************
//Global variables
//*********************************************************
int red_duty;
int Intcount;
//*********************************************************
//100Hz pwm (interrupt at 25.8kHz
//*********************************************************
#INT_TIMER1
void timer_irq()
{
set_timer1(preload);
if (Intcount < 255)
{
if (Intcount == red_duty)
{
output_low(red);
}
}
else if(Intcount == 255)
{
Intcount = 0;
if (red_duty==0) output_low(red); else
output_high(red);
}
Intcount++;
}
//**********************************************************
// Main Program
//**********************************************************
void main()
{
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_8);
setup_timer_1(T1_INTERNAL | T1_DIV_BY_1);
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
//red_duty = 126;
Intcount = 0;
//printf("start");
while (TRUE)
{
c=0;
if (input(PIN_A1)) while (input(PIN_A1));
while(!input(PIN_A1));
set_timer0(0);
while ((input(PIN_A1))) { if (c==0) {if (get_timer0()==126) {set_timer0(0);c++;}};};
t=c*126+get_timer0()-128 -c*2;
//printf("%u\r\n",t);
if (t>63) {b=t-63;
// blue_duty=0;
output_low(PIN_A4);
if (b>=63) red_duty=255; else {red_duty=4*b;}
}
if (t<63) {b=63-t;
// blue_duty=0;
output_high(PIN_A4);
if (b>=63) red_duty=255; else {red_duty=4*b;}
}
if ((t>=62)&&(t<=64)) red_duty=0;
//63 middle
// 126 max
// 0 min
}
}
|
The thing is that without that pwm, it works just fine : t=0 full bkw, t = 63 neutral, t=126 full fw.
When I use the sf pwm using interrupt t isn't the same because of the breaks, if I try to use rs232 to print the values again I get ascii unreadable code.
My question is how do I get to know the new val? |
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
|
Posted: Thu Aug 06, 2009 12:19 pm |
|
|
Hi,
Maybe it's just me, but I find your 'C' writing style to be almost totally unreadable ! Sure, it's probably compilable, but it's really tough to follow the flow. I just gave up, and I suspect I won't be alone....
Doug |
|
![](templates/subSilver/images/spacer.gif) |
noyz
Joined: 31 Jan 2009 Posts: 59
|
|
Posted: Thu Aug 06, 2009 7:22 pm |
|
|
The thing is that I get in t a value from 0 to 126 1ms to 2ms signal.
The t is counted for a full time minus a 1ms that is always there.
I don't know how to explain exactly.
The pwm part I took it from the internet and seem to work fine by setting red_duty=val;
and there is a translation from 0 to 63 into 255 down to 0 for backw and 63 to 126 into 0 to 255 for fw. |
|
![](templates/subSilver/images/spacer.gif) |
|