|
|
View previous topic :: View next topic |
Author |
Message |
Ravenfield
Joined: 18 May 2010 Posts: 6 Location: Finland
|
PIC16F84a + RS232 program project problem |
Posted: Tue May 18, 2010 11:45 pm |
|
|
Heyah all,
I've just recently gotten into all these PIC programming things and am not very good at it yet. Still, I have a project going on and when it comes to programming and stuff...
So about my project:
-I have the circuit ready
-It's suppose to communicate with computer(Linux) via COM port
-It has 4 button attached for entering commands
-also 4 lamps/leds behind the buttons to flash and give signals
-Using PIC16F84A and MAX232 micros
Basically the program should check that if something comes in from RS it should update the lights (more of it later).
Then it should wait for button to be pressed and send the pushed button data to computer where program reacts to it.
Pins A0, A1, A2 and A3 are connected to buttons and B3, B4, B5 and B6 for the lamps.
I'm using MPLAB IDE for Windows 5.50.00
Processor verison is 7.5000, Disassembler 2.0000, Device File 5.00.00
Aslo PICSTART Plus with Firmware 2.30 and Software DLL 2.50
Some additional information:
Lamps will get either the energy from same power the circuit gets or from external powersource.
Pins where buttons are connected are always receiving power unless the button is pressed (button connected to Ground).
The program is to be connected with photoboot where 2 buttons is used to select background (left/right buttons), one button to take the photo('ok' button) and one to print the picture ('Print' button).
The program on computer is not being made by me so that isn't a problem.
The program however works something like this while commicating with control circuit:
-Telling the user to choose picture with left/right buttons (maybe flashing buton lights)
-telling the user to press 'OK' button when ready (propably waiting a while after choose buttons are pressed to make sure user have selected the background they like (again flashing the 'ok' button lights)
-taking the picture and giving options to print or take a new one (either press print button or choosing to take another)
Like I said before, I'm really bad at programming. The circuit was a piece of cake to do, but the programming -.-
So basically any tips/hints I could use are very much welcome. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed May 19, 2010 12:27 am |
|
|
You have a really old version of MPLAB. Do you have the CCS compiler ?
That's what this forum is about: the CCS C compiler. It can run in
MPLAB. You also need to know the C programming language. |
|
|
Ravenfield
Joined: 18 May 2010 Posts: 6 Location: Finland
|
|
Posted: Wed May 19, 2010 12:37 am |
|
|
well, i know the basics of both c and c++ (but never been really good at those, can build some basic stuff)
and yeah, this MPLAB have CCS compiler (where to find version though?) and i have built some test programs to make sure everything is connected right(like loop to flash lights for a while and to see the buttons work, to make sure rs connection can get and receive data (putc() and getc() stuff)) |
|
|
Ravenfield
Joined: 18 May 2010 Posts: 6 Location: Finland
|
|
Posted: Wed May 19, 2010 3:29 am |
|
|
well, regarding my first post
how do you read stuff which comes from the computer via RS and can you use output_port(B) to light up leds in different combinations (my first post included pins my leds/lamps were connected)? |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Wed May 19, 2010 4:12 am |
|
|
Ravenfield wrote: | well, i know the basics of both c and c++ (but never been really good at those, can build some basic stuff)
and yeah, this MPLAB have CCS compiler (where to find version though?) and i have built some test programs to make sure everything is connected right(like loop to flash lights for a while and to see the buttons work, to make sure rs connection can get and receive data (putc() and getc() stuff)) |
So you have written some simple programs to read the iputs, alter the outputs and read/write to the serial port (PC)
Ravenfield wrote: | well, regarding my first post
how do you read stuff which comes from the computer via RS and can you use output_port(B) to light up leds in different combinations (my first post included pins my leds/lamps were connected)? |
So I am not sure why you are asking for this information if you have already done it ?
Take one of your test programs and add to it the stuff from your other test programs, once you have a basic main loop running where you check for data from the PC and make changes to your outputs based on this and also monitor the inputs and write data to the outputs you can then ask more specific questions on why your code isn't working.
I am sure I am wrong ;) but it does seem like your asking us to write your program (homework) for you!
you need to make a start, show us what you have done and where you are stuck and slowley we can work together to get your program working |
|
|
Ravenfield
Joined: 18 May 2010 Posts: 6 Location: Finland
|
|
Posted: Wed May 19, 2010 4:19 am |
|
|
Wayne_ wrote: | I am sure I am wrong ;) but it does seem like your asking us to write your program (homework) for you! |
Yes, you sure are terribly wrong.
What good it would do to me if I were to get a ready program from here?
I would still suck at this and could not take any credits for myself as I wouldn't have made a single bit of it.
And it's not a homework, it's a project.
It's just that my brains can't comprehend this programming language too good. Some time ago I even used to like programming, but the "fire" has gone away to somewhere.
But I'll try patch my test programs together and show the code for you, because I really am pretty bad at this stuff, not that it has ever stopped me trying.
But yeah...
**EDITED**
splargh!
I'm thinking too complicated methods
PS. and my brains tend to not think everything till the end, way too often
Oh yeah, CCS compiler version is 5.1.2600.5512 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed May 19, 2010 12:50 pm |
|
|
Quote: | Oh yeah, CCS compiler version is 5.1.2600.5512 |
Not a chance. That's your version of Windows XP.
Source: Wikipedia article:
http://en.wikipedia.org/wiki/Windows_XP
Your compiler version will be at the top of the .LST file in your project
directory. The .LST file will be there after a successful compilation with
no errors. The version is a 4-digit number in this format: x.xxx |
|
|
Ravenfield
Joined: 18 May 2010 Posts: 6 Location: Finland
|
|
Posted: Wed May 19, 2010 11:01 pm |
|
|
blah... 3.067
can learn new things everyday, eh?
by the way
Code: |
#INCLUDE <16F84A.h>
#USE DELAY (CLOCK=4000000)
#USE RS232 (baud=9600,xmit=PIN_B2,rcv=PIN_B1)
//////////////////////////////////////////////////////////////////////
// //
// controller program info //
// //
// -PIN_A0=button1, PIN_A1=button2, PIN_A2=button3, PIN_A3=button4 //
// -PIN_B3=lamp1, PIN_B4=lamp2, PIN_B5=lamp3, PIN_B6=lamp4 //
// -PIN_B1 connected to R1out from MAX232, //
// PIN_B2 connected to T1in from MAX232 //
// //
//////////////////////////////////////////////////////////////////////
void main(){
int cmd; // command from computer
do{
output_low(PIN_B3); // lamp1 off
output_low(PIN_B4); // lamp2 off
output_low(PIN_B5); // lamp3 off
output_low(PIN_B6); // lamp4 off
}while(!input(PIN_B2)); // waiting B2 to go high
cmd=getc(); // not sure if it's read this way :P
switch(cmd){
case 1:
output_high(PIN_B3); // When getting '1' from computer
break; // lamp1 goes on
case 2:
output_high(PIN_B4); // Also, it's not expected for user
break; // to push many buttons at a same time
case 3: // since you don't have to
output_high(PIN_B5);
break;
case 4:
output_high(PIN_B6);
break;
default:
output_low(PIN_B3); // lamp1 off
output_low(PIN_B4); // lamp2 off
output_low(PIN_B5); // lamp3 off
output_low(PIN_B6); // lamp4 off
break;
}
if(!input(PIN_A0)) // so the power is 1 on default
putc(5); // and when button is pressed
if(!input(PIN_A1)) // power goes to ground and pin
putc(6); // value = 0, sending specific
if(!input(PIN_A2)) // number to computer
putc(7); // Or to use puts() to give command words,
if(!input(PIN_A3)) // like puts("left"), puts("ok"), puts("print")?
putc(8);
else
putc(0);
while(1);
}
|
so the inputs from comp (1234) controls the lights, while (5678) are sent to comp to scroll pictures/whatever
pretty sure there is some amateur mistakes in this, can't test it yet 'cause the lamp/button parts haven't arrived yet (blasted slowpokes) |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed May 19, 2010 11:35 pm |
|
|
Quote: | switch(cmd){
case 1:
output_high(PIN_B3); // When getting '1' from computer
break; // lamp1 goes on
case 2:
output_high(PIN_B4); // Also, it's not expected for user
break; // to push many buttons at a same time
case 3: // since you don't have to
output_high(PIN_B5);
break;
case 4:
output_high(PIN_B6); |
If you're typing these numbers into a terminal window on your PC,
then you're testing for the wrong thing. You're getting ASCII values
from the PC. Put single-quotes around those case numbers to make
them be ASCII. Example:
Quote: | if(!input(PIN_A0)) // so the power is 1 on default
putc(5); // and when button is pressed
if(!input(PIN_A1)) // power goes to ground and pin
putc(6); // value = 0, sending specific
if(!input(PIN_A2)) // number to computer
putc(7); // Or to use puts() to give command words,
if(!input(PIN_A3)) // like puts("left"), puts("ok"), puts("print")?
putc(8);
else
putc(0);
|
If you want to send displayable ASCII numbers to the terminal window
on the PC, then the same thing applies with the putc statements. Put
single-quotes around each number. |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Thu May 20, 2010 2:46 am |
|
|
OK, not a bad start but first of all we need to verify what PCM said, what are you using on the PC to send/recieve data ? Hyperterm ?
To point out a few things,
The PIC you are using does not have a UART so will be using a software serial interface.
The code as it stands will run through once and then stop.
Some changes I might make to tidy things up:-
Code: |
void main(){
int cmd; // command from computer
// Initialise the outputs, no need for a loop, just do it once
output_low(PIN_B3); // lamp1 off
output_low(PIN_B4); // lamp2 off
output_low(PIN_B5); // lamp3 off
output_low(PIN_B6); // lamp4 off
// Our main loop
while(true)
{
// getc() will wait for an input so will stop the program until it gets something, a better way would be to check kbhit first
if (kbhit())
cmd=getc(); // not sure if it's read this way :P
else
cmd = 0; // Need to clear cmd if no input otherwise it will keep executing the last command.
switch(cmd){
case '1':
output_high(PIN_B3); // When getting '1' from computer
break; // lamp1 goes on
case '2':
output_high(PIN_B4); // Also, it's not expected for user
break; // to push many buttons at a same time
case '3': // since you don't have to
output_high(PIN_B5);
break;
case '4':
output_high(PIN_B6);
break;
default:
output_low(PIN_B3); // lamp1 off
output_low(PIN_B4); // lamp2 off
output_low(PIN_B5); // lamp3 off
output_low(PIN_B6); // lamp4 off
break;
}
// Yes you can use puts("ok"); to output more meaningfull data
if(!input(PIN_A0)) // so the power is 1 on default
putc('5'); // and when button is pressed
if(!input(PIN_A1)) // power goes to ground and pin
putc('6'); // value = 0, sending specific
if(!input(PIN_A2)) // number to computer
putc('7'); // Or to use puts() to give command words,
if(!input(PIN_A3)) // like puts("left"), puts("ok"), puts("print")?
putc('8');
// because we are now looping this would keep outputting '0' to the screen which we don't want!
// else
// putc('0');
}
}
|
|
|
|
Ravenfield
Joined: 18 May 2010 Posts: 6 Location: Finland
|
|
Posted: Thu May 20, 2010 5:41 am |
|
|
Wayne_ wrote: | OK, not a bad start but first of all we need to verify what PCM said, what are you using on the PC to send/recieve data ? Hyperterm ?
|
I'm not quite sure what the computer will use as the actual program will be running on Linux (Ubuntu) computer with the photo program running.
(basically greenscreen trick with different background options)
well i quess the photo program can understand data from serial, as the device i'm making is kind of a controller
Code: |
void main(){
int cmd; // command from computer
// Initialise the outputs, no need for a loop, just do it once
output_low(PIN_B3); // lamp1 off
output_low(PIN_B4); // lamp2 off
output_low(PIN_B5); // lamp3 off
output_low(PIN_B6); // lamp4 off
// Our main loop
while(true){
// getc() will wait for an input so will stop the program until it gets something, a better way would be to check kbhit first
if (kbhit())
cmd=getc();
else
cmd = 0; // Need to clear cmd if no input otherwise it will keep executing the last command.
switch(cmd){
case '1':
output_high(PIN_B3); // When getting '1' from computer
break; // lamp1 goes on
case '2':
output_high(PIN_B4); // Also, it's not expected for user
break; // to push many buttons at a same time
case '3': // since you don't have to
output_high(PIN_B5);
break;
case '4':
output_high(PIN_B6);
break;
default:
output_low(PIN_B3); // lamp1 off
output_low(PIN_B4); // lamp2 off
output_low(PIN_B5); // lamp3 off
output_low(PIN_B6); // lamp4 off
break;
}
// Yes you can use puts("ok"); to output more meaningfull data
if(!input(PIN_A0)) // so the power is 1 on default
puts("left"); // and when button is pressed
if(!input(PIN_A1)) // power goes to ground and pin
puts("right"); // value = 0, sending specific
if(!input(PIN_A2)) // number to computer
puts("ok"); // Or to use puts() to give command words,
if(!input(PIN_A3)) // like puts("left"), puts("ok"), puts("print")?
puts("print");
// because we are now looping this would keep outputting '0' to the screen which we don't want!
// else
// putc('0');
}
}
|
so this could work, implying the photo program can understand/be edited to understand what "print" is supposed to do in program |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Thu May 20, 2010 7:22 am |
|
|
Quote: | The program on computer is not being made by me so that isn't a problem.
The program however works something like this while commicating with control circuit:
-Telling the user to choose picture with left/right buttons (maybe flashing buton lights)
-telling the user to press 'OK' button when ready (propably waiting a while after choose buttons are pressed to make sure user have selected the background they like (again flashing the 'ok' button lights)
-taking the picture and giving options to print or take a new one (either press print button or choosing to take another)
|
OK, this is the first problem for us.
If the program is not made by you then it is a problem. You need the protocol that the program uses to communicate with external equipment over the serial port. Without this you can not write your pic code.
If you wrote the program you would know what the protocol was or even be able to change it.
You need to be specific with regards to this protocol,
It expects "Left<CR>" as an ascii string as a left button input.
It outputs 1 byte hex value 0x02 to turn on LED 2
Without this we have no idea what you need to be looking for or sending back. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|