View previous topic :: View next topic |
Author |
Message |
Blima
Joined: 13 Feb 2009 Posts: 3
|
Communication between PIC 16f628A and hyperterminal |
Posted: Thu Feb 19, 2009 3:26 am |
|
|
Hi,
I have programmed a PIC16F628A and I can make it to send data to the hyper terminal of windows.
My problem is when I send a character from the PC to PIC it does not recognize it, e.g. try to send character '1' to PIC, to active one port, but it tells me that the key is incorrect!
I have tested the program in PROTEUS and it works great.
Have any ideas?
Thanks in advance,
Bruno
Code: | #include<16f628a.h>
#use delay(clock=4000000)
#fuses HS,NOWDT,PUT,BROWNOUT,MCLR,NOLVP, NOCPD, NOPROTECT
#use fast_io(a)
#use fast_io(b)
#use rs232(baud=9600, parity=N, xmit=PIN_B2, rcv=PIN_B1)
short int ENT0; //Bit RB3
short int ENT1; //Bit RB4
short int ENT2; //Bit RB5
short int ENT3; //Bit RB6
char dado;
main()
{
set_tris_a(0xE0);
set_tris_b(0x7A);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
loop:
printf("******************************************\r\n");
printf("Press 1: Activates RA0\r\n");
printf("Press 2: Activates RA1\r\n");
printf("Press 3: Activates RA2\r\n");
printf("Press 4: Deactivates RA0\r\n");
printf("Press 5: Deactivates RA1\r\n");
printf("Press 5: Deactivates RA2\r\n");
printf("Prima 7: See the state of the inputs\r\n");
printf("*******************************************\r\n");
dado=getc();
switch(dado)
{
Case '1':
output_high(PIN_A0);
printf("**********************\r\n");
printf(" Activated A0!!!\r\n");
printf("**********************\r\n");
break;
Case '2':
output_high(PIN_A1);
printf("**********************\r\n");
printf(" Activated A1!!!\r\n");
printf("**********************\r\n");
break;
Case '3':
output_high(PIN_A2);
printf("**********************\r\n");
printf(" Activated A2!!!\r\n");
printf("**********************\r\n");
break;
Case '4':
output_low(PIN_A0);
printf("**********************\r\n");
printf(" Deactivated A0!!!\r\n");
printf("**********************\r\n");
break;
Case '5':
output_low(PIN_A1);
printf("**********************\r\n");
printf(" Deactivated A1!!!\r\n");
printf("**********************\r\n");
break;
Case '6':
output_low(PIN_A2);
printf("**********************\r\n");
printf(" Deactivated A2!!!\r\n");
printf("**********************\r\n");
break;
Case '7':
ENT0 = input (PIN_B3);
ENT1 = input (PIN_B4);
ENT2 = input (PIN_B5);
ENT3 = input (PIN_B6);
if (!(ENT0)) printf("Deactivated RB3!!!\r\n");
if (!(ENT1)) printf("Deactivated RB4!!!\r\n");
if (!(ENT2)) printf("Deactivated RB5!!!\r\n");
if (!(ENT3)) printf("Deactivated RB6!!!\r\n");
if (ENT0&ENT1&ENT2&ENT3) printf("All entries are activated\r\n");
break;
Default:
printf("**********************\r\n");
printf("Wrong key!!!\r\n");
printf("**********************\r\n");
}
goto loop;
} |
|
|
|
ECACE
Joined: 24 Jul 2006 Posts: 94
|
|
Posted: Thu Feb 19, 2009 12:44 pm |
|
|
I think you need to convert your entry from ASCII to and INT. I recently had a project doing this same type of entry.
Change:
Code: | dado=getc();
switch(dado)
{
Case '1': |
-------------------------
Change it to:
Code: | int cmd;
dado=getc();
cmd = atoi(dado);
switch(cmd)
{
Case '1':
...
...
//all your other case statements here//
...
...
default:
printf("\n\fBad Command!\n\f");
delay_ms(5000);
} |
_________________ A HW Engineer 'trying' to do SW !!! Run!!! |
|
|
Blima
Joined: 13 Feb 2009 Posts: 3
|
|
Posted: Thu Feb 19, 2009 4:53 pm |
|
|
Hi,
Thanks for the quick answer. Unfortunately it didn’t solve my problem.
I’m thinking in using another software similar to hyper terminal to see if the problem lies on my program or at the software. Do you know any good one?
Do you have any example code/link of using the PC to send character to the PIC?
Thanks in advance,
Bruno |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Feb 19, 2009 5:03 pm |
|
|
His original concept should work. The terminal program sends ASCII
numbers. Also, atoi() expects a string as a parameter. It doesn't accept
a char variable such as 'dado'.
My advice is to first prove that your communications work with the PC.
Try a simple program like this, in which the PIC receives a character
sent by the PC and then sends it back to the PC. You can type in
characters and see them on the terminal window. Be sure to disable
the option for "local echo" in the setup screen of your terminal program.
Then you won't see double characters.
Code: | #include <16F628A.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)
#use rs232(baud=9600, xmit=PIN_B2, rcv=PIN_B1, ERRORS)
//======================================
void main()
{
char c;
while(1)
{
c = getc();
putc(c);
}
} |
Also, let the compiler set the TRIS. It will do it automatically for you.
It will set pins to inputs or outputs as required by the peripheral device
(UART) or the CCS function that is used. You don't need the fast_io
and set_tris_x() statements. |
|
|
ECACE
Joined: 24 Jul 2006 Posts: 94
|
|
Posted: Thu Feb 19, 2009 5:26 pm |
|
|
OOPS! You're right. I was using a gets() in mine, not getc() like he is using.
Let's see the result of his test program.
I would suggest you try using the SIO program that comes with CCS. _________________ A HW Engineer 'trying' to do SW !!! Run!!! |
|
|
Blima
Joined: 13 Feb 2009 Posts: 3
|
|
Posted: Tue Feb 24, 2009 4:38 pm |
|
|
The program is now running thanks to your advice.
Regards,
Bruno |
|
|
|