Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

c programming problem

Status
Not open for further replies.

kapcai

New Member
hi geng...
i kind of new here...

this is my programming...

include <p18f4520.h>
#include "delays.h"

#pragma config PWRT = OFF
#pragma config MCLRE = ON
#pragma config WDT = OFF
#pragma config LVP = OFF
#pragma config DEBUG = OFF


void msDelay(unsigned int itime);
void AD_ISR(void);
void chk_isr(void);
void initial (void);
void forward (void);
void backward (void);


#pragma code High_Priority_Int = 0x0008
void High_Priority_Int(void)
{
chk_isr();
}

#pragma code

#pragma interrupt chk_isr
void chk_isr(void)
{
if (PIR1bits.ADIF==1)
{
AD_ISR();
}
}

void main(void)
{

TRISB = 0;
TRISC = 0;
TRISD = 0;
PORTB = 0;
TRISAbits.TRISA0 = 1;

ADCON0 = 0b01000001;
ADCON1 = 0b10000100;

PIR1bits.ADIF=0;
PIE1bits.ADIE = 1;
INTCONbits.PEIE =1;
INTCONbits.GIE = 1;
msDelay(100);


while(1)
{
ADCON0bits.GO = 1;
msDelay(100);
}
}

void AD_ISR(void)
{
PORTD = ADRESH;
msDelay(130);

if (PORTB=0)
initial();


else if (PORTB<PORTD)
forward();


else if (PORTB>PORTD)
backward();


else if (PORTD=PORTB)
PORTC = 0;


PIR1bits.ADIF=0;

}


void initial (void)
{
.
.
.
}


void forward (void)
{
.
.
.
}

void backward (void)
{
.
.
.
}


void msDelay(unsigned int itime)
{
unsigned int i, j;
for (i=0;i<itime;i++)
for (j=0;j<135;j++);
}

my problem is , this program would not jump to initial,forward and backward subroutine.... :confused::confused::confused:
 
You need a goto at the ISR vector,

Code:
#pragma code high_vector=0x08    //setup the ISR vector
void high_interrupt (){
  _asm GOTO high_isr _endasm    //jump to interrupt handler
}
#pragma code


void high_isr(){

BTW, if you type [code] before your code and [/code] after it then it will keep it's formatting.

Mike.
 
Last edited:
i'll be looking into it...

the funny thing its working if i'm writing my program like this
void AD_ISR(void)
{
PORTD = ADRESH;
msDelay(130);

if (PORTD==0b00000000)
{
PORTC = 0x55;

}

else if(PORTD>=0b00000001&&PORTD<=0b00011001)
{
PORTC = 0x66;
msDelay(100);
PORTC = 0xCC;
msDelay(100);
PORTC = 0x99;
msDelay(100);
PORTC = 0x33;
msDelay(100);
PORTC = 0x66;
msDelay(100);
PORTC = 0xCC;
msDelay(100);
PORTC = 0x99;
msDelay(100);
PORTC = 0x33;
PORTB = PORTD;
msDelay(3000);

}

else if(PORTD>=0b00011010&&PORTD<=0b00110011)
{
PORTC = 0x66;
msDelay(100);
PORTC = 0xCC;
msDelay(100);
PORTC = 0x99;
msDelay(100);
PORTC = 0x33;
msDelay(100);
PORTC = 0x66;
msDelay(100);
PORTC = 0xCC;
msDelay(100);
PORTC = 0x99;
msDelay(100);
PORTC = 0x33;
msDelay(100);
PORTC = 0x66;
msDelay(100);
PORTC = 0xCC;
msDelay(100);
PORTC = 0x99;
msDelay(100);
PORTC = 0x33;
msDelay(100);
PORTC = 0x66;
msDelay(100);
PORTC = 0xCC;
msDelay(100);
PORTC = 0x99;
msDelay(100);
PORTC = 0x33;
PORTB = PORTD;
msDelay(3000);
}
PIR1bits.ADIF=0;

}


but... when i'm adding

void AD_ISR(void)
{
PORTD = ADRESH;
msDelay(130);

if (PORTB=0)
initial();


else if (PORTB<PORTD)
forward();


else if (PORTB>PORTD)
backward();


else if (PORTD=PORTB)
PORTC = 0;


PIR1bits.ADIF=0;

}

it just didn't work.. not reading the subroutine...


by the way this program is to move my stepper motor to certain degree when certain voltage input given
 
btw..
what do you means by type
Code:
 before your code and
after it then it will keep it's formatting.
 
btw..
what do you means by type
Code:
 before your code and
after it then it will keep it's formatting.
If you click on the # icon before pasting your code, or type [code] ahead of your code and [/code] after it, your code will look nice and properly formatted (provided it was that way to begin with) like this:
Code:
#include <system.h>
#pragma	CLOCK_FREQ	8000000
#pragma DATA    _CONFIG1H, _INTIO2_OSC_1H
#pragma DATA    _CONFIG2H, _WDT_OFF_2H
#pragma DATA    _CONFIG3H, _MCLRE_ON_3H
#pragma DATA    _CONFIG4L, _LVP_OFF_4L

void main()
{
    osccon = 0x72;          //set int osc to 8MHz
    adcon1 = 0b01111111;    //all digital
    trisa = 0;              //set data directions
    trisb = 0b00010000;

    while(1){
        latb.RB1 = 1;
        delay_s(1);
        latb.RB1 = 0;
        delay_s(1);
    }
}
instead of looking like this unreadable mess:

#include <system.h>
#pragma CLOCK_FREQ 8000000
#pragma DATA _CONFIG1H, _INTIO2_OSC_1H
#pragma DATA _CONFIG2H, _WDT_OFF_2H
#pragma DATA _CONFIG3H, _MCLRE_ON_3H
#pragma DATA _CONFIG4L, _LVP_OFF_4L

void main()
{
osccon = 0x72; //set int osc to 8MHz
adcon1 = 0b01111111; //all digital
trisa = 0; //set data directions
trisb = 0b00010000;

while(1){
latb.RB1 = 1;
delay_s(1);
latb.RB1 = 0;
delay_s(1);
}
}
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top