PDA

View Full Version : C to PICbasic



.nanaa
- 22nd March 2009, 22:10
I have a .c code but I need to convert it to .hex, 'cause I want to do a Bi-color led project using the PIC 16F84...something like 4 led's blinking according with the functions.

The .c code :


//include file
#include <pic.h>

//16F84 configuration
__CONFIG(0x3FF1);

//portb macro
#define PORTBIT(adr, bit) ((unsigned)(&adr)*8+(bit))

//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);

//global variables
unsigned int i; //for loop pause
unsigned int c; //for loop event loop

//functions
void pause_action(); //pause
void blink_redgreen(); //blink red then green
void blink_baf(); //blink red and green back and forth
void alt_blink(); //every other red then green
void blink_sequence(); //blinks red one at a time
//end functions

//main function
void main(void)
{
TRISB = 0x00;
PORTB = 0b00000000;

while(1)
{

blink_redgreen();
blink_baf();
alt_blink();
blink_sequence();

};

}
//end main function

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_redgreen()
{

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

PORTB = 0b10101010;
pause_action();

PORTB = 0b01010101;
pause_action();

};

};

void blink_baf()
{

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

PORTB = 0b10100101;
pause_action();

PORTB = 0b01011010;
pause_action();

};

};

void alt_blink()
{

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

PORTB = 0b10011001;
pause_action();

PORTB = 0b01100110;
pause_action();

};

};

void blink_sequence()
{

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

PORTB = 0b10010101;
pause_action();

PORTB = 0b01100101;
pause_action();

PORTB = 0b01011001;
pause_action();

PORTB = 0b01010110;
pause_action();

};

};



Thanks for any help.

Ioannis
- 23rd March 2009, 18:08
Sorry only Basic here!

Look for the C forum of your compiler.

Ioannis

mister_e
- 26th March 2009, 01:51
Should be something like



C var byte
TRISB = 0
PORTB = 0

Main
gosub blink_redgreen
gosub blink_baf
gosub alt_blink
gosub blink_sequence
goto Main


pause_action:
pause 500 ' arbitrary delay... 500 mSec here
return


blink_redgreen:
for c=0 to 9
PORTB = %10101010
gosub pause_action
PORTB = %01010101
gosub pause_action

next
return

blink_baf:
for c=0 to 9
PORTB = %10100101
gosub pause_action
PORTB = %01011010
gosub pause_action
next
return

alt_blink:
for c=0 to 9
PORTB = %10011001
gosub pause_action
PORTB = %01100110
gosub pause_action
next
return

blink_sequence:
for c=0 to 9
PORTB = %10010101
gosub pause_action
PORTB = %01100101
gosub pause_action
PORTB = %01011001
gosub pause_action
PORTB = %01010110
gosub pause_action
next
return