Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

urgent help with c code to blink LEDS

Status
Not open for further replies.

romarioo

Newbie level 4
Joined
Jan 13, 2007
Messages
7
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Activity points
1,446
iam having atrouble to debug a code for pic 16f871 to blink the Leds for port B and c...plz help me with it


void main()
{

setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);

// TODO: USER CODE!!

}//include file




#define PORTBIT(adr, bit) ((unsigned)(&adr)*8+(bit))
#define LED0,LED1,LED2,LED3,LED4,LED5,LED6,LED7
//portb pin assignment
static bit LED0 @ PORTBIT(PORTB, 0);
static bit LED1 @ PORTBIT(PORTB, 1);
static bit LED2 @ PORTBIT(PORTB, 2);
static bit LED3 @ PORTBIT(PORTB, 3);
static bit LED4 @ PORTBIT(PORTB, 4);
static bit LED5 @ PORTBIT(PORTB, 5);
static bit LED6 @ PORTBIT(PORTB, 6);
static bit LED7 @ PORTBIT(PORTB, 7);
static bit LED0 @ PORTBIT(PORTC, 0);
static bit LED1 @ PORTBIT(PORTC, 1);
static bit LED2 @ PORTBIT(PORTC, 2);
static bit LED3 @ PORTBIT(PORTC, 3);
static bit LED4 @ PORTBIT(PORTC, 4);
static bit LED5 @ PORTBIT(PORTC, 5);
static bit LED6 @ PORTBIT(PORTC, 6);
static bit LED7 @ PORTBIT(PORTC, 7);
unsigned int i;
unsigned int n;

//functions
void pause_action(); //pause
void blink_red();

//main function
void main(void)
{
TRISB = 0x00;
PORTB = 0b00000000;
TRISC = 0x00;
PortC=0c00000000;
while(1)
{

blink_red();

blink_sequence();

};

}


void pause_action()
{

for(i=0; i<4000; i++);
for(i=0; i<4000; i++);
for(i=0; i<4000; i++);
for(i=0; i<4000; i++);
for(i=0; i<4000; i++);
for(i=0; i<4000; i++);

};

void blink_red()
{

for(n=0; n<10; n++)
{

PORTB = 0b00001111;
pause_action();

PORTB = 0b01010101;
pause_action();

};

};



void blink_sequence()
{

for(n=0; n<10; n++)
{

PORTB = 0b01010101;

pause_action();

PORTB = 0b01111111;
pause_action();

PORTB = 0b01011001;
pause_action();

PORTB = 0b01010110;
pause_action();


};
};

{
void blink sequence()
{
for (n=0; n<10; n++)
PORTC=oc00110011;
pause_action();

PORTC=oc11001100;
pause_action();
};
};

---------- Post added at 03:15 ---------- Previous post was at 03:11 ----------

most of the errors aere related to the LED assignings and recognitions
 

You cannot assign the same name to more than one pin. Give them each a unique name or the compiler will not be able to decide which one you mean to use.
Also consider using a timer to create your delays or if the compiler has one, a built-in delay function. Your software "pause_action()" delays are dependent on the clock speed and will be different if you change it.
Do you really mean to prefix some of the values with 0c, shouldn't it be 0b for binary?

Brian.
 

if you are using Mikro C pro compiler it would be eaiser to you to blink leds.

for example if you want to blink leds on PORTB and PORTC so it is a very simple code from this you can implment your logic

m using Mikro c Pro Compiler



Code:
void main()
{

TRISB=0x00; // set Port b to output
TRISC=0x00; // set Port C to output

while(1)  // always true
{
PORTB=0xff;// set PORTB LEDS ON
PORTC=0xff; //set PORTC leds On
Delay_ms(1000) // delay 1000 milliseconds =1 sec if you are using 4mhz crystal(it is built in function in mikro c compiler)
PORTB=0x00; //set PORTC lEDS OFF
PORTC=0x00; //ser PORTC leds off

}// end of while loop

}// main function ends here

this is code used for Pic16f877..but from this you can get the logic that you want..
 
thanks alot guys for ur quick repsonce.I appreciate ur help
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top