| Author |
Message |
darrellc
Joined: 13 Jul 2009 Posts: 3
|
14 Jul 2009 1:29 delay_us on mikroc |
|
|
|
|
I'm trying to run a servo motor with this program code. I'm using the mikroC basic compiler for PIC. It's over the limit, can't find any way to shorten the program. Can somebody help me? Would appreciate all help.
| Code: |
//Servo Motor Auto-Mode Test
int i;
void main() {
TRISB = 0;
ADCON1= 0x07;
PORTB = 0;
do {
for(i=0;i<=25;i++){
PORTB = 0x01;
Delay_us(1300);
PORTB = 0x00;
Delay_us(18700);
}
for(i=0;i<=25;i++){
PORTB = 0x01;
Delay_us(1310);
PORTB = 0x00;
Delay_us(18690);
}
for(i=0;i<=25;i++){
PORTB = 0x01;
Delay_us(1320);
PORTB = 0x00;
Delay_us(18680);
}
for(i=0;i<=25;i++){
PORTB = 0x01;
Delay_us(1330);
PORTB = 0x00;
Delay_us(18670);
}
for(i=0;i<=25;i++){
PORTB = 0x01;
Delay_us(1340);
PORTB = 0x00;
Delay_us(18660);
}
for(i=0;i<=25;i++){
PORTB = 0x01;
Delay_us(1350);
PORTB = 0x00;
Delay_us(18650);
}
for(i=0;i<=25;i++){
PORTB = 0x01;
Delay_us(1360);
PORTB = 0x00;
Delay_us(18640);
}
for(i=0;i<=25;i++){
PORTB = 0x01;
Delay_us(1370);
PORTB = 0x00;
Delay_us(18630);
}
for(i=0;i<=25;i++){
PORTB = 0x01;
Delay_us(1380);
PORTB = 0x00;
Delay_us(18620);
}
for(i=0;i<=25;i++){
PORTB = 0x01;
Delay_us(1390);
PORTB = 0x00;
Delay_us(18610);
}
for(i=0;i<=25;i++){
PORTB = 0x01;
Delay_us(1400);
PORTB = 0x00;
Delay_us(18600);
}
for(i=0;i<=25;i++){
PORTB = 0x01;
Delay_us(1410);
PORTB = 0x00;
Delay_us(18590);
}
for(i=0;i<=25;i++){
PORTB = 0x01;
Delay_us(1420);
PORTB = 0x00;
Delay_us(18580);
}
for(i=0;i<=25;i++){
PORTB = 0x01;
Delay_us(1430);
PORTB = 0x00;
Delay_us(18570);
}
for(i=0;i<=25;i++){
PORTB = 0x01;
Delay_us(1440);
PORTB = 0x00;
Delay_us(18560);
}
for(i=0;i<=25;i++){
PORTB = 0x01;
Delay_us(1450);
PORTB = 0x00;
Delay_us(18550);
}
for(i=0;i<=25;i++){
PORTB = 0x01;
Delay_us(1460);
PORTB = 0x00;
Delay_us(18540);
}
for(i=0;i<=25;i++){
PORTB = 0x01;
Delay_us(1470);
PORTB = 0x00;
Delay_us(18530);
}
for(i=0;i<=25;i++){
PORTB = 0x01;
Delay_us(1480);
PORTB = 0x00;
Delay_us(18520);
}
for(i=0;i<=25;i++){
PORTB = 0x01;
Delay_us(1490);
PORTB = 0x00;
Delay_us(18510);
}
for(i=0;i<=25;i++){
PORTB = 0x01;
Delay_us(1500);
PORTB = 0x00;
Delay_us(18500);
}
for(i=0;i<=25;i++){
PORTB = 0x01;
Delay_us(1510);
PORTB = 0x00;
Delay_us(18490);
}
for(i=0;i<=25;i++){
PORTB = 0x01;
Delay_us(1520);
PORTB = 0x00;
Delay_us(18480);
}
for(i=0;i<=25;i++){
PORTB = 0x01;
Delay_us(1530);
PORTB = 0x00;
Delay_us(18470);
}
for(i=0;i<=25;i++){
PORTB = 0x01;
Delay_us(1540);
PORTB = 0x00;
Delay_us(18460);
}
for(i=0;i<=25;i++){
PORTB = 0x01;
Delay_us(1550);
PORTB = 0x00;
Delay_us(18450);
}
for(i=0;i<=25;i++){
PORTB = 0x01;
Delay_us(1560);
PORTB = 0x00;
Delay_us(18440);
}
for(i=0;i<=25;i++){
PORTB = 0x01;
Delay_us(1570);
PORTB = 0x00;
Delay_us(18430);
}
for(i=0;i<=25;i++){
PORTB = 0x01;
Delay_us(1580);
PORTB = 0x00;
Delay_us(18420);
}
for(i=0;i<=25;i++){
PORTB = 0x01;
Delay_us(1590);
PORTB = 0x00;
Delay_us(18410);
}
for(i=0;i<=25;i++){
PORTB = 0x01;
Delay_us(1600);
PORTB = 0x00;
Delay_us(18400);
}
for(i=0;i<=25;i++){
PORTB = 0x01;
Delay_us(1610);
PORTB = 0x00;
Delay_us(18390);
}
for(i=0;i<=25;i++){
PORTB = 0x01;
Delay_us(1620);
PORTB = 0x00;
Delay_us(18380);
}
for(i=0;i<=25;i++){
PORTB = 0x01;
Delay_us(1630);
PORTB = 0x00;
Delay_us(18370);
}
for(i=0;i<=25;i++){
PORTB = 0x01;
Delay_us(1640);
PORTB = 0x00;
Delay_us(18360);
}
for(i=0;i<=25;i++){
PORTB = 0x01;
Delay_us(1650);
PORTB = 0x00;
Delay_us(18350);
}
for(i=0;i<=25;i++){
PORTB = 0x01;
Delay_us(1660);
PORTB = 0x00;
Delay_us(18340);
}
for(i=0;i<=25;i++){
PORTB = 0x01;
Delay_us(1670);
PORTB = 0x00;
Delay_us(18330);
}
for(i=0;i<=25;i++){
PORTB = 0x01;
Delay_us(1680);
PORTB = 0x00;
Delay_us(18320);
}
for(i=0;i<=25;i++){
PORTB = 0x01;
Delay_us(1690);
PORTB = 0x00;
Delay_us(18310);
}
for(i=0;i<=25;i++){
PORTB = 0x01;
Delay_us(1700);
PORTB = 0x00;
Delay_us(18300);
}
} while(1);
} |
I tried with Vdelay_ms() and doing it like Vdelay_ms(j), where j = 1.3. Then use j = j + count, where count = 0.01. However it's unable to calculate decimals. I'm just a rookie, don't know much. Hope someone would be able to help. Thanks.
|
|
| Back to top |
|
 |
arthur0
Joined: 28 Nov 2003 Posts: 73 Helped: 9 Location: Stockholm, Sweden
|
14 Jul 2009 9:22 programming servo motors |
|
|
|
|
Hi.
I have no idea what this program is good for, but I can suggests a small size optimization like this:
| Code: |
//Servo Motor Auto-Mode Test
int i, j;
void main()
{
TRISB = 0;
ADCON1= 0x07;
PORTB = 0;
do
{
for(j=1300; j<=1700; j+=10)
{
for(i=0; i<=25; i++)
{
PORTB = 0x01;
Delay_us(j);
PORTB = 0x00;
Delay_us(20000-j);
}
}
}while(1);
}
|
I hope this helps,
Arthur
|
|
| Back to top |
|
 |
darrellc
Joined: 13 Jul 2009 Posts: 3
|
14 Jul 2009 9:29 how to run servo motor |
|
|
|
|
| The thing is that I am unable to put a variable into the 'Delay_us()'. I'm using the mikroC PIC compiler.
|
|
| Back to top |
|
 |
arthur0
Joined: 28 Nov 2003 Posts: 73 Helped: 9 Location: Stockholm, Sweden
|
14 Jul 2009 9:53 help delay_us |
|
|
|
|
How 'bout this then:
| Code: |
//Servo Motor Auto-Mode Test
int i, j;
void Delay_10us(int dly)
{
while(--dly > 0)
{
Delay_us(10);
}
}
void main()
{
TRISB = 0;
ADCON1= 0x07;
PORTB = 0;
do
{
for(j=130; j<=170; j++)
{
for(i=0; i<=25; i++)
{
PORTB = 0x01;
Delay_10us(j);
PORTB = 0x00;
Delay_10us(2000-j);
}
}
}while(1);
} |
|
|
| Back to top |
|
 |
Google AdSense

|
14 Jul 2009 9:53 Ads |
|
|
|
|
|
|
| Back to top |
|
 |
darrellc
Joined: 13 Jul 2009 Posts: 3
|
14 Jul 2009 11:43 servo motor test program |
|
|
|
|
Hmm, error message 'Delay_10us Identifier redefined'. However, I tried changing the name of the function and it managed to compile! Now I just have to test it out on my motor later on. Thanks!
|
|
| Back to top |
|
 |