View previous topic :: View next topic |
Author |
Message |
jths21
Joined: 14 Jan 2008 Posts: 3 Location: Malaysia
|
PIC16F876 output problem |
Posted: Mon Jan 14, 2008 8:06 pm |
|
|
Hi, I am new here and I'm new to PIC programming. I'm currently on a project of controlling a servo motor using PIC16F876. In the programming of the IC, I need to use it to output 0-255 byte using 8 pins, which I used up RB0 to RB7 as for the rotation angles of the servo.
I am using C to program the chip. On the variable declaration part, do I have to use byte or integer as the definition? The program part which I wrote for it is something like this:
byte i;
i=getc();
portb=i;
However, it doesn't seem to be working. I've even tried up using integer as the variable declaration. May I have some advice and help here on this? Thanks a lot in advance.
JustinTeoh _________________ jths21 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jan 14, 2008 9:17 pm |
|
|
Look at the gethex() function in the CCS file input.c.
It's in this directory:
Quote: | c:\program files\picc\drivers\input.c |
It can receive two hex characters typed in from Hyperterminal
and convert them to an integer. Then you can output the integer
to a port. |
|
|
jths21
Joined: 14 Jan 2008 Posts: 3 Location: Malaysia
|
|
Posted: Mon Jan 14, 2008 10:24 pm |
|
|
hm..i think i'll paste up the actual program that i'm running..hope can give as reference and get advice from you all..thanks a lot...
Code: |
#include<16F876.H>
#use delay(clock=20000000)
#use RS232(baud=115200,xmit=PIN_C6,rcv=PIN_C7)
#fuses hs, nowdt, nolvp, noprotect
#byte PORTC=7
#byte PORTB=8
#byte PORTA=5
char a; // ID
char b; // Function Selection
char mode; // Mode Set
char drive; // Driver Mode
char rot; // Rotation Mode
char sys; // System Mode
char chg; // Encoder Changes
char cntplr; // Counter Polarity Set
char rst; // Counter Reset
unsigned n; // Motor Rotation Output Angle
// Main program
void main()
{
set_tris_c(0x80);
set_tris_b(0x00);
set_tris_a(0x00);
do
{
a=0;
b=0;
a=getc(); // Get ID
b=getc(); // Get the symbol for which function to run
PORTB = 0; // Set all pins low for no output
while( a == '1' )
{
if( b == 'A'){
mode = getc(); // Get mode set
while( mode == 'C' ){
output_high(PIN_A0);
delay_ms(500);
output_low(PIN_A0);
delay_ms(500);
output_high(PIN_A0);
break;
}
while( mode == 'T' ){
output_high(PIN_A0);
break;
}
while( mode == 'F' ){
output_low(PIN_A0);
break;
}
}
else if( b == 'B'){
drive = getc(); // Get driving mode HOM or SERVO
while( drive == 'H'){
output_high(PIN_A1);
break;
}
while( drive == 'S'){
output_low(PIN_A1);
break;
}
}
else if( b == 'C'){
rot = getc(); // Get rotation mode CW or CCW
while( rot == 'R'){
output_high(PIN_A2);
break;
}
while( rot == 'C'){
output_low(PIN_A2);
break;
}
}
else if( b == 'D'){
sys = getc(); // Get system set mode
while( sys == 'T'){
output_high(PIN_A3);
break;
}
while( sys == 'F'){
output_low(PIN_A3);
break;
}
}
else if( b == 'E'){
n = getc(); // Get motor positioning rotation angle
portb=n;
break;
}
else if( b == 'F'){
chg = getc(); // Get encoder change set
while( chg == 'T'){
output_high(PIN_C0);
break;
}
while( chg == 'F'){
output_low(PIN_C0);
break;
}
}
else if( b == 'G'){
cntplr = getc(); // Get counter polarity set
while( cntplr == 'T'){
output_high(PIN_C1);
break;
}
while( cntplr == 'F'){
output_low(PIN_C1);
break;
}
while( mode == 'C' ){
output_high(PIN_C1);
delay_ms(500);
output_low(PIN_C1);
delay_ms(500);
output_high(PIN_C1);
break;
}
}
else if( b == 'H'){
rst = getc(); // Get counter reset
while ( rst == 'T'){
output_high(PIN_C2);
break;
}
while( rst == 'F'){
output_low(PIN_C2);
break;
}
}
break;
}
while( a != '1') // All output to low if different ID
{output_low(PIN_C0);
output_low(PIN_C1);
output_low(PIN_C2);
output_low(PIN_B0);
output_low(PIN_B1);
output_low(PIN_B2);
output_low(PIN_B3);
output_low(PIN_B4);
output_low(PIN_B5);
output_low(PIN_B6);
output_low(PIN_B7);
output_low(PIN_A0);
output_low(PIN_A1);
output_low(PIN_A2);
output_low(PIN_A3);
break;}
delay_ms(100);
}while(1);
}
|
the above are the codes i'm using..i'm very new to programming pic..never touch that before till now...thanks... _________________ jths21
Last edited by jths21 on Tue Jan 15, 2008 7:06 pm; edited 1 time in total |
|
|
SET
Joined: 15 Nov 2005 Posts: 161 Location: Glasgow, UK
|
|
Posted: Tue Jan 15, 2008 1:20 pm |
|
|
You use this construct a lot:
Quote: | Code: | while( rst == 'F'){
output_low(PIN_C2);
break;
} |
|
Why not use:
Code: | if( rst == 'F'){
output_low(PIN_C2);
} |
The 'while' implies that the loop will execute more than once, so the
'if' is clearer. (Left the braces in for the more general case)
Lots of other issues - for example, why do this
Quote: | Code: | #byte PORTC=7
#byte PORTB=8
#byte PORTA=5 |
|
as the CCS compiler does that for you? So
Quote: | Code: | PORTB = 0; // Set all pins low for no output |
|
would be
You should use 'Code' button and edit your post, make it a bit easier to follow |
|
|
jths21
Joined: 14 Jan 2008 Posts: 3 Location: Malaysia
|
|
Posted: Tue Jan 15, 2008 7:08 pm |
|
|
thanks for your suggestion...thanks a lot...
i'm still on the beginner learning stage..so a lot to learn up...
thanks....but my main question is on this part:
Code: |
else if( b == 'E'){
n = getc(); // Get motor positioning rotation angle
portb=n;
break;
}
|
i cant make it right there... _________________ jths21 |
|
|
|